Short-Time Fourier Transform (STFT) in Signal Processing - Time & Space Complexity
We want to understand how the time it takes to compute the Short-Time Fourier Transform changes as the input signal gets longer.
How does the work grow when we analyze more data?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.signal import stft
# x is the input signal array
# fs is the sampling frequency
f, t, Zxx = stft(x, fs=fs, nperseg=window_size, noverlap=overlap)
This code splits the signal into overlapping windows and computes the Fourier transform on each window.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Computing the Fourier transform on each window segment.
- How many times: Once for each window, which depends on the signal length and window step size.
As the input signal length grows, the number of windows increases roughly in proportion to the signal length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10,000 samples | About 10,000 / (window_size - overlap) Fourier transforms |
| 100,000 samples | About 10 times more Fourier transforms than 10,000 samples |
| 1,000,000 samples | About 100 times more Fourier transforms than 10,000 samples |
Pattern observation: The total work grows roughly linearly with the input size.
Time Complexity: O(n \log m)
This means the time grows roughly proportional to the input length times the cost of each Fourier transform on a window of size m.
[X] Wrong: "The STFT time grows only with the number of windows, ignoring the cost of each Fourier transform."
[OK] Correct: Each window requires a Fourier transform, which itself takes time proportional to the window size times a logarithm factor, so ignoring this underestimates the total time.
Understanding how STFT scales helps you explain performance in real signal analysis tasks, showing you can think about both data size and algorithm steps clearly.
"What if we doubled the window size while keeping the signal length the same? How would the time complexity change?"
Practice
Solution
Step 1: Understand STFT function
STFT breaks a signal into small time segments and finds frequency content in each segment.Step 2: Identify main goal
This helps us see how frequencies change over time, unlike a normal Fourier transform which shows overall frequencies.Final Answer:
To analyze how frequencies change over time in a signal -> Option BQuick Check:
STFT = frequency changes over time [OK]
- Confusing STFT with noise removal
- Thinking STFT compresses data
- Mixing STFT with analog conversion
signal with a window size of 256?Solution
Step 1: Recall scipy STFT syntax
The STFT function is in scipy.signal as stft(), and the window size is set by nperseg parameter.Step 2: Match correct syntax
scipy.signal.stft(signal, nperseg=256) uses scipy.signal.stft(signal, nperseg=256), which is correct syntax.Final Answer:
scipy.signal.stft(signal, nperseg=256) -> Option DQuick Check:
Correct function and parameter = scipy.signal.stft(signal, nperseg=256) [OK]
- Using scipy.stft instead of scipy.signal.stft
- Confusing fft() with stft()
- Passing window size as 'window' instead of 'nperseg'
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)
Solution
Step 1: Calculate frequency bins
With nperseg=20, frequency bins = nperseg//2 + 1 = 10 + 1 = 11 rows.Step 2: Calculate time segments
Signal length=100, window=20, default no overlap means about 6 segments (100/20 rounded up).Final Answer:
(11, 6) -> Option AQuick Check:
Zxx shape = (freq bins, time segments) = (11, 6) [OK]
- Confusing nperseg with output shape rows
- Miscounting time segments
- Assuming output shape equals input length
from scipy.signal import stft signal = [1, 2, 3, 4, 5] f, t, Zxx = stft(signal, nperseg=10) print(Zxx)
Solution
Step 1: Check nperseg vs signal length
Signal length is 5, but nperseg=10 is larger, which causes a warning or error in scipy.Step 2: Fix nperseg value
Set nperseg to 5 or less to match signal length and avoid error.Final Answer:
Error: nperseg is larger than signal length; fix by setting nperseg=5 -> Option AQuick Check:
nperseg ≤ signal length to avoid error [OK]
- Ignoring nperseg size error
- Thinking signal type causes error
- Adding unnecessary window parameter
Solution
Step 1: Understand window size effect
Small window size improves time resolution but reduces frequency resolution.Step 2: Understand overlap effect
High overlap keeps time segments close, improving time detail further.Final Answer:
Small window size with high overlap -> Option CQuick Check:
Better time resolution = small window + high overlap [OK]
- Choosing large window for better time resolution
- Ignoring overlap effect
- Assuming low overlap improves time resolution
Ready to prove it?
Pass the test and this pattern moves unknown
