Bird
Raised Fist0
SciPydata~20 mins

FFT computation (fft) in SciPy - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the scipy.fft.fft() function do in data analysis?
easy
A. It converts time-domain data into frequency-domain data.
B. It sorts data in ascending order.
C. It calculates the average of the data points.
D. It removes noise from the data.

Solution

  1. Step 1: Understand the purpose of FFT

    FFT stands for Fast Fourier Transform, which changes data from time domain to frequency domain. This function computes the FFT, converting time-based signals into frequency components.
  2. Final Answer:

    It converts time-domain data into frequency-domain data. -> Option A
  3. Quick Check:

    FFT = Time to Frequency [OK]
Hint: FFT changes time data to frequency data fast [OK]
Common Mistakes:
  • Thinking FFT sorts or averages data
  • Confusing FFT with noise removal
  • Assuming FFT works only on sorted data
2. Which of the following is the correct way to import and use FFT from scipy?
easy
A. from scipy import fft result = fft(data)
B. from scipy.fft import fft result = fft(data)
C. import scipy.fft result = scipy.fft(data)
D. import fft from scipy result = fft(data)

Solution

  1. Step 1: Check correct import syntax

    The correct import is from scipy.fft import fft, not from scipy import fft or import fft from scipy. After importing, call fft(data) directly. Using scipy.fft(data) is incorrect because scipy.fft is a module, not a function.
  2. Final Answer:

    from scipy.fft import fft result = fft(data) -> Option B
  3. Quick Check:

    Correct import and call = from scipy.fft import fft result = fft(data) [OK]
Hint: Import fft directly from scipy.fft module [OK]
Common Mistakes:
  • Trying to import fft directly from scipy
  • Calling scipy.fft(data) instead of fft(data)
  • Using wrong import syntax causing errors
3. What is the output of this code snippet?
import numpy as np
from scipy.fft import fft
x = np.array([1, 0, -1, 0])
result = fft(x)
print(np.round(result, 2))
medium
A. [0.+0.j 2.+0.j 0.+0.j 2.+0.j]
B. [0.+0.j 0.+0.j 0.+0.j 0.+0.j]
C. [0.+0.j 0.-2.j 0.+0.j 0.+2.j]
D. [0.+0.j 2.+0.j 0.+0.j -2.+0.j]

Solution

  1. Step 1: Understand input signal

    The input array is [1, 0, -1, 0], a simple wave with 4 points. FFT of this sequence results in [0.+0.j, 2.+0.j, 0.+0.j, -2.+0.j].
  2. Final Answer:

    [0.+0.j 2.+0.j 0.+0.j -2.+0.j] -> Option D
  3. Quick Check:

    FFT of wave = [0.+0.j 2.+0.j 0.+0.j -2.+0.j] [OK]
Hint: FFT of simple wave shows peaks at frequency bins [OK]
Common Mistakes:
  • Miscalculating the last frequency bin
  • Confusing real and imaginary parts
  • Rounding errors changing values
4. Identify the error in this FFT code and choose the fix:
import numpy as np
from scipy.fft import fft
x = [1, 2, 3, 4]
result = fft(x)
print(result.real)
medium
A. No error; code runs correctly.
B. x should be a numpy array, fix with x = np.array(x).
C. Import fft incorrectly; use import scipy.fft instead.
D. fft requires integer input only.

Solution

  1. Step 1: Check input type requirements

    scipy.fft.fft accepts lists or numpy arrays as input. Using a Python list as input works fine; printing real part is valid.
  2. Final Answer:

    No error; code runs correctly. -> Option A
  3. Quick Check:

    List input accepted = No error; code runs correctly. [OK]
Hint: FFT accepts lists or arrays as input [OK]
Common Mistakes:
  • Assuming input must be numpy array
  • Wrong import statements causing errors
  • Thinking fft only works with integers
5. You have a noisy signal sampled at 8 points: [1, 0, 1, 0, 1, 0, 1, 0]. You want to find the strongest frequency component using FFT. Which code correctly finds the frequency with the highest amplitude?
hard
A. import numpy as np from scipy.fft import fft x = np.array([1,0,1,0,1,0,1,0]) fft_vals = fft(x) freq_index = np.argmin(np.abs(fft_vals)) print(freq_index)
B. import numpy as np from scipy.fft import fft x = [1,0,1,0,1,0,1,0] fft_vals = fft(x) freq_index = np.min(np.abs(fft_vals)) print(freq_index)
C. import numpy as np from scipy.fft import fft x = np.array([1,0,1,0,1,0,1,0]) fft_vals = fft(x) freq_index = np.argmax(np.abs(fft_vals)) print(freq_index)
D. import numpy as np from scipy.fft import fft x = np.array([1,0,1,0,1,0,1,0]) fft_vals = fft(x) freq_index = np.max(fft_vals.real) print(freq_index)

Solution

  1. Step 1: Compute FFT and get amplitudes

    Use fft(x) to get frequency components, then take absolute values to get amplitudes. Use np.argmax on amplitudes to find the strongest frequency index.
  2. Final Answer:

    Use np.argmax(np.abs(fft_vals)) to find strongest frequency index. -> Option C
  3. Quick Check:

    Max amplitude index = import numpy as np from scipy.fft import fft x = np.array([1,0,1,0,1,0,1,0]) fft_vals = fft(x) freq_index = np.argmax(np.abs(fft_vals)) print(freq_index) [OK]
Hint: Use np.argmax on absolute FFT values for strongest frequency [OK]
Common Mistakes:
  • Using np.min or np.argmin instead of max
  • Using only real part instead of magnitude
  • Passing list without converting to array (still works but less common)
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