MATLAB - Windowing Part 2
12 Aug 2008 Quan Quach 4 comments 2,600 views
This is the seventh post in the blinkdagger signal processing series.
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.
Contents
- Quick Recap from Last Time
- Windowing
- Apply the Window to the Input Signal
- Comparison of Non-windowed and Windowed Signal
- Next Time - How to Choose a Window that’s Best for You
Quick Recap from Last Time
In the last post, we tried taking the Fourier transform of the following sine wave:
fo = 10; %frequency of the sine wave Fs = 200; %sampling rate Ts = 1/Fs; %sampling period 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 sinePlot = figure; plot(t2,y2)
Which yielded the following result:
Let’s do an example of windowing to show how it can mitigate the effects of spectral leakage.
Windowing
A window basically forces a signal to start and end at zero as to mimic a periodic signal. This is important because the fft command makes the assumption that the input signal is periodic over negative infinity to positive infinity. Without windowing, there would be “discontinuities” in the repeated signal. Since most signals that we work with are not periodic to begin with, it is desired to use a window to enforce periodicity. MATLAB has a bunch of windows that are pre-programmed into the software. Depending on the situation and application, a particular type of window may work better than others (we will discuss this in the next post). For now, we are going to use a Hanning Window, which is shown below.
windowHanning = window(@hann,n2).'; %create a hanning window vector hanningWindowFigure = figure; plot(windowHanning); %plot the hanning window
Apply the Window to the Input Signal
Now, we are going to multiply the Hanning window with our original signal. The output is shown in the plot below. Notice that the modified signal is starting and ending at an amplitude of zero!
windowedSignal = windowHanning.*y2; %multiply the inputsignal with this window windowedSignalPlot = figure; plot(t2,windowedSignal) %plot the windowed signal
Comparison of Non-windowed and Windowed Signal
Let’s take the Fourier transform of both the non-windowed and the windowed signal and plot them on the same graph. As you can see, the effects of spectral leakage have been mitigated! Because of the distortion introduced by the window, we multiply the output by the coherent gain factor to normalize the amplitude (note that the coherent gain is different depending on the window used). The important thing to take away from this is that there is less energy surrounding the peak, making it easier to identify what the peak is.
[a,b] = positiveFFT(y2,Fs); %calculate positive fft for non-windowed signal [c,d] = positiveFFT(windowedSignal,Fs); %calculate positive fft for windowed signal c = c * 2; %multiply by the coherence factor fftWindowedSignalLinear = figure; plot(b,abs(a),d,abs(c),'r') legend('Non-windowed signal','Windowed signal')
But wait, lets plot the same data on a log scale! In many disciplines, the magnitude is represented in units of dB, so it is useful to show this here. Notice that the effects of windowing are even MORE pronounced when using a log scale!
fftWindowedSignalLog = figure; plot(b,20*log10(abs(a)),d,20*log10(abs(c)),'r') %plot the windowed signal in log scale
Next Time - How to Choose a Window that’s Best for You
In the next tutorial, we will discuss the different types of windows and how to go about choosing the best window for a particular application.
Download
Download the source file here.
4 Responses to “MATLAB - Windowing Part 2”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


How about a tutorial on how to create blog entries from published MATLAB scripts? I’ve been using the publish feature for a while to post a our internal company website, but I’m trying to get started doing so externally (you guys have inspired me!). Even if you don’t do a tutorial, some hints on what works/doesn’t work would be extremely helpful.
Thanks!
Hey Ted,
We will be glad to share our method/procedure for publishing MATLAB scripts! Hopefully we can get that done sooner rather than later. If you have specific questions, just send us a holler through the contact form!
Quan
wow, that was a great explanation! thank you very much, you have clarified a lot of stuff for me!
Hi Quan Quach,
In the matlab code
c = c * 2; %multiply by the coherence factor
but in
http://www.mathworks.com/matlabcentral/fileexchange/3770&watching=3770
i saw they wrote:
multply by sqrt(8/3) because of effects of hanning window
which is correct ?