.. 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_SNR_add_noise_to_datasets.py: ========================================== Add Noise to Speech at Specific SNR Levels ========================================== Add noise to speech at specific signal-to-noise ratio levels. To see how soundpy implements this, see `soundpy.dsp.add_backgroundsound`. Let's import soundpy, and ipd for playing audio data .. code-block:: default import soundpy as sp import IPython.display as ipd Define the speech and noise data samples ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I will use speech and noise data from the soundpy repo. Designate path relevant for accessing audiodata .. code-block:: default sp_dir = '../../../' Speech sample: .. code-block:: default speech_sample = '{}audiodata/python.wav'.format(sp_dir) speech_sample = sp.utils.string2pathlib(speech_sample) # as pathlib object, can do the following: word = speech_sample.stem word .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 'python' Noise sample: .. code-block:: default noise_sample = '{}audiodata/background_samples/cafe.wav'.format(sp_dir) noise_sample = sp.utils.string2pathlib(noise_sample) # as pathlib object, can do the following: noise = noise_sample.stem noise .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 'cafe' Hear Clean Speech ~~~~~~~~~~~~~~~~~ I'm using a higher sample rate here as calculating SNR performs best upwards of 44100 Hz. .. code-block:: default sr = 44100 s, sr = sp.loadsound(speech_sample, sr = sr) ipd.Audio(s,rate=sr) .. only:: builder_html .. raw:: html

Hear Noise ~~~~~~~~~~ .. code-block:: default n, sr = sp.loadsound(noise_sample, sr = sr) ipd.Audio(n,rate=sr) .. only:: builder_html .. raw:: html

Hear Signal-to-Noise Ratio 20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound( speech_sample, noise_sample, sr = sr, snr = 20) ipd.Audio(noisyspeech_20snr,rate=sr) .. 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) .. only:: builder_html .. raw:: html

`snr20` is simply the measured SNR post adjustment fo the noise signal. This is useful to check that the indicated snr is at least close to the resulting snr. .. code-block:: default snr20 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 19.999968503067556 Hear Signal-to-Noise Ratio 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default noisyspeech_5snr, snr5 = sp.dsp.add_backgroundsound( speech_sample, noise_sample, sr = sr, snr = 5) ipd.Audio(noisyspeech_5snr,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default snr5 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 5.000011686690687 Visualize the Audio Samples ^^^^^^^^^^^^^^^^^^^^^^^^^^^ See Clean Speech (raw signal) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(speech_sample, feature_type='signal', sr = sr, title = 'Speech: ' + word.upper(), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_001.png :alt: Speech: PYTHON :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) See Clean Speech (stft) ~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(speech_sample, feature_type='stft', sr = sr, title = 'Speech: ' + word.upper(), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_002.png :alt: Speech: PYTHON :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) ~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(noise_sample, feature_type='signal', title = 'Noise: ' + noise.upper(), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_003.png :alt: Noise: CAFE :class: sphx-glr-single-img ~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(noise_sample, feature_type='stft', title = 'Noise: ' + noise.upper(), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_004.png :alt: Noise: CAFE :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) See Noisy Speech: SNR 20 (raw signal) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal', title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_005.png :alt: "PYTHON" with CAFE noise at SNR 20 :class: sphx-glr-single-img See Noisy Speech: SNR 20 (stft) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'stft', title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_006.png :alt: "PYTHON" with CAFE noise at SNR 20 :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) See Noisy Speech: SNR 5 (raw signal) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(noisyspeech_5snr, sr = sr, feature_type = 'signal', title = '"{}" with {} noise at SNR 5'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_007.png :alt: "PYTHON" with CAFE noise at SNR 5 :class: sphx-glr-single-img See Noisy Speech: SNR 5 (stft) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'stft', title = '"{}" with {} noise at SNR 5'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_008.png :alt: "PYTHON" with CAFE noise at SNR 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) Make Combined Sound Longer ^^^^^^^^^^^^^^^^^^^^^^^^^^ Pad Speech and Set Total Length ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: default noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound( speech_sample, noise_sample, sr = sr, snr = 20, pad_mainsound_sec = 1, total_len_sec = 4) .. 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) .. code-block:: default ipd.Audio(noisyspeech_20snr,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal', title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_009.png :alt: "PYTHON" with CAFE noise at SNR 20 :class: sphx-glr-single-img Make Combined Sound Shorter ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set Total Length ~~~~~~~~~~~~~~~~ .. code-block:: default noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound( speech_sample, noise_sample, sr = sr, snr = 20, total_len_sec = 0.75) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/airos/Projects/github/a-n-rose/Python-Sound-Tool/soundpy/dsp.py:531: UserWarning: The length of `audio_main` and `pad_mainsound_sec `exceeds `total_len_sec`. 5018 samples from `audio_main` will be cut off in the `combined` audio signal. warnings.warn('The length of `audio_main` and `pad_mainsound_sec `'+\ /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) .. code-block:: default ipd.Audio(noisyspeech_20snr,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal', title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_010.png :alt: "PYTHON" with CAFE noise at SNR 20 :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) Wrap the Background Sound ^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: default noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound( speech_sample, noise_sample, sr = sr, snr = 20, wrap = True, pad_mainsound_sec = 2, total_len_sec = 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) .. code-block:: default ipd.Audio(noisyspeech_20snr,rate=sr) .. only:: builder_html .. raw:: html

.. code-block:: default sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal', title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()), subprocess=True) .. image:: /auto_examples/images/sphx_glr_plot_SNR_add_noise_to_datasets_011.png :alt: "PYTHON" with CAFE noise at SNR 20 :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 5.720 seconds) .. _sphx_glr_download_auto_examples_plot_SNR_add_noise_to_datasets.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_SNR_add_noise_to_datasets.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_SNR_add_noise_to_datasets.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_