MATLAB - Windowing Part 1
30 Jul 2008 Quan Quach 5 comments 2792 views
This is the sixth post in the blinkdagger signal processing series.
Windowing is a valuable technique that is used primarily to reduce/redistribute sprectral leakage (error in the fft measurement) to better represent the frequency spectrum of the data. In addition, windowing can make it easier to identify peaks at specific frequencies, and can help in accurately indicating the amplitude level of a peak. One of the quirks of using the fft command is the implied periodicty of the input signal. If your signal doesn’t end and start at the same value, then this can lead to spectral leakage in the fourier transform. In this tutorial, we will explain what spectral leakage is, why it occurs, and how windowing can help resolve this problem.
Contents
- What is Spectral Leakage?
- Example Sine Wave #1 with Frequency Spectrum
- Example Sine Wave #2 with Frequency Spectrum
- Implied Periodicity in the fft
- Next Time
What is Spectral Leakage?
Spectral leakage refers to the small amounts of signal energy that are observed in frequency components that do not exist in the original waveform. How does spectral leakage come about? Let’s quickly investigate this matter. Let’s start with a sine wave that has fundamental frequency at 10 Hz.
[tex]y(t)=2sin(2pi{f}_{o}t)[/tex]
Example Sine Wave #1 with Frequency Spectrum
fo = 10; %frequency of the sine wave Fs = 200; %sampling rate Ts = 1/Fs; %sampling period t1 = 0:Ts:1 - Ts; %time vector n1 = length(t1); %number of samples y1 = 2*sin(2*pi*fo*t1); %plot the curve in the time domain sinePlot1= figure; plot(t1,y1) %plot the sine wave

When we take the fft of the above signal, we expect to get the following result:
[Y1,freq1] = positiveFFT(y1,Fs); %compute the frequency spectrum %positiveFFT is a custom function that is included in the source file fftPlot = figure; stem(freq1,abs(Y1)) %plot the frequency spectrum

So far, so good. Nothing that we did not expect.
Example Sine Wave #2 with Frequency Spectrum
Now, lets look at the following sine wave:
fo = 10; %frequency of the sine wave Fs = 200; %sampling rate Ts = 1/Fs; %sampling periodl t2 = 0:Ts:0.95 -Ts; %time vector(notice the difference here!) n2 = length(t2); %number of samples y2 = 2*sin(2*pi*fo*t2); %plot the curve in the time domain sinePlot2 = figure; plot(t2,y2)
It looks pretty similar to the previous sine wave right? (It does look similar, but you should notice that the plot ends a little bit before Sine Wave #1) Let’s see what the fft of Sine Wave #2 looks like:
fftPlot2 = figure; [Y2,freq2] = positiveFFT(y2,Fs); %compute the frequency spectrum %positiveFFT is a custom function that is included in the source file stem(freq2,abs(Y2)) %plot the frequency spectrum
If you compare the frequency spectrums of the two sample waves, you will notice that they differ greatly. The extra energy around 10 Hz is what we refer to as Spectral Leakage! What is going on here? Weren’t the two example sine waves pretty much identical?
Implied Periodicity in the fft
The fft command assumes that the input signal is periodic from negative infinity to infinity. We’ll illustrate what this means with a simple example. Lets take Sample Wave #1 and repeat it three times. We’ll also do the same for Sample Wave #2.
t3 = (0:length(y1)*3-1)/Fs; %new time vector repeatingSignal = figure; subplot(2,1,1) plot(t3,[y1 y1 y1]) %plot 3 iterations of sine wave #1 ylabel('Sample Wave #1','FontWeight','bold') title('Repeating Signal','FontWeight','bold') t4 = (0:length(y2)*3-1)/Fs; %new time vector subplot(2,1,2) plot(t4,[y2 y2 y2]) %plot 3 iterations of sine wave #2
Now, just imagine that we infinitely repeated the input signal. Did you notice that the second sine wave has discontinuities? This is because the original signal didn’t start and end at the same place! This is the very reason why we get spectral leakage!!
Next Time
In the next tutorial, we will discuss how windowing can help us alleviate this problem. Essentially, windowing forces a signal to start and end at zero so that the modified signal emulates a periodic signal. In the next tutorial, we will continue the discussion on windowing and show some examples on how windowing can help mitigate spectral leakage.
Download Source Files and References
5 Responses to “MATLAB - Windowing Part 1”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Somehow I can’t see the graphs/plots
[...] In the previous windowing post, we discussed what spectral leakage is and how it comes about. In this post, we are going to provide an example of what windowing can do to mitigate spectral leakage. [...]
[...] discussed what spectral leakage is in an earlier post. Basically, spectral leakage is energy within a frequency bin that shouldn’t be there. The [...]
Out of curiosity….
Seeing as DCT has an underline assumption of mirror-symmetry around block-boundaries doesnt that mean that DCT is also the subject of spectral leakage?
[...] public links >> lev Comment on MATLAB - Windowing Part 1 by Lev Saved by christiangroeneveld on Sun 12-10-2008 Leviticus 23-24:9: The Progression of, not [...]