{"id":13449862,"url":"https://github.com/timsainb/noisereduce","last_synced_at":"2025-05-14T11:08:51.153Z","repository":{"id":39604761,"uuid":"174219582","full_name":"timsainb/noisereduce","owner":"timsainb","description":"Noise reduction in python using spectral gating (speech, bioacoustics, audio, time-domain signals)","archived":false,"fork":false,"pushed_at":"2024-12-28T10:44:08.000Z","size":28929,"stargazers_count":1650,"open_issues_count":31,"forks_count":244,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-05-11T15:40:20.765Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timsainb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-06T20:55:12.000Z","updated_at":"2025-05-10T09:04:58.000Z","dependencies_parsed_at":"2023-01-31T03:30:42.638Z","dependency_job_id":"da928f45-ab37-46c1-a9df-2c41710f7698","html_url":"https://github.com/timsainb/noisereduce","commit_stats":{"total_commits":33,"total_committers":7,"mean_commits":4.714285714285714,"dds":"0.21212121212121215","last_synced_commit":"13c9aacb58221a67a3ac25896a1ad0cce64e9779"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timsainb%2Fnoisereduce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timsainb%2Fnoisereduce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timsainb%2Fnoisereduce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timsainb%2Fnoisereduce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timsainb","download_url":"https://codeload.github.com/timsainb/noisereduce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253830956,"owners_count":21971004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T06:01:00.139Z","updated_at":"2025-05-14T11:08:51.127Z","avatar_url":"https://github.com/timsainb.png","language":"Jupyter Notebook","funding_links":[],"categories":["Tooling \u0026 Infrastructure","Jupyter Notebook","Audio ##","Audio Processing \u0026 I/O"],"sub_categories":["Python Libraries"],"readme":"[![Build Status](https://travis-ci.com/timsainb/noisereduce.svg?branch=master)](https://travis-ci.com/timsainb/noisereduce)\n[![Coverage Status](https://coveralls.io/repos/github/timsainb/noisereduce/badge.svg?branch=master)](https://coveralls.io/github/timsainb/noisereduce?branch=master)\n[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/timsainb/noisereduce/master?filepath=notebooks%2F1.0-test-noise-reduction.ipynb)\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/timsainb/noisereduce/blob/master/notebooks/1.0-test-noise-reduction.ipynb)\n[![PyPI version](https://badge.fury.io/py/noisereduce.svg)](https://badge.fury.io/py/noisereduce)\n\n\u003cdiv style=\"text-align:center\"\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"assets/noisereduce.png\", width=\"100%\"\u003e\n\u003c/p\u003e\n\u003c/div\u003e\n\n# Noise reduction in python using spectral gating\nNoisereduce 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](https://en.wikipedia.org/wiki/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. \n\nThe most recent version of noisereduce comprises two algorithms:\n1. **Stationary Noise Reduction**: Keeps the estimated noise threshold at the same level across the whole signal\n2. **Non-stationary Noise Reduction**: Continuously updates the estimated noise threshold over time\n\n### Version 3 Updates:\n- Includes a PyTorch-based implementation of Spectral Gating, an algorithm for denoising audio signals. \n- You can now create a noisereduce nn.Module object which allows it to be used either as a standalone module or as part of a larger neural network architecture.\n- The run time of the algorithm decreases substantially.\n\n### Version 2 Updates:\n- Added two forms of spectral gating noise reduction: stationary noise reduction, and non-stationary noise reduction. \n- Added multiprocessing so you can perform noise reduction on bigger data.\n- The new version breaks the API of the old version. \n- The previous version is still available at `from noisereduce.noisereducev1 import reduce_noise`\n- You can now create a noisereduce object which allows you to reduce noise on subsets of longer recordings\n\n# Stationary Noise Reduction\n- The basic intuition is that statistics are calculated on  each frequency channel to determine a noise gate. Then the gate is applied to the signal.\n- This algorithm is based (but not completely reproducing) on the one [outlined by Audacity](https://wiki.audacityteam.org/wiki/How_Audacity_Noise_Reduction_Works) for the **noise reduction effect** ([Link to C++ code](https://github.com/audacity/audacity/blob/master/src/effects/NoiseReduction.cpp))\n- The algorithm takes two inputs: \n    1. A *noise* clip containing prototypical noise of clip (optional)\n    2. A *signal* clip containing the signal and the noise intended to be removed\n\n### Steps of the Stationary Noise Reduction algorithm\n1. A spectrogram is calculated over the noise audio clip\n2. Statistics are calculated over spectrogram of the noise (in frequency)\n3. A threshold is calculated based upon the statistics of the noise (and the desired sensitivity of the algorithm) \n4. A spectrogram is calculated over the signal\n5. A mask is determined by comparing the signal spectrogram to the threshold\n6. The mask is smoothed with a filter over frequency and time\n7. The mask is applied to the spectrogram of the signal, and is inverted\n*If the noise signal is not provided, the algorithm will treat the signal as the noise clip, which tends to work pretty well*\n\n# Non-stationary Noise Reduction\n- The non-stationary noise reduction algorithm is an extension of the stationary noise reduction algorithm, but allowing the noise gate to change over time. \n- When you know the timescale that your signal occurs on (e.g. a bird call can be a few hundred milliseconds), you can set your noise threshold based on the assumption that events occurring on longer timescales are noise. \n- This algorithm was motivated by a recent method in bioacoustics called Per-Channel Energy Normalization. \n\n### Steps of the Non-stationary Noise Reduction algorithm\n1. A spectrogram is calculated over the signal\n2. A time-smoothed version of the spectrogram is computed using an IIR filter applied forward and backward on each frequency channel.\n3. A mask is computed based on that time-smoothed spectrogram\n4. The mask is smoothed with a filter over frequency and time\n5. The mask is applied to the spectrogram of the signal, and is inverted\n\n# Installation\n`pip install noisereduce`\n\n# Usage\nSee example notebook: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/timsainb/noisereduce/blob/master/notebooks/1.0-test-noise-reduction.ipynb)\nParallel computing example: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/timsainb/noisereduce/blob/master/notebooks/2.0-test-noisereduce-pytorch.ipynb)\n\n## reduce_noise\n\n### Simplest usage\n```\nfrom scipy.io import wavfile\nimport noisereduce as nr\n# load data\nrate, data = wavfile.read(\"mywav.wav\")\n# perform noise reduction\nreduced_noise = nr.reduce_noise(y=data, sr=rate)\nwavfile.write(\"mywav_reduced_noise.wav\", rate, reduced_noise)\n```\n\n### Arguments to `reduce_noise`\n```\ny : np.ndarray [shape=(# frames,) or (# channels, # frames)], real-valued\n      input signal\n  sr : int\n      sample rate of input signal / noise signal\n  y_noise : np.ndarray [shape=(# frames,) or (# channels, # frames)], real-valued\n      noise signal to compute statistics over (only for non-stationary noise reduction).\n  stationary : bool, optional\n      Whether to perform stationary, or non-stationary noise reduction, by default False\n  prop_decrease : float, optional\n      The proportion to reduce the noise by (1.0 = 100%), by default 1.0\n  time_constant_s : float, optional\n      The time constant, in seconds, to compute the noise floor in the non-stationary\n      algorithm, by default 2.0\n  freq_mask_smooth_hz : int, optional\n      The frequency range to smooth the mask over in Hz, by default 500\n  time_mask_smooth_ms : int, optional\n      The time range to smooth the mask over in milliseconds, by default 50\n  thresh_n_mult_nonstationary : int, optional\n      Only used in nonstationary noise reduction., by default 1\n  sigmoid_slope_nonstationary : int, optional\n      Only used in nonstationary noise reduction., by default 10\n  n_std_thresh_stationary : int, optional\n      Number of standard deviations above mean to place the threshold between\n      signal and noise., by default 1.5\n  tmp_folder : [type], optional\n      Temp folder to write waveform to during parallel processing. Defaults to \n      default temp folder for python., by default None\n  chunk_size : int, optional\n      Size of signal chunks to reduce noise over. Larger sizes\n      will take more space in memory, smaller sizes can take longer to compute.\n      , by default 60000\n      padding : int, optional\n      How much to pad each chunk of signal by. Larger pads are\n      needed for larger time constants., by default 30000\n  n_fft : int, optional\n      length of the windowed signal after padding with zeros.\n      The number of rows in the STFT matrix ``D`` is ``(1 + n_fft/2)``.\n      The default value, ``n_fft=2048`` samples, corresponds to a physical\n      duration of 93 milliseconds at a sample rate of 22050 Hz. \n      This value is well adapted for music signals. However, in speech processing, the recommended value is 512,\n      corresponding to 23 milliseconds at a sample rate of 22050 Hz.\n      In any case, we recommend setting ``n_fft`` to a power of two for\n      optimizing the speed of the fast Fourier transform (FFT) algorithm., by default 1024\n  win_length : [type], optional\n      Each frame of audio is windowed by ``window`` of length ``win_length``\n      and then padded with zeros to match ``n_fft``.\n      Smaller values improve the temporal resolution of the STFT (i.e. the\n      ability to discriminate impulses that are closely spaced in time)\n      at the expense of frequency resolution (i.e. the ability to discriminate\n      pure tones that are closely spaced in frequency). This effect is known\n      as the time-frequency localization trade-off and needs to be adjusted\n      according to the properties of the input signal ``y``.\n      If unspecified, defaults to ``win_length = n_fft``., by default None\n  hop_length : [type], optional\n      number of audio samples between adjacent STFT columns.\n      Smaller values increase the number of columns in ``D`` without\n      affecting the frequency resolution of the STFT.\n      If unspecified, defaults to ``win_length // 4`` (see below)., by default None\n  n_jobs : int, optional\n      Number of parallel jobs to run. Set at -1 to use all CPU cores, by default 1\n  use_torch: bool, optional\n      Whether to use the torch version of spectral gating, by default False\n  device: str, optional\n      A device to run the torch spectral gating on, by default \"cuda\"\n```\n\n## Torch\nSee example notebook: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/timsainb/noisereduce/blob/master/notebooks/3.0-torchgate-as-nn-module.ipynb)\n### Simplest usage\n```\nimport torch\nfrom noisereduce.torchgate import TorchGate as TG\ndevice = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\n\n# Create TorchGating instance\ntg = TG(sr=8000, nonstationary=True).to(device)\n\n# Apply Spectral Gate to noisy speech signal\nnoisy_speech = torch.randn(3, 32000, device=device)\nenhanced_speech = tg(noisy_speech)\n```\n### Arguments\n| Parameter                | Description                                                                                           |\n|--------------------------|-------------------------------------------------------------------------------------------------------|\n| sr                       | Sample rate of the input signal.                                                                      |\n| n_fft                    | The size of the FFT.                                                                                  |\n| hop_length               | The number of samples between adjacent STFT columns.                                                  |\n| win_length               | The window size for the STFT. If None, defaults to n_fft.                                             |\n| freq_mask_smooth_hz      | The frequency smoothing width in Hz for the masking filter. If None, no frequency masking is applied. |\n| time_mask_smooth_ms      | The time smoothing width in milliseconds for the masking filter. If None, no time masking is applied. |\n| n_std_thresh_stationary  | The number of standard deviations above the noise mean to consider as signal for stationary noise.    |\n| nonstationary            | Whether to use non-stationary noise masking.                                                          |\n| n_movemean_nonstationary | The number of frames to use for the moving average in the non-stationary noise mask.                  |\n| n_thresh_nonstationary   | The multiplier to apply to the sigmoid function in the non-stationary noise mask.                     |\n| temp_coeff_nonstationary | The temperature coefficient to apply to the sigmoid function in the non-stationary noise mask.        |\n| prop_decrease            | The proportion of decrease to apply to the mask.                                                      |\n\n## Choosing between Stationary and non-stantionary noise reduction \n\nI discuss stationary and non-stationary noise reduction in [this paper](https://www.frontiersin.org/articles/10.3389/fnbeh.2021.811737/full). \n\n\u003cdiv style=\"text-align:center\"\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"assets/stationary-vs-nonstationary.jpg\", width=\"100%\"\u003e\n\u003c/p\u003e\n\u003c/div\u003e\n\n*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.*\n\n## Citation\nIf you use this code in your research, please cite it:\n```\n\n@article{sainburg2020finding,\n  title={Finding, visualizing, and quantifying latent structure across diverse animal vocal repertoires},\n  author={Sainburg, Tim and Thielk, Marvin and Gentner, Timothy Q},\n  journal={PLoS computational biology},\n  volume={16},\n  number={10},\n  pages={e1008228},\n  year={2020},\n  publisher={Public Library of Science}\n}\n\n@software{tim_sainburg_2019_3243139,\n  author       = {Tim Sainburg},\n  title        = {timsainb/noisereduce: v1.0},\n  month        = jun,\n  year         = 2019,\n  publisher    = {Zenodo},\n  version      = {db94fe2},\n  doi          = {10.5281/zenodo.3243139},\n  url          = {https://doi.org/10.5281/zenodo.3243139}\n}\n\n\n```\n--------\n\n\u003cp\u003e\u003csmall\u003eProject based on the \u003ca target=\"_blank\" href=\"https://drivendata.github.io/cookiecutter-data-science/\"\u003ecookiecutter data science project template\u003c/a\u003e. #cookiecutterdatascience\u003c/small\u003e\u003c/p\u003e\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimsainb%2Fnoisereduce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimsainb%2Fnoisereduce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimsainb%2Fnoisereduce/lists"}