def spectral_whitening(data, sr=None, smooth=None, filter=None,
waterlevel=1e-8):
"""
Apply spectral whitening to data
Data is divided by its smoothed (Default: None) amplitude spectrum.
:param data: numpy array with data to manipulate
:param sr: sampling rate (only needed for smoothing)
:param smooth: length of smoothing window in Hz
(default None -> no smoothing)
:param filter: filter spectrum with bandpass after whitening
(tuple with min and max frequency)
:param waterlevel: waterlevel relative to mean of spectrum
:return: whitened data
"""
mask = np.ma.getmask(data)
N = len(data)
nfft = next_fast_len(N)
spec = fft(data, nfft)
spec_ampl = np.sqrt(np.abs(np.multiply(spec, np.conjugate(spec))))
if smooth:
smooth = int(smooth * N / sr)
spec_ampl = ifftshift(smooth_func(fftshift(spec_ampl), smooth))
# save guard against division by 0
wl = waterlevel * np.mean(spec_ampl)
spec_ampl[spec_ampl < wl] = wl
spec /= spec_ampl
if filter is not None:
spec *= _filter_resp(*filter, sr=sr, N=len(spec), whole=True)[1]
ret = np.real(ifft(spec, nfft)[:N])
return _fill_array(ret, mask=mask, fill_value=0.)
评论列表
文章目录