📖 Scenario: Imagine you have a noisy signal from a sensor measuring temperature over time. You want to remove the high-frequency noise to see the smooth trend clearly.
🎯 Goal: You will create a simple signal, set a frequency threshold, apply FFT-based filtering to remove noise, and then display the filtered signal.
📋 What You'll Learn
Create a time series signal with noise using numpy arrays
Set a frequency threshold to separate noise from the main signal
Use FFT to transform the signal to frequency domain
Filter out frequencies above the threshold
Use inverse FFT to get the filtered signal back
Print the filtered signal array
💡 Why This Matters
🌍 Real World
FFT-based filtering is used in many fields like audio processing, sensor data cleaning, and image processing to remove unwanted noise and improve signal quality.
💼 Career
Understanding FFT and filtering is important for data scientists and engineers working with time series data, signal processing, and any application where noise reduction is needed.
Progress0 / 4 steps
1
Create a noisy signal
Create a numpy array called time with 100 points from 0 to 1 using np.linspace(0, 1, 100). Then create a numpy array called signal that is sin(2 * pi * 5 * time) plus random noise using np.random.normal(0, 0.5, 100).
SciPy
Hint
Use np.linspace to create time. Use np.sin and np.random.normal to create signal.
2
Set frequency threshold
Create a variable called freq_threshold and set it to 10. This will be the cutoff frequency to keep in the signal.
SciPy
Hint
Just create a variable freq_threshold and assign it the value 10.
3
Apply FFT and filter frequencies
Use np.fft.fft(signal) to get the FFT of signal and store it in fft_signal. Use np.fft.fftfreq(len(signal), d=1/100) to get the frequency bins and store in freqs. Then set all values in fft_signal to zero where the absolute value of freqs is greater than freq_threshold.
SciPy
Hint
Use np.fft.fft and np.fft.fftfreq. Then zero out frequencies above the threshold.
4
Get filtered signal and print
Use np.fft.ifft(fft_signal) to get the filtered signal and store it in filtered_signal. Then print filtered_signal.real to show the real part of the filtered signal.
SciPy
Hint
Use np.fft.ifft and print the real part of the result.
Practice
(1/5)
1. What is the main purpose of using FFT-based filtering in data analysis?
easy
A. To transform data into frequency domain for easier noise removal
B. To sort data values in ascending order
C. To calculate the average of data points
D. To convert data into text format
Solution
Step 1: Understand FFT transformation
FFT converts data from time domain to frequency domain, revealing frequency components.
Step 2: Purpose of filtering in frequency domain
Filtering in frequency domain allows easy removal of unwanted noise or frequencies.
Final Answer:
To transform data into frequency domain for easier noise removal -> Option A
Quick Check:
FFT filtering = frequency domain noise removal [OK]
Hint: FFT helps remove noise by working in frequency domain [OK]
Common Mistakes:
Confusing FFT with sorting or averaging
Thinking FFT changes data to text
Assuming FFT directly cleans data without filtering
2. Which of the following is the correct way to compute the FFT of a 1D numpy array data using scipy?
easy
A. fft_data = scipy.fft_transform(data)
B. fft_data = scipy.fft(data)
C. fft_data = scipy.fft.fft(data)
D. fft_data = scipy.fft_fft(data)
Solution
Step 1: Identify correct scipy FFT function
The scipy FFT module uses fft() to compute the Fast Fourier Transform.
Step 2: Check syntax correctness
The correct syntax is scipy.fft.fft(data), other options are invalid function names.
Final Answer:
fft_data = scipy.fft.fft(data) -> Option C
Quick Check:
Correct FFT call = scipy.fft.fft(data) [OK]
Hint: Use scipy.fft.fft() to compute FFT of array [OK]
Common Mistakes:
Using incorrect function names like fft_transform
Omitting the .fft part after scipy.fft
Confusing with numpy.fft functions
3. Given the code below, what will be the output of filtered_signal?
FFT of data has length 4, but mask length is 3, causing shape mismatch error.
Step 2: Identify error cause
Multiplying arrays of different lengths raises an error; mask must match FFT length.
Final Answer:
Mask length does not match FFT data length -> Option B
Quick Check:
Mask length must equal FFT length [OK]
Hint: Mask length must match FFT output length [OK]
Common Mistakes:
Using mask shorter than FFT result
Confusing ifft with fft usage
Assuming data must be complex initially
5. You have a noisy signal sampled at 1000 Hz stored in signal. You want to remove frequencies above 100 Hz using FFT-based filtering. Which code snippet correctly implements this filter?
hard
A. import numpy as np
from scipy.fft import fft, ifft
N = len(signal)
freqs = np.fft.fftfreq(N, 1/1000)
fft_signal = fft(signal)
mask = np.abs(freqs) <= 100
filtered_fft = fft_signal * mask
filtered_signal = ifft(filtered_fft).real
B. import numpy as np
from scipy.fft import fft, ifft
N = len(signal)
freqs = np.fft.fftfreq(N)
fft_signal = fft(signal)
mask = freqs < 100
filtered_fft = fft_signal * mask
filtered_signal = ifft(filtered_fft).real
C. import numpy as np
from scipy.fft import fft, ifft
N = len(signal)
freqs = np.fft.fftfreq(N, 1/1000)
fft_signal = fft(signal)
mask = freqs > 100
filtered_fft = fft_signal * mask
filtered_signal = ifft(filtered_fft).real
D. import numpy as np
from scipy.fft import fft, ifft
N = len(signal)
freqs = np.fft.fftfreq(N, 1/1000)
fft_signal = fft(signal)
mask = np.abs(freqs) >= 100
filtered_fft = fft_signal * mask
filtered_signal = ifft(filtered_fft).real
Solution
Step 1: Calculate frequency bins correctly
Use np.fft.fftfreq(N, 1/1000) to get frequencies in Hz for sampling rate 1000 Hz.
Step 2: Create mask to keep frequencies ≤ 100 Hz
Mask should be np.abs(freqs) <= 100 to keep low frequencies and remove higher ones.
Step 3: Apply mask and inverse FFT
Multiply FFT by mask, then apply inverse FFT and take real part for filtered signal.
Final Answer:
mask = np.abs(freqs) <= 100 with freqs = np.fft.fftfreq(N, 1/1000) -> Option A
Quick Check:
Mask keeps frequencies ≤ 100 Hz [OK]
Hint: Use np.abs(freqs) ≤ cutoff for low-pass mask [OK]
Common Mistakes:
Ignoring sampling rate in frequency calculation
Using wrong comparison operators in mask
Not taking absolute value of frequencies
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.