0
0
SciPydata~20 mins

FFT-based filtering in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FFT Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of FFT and inverse FFT on a simple signal
What is the output of the following code snippet that applies FFT and then inverse FFT on a simple signal?
SciPy
import numpy as np
from scipy.fft import fft, ifft

signal = np.array([1, 2, 3, 4])
fft_signal = fft(signal)
recovered_signal = ifft(fft_signal)
print(np.round(recovered_signal.real, 2))
A[1. 2. 3. 4.]
B[0. 0. 0. 0.]
C[1. 2. 3. 5.]
D[4. 3. 2. 1.]
Attempts:
2 left
💡 Hint
FFT followed by inverse FFT should recover the original signal.
data_output
intermediate
1:30remaining
Length of filtered signal after FFT-based low-pass filtering
Given a signal of length 8, after applying FFT-based low-pass filtering by zeroing out frequencies above index 3 and then inverse FFT, what is the length of the resulting filtered signal?
SciPy
import numpy as np
from scipy.fft import fft, ifft

signal = np.arange(8)
fft_signal = fft(signal)
fft_signal[4:] = 0  # zero out frequencies above index 3
filtered_signal = ifft(fft_signal)
print(len(filtered_signal))
A0
B4
C7
D8
Attempts:
2 left
💡 Hint
FFT and inverse FFT do not change the length of the signal.
🔧 Debug
advanced
2:00remaining
Identify the error in FFT-based filtering code
What error does the following code produce when trying to apply FFT-based filtering?
SciPy
import numpy as np
from scipy.fft import fft, ifft

signal = np.array([1, 2, 3, 4])
fft_signal = fft(signal)
# Incorrect slicing to zero out high frequencies
fft_signal[4:] = 0
filtered_signal = ifft(fft_signal)
print(filtered_signal)
AValueError: operands could not be broadcast together with shapes (4,) (3,)
BIndexError: index 4 is out of bounds for axis 0 with size 4
CTypeError: unsupported operand type(s) for +: 'int' and 'complex'
DNo error, prints filtered signal
Attempts:
2 left
💡 Hint
Check the length of the FFT output and the slice used.
🧠 Conceptual
advanced
1:30remaining
Effect of zeroing out frequency components in FFT
What is the effect on the time-domain signal when you zero out the high-frequency components in its FFT representation before applying inverse FFT?
AThe signal becomes more noisy with added high-frequency noise
BThe signal becomes completely zero
CThe signal becomes smoother by removing rapid changes
DThe signal length doubles
Attempts:
2 left
💡 Hint
High frequencies correspond to rapid changes in the signal.
🚀 Application
expert
2:30remaining
Identify the correct code for band-pass filtering using FFT
Which code snippet correctly applies a band-pass filter to keep only frequencies between indices 2 and 5 (inclusive) in the FFT of a signal, then recovers the filtered signal?
A
fft_signal = fft(signal)
filtered_fft = fft_signal.copy()
filtered_fft[:2] = 0
filtered_fft[6:] = 0
filtered_signal = ifft(filtered_fft)
B
fft_signal = fft(signal)
filtered_fft = np.zeros_like(fft_signal)
filtered_fft[2:6] = fft_signal[2:6]
filtered_signal = ifft(filtered_fft)
C
fft_signal = fft(signal)
filtered_fft = fft_signal
filtered_fft[2:5] = 0
filtered_signal = ifft(filtered_fft)
D
fft_signal = fft(signal)
filtered_fft = np.zeros_like(fft_signal)
filtered_fft[3:5] = fft_signal[3:5]
filtered_signal = ifft(filtered_fft)
Attempts:
2 left
💡 Hint
Band-pass means keep frequencies in a range and zero out others.