Cleaning a Noisy Sine Wave

Learn how to filter out the noise from a sine wave.

We'll cover the following...

We won’t cover filtering in detail as it’s a very extensive topic. Instead, we’ll create a simple filter just by using the Fast Fourier Transform (FFT). The goal is to get comfortable with NumPy.

Here is the complete code:

Press + to interact
filtered_freq = []
index = 0
for f in freq:
# Filter between lower and upper limits
# Choosing 950, as closest to 1000. In real world, won't get exact numbers like \these
if index > 950 and index < 1050:
# Has a real value. I'm choosing >1, as many values are like 0.000000001 etc
if f > 1:
filtered_freq.append(f)
else:
filtered_freq.append(0)
else:
filtered_freq.append(0)
index += 1

Now, let’s go over it line by line:

In lines 1–3, we’ll create an empty list called filtered_freq. Remember, freq stores the ...