-1
$\begingroup$

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'); 
$\endgroup$
2
  • $\begingroup$ Please keep in mind that this code has some errors in it. $\endgroup$
    – jojeck
    Commented May 4, 2016 at 8:14
  • $\begingroup$ Hi,Could you elaborate on what errors those are please? $\endgroup$
    – Annabeth
    Commented May 5, 2016 at 0:59

1 Answer 1

0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.