Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Windowing effect on spectral leakage and phase

Subject: Windowing effect on spectral leakage and phase

From: shinchan

Date: 18 Nov, 2009 04:54:19

Message: 1 of 1

I know that in doing DFT, windowed signal has less spectral leakage and better gain. But I got quite the opposite with the code below. I have been trying to figure out why but to no avail. Also, I don't know why I don't get complete zero magnitude at frequency other than 50 and 120Hz, this is screwing up my phase plot. At this point I would appreciate your input. Thanks very much.

format long;
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*cos(2*pi*50*t+0.25*pi) + cos(2*pi*120*t+0.75*pi);
windowHanning = window(@hann, length(x));
y = windowHanning.*x'; % multiply by Hanning window.
z = x'; % Not windowed version.

NFFT=length(y);
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);

NFFTz=length(z);
Z = fft(z,NFFTz)/L;
fz = Fs/2*linspace(0,1,NFFTz/2+1);

% Plot single-sided amplitude spectrum.
figure(1); plot(f,2*abs(Y(1:NFFT/2+1)), fz, 2*abs(Z(1:NFFTz/2+1)), '-r');
legend('Windowed', 'not windowed');
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

%Plot non-windowed phase spectrum.
phi=angle(Z);
figure(2); plot(f, phi(1:NFFT/2+1)/pi); %plot in radian.
title('Single-Sided Phase Spectrum of non-windowed orignal signal');
xlabel('Frequency (Hz)');
ylabel('Phase (rad)');
format long;
abs(Y(51))*2%Magnitude at 50Hz.
abs(Y(121))*2%Magnitude at 120Hz.
angle(Y(51))/pi %Phase angle (rad) at 50Hz
angle(Y(121))/pi %Phase angle (rad) at120Hz

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us