C9 · Lesson 26 · The short-time Fourier transform
Spectra that change with time, and the trade between seeing when and seeing which frequency
A DFT of a whole recording (lesson 24) says which frequencies occur but not when: a bird's rising chirp and a falling one have the same spectrum. Speech, music, radar returns and machine vibration all change from moment to moment, so engineers cut the signal into short windowed pieces (lesson 25), take a DFT of each, and stack the results into a spectrogram: time across, frequency up, brightness for magnitude. Choosing the piece length is the whole art. Short windows locate events sharply in time and blur frequency; long windows do the opposite, the duration-bandwidth limit of lesson 17 in a new form.
- For a window of samples at sample rate = 8 kHz, the solid curve is the frequency resolution (Hann main-lobe half-width, ) and the dashed curve the time resolution, , both against window length.
- They move in opposite directions; their product is fixed at 2.
- A chirp sweeping at Hz per second smears across hertz during a window of length , so for a chirp there's a best window: where the smear equals the window's own resolution.
Predict
Speech sampled at 8 kHz is analyzed with 256-sample windows hopped by 128 samples. How many spectrogram frames does one second give?
Frames start every 128 samples and need 256 samples each, so in 8000 samples there are = 61. Each frame covers 32 ms with bins 31.25 Hz apart, a common choice for speech: short enough to follow syllables, long enough to resolve the harmonics of a voice.
Name it
The short-time Fourier transform
Multiply the signal by a window of length placed at hop , and take the DFT:
with hop . Frame describes the signal around time ; bin is frequency .
Resolution
- Time: an event is located to within about the window's length, .
- Frequency: bins are apart, and a window's main lobe spreads a pure tone over several bins: a Hann window's main lobe reaches . Two tones are cleanly separated when they're about that far apart.
Their product is constant: halving the window doubles the frequency blur. Zero-padding each frame makes the plot smoother but doesn't change the resolution.
Hop and overlap
Windows usually overlap by 50 to 75%, so nothing between frames is missed and the display is smooth. The number of frames from samples is . With a Hann window at 50% overlap, the shifted windows add up to a constant, so the frames can be modified and added back together to rebuild the signal: the basis of overlap-add processing.
The best window for a chirp
A tone whose frequency rises at Hz/s moves by during a window of length , while the window's own resolution is about . The total blur is smallest when the two are equal, at .
Worked example
Design a spectrogram for 8 kHz speech with 32 ms frames and 50% overlap. Give , , the bin spacing, the Hann main-lobe half-width, and the frames per second.
Show the solution
- Frame and hop.
- Bins and main lobe. The bins are apart, and a Hann window spreads each tone over ±62.5 Hz.
- Frames per second. One second gives frames, a new frame every 16 ms.
- What it resolves. A voice's harmonics, 100 to 250 Hz apart, are resolved; a 10 ms plosive is located to within one frame.
Answer L = 256 and H = 128, bins 31.25 Hz apart, a Hann main-lobe half-width of 62.5 Hz, and 61 frames a second.
Worked example
A vibration signal sampled at 8 kHz contains two tones 20 Hz apart. How long a Hann window is needed to separate them, and what time resolution does that leave?
Show the solution
- Window length. The main-lobe half-width must be at most 20 Hz, so which is 100 ms.
- Time resolution. Events shorter than a tenth of a second will be smeared.
- When both matter. If both separations matter, one display can't show them; analysts make two spectrograms, or use a wavelet transform whose window shrinks at high frequencies.
Answer at least 800 samples (100 ms), which smears events shorter than a tenth of a second.
Worked example
A bat's chirp sweeps 1000 Hz per millisecond, Hz/s. What window length shows it most sharply?
Show the solution
- Best window length.
- Why it balances. During 1 ms the chirp moves 1 kHz, and a 1 ms window's own resolution is about 1 kHz: the two blurs balance.
- Longer or shorter. A 10 ms window would smear the sweep over 10 kHz; a 0.1 ms window would blur each moment's frequency over 10 kHz. Either way it's ten times worse.
Answer a window about 1 ms long.
Check
A spectrogram of a 16 kHz signal uses 256-sample frames. How far apart are its frequency bins?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A record of 16000 samples is analyzed with 256-sample frames hopped by 256/4. How many complete frames fit?
Check
Tones 5 Hz apart, sampled at 16 kHz, must be separated with a Hann window. What's the shortest window, in samples, whose main-lobe half-width is at most 5 Hz?
Check
A chirp sweeps 1000 Hz per second. About what window length shows it most sharply?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
How long is a 512-sample frame at 8 kHz?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A spectrogram's frames are zero-padded from 256 to 4096 points before the DFT. What changes?
Check
Track a chirp with a spectrogram. At fs = 8000, the signal is x = cos(2*pi*(500*t + b*t**2/2)) for one second (t = n/fs), so its frequency is 500 + b*t Hz. With Hann frames of L = 64 samples, hop L//2, find each frame's peak bin, convert it to Hz with k*fs/L, and compare with the true frequency at the frame's center. Set rms_err (the RMS tracking error in Hz over all frames) and frames.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A technician uses a spectrogram to find an intermittent 2 ms glitch on a motor drive's current, and also to tell apart two vibration tones 3 Hz apart. Why can't one window setting do both, and what should they do?
A spectrogram's time and frequency resolution trade against each other: a frame of length locates events to about and separates frequencies only about apart with a Hann window. Seeing a 2 ms glitch needs frames of a few milliseconds, which blur frequency over hundreds of hertz; separating tones 3 Hz apart needs frames of about two-thirds of a second, which smear any 2 ms event across the whole frame. No single frame length meets both, because their product is fixed. The technician should make two spectrograms with different window lengths, or use a long DFT for the steady tones and a short-window or time-domain analysis for the glitch.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.