maad.sound.fir_filter

maad.sound.fir_filter(x, kernel, axis=0)[source]

Filter a signal using a 1d finite impulse response filter.

This function uses a digital filter based on convolution of 1d kernel over a vector or along an axis of a matrix.

Parameters:
xarray_like

1d vector or 2d matrix of scalars to be filtered

kernelarray_like or tuple

Pass directly the kernel (1d vector of scalars) Or pass the arguments in a tuple to create a kernel. Arguments are: - window : string, float, or tuple. The type of window to create. boxcar, triang, blackman, hamming, hann, bartlett, flattop, parzen, bohman, blackmanharris, nuttall, barthann, - (kaiser, beta), - (gaussian, standard deviation), - (general_gaussian, power, width), - (slepian, width), - (dpss, normalized half-bandwidth), - (chebwin, attenuation), - (exponential, decay scale), - (tukey, taper fraction) - N : length of the kernel Examples: - kernel = (‘boxcar’, 9) - kernel = ((‘gaussian’, 0.5), 5) - kernel = [1 3 5 7 5 3 1]

axisint

Determine along which axis is performed the filtering in case of 2d matrix axis = 0 : vertical axis = 1 : horizontal

Returns:
yarray_like

The filtered output with the same shape and phase as x

See also

select_bandwidth

Lowpass, highpass, bandpass or bandstop a 1d signal with an iir filter

Examples

>>> import maad

Load and display the spectrogram of a sound waveform

>>> w, fs = maad.sound.load('../data/cold_forest_daylight.wav') 
>>> Sxx_power, tn, fn, ext = maad.sound.spectrogram(w,fs)
>>> Lxx = maad.spl.power2dBSPL(Sxx_power, gain=42) # convert into dB SPL
>>> fig_kwargs = {'vmax': Lxx.max(),                    'vmin':0,                    'extent': ext,                    'figsize': (4,13),                    'title': 'Power spectrogram density (PSD)',                    'xlabel': 'Time [sec]',                    'ylabel': 'Frequency [Hz]',                    }
>>> ax, fig = maad.util.plot2d(Lxx,**fig_kwargs)
>>> print('The energy in the spectrogram is {} dB SPL'.format(maad.util.add_dB(maad.util.add_dB(Lxx))))
The energy in the spectrogram is 90.68996875800728 dB SPL

Smooth the waveform (lowpass)

>>> w_filtered = maad.sound.fir_filter(w, kernel=(('gaussian', 2), 5))
>>> Sxx_power_filtered,tn,fn,_ = maad.sound.spectrogram(w_filtered,fs)
>>> Lxx_filtered = maad.spl.power2dBSPL(Sxx_power_filtered, gain=42) # convert into dB SPL
>>> ax, fig = maad.util.plot2d(Lxx_filtered,**fig_kwargs)
>>> print('The energy in the spectrogram is {} dB SPL'.format(maad.util.add_dB(maad.util.add_dB(Lxx_filtered))))
The energy in the spectrogram is 89.51776241848223 dB SPL

Smooth the spectrogram, frequency by frequency (blurr)

>>> Lxx_blurr = maad.sound.fir_filter(Lxx, kernel=(('gaussian', 1), 5), axis=1)
>>> ax, fig = maad.util.plot2d(Lxx_blurr,**fig_kwargs)
>>> print('The energy in the spectrogram is {} dB SPL'.format(maad.util.add_dB(maad.util.add_dB(Lxx_blurr))))
The energy in the spectrogram is 88.83611122906812 dB SPL