Bird
Raised Fist0
Signal Processingdata~20 mins

Short-Time Fourier Transform (STFT) in Signal Processing - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
STFT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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
A(128, 8)
B(128, 7)
C(129, 8)
D(129, 7)
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.
❓ data_output
intermediate
2: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]
A140.625
B156.25
C125.0
D160.0
Attempts:
2 left
💡 Hint
Frequency bins are spaced by fs/nperseg.
❓ visualization
advanced
3: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()
AA single bright line at 200 Hz representing the average frequency
BRandom scattered bright spots with no clear frequency lines
CTwo bright horizontal lines at 100 Hz and 300 Hz across all time segments
DBright vertical lines at 100 Hz and 300 Hz only at the start of the signal
Attempts:
2 left
💡 Hint
Each sine wave frequency appears as a horizontal line in the spectrogram.
🧠 Conceptual
advanced
2:00remaining
Effect of Window Overlap on STFT
What is the main effect of increasing the overlap between windows in STFT computation?
ADecreases frequency resolution and reduces computational cost
BImproves time resolution but increases computational cost
CImproves frequency resolution but decreases time resolution
DIncreases both time and frequency resolution without cost
Attempts:
2 left
💡 Hint
More overlap means windows slide less between segments.
🔧 Debug
expert
2: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)
AValueError: nperseg must not be greater than input length
BTypeError: unsupported operand type(s) for -: 'int' and 'NoneType'
CNo error, STFT will zero-pad automatically
DIndexError: index out of range
Attempts:
2 left
💡 Hint
Check the input length compared to window size.

Practice

(1/5)
1. What is the main purpose of the Short-Time Fourier Transform (STFT) in signal processing?
easy
A. To compress a signal into fewer data points
B. To analyze how frequencies change over time in a signal
C. To remove noise from a signal completely
D. To convert a signal from digital to analog form

Solution

  1. Step 1: Understand STFT function

    STFT breaks a signal into small time segments and finds frequency content in each segment.
  2. Step 2: Identify main goal

    This helps us see how frequencies change over time, unlike a normal Fourier transform which shows overall frequencies.
  3. Final Answer:

    To analyze how frequencies change over time in a signal -> Option B
  4. Quick Check:

    STFT = frequency changes over time [OK]
Hint: STFT = frequency analysis in short time windows [OK]
Common Mistakes:
  • Confusing STFT with noise removal
  • Thinking STFT compresses data
  • Mixing STFT with analog conversion
2. Which of the following is the correct Python function call to compute STFT using the scipy library on a signal array named signal with a window size of 256?
easy
A. scipy.stft(signal, nperseg=256)
B. scipy.stft(signal, window=256)
C. scipy.signal.fft(signal, window=256)
D. scipy.signal.stft(signal, nperseg=256)

Solution

  1. Step 1: Recall scipy STFT syntax

    The STFT function is in scipy.signal as stft(), and the window size is set by nperseg parameter.
  2. Step 2: Match correct syntax

    scipy.signal.stft(signal, nperseg=256) uses scipy.signal.stft(signal, nperseg=256), which is correct syntax.
  3. Final Answer:

    scipy.signal.stft(signal, nperseg=256) -> Option D
  4. Quick Check:

    Correct function and parameter = scipy.signal.stft(signal, nperseg=256) [OK]
Hint: Use scipy.signal.stft with nperseg for window size [OK]
Common Mistakes:
  • Using scipy.stft instead of scipy.signal.stft
  • Confusing fft() with stft()
  • Passing window size as 'window' instead of 'nperseg'
3. Given the following Python code using scipy.signal.stft, what is the shape of the output array Zxx?
import numpy as np
from scipy.signal import stft
signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100))
f, t, Zxx = stft(signal, nperseg=20)
print(Zxx.shape)
medium
A. (11, 6)
B. (20, 5)
C. (50, 2)
D. (10, 10)

Solution

  1. Step 1: Calculate frequency bins

    With nperseg=20, frequency bins = nperseg//2 + 1 = 10 + 1 = 11 rows.
  2. Step 2: Calculate time segments

    Signal length=100, window=20, default no overlap means about 6 segments (100/20 rounded up).
  3. Final Answer:

    (11, 6) -> Option A
  4. Quick Check:

    Zxx shape = (freq bins, time segments) = (11, 6) [OK]
Hint: Output shape = (nperseg/2+1, number of windows) [OK]
Common Mistakes:
  • Confusing nperseg with output shape rows
  • Miscounting time segments
  • Assuming output shape equals input length
4. Identify the error in this STFT code snippet and choose the correct fix:
from scipy.signal import stft
signal = [1, 2, 3, 4, 5]
f, t, Zxx = stft(signal, nperseg=10)
print(Zxx)
medium
A. Error: nperseg is larger than signal length; fix by setting nperseg=5
B. Error: signal must be a numpy array; fix by converting signal to np.array(signal)
C. Error: stft requires window parameter; fix by adding window='hann'
D. No error; code runs fine

Solution

  1. Step 1: Check nperseg vs signal length

    Signal length is 5, but nperseg=10 is larger, which causes a warning or error in scipy.
  2. Step 2: Fix nperseg value

    Set nperseg to 5 or less to match signal length and avoid error.
  3. Final Answer:

    Error: nperseg is larger than signal length; fix by setting nperseg=5 -> Option A
  4. Quick Check:

    nperseg ≤ signal length to avoid error [OK]
Hint: nperseg must not exceed signal length [OK]
Common Mistakes:
  • Ignoring nperseg size error
  • Thinking signal type causes error
  • Adding unnecessary window parameter
5. You want to analyze a music clip with fast-changing notes using STFT. Which combination of window size and overlap will give you better time resolution but less frequency resolution?
hard
A. Large window size with high overlap
B. Large window size with low overlap
C. Small window size with high overlap
D. Small window size with low overlap

Solution

  1. Step 1: Understand window size effect

    Small window size improves time resolution but reduces frequency resolution.
  2. Step 2: Understand overlap effect

    High overlap keeps time segments close, improving time detail further.
  3. Final Answer:

    Small window size with high overlap -> Option C
  4. Quick Check:

    Better time resolution = small window + high overlap [OK]
Hint: Small window + high overlap = better time detail [OK]
Common Mistakes:
  • Choosing large window for better time resolution
  • Ignoring overlap effect
  • Assuming low overlap improves time resolution
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