Noisereduce is a noise reduction algorithm in python that reduces noise in time-domain signals like speech, bioacoustics, and physiological signals. It relies on a method called "spectral gating" which is a form of Noise Gate. It works by computing a spectrogram of a signal (and optionally a noise signal) and estimating a noise threshold (or gate) for each frequency band of that signal/noise. That threshold is used to compute a mask, which gates noise below the frequency-varying threshold.
The most recent version of noisereduce comprises two algorithms:
from noisereduce.noisereducev1 import reduce_noise
pip install noisereduce
See example notebook: Parallel computing example:
from scipy.io import wavfile
import noisereduce as nr
# load data
rate, data = wavfile.read("mywav.wav")
# perform noise reduction
reduced_noise = nr.reduce_noise(y=data, sr=rate)
wavfile.write("mywav_reduced_noise.wav", rate, reduced_noise)
reduce_noise
y : np.ndarray [shape=(# frames,) or (# channels, # frames)], real-valued
input signal
sr : int
sample rate of input signal / noise signal
y_noise : np.ndarray [shape=(# frames,) or (# channels, # frames)], real-valued
noise signal to compute statistics over (only for non-stationary noise reduction).
stationary : bool, optional
Whether to perform stationary, or non-stationary noise reduction, by default False
prop_decrease : float, optional
The proportion to reduce the noise by (1.0 = 100%), by default 1.0
time_constant_s : float, optional
The time constant, in seconds, to compute the noise floor in the non-stationary
algorithm, by default 2.0
freq_mask_smooth_hz : int, optional
The frequency range to smooth the mask over in Hz, by default 500
time_mask_smooth_ms : int, optional
The time range to smooth the mask over in milliseconds, by default 50
thresh_n_mult_nonstationary : int, optional
Only used in nonstationary noise reduction., by default 1
sigmoid_slope_nonstationary : int, optional
Only used in nonstationary noise reduction., by default 10
n_std_thresh_stationary : int, optional
Number of standard deviations above mean to place the threshold between
signal and noise., by default 1.5
tmp_folder : [type], optional
Temp folder to write waveform to during parallel processing. Defaults to
default temp folder for python., by default None
chunk_size : int, optional
Size of signal chunks to reduce noise over. Larger sizes
will take more space in memory, smaller sizes can take longer to compute.
, by default 60000
padding : int, optional
How much to pad each chunk of signal by. Larger pads are
needed for larger time constants., by default 30000
n_fft : int, optional
length of the windowed signal after padding with zeros.
The number of rows in the STFT matrix ``D`` is ``(1 + n_fft/2)``.
The default value, ``n_fft=2048`` samples, corresponds to a physical
duration of 93 milliseconds at a sample rate of 22050 Hz.
This value is well adapted for music signals. However, in speech processing, the recommended value is 512,
corresponding to 23 milliseconds at a sample rate of 22050 Hz.
In any case, we recommend setting ``n_fft`` to a power of two for
optimizing the speed of the fast Fourier transform (FFT) algorithm., by default 1024
win_length : [type], optional
Each frame of audio is windowed by ``window`` of length ``win_length``
and then padded with zeros to match ``n_fft``.
Smaller values improve the temporal resolution of the STFT (i.e. the
ability to discriminate impulses that are closely spaced in time)
at the expense of frequency resolution (i.e. the ability to discriminate
pure tones that are closely spaced in frequency). This effect is known
as the time-frequency localization trade-off and needs to be adjusted
according to the properties of the input signal ``y``.
If unspecified, defaults to ``win_length = n_fft``., by default None
hop_length : [type], optional
number of audio samples between adjacent STFT columns.
Smaller values increase the number of columns in ``D`` without
affecting the frequency resolution of the STFT.
If unspecified, defaults to ``win_length // 4`` (see below)., by default None
n_jobs : int, optional
Number of parallel jobs to run. Set at -1 to use all CPU cores, by default 1
use_torch: bool, optional
Whether to use the torch version of spectral gating, by default False
device: str, optional
A device to run the torch spectral gating on, by default "cuda"
import torch
from noisereduce.torchgate import TorchGate as TG
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
# Create TorchGating instance
tg = TG(sr=8000, nonstationary=True).to(device)
# Apply Spectral Gate to noisy speech signal
noisy_speech = torch.randn(3, 32000, device=device)
enhanced_speech = tg(noisy_speech)
Parameter | Description |
---|---|
sr | Sample rate of the input signal. |
n_fft | The size of the FFT. |
hop_length | The number of samples between adjacent STFT columns. |
win_length | The window size for the STFT. If None, defaults to n_fft. |
freq_mask_smooth_hz | The frequency smoothing width in Hz for the masking filter. If None, no frequency masking is applied. |
time_mask_smooth_ms | The time smoothing width in milliseconds for the masking filter. If None, no time masking is applied. |
n_std_thresh_stationary | The number of standard deviations above the noise mean to consider as signal for stationary noise. |
nonstationary | Whether to use non-stationary noise masking. |
n_movemean_nonstationary | The number of frames to use for the moving average in the non-stationary noise mask. |
n_thresh_nonstationary | The multiplier to apply to the sigmoid function in the non-stationary noise mask. |
temp_coeff_nonstationary | The temperature coefficient to apply to the sigmoid function in the non-stationary noise mask. |
prop_decrease | The proportion of decrease to apply to the mask. |
I discuss stationary and non-stationary noise reduction in this paper.
Figure caption: Stationary and non-stationary spectral gating noise reduction. (A) An overview of each algorithm. Stationary noise reduction typically takes in an explicit noise signal to calculate statistics and performs noise reduction over the entire signal uniformly. Non-stationary noise reduction dynamically estimates and reduces noise concurrently. (B) Stationary and non-stationary spectral gating noise reduction using the noisereduce Python package (Sainburg, 2019) applied to a Common chiffchaff (Phylloscopus collybita) song (Stowell et al., 2019) with an airplane noise in the background. The bottom frame depicts the difference between the two algorithms.
If you use this code in your research, please cite it:
@article{sainburg2020finding,
title={Finding, visualizing, and quantifying latent structure across diverse animal vocal repertoires},
author={Sainburg, Tim and Thielk, Marvin and Gentner, Timothy Q},
journal={PLoS computational biology},
volume={16},
number={10},
pages={e1008228},
year={2020},
publisher={Public Library of Science}
}
@software{tim_sainburg_2019_3243139,
author = {Tim Sainburg},
title = {timsainb/noisereduce: v1.0},
month = jun,
year = 2019,
publisher = {Zenodo},
version = {db94fe2},
doi = {10.5281/zenodo.3243139},
url = {https://doi.org/10.5281/zenodo.3243139}
}
Project based on the cookiecutter data science project template. #cookiecutterdatascience