S7 · Lesson 30 · The short-time Fourier transform
Spectra that change with time, window length against time and frequency resolution, hop size and overlap, spectrograms, and perfect reconstruction by overlap-add
Speech, music, radar returns and machine vibration all have spectra that change from moment to moment, and a single FFT of the whole recording averages that change away. The short-time Fourier transform slides a window along the signal and takes an FFT of each windowed piece, giving a spectrum for every moment: a spectrogram. The window length decides what can be seen. A long window resolves close frequencies but blurs events in time; a short one catches fast changes but smears frequencies. Choosing it well is a quantitative trade, and for some signals there's an exact best length.
- The frequency spread of a linear chirp sweeping R Hz per second, seen through a Hann window of length T: the sweep during the window, RT (dotted), plus the window's own resolution, 2/T (dashed), against T.
- Short windows are limited by their resolution and long ones by the chirp's motion; the total (solid) is smallest at .
- For a chirp sweeping 1 kHz per second that's a 44.7 ms window, with a spread of 89 Hz. Faster chirps need shorter windows and blur more.
Predict
A speech analyzer at 16 kHz uses 25 ms Hann windows every 10 ms. How finely can it resolve frequencies, and how many spectra does it produce per second?
A 25 ms Hann window resolves about 2/T = 80 Hz, enough to separate the harmonics of most voices (100 to 250 Hz apart) yet short enough that a vowel's spectrum barely changes within it. Hopping 10 ms gives 100 frames per second. The window is 400 samples, usually zero-padded to a 512-point FFT.
Name it
Definition
a window of L samples, hopped by R samples, each frame transformed by an N-point DFT (N ≥ L). Its squared magnitude, plotted against time and frequency, is the spectrogram.
Resolution
- Frequency: about for a rectangular window, 2f_s/L for Hann and Hamming (lesson 29).
- Time: about the window length, .
- Their product can't be made small: Δt·Δf is at least of order 1, the time–bandwidth (uncertainty) principle. A wideband spectrogram (short window) shows pitch pulses in time; a narrowband one (long window) shows individual harmonics.
- For a chirp sweeping R Hz/s, the spread is about with a Hann window, smallest at , where it's .
Hop size and reconstruction
- The hop R sets the frame rate . With a real input, the STFT produces about N/(2R) complex values per input sample, so it's redundant unless R = N.
- Overlap-add synthesis: inverse-FFT each frame, then add the frames at their positions. The original returns exactly if the shifted windows sum to a constant: const (the COLA condition). The periodic Hann window at 50% overlap sums to exactly 1; Hamming at 50% sums to 1.08.
- Modifying the frames before synthesis gives time-varying filtering, noise reduction and time-scale changes.
Worked example
A chirp sweeps 1 kHz per second. What window length gives the sharpest spectrogram, and how sharp is it?
Show the solution
- Best window length.
- The window's resolution.
- The chirp's motion. During the window the chirp moves The two contributions are equal at the optimum.
- Total spread.
- Away from the optimum. Halving or doubling T raises it to 112 Hz.
Answer a 44.7 ms window, which smears the chirp over 89.4 Hz.
Worked example
Show that the periodic Hann window satisfies the COLA condition at 50% overlap for N = 8.
Show the solution
- The window.
- Add a frame 4 samples on. for every m, since the two cosines cancel.
- Conclusion. Frames hopped by 4 therefore add back to exactly the original signal.
Answer for every m, so the periodic Hann window satisfies COLA at 50% overlap.
Worked example
Speech sampled at 16 kHz has a pitch of 120 Hz. Compare 5 ms and 40 ms Hann windows.
Show the solution
- The 5 ms window. That's 80 samples, with about of frequency resolution and 5 ms of time resolution.
- What it shows. The harmonics, 120 Hz apart, blur together, but each glottal pulse (every 8.3 ms) shows as a vertical stripe.
- The 40 ms window. That's 640 samples, with 50 Hz resolution: the harmonics separate into horizontal lines, but the pulses merge.
- In practice. Speech analysis uses both: wideband for timing, narrowband for pitch.
Answer the 5 ms window (400 Hz resolution) shows each pitch pulse but blurs the harmonics; the 40 ms window (50 Hz) resolves the harmonics but merges the pulses.
Check
A 25 ms window at 8 kHz holds how many samples?
Check
Roughly what frequency resolution does a 25 ms Hann window give?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
An STFT at 16 kHz hops 64 samples per frame. How many frames per second?
Check
A chirp sweeps 5000 Hz per second. What Hann 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
A chirp at 5000 Hz/s is analyzed with a 5 ms Hann window. Roughly how wide does it appear?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A real signal's STFT uses 1024-point FFTs with a hop of 128. How many unique complex values does it produce per input sample?
Check
A 20 ms Hann window analyzes 8 kHz audio whose harmonics are 200 Hz apart.
(a)
How many samples does the window hold?
(b)
Roughly what frequency resolution does it give?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A chirp sweeps R Hz per second through a Hann window T seconds long.
Step 1
Roughly how wide does it appear?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What window length minimizes that?
Step 3
What is the minimum width?
Explain why
Why can't a spectrogram be sharp in both time and frequency at once?
Each column of a spectrogram is the spectrum of a windowed stretch of signal, and a window of length T can only separate frequencies differing by about 1/T, since two tones closer than that don't complete enough relative cycles within the window to look different. Making T short, to localize events in time, therefore widens every frequency peak; making T long, to sharpen the peaks, averages over everything that happened during the window. This is the same time–bandwidth trade that governs all Fourier analysis: a signal concentrated in a short time necessarily has a wide spectrum. The choice of window length is a decision about which kind of detail matters more for the signal at hand.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.