0
0
SciPydata~20 mins

FFT computation (fft) in SciPy - 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?
SciPy
import numpy as np
from scipy.fft import fft

signal = np.array([1, 0, 1, 0])
result = fft(signal)
print(result)
A[1.+0.j 0.+0.j 1.+0.j 0.+0.j]
B[2.+0.j 1.+0.j 0.+0.j 1.+0.j]
C[2.+0.j 0.+0.j 2.+0.j 0.+0.j]
D[4.+0.j 0.+0.j 0.+0.j 0.+0.j]
Attempts:
2 left
💡 Hint
Recall that FFT output length matches input and sums frequency components.
data_output
intermediate
1:30remaining
Length of FFT output array
Given a real input array of length 8, what is the length of the output array from scipy.fft.fft?
SciPy
import numpy as np
from scipy.fft import fft

x = np.arange(8)
y = fft(x)
print(len(y))
A4
B8
C16
D7
Attempts:
2 left
💡 Hint
FFT output length equals input length for scipy.fft.fft.
🔧 Debug
advanced
1:30remaining
Identify the error in FFT usage
What error does this code raise when trying to compute FFT on a list of strings?
SciPy
from scipy.fft import fft
signal = ['1', '0', '1', '0']
result = fft(signal)
ATypeError: ufunc 'add' did not contain a loop with signature matching types
BValueError: Input contains NaN
CSyntaxError: invalid syntax
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
FFT expects numeric input, not strings.
🚀 Application
advanced
2:30remaining
Using FFT to find dominant frequency
Given a sampled signal with 1000 points and sampling rate 1000 Hz, which code snippet correctly finds the frequency with the highest amplitude?
SciPy
import numpy as np
from scipy.fft import fft, fftfreq

sampling_rate = 1000
N = 1000
x = np.linspace(0, 1, N, endpoint=False)
signal = np.sin(2*np.pi*50*x) + 0.5*np.sin(2*np.pi*120*x)

# Find dominant frequency code here
A
yf = fft(signal)
xf = fftfreq(N, 1/sampling_rate)
idx = np.argmax(np.abs(yf))
print(xf[idx])
B
yf = fft(signal)
xf = fftfreq(N, sampling_rate)
idx = np.argmin(np.abs(yf))
print(xf[idx])
C
yf = fft(signal)
xf = fftfreq(N, 1/sampling_rate)
idx = np.argmin(np.abs(yf))
print(xf[idx])
D
yf = fft(signal)
xf = fftfreq(N, 1/sampling_rate)
idx = np.argmax(yf)
print(xf[idx])
Attempts:
2 left
💡 Hint
Use fftfreq with correct sample spacing and find index of max amplitude.
🧠 Conceptual
expert
2:00remaining
Understanding FFT symmetry for real inputs
For a real-valued input signal of length N, which statement about the FFT output is true?
AThe FFT output length is half the input length for real inputs.
BThe FFT output is always purely real for real inputs.
CThe FFT output contains only positive frequencies for real inputs.
DThe FFT output is symmetric, so the second half is the complex conjugate of the first half reversed.
Attempts:
2 left
💡 Hint
Think about conjugate symmetry property of FFT for real signals.