# Effect of windowing on FFT magnitude

I have 2 sin waves:

Fs = 1500;
N = 100;
t = (0:N-1)'/Fs;

sinwave = sin(2*pi*15*t);
sinwaveshifted = sin(2*pi*15*t + 1.3);


If I get the magnitude at 15Hz using an fft (need to use an FFT that is a power of 2 for reasons beyond my control):

fftsin = fft(sinwave,128);
fftsinshift = fft(sinwaveshifted,128);

% 2 corresponds to 15Hz

abs(fftsin(2)) = 51.819;
abs(fftsinshift(2)) = 41.6151;


Why the difference? I'm assuming because my bins do not line up perfectly there is some leakage occurring. Also, I haven't done any windowing.

If I do window my signals (I used a flat top because I read that was best in terms of magnitude accuracy), these are my results:

w = window(@flattopwin, 100);
sinwin = sinwave .* w;
sinshiftwin = sinwaveshift .* w;

fftsin = fft(sinwin ,128);
fftsinshift = fft(sinshiftwin ,128);

% 2 corresponds to 15Hz

abs(fftsin(2)) = 2.7283;
abs(fftsinshift(2)) = 17.822;


Why the huge difference?

I presume you are correct in that the small difference in the original signal fft's is due to leakage. However, the huge difference in the windowed results arises because you simply do not have enough data. Your original signals only have data for one period of the sinusoid. When you window this, you are clamping the edges of your signal to zero, effectively leaving you with only the middle portion of the signal. So you are left with signals that have only a fraction of a single period worth of information, definitely not enough to have any accuracy in the extracted frequency analysis. To illustrate this, here are matlab images of your original functions before and after windowing: and you can easily see the loss of much of the meaningful frequency information from the signals. A better candidate for windowing would be something like below, using 10 periods of your sinusoid signals: which demonstrates the balance between retaining frequency information and clamping the edges to reduce leakage. This paper provides a more in depth look at different windowing functions and their performance on different classes of signals if you are interested.

• This answer also clearly illustrates the benefit of actually looking at signals and data that one is processing – lxop Apr 16 '13 at 20:54

Couple of things:

1. With an FFT of 128 points your frequency resolution is 11.71 Hz, so you can't really directly look at 15 Hz. Your assertion that $fftsin(2)$ corresponds to 15 Hz is wrong.
2. By doing an FFT of 128 points over a 100 time domain samples, you basically append 28 zeros to the waveform. That changes the spectrum significantly.
3. The window is a lot shorter than a single wavelength so the exact lineup between window and sine wave makes a huge difference. You get a lot more energy if you window around the peak of the sine wave as compared to the zero crossing. The window would work better for a much higher frequency.

Windowing in the time domain is a convolution in the frequency domain. A window function will thus, in the frequency domain, convolve a single spectral frequency point with a spreading function. For very low frequencies, this window spreading function can spread an FFT frequency bin and its corresponding negative frequency bin right across DC so that the 2 frequencies (positive and negative) interfere or even partially cancel each other out.

The amount of cancellation between the positive and negative bins will depend on the phase of the sinusoid with respect to the FFT aperture center. Even, cosine or real components will add. Odd, sine or imaginary components will cancel. (Because real time domain input produces complex conjugate symmetry in the FFT result).

Thus windowing should not be used (or used very carefully, taking into account phase) for measuring the magnitude of frequencies very near DC (around the first FFT result bin or two), or very near Fs/2.