FFT-based filtering helps us remove unwanted parts of a signal or data by changing it into frequencies, then filtering, and changing it back.
FFT-based filtering in SciPy
from scipy.fft import fft, ifft # fft_data = fft(original_data) # filtered_fft = fft_data * filter_mask # filtered_data = ifft(filtered_fft)
Use fft to convert data to frequency space.
Multiply by a filter mask to keep or remove frequencies.
from scipy.fft import fft, ifft import numpy as np # Create a simple signal signal = np.array([1, 2, 3, 4]) # FFT freq_data = fft(signal) # Simple filter: keep all frequencies filtered_freq = freq_data * np.array([1, 1, 1, 1]) # Inverse FFT filtered_signal = ifft(filtered_freq)
from scipy.fft import fft, ifft import numpy as np signal = np.array([1, 2, 3, 4]) freq_data = fft(signal) # Filter: zero out high frequencies filter_mask = np.array([1, 1, 0, 0]) filtered_freq = freq_data * filter_mask filtered_signal = ifft(filtered_freq)
This program creates a signal with slow and fast parts, removes fast noise using FFT filtering, and shows both signals on a plot.
from scipy.fft import fft, ifft import numpy as np import matplotlib.pyplot as plt # Create a noisy signal: slow wave + fast noise np.random.seed(0) time = np.linspace(0, 1, 500) slow_wave = np.sin(2 * np.pi * 5 * time) # 5 Hz noise = 0.5 * np.sin(2 * np.pi * 50 * time) # 50 Hz noise signal = slow_wave + noise # FFT freq_data = fft(signal) # Frequency array freq = np.fft.fftfreq(len(signal), d=time[1] - time[0]) # Create filter mask: keep frequencies below 10 Hz filter_mask = np.abs(freq) < 10 # Apply filter filtered_freq = freq_data * filter_mask # Inverse FFT to get filtered signal filtered_signal = ifft(filtered_freq) # Plot original and filtered signals plt.figure(figsize=(10, 6)) plt.plot(time, signal, label='Original Signal') plt.plot(time, filtered_signal.real, label='Filtered Signal', linewidth=2) plt.xlabel('Time (seconds)') plt.ylabel('Amplitude') plt.title('FFT-based Filtering Example') plt.legend() plt.show()
FFT output is complex numbers; use .real to get real part after inverse FFT.
Frequency filtering works by zeroing or reducing unwanted frequency components.
Make sure your filter mask matches the FFT frequency array size.
FFT-based filtering changes data to frequency space to remove unwanted parts.
Use fft and ifft from scipy.fft for this process.
Apply a mask to keep or remove frequencies, then convert back to original space.
Practice
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 AQuick Check:
FFT filtering = frequency domain noise removal [OK]
- Confusing FFT with sorting or averaging
- Thinking FFT changes data to text
- Assuming FFT directly cleans data without filtering
data using scipy?Solution
Step 1: Identify correct scipy FFT function
The scipy FFT module usesfft()to compute the Fast Fourier Transform.Step 2: Check syntax correctness
The correct syntax isscipy.fft.fft(data), other options are invalid function names.Final Answer:
fft_data = scipy.fft.fft(data) -> Option CQuick Check:
Correct FFT call = scipy.fft.fft(data) [OK]
- Using incorrect function names like fft_transform
- Omitting the .fft part after scipy.fft
- Confusing with numpy.fft functions
filtered_signal?
import numpy as np from scipy.fft import fft, ifft data = np.array([1, 2, 3, 4]) fft_data = fft(data) mask = np.array([1, 0, 0, 1]) filtered_fft = fft_data * mask filtered_signal = ifft(filtered_fft).real print(np.round(filtered_signal, 2))
Solution
Step 1: Compute FFT and apply mask
FFT of data is computed, then multiplied by mask [1,0,0,1], zeroing middle frequencies.Step 2: Compute inverse FFT and round
Inverse FFT of masked data gives filtered signal; rounding to 2 decimals yields [2. 0. 0. 2.].Final Answer:
[2. 0. 0. 2.] -> Option DQuick Check:
Mask zeros middle frequencies, inverse FFT smooths signal [OK]
- Forgetting to multiply FFT by mask
- Not taking real part after inverse FFT
- Rounding incorrectly or not at all
import numpy as np from scipy.fft import fft, ifft data = np.array([1, 2, 3, 4]) fft_data = fft(data) mask = np.array([1, 0, 0]) filtered_fft = fft_data * mask filtered_signal = ifft(filtered_fft).real print(filtered_signal)
Solution
Step 1: Check mask and FFT data lengths
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 BQuick Check:
Mask length must equal FFT length [OK]
- Using mask shorter than FFT result
- Confusing ifft with fft usage
- Assuming data must be complex initially
signal. You want to remove frequencies above 100 Hz using FFT-based filtering. Which code snippet correctly implements this filter?Solution
Step 1: Calculate frequency bins correctly
Usenp.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 benp.abs(freqs) <= 100to 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 AQuick Check:
Mask keeps frequencies ≤ 100 Hz [OK]
- Ignoring sampling rate in frequency calculation
- Using wrong comparison operators in mask
- Not taking absolute value of frequencies
Ready to prove it?
Pass the test and this pattern moves unknown
