Window Functions

March 21, 2026 · View on GitHub

Overview & Motivation

A window function is a mathematical taper applied to a finite-length signal before spectral analysis. When we extract a segment of NN samples from a continuous signal, we implicitly multiply by a rectangular window — a hard cut that introduces sharp discontinuities at the segment boundaries. In the frequency domain these discontinuities spread energy across all bins, an artefact called spectral leakage.

Window functions taper the segment smoothly toward zero at both ends, trading a small amount of frequency resolution (wider main lobe) for dramatically reduced leakage (lower side lobes). Choosing the right window is a fundamental step in every FFT-based pipeline.

Mathematical Theory

General Form

A symmetric window of length NN is a sequence w[n],  n=0,1,,N1w[n],\; n = 0, 1, \ldots, N-1, designed so that w[0]w[N1]0w[0] \approx w[N-1] \approx 0 (except for the rectangular case).

Common Windows

WindowDefinitionMain-Lobe WidthPeak Side Lobe
Rectangularw[n]=1w[n] = 1$2/N$13-13 dB
Hammingw[n]=0.540.46cos ⁣(2πnN1)w[n] = 0.54 - 0.46\cos\!\left(\frac{2\pi n}{N-1}\right)$4/N$43-43 dB
Hannw[n]=0.5(1cos ⁣(2πnN1))w[n] = 0.5\left(1 - \cos\!\left(\frac{2\pi n}{N-1}\right)\right)$4/N$32-32 dB
Blackmanw[n]=0.420.5cos ⁣(2πnN1)+0.08cos ⁣(4πnN1)w[n] = 0.42 - 0.5\cos\!\left(\frac{2\pi n}{N-1}\right) + 0.08\cos\!\left(\frac{4\pi n}{N-1}\right)$6/N$58-58 dB

Frequency-Domain Perspective

In the frequency domain, windowing is convolution with the window's Fourier transform W(f)W(f). Each spectral line of the signal is smeared by W(f)W(f). A narrower main lobe preserves resolution; lower side lobes suppress leakage.

Coherent Gain

The coherent gain CGCG of a window normalizes the amplitude after windowing:

CG=1Nn=0N1w[n]CG = \frac{1}{N}\sum_{n=0}^{N-1} w[n]

For amplitude-accurate measurements, divide the windowed FFT output by CGCG.

Processing Gain

The processing gain PGPG compares the noise bandwidth of the window to the rectangular window:

PG=(w[n])2Nw[n]2PG = \frac{\left(\sum w[n]\right)^2}{N \sum w[n]^2}

WindowProcessing Gain
Rectangular1.00
Hamming0.73
Hann0.67
Blackman0.57

Complexity Analysis

OperationTimeSpace
Generate NN window coefficientsO(N)O(N)O(N)O(N)
Apply window (element-wise multiply)O(N)O(N)O(1)O(1) extra
Pre-computed lookupO(1)O(1) per sampleO(N)O(N)

Window generation is dominated by trigonometric evaluations (cos\cos). For repeated use at a fixed NN, pre-compute and store the coefficients in a lookup table.

Step-by-Step Walkthrough

Scenario: Apply a Hann window to N=8N = 8 samples of a pure tone at bin 2.5 (non-integer frequency → leakage expected).

Step 1 — Generate the Hann window

nnw[n]=0.5(1cos(2πn/7))w[n] = 0.5(1 - \cos(2\pi n / 7))
00.000
10.188
20.611
30.950
40.950
50.611
60.188
70.000

Step 2 — Element-wise multiply

xw[n]=x[n]w[n]x_w[n] = x[n] \cdot w[n]

The signal tapers to zero at both ends, removing the hard edges.

Step 3 — FFT of windowed signal

Compared to the un-windowed (rectangular) FFT:

  • The peak at bin 2.5 is slightly broader (main lobe widens from 2 to 4 bins).
  • The leakage floor drops from 13-13 dB to 32-32 dB.
graph LR
    subgraph Before Windowing
        R["Rectangular window<br/>Sharp edges → leakage"]
    end
    subgraph After Windowing
        H["Hann window<br/>Smooth taper → low side lobes"]
    end
    R --> |"apply Hann"| H
    H --> FFT["FFT → cleaner spectrum"]

Pitfalls & Edge Cases

  • Rectangular is not "no window." Truncation is a rectangular window — acknowledge that you are already windowing.
  • Zero-padding after windowing. Always window before zero-padding, not after, to avoid creating new discontinuities.
  • Amplitude bias. Windowing attenuates the signal. Correct with the coherent gain CGCG for amplitude measurements, or with the equivalent noise bandwidth for power measurements.
  • Overlap in Welch/STFT. When segments overlap, the combined window must be flat (constant overlap-add). Hann with 50 % overlap satisfies this; Hamming does not perfectly.
  • Fixed-point overflow. All windows in this library include a $0.9999$ scaling factor to keep values strictly below 1.0 in Q15/Q31 representation.
  • Length-1 window. A single-sample window should return 1.0 (or $0.9999forfixedpoint).Ensurenodivisionbyzeroinfor fixed-point). Ensure no division by zero inN-1$ terms.

Variants & Generalizations

VariantKey Difference
Kaiser windowParameterized by β\beta; allows continuous trade-off between main-lobe width and side-lobe level
Flat-top windowNear-zero amplitude error at the cost of very wide main lobe; used for calibration
Gaussian windowSmooth, no side-lobe discontinuities; parameterized by σ\sigma
Dolph–ChebyshevEquiripple side lobes at a prescribed level; optimal for a given main-lobe width
DPSS (Slepian)Maximum energy concentration in a given bandwidth; used in multi-taper spectral estimation

Applications

  • Spectrum analysis — Applied before the FFT to reduce leakage.
  • Power spectral density — The PSD (Welch) method applies a window to every overlapping segment.
  • FIR filter design — Truncated ideal impulse responses are multiplied by a window to control passband ripple and stopband attenuation.
  • Short-Time Fourier Transform (STFT) — Sliding windowed FFT for time-frequency analysis.
  • Audio processing — Speech and music analysis use overlapping Hann windows.

Connections to Other Algorithms

graph TD
    W["Window Functions"]
    FFT["Fast Fourier Transform"]
    PSD["Power Density Spectrum"]
    DCT["Discrete Cosine Transform"]
    W --> FFT
    W --> PSD
    W --> DCT
AlgorithmRelationship
FFTWindow is applied before the FFT to reduce spectral leakage
Power Density SpectrumBuilt-in windowing per segment in Welch's method
DCTDCT's implicit symmetry provides partial leakage suppression, but windowing still helps

References & Further Reading

  • Harris, F.J., "On the use of windows for harmonic analysis with the discrete Fourier transform", Proceedings of the IEEE, 66(1), 1978 — the definitive window survey.
  • Oppenheim, A.V. and Schafer, R.W., Discrete-Time Signal Processing, 3rd ed., Pearson, 2010 — Chapter 10.
  • Nuttall, A., "Some windows with very good sidelobe behavior", IEEE Trans. ASSP, 29(1), 1981.