# Explain how to apply Hanning window to Fourier amplitude spectrum

This example code was used to answer a question about applying a window on the Fourier amplitude spectrum. How to apply Hamming Window?

I have a question about the section of code related to the variable mYdft. It's altered 3 times and I'm not sure why. Could someone elaborate on what's going on here?

Complete example code for reference:

Ts = 50e-6;                  % Sampling Time(s)
Fs = 1/Ts;                   % Sampling rate, Sampling Freq (Hz)
f0 = 50;                     % Frequency of interest (Hz)
duraT = 1;

%Calculate time axis
dt = 1/Fs;
tAxis = dt:dt:(duraT-dt);

y = cos(2*pi*f0*tAxis) +  2*sin(2*pi*10*tAxis);   y=y';

L = length (y); % Window Length of FFT
nfft = 2^nextpow2(L); % Transform length

y_HannWnd = y.*hanning(L);
Ydft_HannWnd = fft(y_HannWnd,nfft)/L;

% at all frequencies except zero and the Nyquist
mYdft = abs(Ydft_HannWnd);
mYdft = mYdft (1:nfft/2+1);
mYdft (2:end-1) = 2* mYdft(2:end-1);

f = Fs/2*linspace(0,1,nfft/2+1);

figure(1),
subplot(2,1,1)
plot(tAxis,y)
title('Time Domain y(t)');
xlabel('Time,s');
ylabel('y');
subplot(2,1,2)
plot(f,2*mYdft); % why need *2 ? Bcoz, Hanning Wnd Amplitude Correction Factor = 2
axis ([0 500 0 5]); %Zoom in
title('Amplitude Spectrum with Hann Wnd');
xlabel('Frequency (Hz)with hanning window');

• Please keep in mind that this code has some errors in it. – jojek May 4 '16 at 8:14
• Hi,Could you elaborate on what errors those are please? – Annabeth May 5 '16 at 0:59

From mYdft = abs(Ydft_HannWnd), the variable mYdft is the absolute value of the elements of the Fourier transform of the time-domain windowed signal y_HannWnd.
In the line mYdft = mYdft (1:nfft/2+1), a single side of the spectrum is taken instead of the full vector. Since half of the energy is on the positive side of the spectrum and half on the negative side, for a complete representation of the single side spectrum you multiply all the elements by a factor 2 except for the DC value, i.e. 2:end-1.
In the plot, plot(f,2*mYdft), the resulting single-side spectrum is simply multiplied by a factor of 2.