.. 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_signals_and_features.py:
=======================
Create and Plot Signals
=======================
Create and plot signals / noise; combine them at a specific SNR.
To see how soundpy implements this, see `soundpy.dsp.generate_sound`,
`soundpy.dsp.generate_noise` and `soundpy.dsp.add_backgroundsound`.
Let's import soundpy
.. code-block:: default
import soundpy as sp
Create a Signal
^^^^^^^^^^^^^^^
First let's set what sample rate we want to use
.. code-block:: default
sr = 44100
Let's create a signal of 10 Hz
.. code-block:: default
sig1_hz = 10
sig1, sr = sp.generate_sound(freq=sig1_hz, amplitude = 0.4, sr=sr, dur_sec=1)
sp.plotsound(sig1, sr=sr, feature_type = 'signal',
title = 'Signal: {} Hz'.format(sig1_hz), subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_001.png
:alt: Signal: 10 Hz
: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)
Let's create a signal of 20 Hz
.. code-block:: default
sig2_hz = 20
sig2, sr = sp.generate_sound(freq=sig2_hz, amplitude= 0.4, sr=sr, dur_sec=1)
sp.plotsound(sig2, sr=sr, feature_type = 'signal',
title = 'Signal: {} Hz'.format(sig2_hz), subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_002.png
:alt: Signal: 20 Hz
: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)
Combine Signals
^^^^^^^^^^^^^^^
Add them together and see what they look like:
.. code-block:: default
sig3 = sig1 + sig2
sp.plotsound(sig3, sr=sr, feature_type = 'signal',
title='Mixed Signals: {} Hz + {} Hz'.format(sig1_hz, sig2_hz),
subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_003.png
:alt: Mixed Signals: 10 Hz + 20 Hz
: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)
Generate Pseudo-Random Noise
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Create noise to add to the signal:
.. code-block:: default
noise = sp.generate_noise(len(sig3), amplitude=0.02, random_seed=40)
sp.plotsound(noise, sr=sr, feature_type = 'signal',
title='Random Noise', subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_004.png
:alt: Random Noise
: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)
Control SNR: Adding a Background Sound
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Add noise at signal-to-noise ratio of 40
.. code-block:: default
sig_noisy, snr = sp.dsp.add_backgroundsound(
audio_main = sig3,
audio_background = noise,
sr = sr,
snr = 40,
clip_at_zero = False)
# keep energy between 1 and -1
sig_noisy = sp.dsp.scalesound(sig_noisy, max_val=1)
sp.plotsound(sig_noisy, sr=sr, feature_type = 'signal', title='Signal + Noise: 40 SNR',
subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_005.png
:alt: Signal + Noise: 40 SNR
: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/dsp.py:769: UserWarning:
Warning: `soundpy.dsp.clip_at_zero` found no samples close to zero. Clipping was not applied.
warnings.warn(msg)
/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)
Add noise at signal-to-noise ratio of 20
.. code-block:: default
sig_noisy, snr = sp.dsp.add_backgroundsound(
audio_main = sig3,
audio_background = noise,
sr = sr,
snr = 20)
# keep energy between 1 and -1
sig_noisy = sp.dsp.scalesound(sig_noisy, max_val=1)
sp.plotsound(sig_noisy, sr=sr, feature_type = 'signal', title='Signal + Noise: 20 SNR',
subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_006.png
:alt: Signal + Noise: 20 SNR
: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/dsp.py:769: UserWarning:
Warning: `soundpy.dsp.clip_at_zero` found no samples close to zero. Clipping was not applied.
warnings.warn(msg)
/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)
Add noise at signal-to-noise ratio of 10
.. code-block:: default
sig_noisy, snr = sp.dsp.add_backgroundsound(
audio_main = sig3,
audio_background = noise,
sr = sr,
snr = 10)
# keep energy between 1 and -1
sig_noisy = sp.dsp.scalesound(sig_noisy, max_val=1)
sp.plotsound(sig_noisy, sr=sr, feature_type = 'signal', title='Signal + Noise: 10 SNR',
subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_007.png
:alt: Signal + Noise: 10 SNR
: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/dsp.py:769: UserWarning:
Warning: `soundpy.dsp.clip_at_zero` found no samples close to zero. Clipping was not applied.
warnings.warn(msg)
/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)
Add noise at signal-to-noise ratio of 0
.. code-block:: default
sig_noisy, snr = sp.dsp.add_backgroundsound(
audio_main = sig3,
audio_background = noise,
sr = sr,
snr = 0)
# keep energy between 1 and -1
sig_noisy = sp.dsp.scalesound(sig_noisy, max_val=1)
sp.plotsound(sig_noisy, sr=sr, feature_type = 'signal', title='Signal + Noise: 0 SNR',
subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_008.png
:alt: Signal + Noise: 0 SNR
: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)
Add noise at signal-to-noise ratio of -10
.. code-block:: default
sig_noisy, snr = sp.dsp.add_backgroundsound(
audio_main = sig3,
audio_background = noise,
sr = sr,
snr = -10)
# keep energy between 1 and -1
sig_noisy = sp.dsp.scalesound(sig_noisy, max_val=1)
sp.plotsound(sig_noisy, sr=sr, feature_type = 'signal', title='Signal + Noise: -10 SNR',
subprocess=True)
.. image:: /auto_examples/images/sphx_glr_plot_signals_and_features_009.png
:alt: Signal + Noise: -10 SNR
: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.053 seconds)
.. _sphx_glr_download_auto_examples_plot_signals_and_features.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_signals_and_features.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_signals_and_features.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_