Fourier & the frequency domain · 07 · Real spectra, honest spectra · 9 min
HardWindowing, leakage & the STFT
Theory transforms infinite signals; your computer grabs a snippet. That grab — slamming a rectangular window onto the data — has spectral consequences: energy smears across bins. Windowing is the craft of cutting gently, and it leads straight to the spectrogram.
Build the intuition
Why leakage happens
The DFT assumes your N-sample frame tiles forever. If a sine completes whole cycles in the frame, the tiling is seamless: one clean bin. If not, copies meet at a cliff — and cliffs, like all sharp features, demand energy across many frequencies. The smear is called spectral leakage; the cliff was your rectangular cut.
Windows: fade, don't slam
Multiply the frame by a taper that falls to zero at the edges — Hann's raised cosine being the everyday choice — and the seam vanishes; leakage skirts collapse by orders of magnitude. The cost (uncertainty's invoice): the main peak widens slightly. The window catalog — Hann, Hamming, Blackman, flat-top — is a menu of trade-offs between skirt suppression and peak sharpness; engineers pick per task: Hann for general use, flat-top for amplitude accuracy, rectangular only when cycles fit exactly.
STFT: spectra on a timeline
A single spectrum of a whole song answers “what frequencies, ever?” — useless for melody. Instead: slide a window along the signal, FFT each position, and stack the results into an image of frequency versus time — the Short-Time Fourier Transform, displayed as a spectrogram. Window length sets the trade: long windows resolve pitch finely but blur timing; short ones catch onsets but smear pitch. Uncertainty again — now as a usable dial.
See it move
frequency bin
Leakage and its cure, computed live: park the frequency between bins to raise the smear, then fade the edges with Hann and watch the skirts collapse.
A worked example
Two tones, one decision
A vibration sensor shows tones at 100 Hz (strong) and 104 Hz (faint warning sign). Frame: 0.5 s → bins every 2 Hz.
Rectangular window: the 100 Hz peak's leakage skirts bury the faint 104 Hz neighbor entirely.
Hann window: skirts drop ~30 dB; the warning tone stands clear at bin 52. Same data, same FFT — the window choice was the diagnosis.
Out in the world
Spectrograms are how machines hear
Speech recognizers, song identifiers, and birdsong classifiers all consume spectrograms — STFT images where vowels are stacked harmonic bands and consonants are vertical bursts. When you hum to find a song, a windowed FFT pipeline turns your hum into the fingerprint being matched.
Common confusion, cleared
“Leakage is an FFT bug better algorithms could fix.”
It's mathematics: finite observation genuinely cannot determine frequency perfectly (uncertainty). Windows don't cheat the limit — they spend it wisely.
“One best window exists; always use it.”
Windows are trade-offs: Hann balances, flat-top measures amplitudes, rectangular wins only for exact-cycle frames. “Which window?” means “what do you need to see?”
Check yourself
PracticeQuick check
A sine completes exactly 7 cycles in your DFT frame. Leakage is…
Recap
- Finite frames imply seams; seams imply leakage across bins.
- Tapered windows (Hann & co.) trade slight peak width for huge skirt suppression.
- STFT = sliding windows + FFTs = the spectrogram; window length sets time–frequency balance.
Progress saves in this browser.