.. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_filter_out_noise.py: =========================== Filter Out Background Noise =========================== Filter out background noise from noisy speech signals. To see how soundpy implements this, see `soundpy.builtin.filtersignal`. As a general note for filtering, the Wiener Filter is the default filter for soundpy. It seems to filter signals more consequently than the Band Spectral Subtraction Filter. .. code-block:: default # Let's import soundpy, and ipd for playing audio data import soundpy as sp import IPython.display as ipd Define the noisy and clean speech audio files. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Note: these files are available in the soundpy repo. Designate path relevant for accessing audiodata .. code-block:: default sp_dir = '../../../' Noise sample: .. code-block:: default noise = '{}audiodata/background_samples/traffic.wav'.format(sp_dir) noise = sp.string2pathlib(noise) speech = '{}audiodata/python.wav'.format(sp_dir) speech = sp.utils.string2pathlib(speech) For filtering, we will set the sample rate to be quite high: .. code-block:: default sr = 48000 Create noisy speech signal as SNR 10 .. code-block:: default noisy, snr_measured = sp.dsp.add_backgroundsound( speech, noise, sr = sr, snr = 10, total_len_sec = 2, pad_mainsound_sec = 0.5) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/dsp.py:769: UserWarning: Warning: `soundpy.dsp.clip_at_zero` found no samples close to zero. Clipping was not applied. warnings.warn(msg) Hear and see the noisy speech ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default ipd.Audio(noisy,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy, sr=sr, feature_type='signal', title = 'Noisy Speech', subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_001.png :alt: Noisy Speech :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Hear and see the clean speech ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default s, sr = sp.loadsound(speech, sr=sr) ipd.Audio(s,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(s, sr=sr, feature_type='signal', title = 'Clean Speech', subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_002.png :alt: Clean Speech :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Filter the noisy speech ^^^^^^^^^^^^^^^^^^^^^^^ Wiener Filter ~~~~~~~~~~~~~ Let's filter with a Wiener filter: .. code-block:: default noisy_wf, sr = sp.filtersignal(noisy, sr = sr, filter_type = 'wiener') # default .. code-block:: default ipd.Audio(noisy_wf,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy_wf, sr = sr, feature_type = 'signal', title = 'Noisy Speech: Wiener Filter', subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_003.png :alt: Noisy Speech: Wiener Filter :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Wiener Filter with Postfilter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Let's filter with a Wiener filter and postfilter .. code-block:: default noisy_wfpf, sr = sp.filtersignal(noisy, sr = sr, filter_type = 'wiener', apply_postfilter = True) .. code-block:: default ipd.Audio(noisy_wfpf,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy_wfpf, sr=sr, feature_type = 'signal', title = 'Noisy Speech: Wiener Filter with Postfilter', subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_004.png :alt: Noisy Speech: Wiener Filter with Postfilter :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Band Spectral Subtraction ~~~~~~~~~~~~~~~~~~~~~~~~~ Let's filter using band spectral subtraction .. code-block:: default noisy_bs, sr = sp.filtersignal(noisy, sr = sr, filter_type = 'bandspec') .. code-block:: default ipd.Audio(noisy_bs,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy_bs, sr = sr, feature_type = 'signal', title = 'Noisy Speech: Band Spectral Subtraction', subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_005.png :alt: Noisy Speech: Band Spectral Subtraction :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Band Spectral Subtraction with Postfilter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Finally, let's filter using band spectral subtraction with a postfilter .. code-block:: default noisy_bspf, sr = sp.filtersignal(noisy, sr = sr, filter_type = 'bandspec', apply_postfilter = True) .. code-block:: default ipd.Audio(noisy_bspf,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy_bspf, sr = sr, feature_type = 'signal', title = 'Noisy Speech: Band Spectral Subtraction with Postfilter', subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_006.png :alt: Noisy Speech: Band Spectral Subtraction with Postfilter :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Filter: increase the scale ^^^^^^^^^^^^^^^^^^^^^^^^^^ Let's filter with a Wiener filter: .. code-block:: default filter_scale = 5 noisy_wf, sr = sp.filtersignal(noisy, sr=sr, filter_type = 'wiener', filter_scale = filter_scale) Wiener Filter ~~~~~~~~~~~~~ .. code-block:: default ipd.Audio(noisy_wf,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy_wf, sr = sr, feature_type = 'signal', title = 'Noisy Speech: Wiener Filter Scale {}'.format(filter_scale), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_007.png :alt: Noisy Speech: Wiener Filter Scale 5 :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) Wiener Filter with Postfilter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Let's filter with a Wiener filter and postfilter .. code-block:: default noisy_wfpf, sr = sp.filtersignal(noisy, sr = sr, filter_type = 'wiener', apply_postfilter = True, filter_scale = filter_scale) .. code-block:: default ipd.Audio(noisy_wfpf,rate = sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisy_wfpf, sr = sr, feature_type = 'signal', title = 'Noisy Speech: Wiener Filter with Postfilter Scale {}'.format(filter_scale), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_filter_out_noise_008.png :alt: Noisy Speech: Wiener Filter with Postfilter Scale 5 :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/feats.py:117: UserWarning: Due to matplotlib using AGG backend, cannot display plot. Therefore, the plot will be saved here: current working directory warnings.warn(msg) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.981 seconds) .. _sphx_glr_download_auto_examples_plot_filter_out_noise.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_filter_out_noise.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_filter_out_noise.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_