This is the seventh post in the blinkdagger signal processing series.

Matlab Logo 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

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)

Windowing Part 2 Pic 1

Which yielded the following result:

Windowing Part 2 Pic 2

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

Windowing Part 2 Pic 3

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

Windowing Part 2 Pic 4

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')

Windowing Part 2 Pic 5

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

Windowing Part 2 Pic 6

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.

Reference 1

Reference 2