maad.sound.remove_background_along_axis

maad.sound.remove_background_along_axis(Sxx, mode='median', axis=1, N=25, N_bins=50, display=False, savefig=None, **kwargs)[source]

Get the noisy profile along the defined axis and remove this profile from the spectrogram.

Parameters:
Sxx2D numpy array

Original spectrogram (or image)

modestr, optional, default is ‘median’

Select the mode to remove the noise Possible values for mode are : - ‘ale’ : Adaptative Level Equalization algorithm [Lamel & al. 1981] - ‘median’ : subtract the median value - ‘mean’ : subtract the mean value (DC)

axisinteger, default is 1

if matrix, estimate the mode for each row (axis=0) or each column (axis=1)

Nint, default is 25

length of window to compute the running mean of the noise profile

N_binsint (only for mode = “ale”), default is 50

number of bins to compute the histogram

displayboolean, optional, default is False

Display the signal if True

savefigstring, optional, default is None

Root filename (with full path) is required to save the figures. Postfix is added to the root filename.

kwargs, optional. This parameter is used by plt.plot and savefig functions
  • savefilenamestr, optional, default :’_spectro_after_noise_subtraction.png’

    Postfix of the figure filename

  • figsizetuple of integers, optional, default: (4,10)

    width, height in inches.

  • titlestring, optional, default‘Spectrogram’

    title of the figure

  • xlabelstring, optional, default‘Time [s]’

    label of the horizontal axis

  • ylabelstring, optional, default‘Amplitude [AU]’

    label of the vertical axis

  • cmapstring or Colormap object, optional, default is ‘gray’

    See https://matplotlib.org/examples/color/colormaps_reference.html in order to get all the existing colormaps examples: ‘hsv’, ‘hot’, ‘bone’, ‘tab20c’, ‘jet’, ‘seismic’, ‘viridis’…

  • vmin, vmaxscalar, optional, default: None

    vmin and vmax are used in conjunction with norm to normalize luminance data. Note if you pass a norm instance, your settings for vmin and vmax will be ignored.

  • extentscalars (left, right, bottom, top), optional, default: None

    The location, in data-coordinates, of the lower-left and upper-right corners. If None, the image is positioned such that the pixel centers fall on zero-based (row, column) indices.

  • dpiinteger, optional, default is 96

    Dot per inch. For printed version, choose high dpi (i.e. dpi=300) => slow For screen version, choose low dpi (i.e. dpi=96) => fast

  • formatstring, optional, default is ‘png’

    Format to save the figure

… and more, see matplotlib

Returns:
Sxx_out2d ndarray of scalar

Spectrogram after denoising

noise_profile1d ndarray of scalar

Noise profile

References

[1]

Towsey, M., 2013. Noise Removal from Wave-forms and Spectrograms Derived from Natural Recordings of the Environment. Queensland University of Technology, Brisbane

Examples

Load audio recording and convert it into spectrogram

>>> s, fs = maad.sound.load('../data/rock_savanna.wav')
>>> Sxx,tn,fn,ext = maad.sound.spectrogram (s, fs)   

Convert linear spectrogram into dB

>>> Sxx_dB = maad.util.power2dB(Sxx) + 96

Remove stationnary noise from the spectrogram with modes ‘ale’, ‘median’, and ‘mean’.

>>> Sxx_dB_noNoise_ale,_ = maad.sound.remove_background_along_axis(Sxx_dB, mode='ale')    
>>> Sxx_dB_noNoise_med,_ = maad.sound.remove_background_along_axis(Sxx_dB, mode='median')
>>> Sxx_dB_noNoise_mean,_ = maad.sound.remove_background_along_axis(Sxx_dB, mode='mean')

Plot spectrograms

>>> import matplotlib.pyplot as plt 
>>> import numpy as np
>>> fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1)
>>> maad.util.plot2d(Sxx_dB, ax=ax1, extent=ext, title='original', vmin=np.median(Sxx_dB), vmax=np.median(Sxx_dB)+40)
>>> maad.util.plot2d(Sxx_dB_noNoise_ale, ax=ax2, extent=ext, title='Without stationary noise (mode = ''ale'')',vmin=np.median(Sxx_dB_noNoise_ale), vmax=np.median(Sxx_dB_noNoise_ale)+40)
>>> maad.util.plot2d(Sxx_dB_noNoise_med, ax=ax3, extent=ext, title='Without stationary noise (mode = ''med'')',vmin=np.median(Sxx_dB_noNoise_med), vmax=np.median(Sxx_dB_noNoise_med)+40)
>>> maad.util.plot2d(Sxx_dB_noNoise_mean, ax=ax4, extent=ext, title='Without stationary noise (mode = ''mean'')',vmin=np.median(Sxx_dB_noNoise_mean), vmax=np.median(Sxx_dB_noNoise_mean)+40)
>>> fig.set_size_inches(8,10)
>>> fig.tight_layout()