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:L1)*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 singlesided 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('SingleSided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('Y(f)')
%Plot nonwindowed phase spectrum.
phi=angle(Z);
figure(2); plot(f, phi(1:NFFT/2+1)/pi); %plot in radian.
title('SingleSided Phase Spectrum of nonwindowed 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
