0
0
NumPydata~20 mins

FFT with np.fft module in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FFT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of FFT on a simple signal
What is the output of the following code that computes the FFT of a simple signal?
NumPy
import numpy as np
signal = np.array([1, 0, -1, 0])
fft_result = np.fft.fft(signal)
print(np.round(fft_result, 2))
A[0.+0.j 0.+0.j 0.+0.j 0.+0.j]
B[0.+0.j 0.+2.j 0.+0.j 0.-2.j]
C[0.+0.j 2.+0.j 0.+0.j 2.+0.j]
D[0.+0.j 1.+0.j 0.+0.j 1.+0.j]
Attempts:
2 left
💡 Hint
Recall that FFT output is complex and symmetric for real inputs.
data_output
intermediate
1:00remaining
Length of FFT output array
Given a real input array of length 8, what is the length of the output array from np.fft.fft?
NumPy
import numpy as np
x = np.arange(8)
fft_x = np.fft.fft(x)
print(len(fft_x))
A4
B16
C7
D8
Attempts:
2 left
💡 Hint
FFT output length equals input length for np.fft.fft.
🔧 Debug
advanced
1:30remaining
Identify the error in FFT frequency calculation
What error does the following code raise when trying to compute FFT frequencies for a signal of length 5? import numpy as np freqs = np.fft.fftfreq(5, d=0) print(freqs)
NumPy
import numpy as np
freqs = np.fft.fftfreq(5, d=0)
print(freqs)
AZeroDivisionError
BTypeError
CValueError
DNo error, prints frequencies
Attempts:
2 left
💡 Hint
Check the meaning of the parameter d in fftfreq.
🚀 Application
advanced
2:30remaining
Using FFT to find dominant frequency
Given a noisy signal composed of two sine waves at 5 Hz and 20 Hz sampled at 100 Hz, which code snippet correctly identifies the dominant frequency using FFT?
NumPy
import numpy as np
fs = 100
T = 1/fs
t = np.linspace(0, 1, fs, endpoint=False)
signal = np.sin(2*np.pi*5*t) + 0.5*np.sin(2*np.pi*20*t) + 0.1*np.random.randn(fs)
A
fft_vals = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(signal), T)
dominant_freq = freqs[np.argmax(np.abs(fft_vals))]
print(round(dominant_freq, 1))
B
fft_vals = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(signal), 1)
dominant_freq = freqs[np.argmax(np.abs(fft_vals))]
print(round(dominant_freq, 1))
C
fft_vals = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(signal))
dominant_freq = freqs[np.argmax(np.abs(fft_vals))]
print(round(dominant_freq, 1))
D
fft_vals = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(signal), 0)
dominant_freq = freqs[np.argmax(np.abs(fft_vals))]
print(round(dominant_freq, 1))
Attempts:
2 left
💡 Hint
Sample spacing d should be the inverse of sampling frequency fs.
🧠 Conceptual
expert
2:00remaining
Interpretation of FFT output symmetry
For a real-valued input signal of length N, which statement about the FFT output from np.fft.fft is true?
AThe FFT output is always purely real.
BThe FFT output contains only positive frequencies.
CThe FFT output is symmetric: the second half is the complex conjugate of the first half reversed.
DThe FFT output length is half the input length.
Attempts:
2 left
💡 Hint
Think about properties of FFT for real inputs.