0
0
SciPydata~20 mins

Why Fourier transforms reveal frequencies in SciPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fourier Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fourier Transform on a Simple Signal

What is the output magnitude array when applying the Fourier transform to a pure sine wave?

SciPy
import numpy as np
from scipy.fft import fft

# Create a sine wave signal
fs = 100  # Sampling frequency
T = 1/fs  # Sampling interval
t = np.arange(0, 1, T)  # Time vector
f = 1  # Frequency of sine wave
signal = np.sin(2 * np.pi * f * t)

# Compute Fourier transform
fft_result = fft(signal)
magnitude = np.abs(fft_result)
print(magnitude[:10])
A[0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
B[0.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
C[0.0, 50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
D[0.0, 49.99999999999999, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Attempts:
2 left
💡 Hint

Recall that the magnitude at the sine wave frequency is related to half the number of samples.

🧠 Conceptual
intermediate
1:30remaining
Why does Fourier transform reveal frequencies?

Which explanation best describes why the Fourier transform reveals the frequency components of a signal?

AIt breaks the signal into a sum of sine and cosine waves of different frequencies, showing how much each frequency contributes.
BIt sorts the signal values from smallest to largest to find dominant values.
CIt counts the number of peaks in the time domain signal to estimate frequency.
DIt averages the signal values over time to find the main frequency.
Attempts:
2 left
💡 Hint

Think about how sine and cosine waves relate to frequency.

data_output
advanced
1:30remaining
Frequency bins from FFT of a sampled signal

Given a signal sampled at 200 Hz with 400 samples, what is the frequency value of the 50th FFT bin?

SciPy
import numpy as np
fs = 200  # Sampling frequency
N = 400   # Number of samples
freqs = np.fft.fftfreq(N, 1/fs)
print(round(freqs[50], 2))
A200.0
B50.0
C25.0
D100.0
Attempts:
2 left
💡 Hint

Frequency bin = index * sampling_rate / number_of_samples

🔧 Debug
advanced
1:30remaining
Identify the error in FFT frequency calculation

What error does the following code produce?

import numpy as np
fs = 100
N = 256
freqs = np.fft.fftfreq(N, fs)
print(freqs[:5])
ASyntaxError due to missing parentheses.
BThe output is incorrect frequencies because the sampling interval is passed incorrectly.
CTypeError because fs is not an integer.
DNo error, correct output frequencies.
Attempts:
2 left
💡 Hint

Check the meaning of the second argument in np.fft.fftfreq.

🚀 Application
expert
3:00remaining
Detect dominant frequency in noisy signal

You have a noisy signal composed of a 10 Hz sine wave plus random noise, sampled at 100 Hz for 2 seconds. Which code snippet correctly identifies the dominant frequency?

SciPy
import numpy as np
from scipy.fft import fft
fs = 100
T = 2
N = fs * T
t = np.linspace(0, T, N, endpoint=False)
signal = np.sin(2 * np.pi * 10 * t) + 0.5 * np.random.randn(N)

# Which code finds dominant frequency?
A
freqs = np.fft.fftfreq(N, 1/fs)
fft_vals = fft(signal)
idx = np.argmax(np.abs(fft_vals))
dominant_freq = abs(freqs[idx])
print(round(dominant_freq, 2))
B
freqs = np.fft.fftfreq(N, fs)
fft_vals = fft(signal)
idx = np.argmax(np.abs(fft_vals))
dominant_freq = freqs[idx]
print(dominant_freq)
C
freqs = np.fft.fftfreq(N, 1/fs)
fft_vals = np.abs(fft(signal))
dominant_freq = freqs[np.argmax(fft_vals)]
print(dominant_freq)
D
freqs = np.fft.fftfreq(N, 1/fs)
fft_vals = fft(signal)
idx = np.argmin(np.abs(fft_vals))
dominant_freq = freqs[idx]
print(dominant_freq)
Attempts:
2 left
💡 Hint

Remember to use the correct sampling interval and find the index of the maximum magnitude.