# The practicing instrumentation engineer's guide to the DFT - Part 2: Spectral leakage and windowing

-June 26, 2012

Author's note: There are many books and articles on the Fourier Transform and its implementation available. A quick survey of these resources shows that they are not geared to the needs of the "Practicing Instrumentation Engineer" but to the needs of DSP engineers who work in many fields. This article explains some of the math required to get "Calibrated" Fourier Transforms.

[Part 1 of this article looked at understanding DFT and FFT implementations.]

Spectral Leakage and Windowing
What happens if we change the number of cycles of our input data to a non-integer number? With that in mind let's generate 10.5 cycles of sine wave data to use as the input to our DFT.

Figure 5: For our next test we will see what happens when we generate 10.5 cycles of input data and run a DFT on that.

Figure 6: The result of this DFT is horribly wrong. Bin 10 should be 0 dBV but is at -3.7 dBV and our 300 dBV dynamic range is now no better than some -70 dB. These problems are caused by spectral leakage.

Remember that a DFT calculates the Fourier spectra by assuming that the input waveform that you feed it repeats forever. Looking at figure 1's plot we can see that if we kept repeating the waveform it would always be a nice perfect sine wave with no discontinuities.

But looking at Figure 5's plot we see that adding this waveform results in a large glitch where the waveforms repeat as shown in Figure 7. The DFT can't tell that this is not what we had in mind, it just thinks that this waveform with the glitch repeats forever. And that waveform produces the DFT as shown in figure 6.

Figure 7: This is what the DFT thinks the 10.5 cycle waveform of figure 5 looks like when it repeats. There is a large glitch at the place where the waveforms would repeat and this causes the spectral leakage problem of Figure 6's DFT.

So if we input a coherent, even number of sine wave cycles that happens to fall exactly on a bin center, we get a textbook perfect DFT output.

But real world signals won't have frequencies exactly at bin centers and this causes a problem that is called "Spectral Leakage". Not only is the amplitude wrong in the resulting DFT, but the waveform gets smeared across all frequencies, thus making for a not very textbook display and potentially obscuring waveform features that are below the new higher noise floor.

What is wrong with our data? The middle is fine, it's the ends that have the issue.

This Spectral Leakage problem is commonly solved by applying a Window to our input data, that tapers the data off at both ends - so the DFT never sees any discontinuity.

A common window is the Hanning window (More properly known as the Hann Window as Mr. von Hann first presented it, but everyone calls it "Hanning" so we will too), this window is defined as,

This term Wj is a window factor and it is multiplied to our raw waveform data point by point. Figure 8 shows the function of this window for j=0 to j=499.

Figure 8: Here the Hanning Window function is plotted for 0 to 499 points. You can see that this window applied to our waveform data will make the start and ends of our data approach zero while the center of our waveform is multiplied by 1 so it remains unchanged.

Applying this Hanning window to our existing 10.5 cycle sinewave waveform produces a new data set and DFT as shown in figures 9 and 10.

Figure 9: Our Hanning window applied to our 10.5 cycle sine wave input data. This window helps to remove the discontinuity of the beginning and end of the waveform.

Figure 10: The resulting DFT of Hanning windowed data. The leakage is greatly improved and the far off noise floor is now better than -150 dBV. However the amplitude is still off by over -7 dBV.

The Windowed DFT of Figure 10 shows a great improvement in spectral leakage to adjacent bins, but the absolute amplitude is still in error and by a greater amount than by not using any window at all!