Challenge - 5 Problems
STFT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of STFT Magnitude Shape
Given a 1-second sine wave sampled at 1024 Hz, what is the shape of the magnitude array returned by the STFT with a window size of 256 and hop length of 128?
Signal Processing
import numpy as np from scipy.signal import stft fs = 1024 T = 1.0 t = np.linspace(0, T, int(T*fs), endpoint=False) x = np.sin(2*np.pi*50*t) f, t_seg, Zxx = stft(x, fs=fs, nperseg=256, noverlap=128) result = Zxx.shape
Attempts:
2 left
💡 Hint
Remember that the number of frequency bins is nperseg//2 + 1 and the number of time segments depends on the signal length, window size, and overlap.
✗ Incorrect
The STFT returns frequency bins equal to nperseg//2 + 1 = 129. The number of time segments is calculated as floor((len(x) - noverlap) / (nperseg - noverlap)) = 7. So the shape is (129, 7).
❓ data_output
intermediate2:00remaining
STFT Frequency Bin Values
What is the frequency value of the 10th frequency bin returned by STFT with sampling frequency 8000 Hz and window size 512?
Signal Processing
import numpy as np from scipy.signal import stft fs = 8000 nperseg = 512 freqs = np.fft.rfftfreq(nperseg, 1/fs) result = freqs[9]
Attempts:
2 left
💡 Hint
Frequency bins are spaced by fs/nperseg.
✗ Incorrect
The frequency resolution is fs/nperseg = 8000/512 = 15.625 Hz. The 10th bin (index 9) corresponds to 9 * 15.625 = 140.625 Hz.
❓ visualization
advanced3:00remaining
STFT Spectrogram Interpretation
You compute the STFT of a signal containing two sine waves at 100 Hz and 300 Hz. Which option best describes the expected spectrogram visualization?
Signal Processing
import numpy as np import matplotlib.pyplot as plt from scipy.signal import stft fs = 1000 t = np.linspace(0, 1, fs, endpoint=False) x = np.sin(2*np.pi*100*t) + np.sin(2*np.pi*300*t) f, t_seg, Zxx = stft(x, fs=fs, nperseg=256) plt.pcolormesh(t_seg, f, np.abs(Zxx), shading='gouraud') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.title('STFT Magnitude') plt.show()
Attempts:
2 left
💡 Hint
Each sine wave frequency appears as a horizontal line in the spectrogram.
✗ Incorrect
The STFT spectrogram shows energy at frequencies present in the signal over time. Two sine waves produce two horizontal bright lines at their frequencies.
🧠 Conceptual
advanced2:00remaining
Effect of Window Overlap on STFT
What is the main effect of increasing the overlap between windows in STFT computation?
Attempts:
2 left
💡 Hint
More overlap means windows slide less between segments.
✗ Incorrect
Increasing overlap means more windows cover the signal, improving time resolution but requiring more computations.
🔧 Debug
expert2:00remaining
Identifying Error in STFT Parameters
What error will occur if you call STFT with nperseg=1024 on a signal of length 500 samples without zero-padding?
Signal Processing
from scipy.signal import stft import numpy as np x = np.random.rand(500) f, t, Zxx = stft(x, nperseg=1024, padded=False)
Attempts:
2 left
💡 Hint
Check the input length compared to window size.
✗ Incorrect
STFT requires nperseg to be less than or equal to the input length. Otherwise, it raises a ValueError.