Skip to content
LearnMathora

Fourier & the frequency domain · 07 · Real spectra, honest spectra · 9 min

Hard

Windowing, 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.

w[n]=0.50.5cos ⁣(2πnN1)    (Hann)w[n] = 0.5 - 0.5\cos\!\left(\tfrac{2\pi n}{N-1}\right) \;\; \text{(Hann)}

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

InteractiveSpectral leakage & the window cure

frequency bin

5
5 cycles fit the 64-sample frame exactly — one clean spike. The DFT silently assumes your snippet repeats forever, and right now that assumption is true.

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

  1. A vibration sensor shows tones at 100 Hz (strong) and 104 Hz (faint warning sign). Frame: 0.5 s → bins every 2 Hz.

  2. Rectangular window: the 100 Hz peak's leakage skirts bury the faint 104 Hz neighbor entirely.

  3. 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

  1. 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.