DSP3 min read

Understanding FFT Spectral Leakage and Windowing Functions

An advanced DSP tutorial on the mathematical causes of spectral leakage in Fast Fourier Transforms and how different windowing functions mitigate it.

難易度:Advanced
分類: DSP

Understanding FFT Spectral Leakage and Windowing Functions

In digital signal processing (DSP), the Fast Fourier Transform (FFT) is an indispensable tool for spectral analysis. However, a major practical issue in FFT analysis is spectral leakage. This phenomenon occurs when a signal is analyzed over a finite time window, and the signal frequency does not align perfectly with the FFT bin centers.

In this article, we will mathematically analyze the root cause of spectral leakage and explore how various windowing functions (like Hann, Hamming, and Blackman-Harris) are used to mitigate this effect.

What is Spectral Leakage?

The discrete Fourier transform (DFT) of a finite-duration signal x[n]x[n] of length NN is given by:

X[k]=n=0N1x[n]ej2πNknX[k] = \sum_{n=0}^{N-1} x[n] e^{-j \frac{2\pi}{N} k n}

When we truncate a continuous-time signal to NN samples, we are implicitly multiplying the infinite signal by a rectangular window function w[n]w[n]:

w[n]={10n<N0otherwisew[n] = \begin{cases} 1 & 0 \le n < N \\ 0 & \text{otherwise} \end{cases}

In the frequency domain, multiplication in time corresponds to periodic convolution in frequency. The spectrum of a rectangular window is a Dirichlet kernel (often approximated by a Sinc function):

W(ω)=ejωN12sin(ωN/2)sin(ω/2)W(\omega) = e^{-j \omega \frac{N-1}{2}} \frac{\sin(\omega N / 2)}{\sin(\omega / 2)}

The sidelobes of this Sinc function have relatively high amplitudes. The first sidelobe is only about 13 dB-13\text{ dB} below the main lobe. If the input signal contains a frequency component that does not correspond to an exact integer number of cycles in the observation window, the energy of that component leaks into the sidelobes of W(ω)W(\omega), masking smaller nearby signals.

Windowing Functions

To reduce spectral leakage, we apply a non-rectangular window function w[n]w[n] to the signal before computing the FFT. Good window functions taper smoothly to zero at the boundaries, which reduces the amplitude of the sidelobes in the frequency domain at the cost of widening the main lobe.

                  Window Characteristics
                  
      Rectangular               Hann / Hamming
     +-----------+               .---------.  
     |           |             /             \
     |           |           /                 \
    -+-----------+-         -+-----------------+-

1. The Hann Window

The Hann window is defined as:

w[n]=0.5(1cos(2πnN1))w[n] = 0.5 \left(1 - \cos\left(\frac{2\pi n}{N-1}\right)\right)

It has a maximum sidelobe level of 32 dB-32\text{ dB}, which is a significant improvement over the rectangular window.

2. The Hamming Window

The Hamming window is very similar but does not go exactly to zero at the edges:

w[n]=0.540.46cos(2πnN1)w[n] = 0.54 - 0.46 \cos\left(\frac{2\pi n}{N-1}\right)

It minimizes the peak sidelobe level to 43 dB-43\text{ dB}.

3. Implementing Windowing in TypeScript / JavaScript

Below is a simple implementation of windowing in TypeScript:

export function applyHannWindow(signal: number[]): number[] {
  const N = signal.length;
  const windowed = new Array<number>(N);
  
  for (let n = 0; n < N; n++) {
    const w = 0.5 * (1 - Math.cos((2 * Math.PI * n) / (N - 1)));
    windowed[n] = signal[n] * w;
  }
  
  return windowed;
}

Performance Comparison

Here is a summary of the trade-offs of common windowing functions:

| Window Type | Main Lobe Width (3dB) | Peak Sidelobe Level (dB) | Sidelobe Roll-off Rate | | :--- | :--- | :--- | :--- | | Rectangular | 0.89/N0.89 / N | 13-13 | 6 dB/octave-6\text{ dB/octave} | | Hann | 1.44/N1.44 / N | 32-32 | 18 dB/octave-18\text{ dB/octave} | | Hamming | 1.30/N1.30 / N | 43-43 | 6 dB/octave-6\text{ dB/octave} | | Blackman | 1.64/N1.64 / N | 58-58 | 18 dB/octave-18\text{ dB/octave} |

Choosing the right window depends on whether your application requires high frequency resolution (narrow main lobe) or high dynamic range (low sidelobes).