Adding noise to audio clips
This article discusses how to add noise to audio data (or any other data). This can be important for many applications such as evaluating the performance of machine learning models.
For example a deep learning model which classifies audio data can be affected by noise. So we can alter the original signal samples with noises of varying signal to noise ratios and evaluate the performance of the model under these noisy conditions.
To read audio .wav into an array from file we can use the code below. I have used python for examples shown here
import librosasignal, sr = librosa.load(“path/to/audio.wav”)
To obtain a plot of the signal I used for these experiments (me saying “Leave my dog alone!”)
import matplotlib.pyplot as pltplt.plot(signal)
Signal to Noise Ratio
Signal to noise ratio (SNR) can be defined as follows :
where RMS_signal is the RMS value of signal and RMS_noise is that of noise. log is the logarithm of 10
we can use the code below to calculate RMS value of a signal
import numpy as npRMS=math.sqrt(np.mean(signal**2))
Now its the time to add some noise to the signal
We describe 2 types of noises that can be added to audio data
- Additive White Gaussian Noise (AWGN)
- Real world noises
Please refer to the following GitHub repo for the code
Additive White Gaussian Noise (AWGN)
This kind of noise can be added (arithmetic element-wise addition) to the signal. Also its mean value is zero (randomly sampled from a Gaussian distribution with mean value of zero. standard deviation can vary). It contains all the frequency components in an equal manner (hence “white” noise).
AWGN is important because it is easier to model for analytical analyzes and it’s easier to generate. But it may not represent realistic noise conditions for some applications.
From the code in the GitHub repo, we can use the function get_white_noise to generate AWGN of desired SNR. This has a mean value of approximately 0.0
How to generate AWGN
From SNR definition equation, we can obtain
Here RMS_required is the required RMS of the noise that we should generate.
Also,
Here STD_noise is the standard deviation of noise.
Since mean value of AWGN is zero we can see that
To generate noise, we can sample from a Gaussian distribution with mean =0 and standard deviation = RMS_required
noise=np.random.normal(0, STD_n, signal.shape[0])
If we plot the AWGN we generated,
Frequency analysis of AWGN
To analyze the frequency content we use fast Fourier transformation (refer the code)
X=np.fft.rfft(noise)radius,angle=to_polar(X)
We can see that AWGN we generated has somewhat equal frequency distribution. This satisfies the “white” condition for AWGN.
Finally we can add the noise to signal by
signal_noise = signal+noise
where signal_noise is the noise-added signal
Mixing with real world noises
Here we do not generate any data. Instead we use another audio clip which contains noise. What exactly we mean by noise depends on the particular application.
To do this, we need to calculate the RMS value of the signal and the provided noise. Then we modify the noise (multiply each element of noise by a constant) so the resulting noise would have the required RMS value that would give us the desired SNR.
As in the case of AWGN, we can obtain the RMS_required of noise. Then we should “change” our noise audio clip so that it would have ans RMS value of RMS_required
What happens if we multiply the whole signal (element-wise) by a constant (say “a”) ? Its RMS value gets multiplied by “a” too according to the equation below.
We can calculate the RMS_noise using the noise clip provided. Then we can find the constant “a” using the equation above. After that we can modify the noise by multiplying it with “a” element-wise.
The noise audio clip I used (water running from a faucet):
To listen to the signal and noise I used and also to the noise-added audio files that were created by adding noise to the signal, go to
Thanks for reading!! Please leave a comment/feedback!!!
Follow me on Twitter https://twitter.com/lahirunuwan