maad.sound.remove_background_morpho

maad.sound.remove_background_morpho(Sxx, q=0.1, display=False, savefig=None, **kwargs)[source]

Remove background noise in a spectrogram using mathematical morphology tool.

Parameters:
Sxx2D numpy array

Original spectrogram (or image)

qfloat

Quantile which must be between 0 and 1 inclusive. The closest to one, the finest details are kept

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

BGNxx2d ndarray of scalar

Noise map

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

>>> Sxx_dB_noNoise,_,_ = maad.sound.remove_background_morpho(Sxx_dB, q=0.5)

Plot both spectrograms

>>> import matplotlib.pyplot as plt 
>>> import numpy as np
>>> fig, (ax1, ax2) = plt.subplots(2, 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, ax=ax2, extent=ext, title='Without stationary noise',vmin=np.median(Sxx_dB_noNoise), vmax=np.median(Sxx_dB_noNoise)+40)
>>> fig.set_size_inches(15,8)
>>> fig.tight_layout()     

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, tcrop=(0,20))   
>>> Sxx_dB = maad.util.power2dB(Sxx) +96

Remove stationnary noise from the spectrogram with different q

>>> Sxx_dB_noNoise_q25,_,_ = maad.sound.remove_background_morpho(Sxx_dB, q=0.25)
>>> Sxx_dB_noNoise_q50,_,_ = maad.sound.remove_background_morpho(Sxx_dB, q=0.5)
>>> Sxx_dB_noNoise_q75,_,_ = maad.sound.remove_background_morpho(Sxx_dB, q=0.75)

Plot 3 spectrograms

>>> import matplotlib.pyplot as plt 
>>> fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
>>> maad.util.plot2d(Sxx_dB_noNoise_q25, ax=ax1, extent=ext, title='Without stationary noise (q=0.25)',vmin=np.median(Sxx_dB_noNoise_q25), vmax=np.median(Sxx_dB_noNoise_q25)+40)
>>> maad.util.plot2d(Sxx_dB_noNoise_q50, ax=ax2, extent=ext, title='Without stationary noise (q=0.50)',vmin=np.median(Sxx_dB_noNoise_q50), vmax=np.median(Sxx_dB_noNoise_q50)+40)
>>> maad.util.plot2d(Sxx_dB_noNoise_q75, ax=ax3, extent=ext, title='Without stationary noise (q=0.75)',vmin=np.median(Sxx_dB_noNoise_q75), vmax=np.median(Sxx_dB_noNoise_q75)+40)
>>> fig.set_size_inches(15,9)
>>> fig.tight_layout()