0
0
SciPydata~10 mins

FFT-based filtering in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - FFT-based filtering
Input signal
Apply FFT -> frequency domain
Create filter mask
Multiply FFT by filter mask
Apply inverse FFT -> filtered signal
Output filtered signal
The signal is transformed to frequency domain using FFT, filtered by multiplying with a mask, then transformed back to time domain.
Execution Sample
SciPy
import numpy as np
from scipy.fft import fft, ifft

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
X = fft(x)
mask = np.array([1, 1, 0, 0, 0, 0, 0, 0])
Y = X * mask
y = ifft(Y)
This code applies a simple low-pass filter by zeroing out high frequency components using FFT and inverse FFT.
Execution Table
StepActionVariableValue/Result
1Input signal definedx[1 2 3 4 5 6 7 8]
2Apply FFT to xX[36.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j -4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j]
3Create filter maskmask[1 1 0 0 0 0 0 0]
4Multiply FFT by maskY[36.+0.j -4.+9.65685425j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
5Apply inverse FFT to Yy[4. +0.j 0.5+1.20710678j 0. +0.j 0.5-1.20710678j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
6Output filtered signalreal part of y[4. 0.5 0. 0.5 0. 0. 0. 0.]
💡 Filtering complete after inverse FFT; high frequencies zeroed out by mask.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
x[1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8]
XN/A[36.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j -4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j][36.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j -4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j][36.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j -4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j][36.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j -4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j][36.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j -4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j]
maskN/AN/A[1 1 0 0 0 0 0 0][1 1 0 0 0 0 0 0][1 1 0 0 0 0 0 0][1 1 0 0 0 0 0 0]
YN/AN/AN/A[36.+0.j -4.+9.65685425j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j][36.+0.j -4.+9.65685425j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j][36.+0.j -4.+9.65685425j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
yN/AN/AN/AN/A[4. +0.j 0.5+1.20710678j 0. +0.j 0.5-1.20710678j 0. +0.j 0. +0.j 0. +0.j 0. +0.j][4. +0.j 0.5+1.20710678j 0. +0.j 0.5-1.20710678j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
Key Moments - 3 Insights
Why do some values in the filtered signal have imaginary parts after inverse FFT?
The inverse FFT can produce small imaginary parts due to numerical errors, but the real part is the meaningful filtered signal (see step 5 in execution_table).
Why do we multiply the FFT by a mask instead of filtering the original signal directly?
Multiplying in frequency domain corresponds to filtering specific frequencies, which is more efficient and precise than time-domain filtering (see step 4 in execution_table).
Why are some frequency components set to zero in the mask?
Zeroing frequency components removes those frequencies from the signal, acting as a filter to keep only desired frequencies (see step 3 and 4 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the FFT value of the first frequency component?
A-4 + 9.65j
B36 + 0j
C0 + 0j
D4 + 0j
💡 Hint
Check the 'Value/Result' column for variable X at step 2.
At which step does the frequency mask get applied to the FFT result?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where variable Y is defined as X multiplied by mask.
If the mask was all ones, what would be the output signal after inverse FFT?
AOnly high frequencies remain
BA zero signal
CThe original input signal
DOnly low frequencies remain
💡 Hint
Refer to variable_tracker for mask and its effect on Y and y.
Concept Snapshot
FFT-based filtering steps:
1. Transform signal to frequency domain with FFT.
2. Create a frequency mask to keep or remove frequencies.
3. Multiply FFT by mask to filter.
4. Use inverse FFT to get filtered signal back.
Key: Multiplying in frequency domain filters signal components.
Full Transcript
FFT-based filtering transforms a signal from time domain to frequency domain using FFT. Then, a filter mask is created to select which frequencies to keep or remove. The FFT result is multiplied by this mask, zeroing out unwanted frequencies. Finally, inverse FFT converts the filtered frequency data back to time domain, producing the filtered signal. Imaginary parts after inverse FFT are small numerical errors and the real part is the filtered output. This method efficiently filters signals by frequency components.