noisereduce Documentation

repository·master·Indexed 23 days ago

https://github.com/timsainb/noisereduce

A Python library for noise reduction in time-domain signals, such as speech and bioacoustics, using spectral gating. It provides stationary and non-stationary noise reduction algorithms via the reduce_noise function for NumPy arrays and a PyTorch-based TorchGate nn.Module for integration into neural networks with GPU acceleration.

Tokens
2.2K
Snippets
4
Records
10
Agent score
82%

What's inside noisereduce

  1. Choosing between Stationary and non-stationary noise reduction

    master

    The noisereduce package provides two main approaches to noise reduction via spectral gating:

    1. Stationary Noise Reduction: Typically requires an explicit noise signal to calculate statistics. It performs noise reduction over the entire signal uniformly. Use this when you have a clear sample of the noise or when the noise characteristics do not change significantly over time.

    2. Non-stationary Noise Reduction: Dynamically estimates and reduces noise concurrently with the signal. Use this when the noise profile changes over time (e.g., varying background sounds).

  2. How stationary and non-stationary noise reduction work

    master

    Noisereduce uses spectral gating to reduce noise. It computes a spectrogram and estimates a noise threshold (gate) for each frequency band to create a mask that gates noise.

    There are two primary algorithms:

    1. Stationary Noise Reduction: Keeps the estimated noise threshold constant across the entire signal. This is useful when the noise profile does not change over time. It can optionally take a specific y_noise clip to calculate statistics.
    2. Non-stationary Noise Reduction: Continuously updates the noise threshold over time. This is useful when noise characteristics change (e.g., background environmental noise). It is motivated by Per-Channel Energy Normalization (PCEN) and is effective when signal events occur on specific timescales.
  3. Install noisereduce via pip

    master

    Install the noisereduce package using pip to perform noise reduction on time-domain signals like speech, bioacoustics, and physiological signals.

    pip install noisereduce
  4. Sync data to and from S3 using Makefile

    master

    The project provides Makefile commands to synchronize the local data/ directory with an Amazon S3 bucket using the aws s3 sync command. This is useful for managing datasets used by the library.

    Sync local data to S3

    Use make sync_data_to_s3 to recursively upload files from your local data/ directory to an S3 bucket. The destination path will be s3://[your-bucket]/data/.

    Sync S3 data to local

    Use make sync_data_from_s3 to recursively download files from s3://[your-bucket]/data/ to your local data/ directory.

  5. Use reduce_noise for simple noise reduction

    master

    The reduce_noise function is the primary API for performing noise reduction on NumPy arrays. By default, it performs non-stationary noise reduction. To perform stationary noise reduction, set stationary=True.

    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)
  6. Use TorchGate as an nn.Module

    master

    For PyTorch-based workflows, you can use TorchGate (imported as TG). This allows the noise reduction algorithm to be used as a standalone module or integrated into a larger neural network architecture. It supports GPU acceleration via the device parameter.

    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)
  7. Reference: reduce_noise arguments

    master

    Arguments for the noisereduce.reduce_noise function:

    ArgumentTypeDescription
    ynp.ndarrayInput signal. Shape: (# frames,) or (# channels, # frames).
    srintSample rate of input signal / noise signal.
    y_noisenp.ndarrayNoise signal to compute statistics over (only for non-stationary noise reduction).
    stationaryboolWhether to perform stationary, or non-stationary noise reduction (default: False).
    prop_decreasefloatThe proportion to reduce the noise by (1.0 = 100%) (default: 1.0).
    time_constant_sfloatThe time constant, in seconds, to compute the noise floor in the non-stationary algorithm (default: 2.0).
    freq_mask_smooth_hzintThe frequency range to smooth the mask over in Hz (default: 500).
    time_mask_smooth_msintThe time range to smooth the mask over in milliseconds (default: 50).
    thresh_n_mult_nonstationaryintOnly used in nonstationary noise reduction (default: 1).
    sigmoid_slope_nonstationaryintOnly used in nonstationary noise reduction (default: 10).
    n_std_thresh_stationaryintNumber of standard deviations above mean to place the threshold between signal and noise (default: 1.5).
    tmp_folder[type]Temp folder to write waveform to during parallel processing (default: None).
    chunk_sizeintSize of signal chunks to reduce noise over (default: 60000).
    paddingintHow much to pad each chunk of signal by (default: 30000).
    n_fftintLength of the windowed signal after padding with zeros (default: 1024). Recommended power of two.
    win_length[type]Window length for each frame. If None, defaults to n_fft.
    hop_lengthintNumber of audio samples between adjacent STFT columns. If None, defaults to win_length // 4.
    n_jobsintNumber of parallel jobs to run. Set to -1 to use all CPU cores (default: 1).
    use_torchboolWhether to use the torch version of spectral gating (default: False).
    devicestrA device to run the torch spectral gating on (default: `
  8. Reference: TorchGate arguments

    master

    Arguments for the noisereduce.torchgate.TorchGate class:

    ParameterDescription
    srSample rate of the input signal.
    n_fftThe size of the FFT.
    hop_lengthThe number of samples between adjacent STFT columns.
    win_lengthThe window size for the STFT. If None, defaults to n_fft.
    freq_mask_smooth_hzThe frequency smoothing width in Hz for the masking filter. If None, no frequency masking is applied.
    time_mask_smooth_msThe time smoothing width in milliseconds for the masking filter. If None, no time masking is applied.
    n_std_thresh_stationaryThe number of standard deviations above the noise mean to consider as signal for stationary noise.
    nonstationaryWhether to use non-stationary noise masking.
    n_movemean_nonstationaryThe number of frames to use for the moving average in the non-stationary noise mask.
    n_thresh_nonstationaryThe multiplier to apply to the sigmoid function in the non-stationary noise mask.
    temp_coeff_nonstationaryThe temperature coefficient to apply to the sigmoid function in the non-stationary noise mask.
    prop_decreaseThe proportion of decrease to apply to the mask.
  9. Use TorchGate as a PyTorch nn.Module

    master

    The TorchGate class (aliased as TG in the example) can be used directly as a PyTorch nn.Module. This allows you to integrate noise reduction into deep learning pipelines. You can instantiate it with a specific sampling rate (sr) and toggle non-stationary noise reduction using the nonstationary boolean flag. Once instantiated, you can move the module to a specific device (CPU or CUDA) using .to(device) and pass noisy audio tensors directly to the instance to receive enhanced audio.

    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
    # Input shape expected: (Batch, Samples) or similar torch tensor
    noisy_speech = torch.randn(3, 32000, device=device)
    enhanced_speech = tg(noisy_speech)