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

import soundpy as sp

Create a Signal

First let’s set what sample rate we want to use

sr = 44100

Let’s create a signal of 10 Hz

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)
Signal: 10 Hz

Out:

/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

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)
Signal: 20 Hz

Out:

/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:

sig3 = sig1 + sig2
sp.plotsound(sig3, sr=sr, feature_type = 'signal',
               title='Mixed Signals: {} Hz + {} Hz'.format(sig1_hz, sig2_hz),
               subprocess=True)
Mixed Signals: 10 Hz + 20 Hz

Out:

/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:

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)
Random Noise

Out:

/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

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)
Signal + Noise: 40 SNR

Out:

/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

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)
Signal + Noise: 20 SNR

Out:

/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

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)
Signal + Noise: 10 SNR

Out:

/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

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)
Signal + Noise: 0 SNR

Out:

/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

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)
Signal + Noise: -10 SNR

Out:

/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)

Total running time of the script: ( 0 minutes 3.053 seconds)

Gallery generated by Sphinx-Gallery