Bird
Raised Fist0
SciPydata~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand FFT transformation

    FFT converts data from time domain to frequency domain, revealing frequency components.
  2. Step 2: Purpose of filtering in frequency domain

    Filtering in frequency domain allows easy removal of unwanted noise or frequencies.
  3. Final Answer:

    To transform data into frequency domain for easier noise removal -> Option A
  4. 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

  1. Step 1: Identify correct scipy FFT function

    The scipy FFT module uses fft() to compute the Fast Fourier Transform.
  2. Step 2: Check syntax correctness

    The correct syntax is scipy.fft.fft(data), other options are invalid function names.
  3. Final Answer:

    fft_data = scipy.fft.fft(data) -> Option C
  4. 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?
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))
medium
A. [1. 0. 0. 4.]
B. [2. 2. 3. 3.]
C. [1.5 0. 0. 3.5]
D. [2. 0. 0. 2.]

Solution

  1. Step 1: Compute FFT and apply mask

    FFT of data is computed, then multiplied by mask [1,0,0,1], zeroing middle frequencies.
  2. Step 2: Compute inverse FFT and round

    Inverse FFT of masked data gives filtered signal; rounding to 2 decimals yields [2. 0. 0. 2.].
  3. Final Answer:

    [2. 0. 0. 2.] -> Option D
  4. Quick Check:

    Mask zeros middle frequencies, inverse FFT smooths signal [OK]
Hint: Mask zeros frequencies, inverse FFT returns filtered signal [OK]
Common Mistakes:
  • Forgetting to multiply FFT by mask
  • Not taking real part after inverse FFT
  • Rounding incorrectly or not at all
4. Identify the error in the following FFT-based filtering code snippet:
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)
medium
A. Data array should be complex type
B. Mask length does not match FFT data length
C. Using ifft instead of fft
D. Missing import for numpy

Solution

  1. Step 1: Check mask and FFT data lengths

    FFT of data has length 4, but mask length is 3, causing shape mismatch error.
  2. Step 2: Identify error cause

    Multiplying arrays of different lengths raises an error; mask must match FFT length.
  3. Final Answer:

    Mask length does not match FFT data length -> Option B
  4. 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

  1. Step 1: Calculate frequency bins correctly

    Use np.fft.fftfreq(N, 1/1000) to get frequencies in Hz for sampling rate 1000 Hz.
  2. Step 2: Create mask to keep frequencies ≤ 100 Hz

    Mask should be np.abs(freqs) <= 100 to keep low frequencies and remove higher ones.
  3. Step 3: Apply mask and inverse FFT

    Multiply FFT by mask, then apply inverse FFT and take real part for filtered signal.
  4. Final Answer:

    mask = np.abs(freqs) <= 100 with freqs = np.fft.fftfreq(N, 1/1000) -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard