from cosgate import cosgate from scipy.signal import butter, filtfilt import numpy as np import math def removeSilence(y, fs, sampleDuration=750e-3): sampleDuration = np.array([sampleDuration, sampleDuration]) d1 = sampleDuration[0] d2 = sampleDuration[1] b, a = butter(3, 50*2.0/fs, 'low') fil = filtfilt(b, a, y) fil = np.expand_dims(fil, axis=1) e = np.max(fil, 1) e = cosgate(e, fs, 20e-3) c = 2 * np.max([np.array([[e[0:int(math.floor(d1*fs))]], [e[len(e)-int(math.floor(d2*fs)):]]])]) # FIXME : There is a clicking at the beginning and end of sound. Speak to Etienne # Onset i1 = np.min(((e[:-1] < c) & (e[1:] >= c)).nonzero()[0]) # offset i2 = np.max(((e[:-1] < c) & (e[1:] >= c)).nonzero()[0]) m = math.floor(20e-3 * fs) i1 = int(np.max([1, i1 - m])) i2 = int(np.max([len(y), i2 - m])) y = cosgate(y[i1:i2], fs, 1e-3) # bounds = np.array([i1, i2]) return y