maad.sound.remove_background

maad.sound.remove_background(Sxx, gauss_win=50, gauss_std=25, beta1=1, beta2=1, llambda=1, verbose=False, display=False, savefig=None, **kwargs)[source]

Remove background noise using spectral subtraction.

Based on the spectrum of the A posteriori noise profile. It computes an atenuation map in the time-frequency domain. See [1] or [2] for more detail about the algorithm.

Parameters:
Sxx2d ndarray of scalars

Spectrogram

gauss_win=50int, optional, default: 50

Number of points in the gaussian window

gauss_std = 25

The standard deviation, sigma used to create the gaussian window

beta1scaler, optional, default: 1

beta1 has to be >0 Should be close to 1

beta2: scaler, optional, default: 1

beta2 has to be >0 better to not change

llambdaint, optional, default: 1

over-subtraction factor to compensate variation of noise amplitude. Should be close to 1

verboseboolean, optional, default is False

Print messages and speed

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 darray of scalar

noise_profile

BGNxx2d ndarray of scalar

Noise map

References

[1]

Steven F. Boll, “Suppression of Acoustic Noise in Speech Using Spectral Subtraction”, IEEE Transactions on Signal Processing, 27(2),pp 113-120,1979 DOI:10.1109/TASSP.1979.1163209

[2]

Y. Ephraim and D. Malah, Speech enhancement using a minimum mean square error short-time spectral amplitude estimator, IEEE. Transactions in Acoust., Speech, Signal Process., vol. 32, no. 6, pp. 11091121, Dec. 1984. DOI:10.1109/TASSP.1984.1164453

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 and add 96dB (which is the maximum dB for 16 bits wav) in order to have positive values

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

Remove stationnary noise from the spectrogram in dB

>>> Sxx_dB_noNoise, noise_profile, _ = maad.sound.remove_background(Sxx_dB)

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()