Plotting a Noisy Sine Wave

Learn how to add noise into an audio file and then plot the data.

Adding noise to a sound wave

In this lesson, we’ll generate a sine wave, add noise to it, and then filter the noise. Let’s start with the code.

Frequency is the number of times a wave repeats a second.

frequency = 1000
noisy_freq = 50
num_samples = 48000

The sampling rate of the analog to digital conversion is:

sampling_rate = 48000.0

The primary frequency is 1000Hz, and we’ll add 50Hz of noise to it.

#Create the sine wave and noise
sine_wave = [np.sin(2 * np.pi * frequency * x1 / sampling_rate) for x1 in range(num_\
samples)]
sine_noise = [np.sin(2 * np.pi * noisy_freq * x1/  sampling_rate) for x1 in range(nu\
m_samples)]
#Convert them to numpy arrays
sine_wave = np.array(sine_wave)
sine_noise = np.array(sine_noise)

We’ll generate two sine waves, one for the signal and one for the noise, and convert them to NumPy arrays.

Now we’ll add these two waves to create a noisy signal.

combined_signal = sine_wave + sine_noise

We’re adding the noise to the signal. As we mentioned earlier, this is only possible with NumPy. With standard Python, we’d have to use a for loop or use list comprehensions. With NumPy, we can add two arrays like they were regular numbers, and NumPy takes care of the low-level detail for us.

All of the above code has already been added in the code widget below, starting from line 17 onwards.

Plotting the original and noisy sine wave

Get hands-on with 1200+ tech skills courses.