% 
% example
%
fs=1500;
T=1/fs;
N=150;
t=[0:N-1]*T;
x=5*cos(400*pi*t)+5*cos(422*pi*t+pi/2)-0.02*sin(1200*pi*t);
Xdfs=abs(fft(x));
zeroindx=find(Xdfs<10^-2); % find those very small values in the spectrum
Xdfs(zeroindx)=10^-2;
f=[0:N-1]*fs/N;
figure;stem(f,20*log10(abs(Xdfs)));axis tight;title('DFS in dB');Xlabel('Hz');
%
% DFT
%
N=256;
t=[0:N-1]*T;
x=5*cos(400*pi*t)+5*cos(422*pi*t+pi/2)-0.02*sin(1200*pi*t);
Xdft=fft(x);
f=[0:N-1]*fs/N;
figure;plot(f,20*log10(abs(Xdft)));axis tight;title('DFT in dB');Xlabel('Hz');
%
% apply window function
%
whm=hamming(N);
rn=ones(1,N);
whn=hanning(N);
Whm=fftshift(fft(whm,1024));
Rn=fftshift(fft(rn,1024));
Whn=fftshift(fft(whn,1024));
ff=[0:1023]*fs/1024;
figure;plot(ff,20*log10(abs(Whm)),'r',ff,20*log10(abs(Rn)),'g',ff,20*log10(abs(Whn)),'b');
Xlabel('Hz');axis tight;title('window spectra in dB');
%
xwhn=x.*whn';
Xwhn=fft(xwhn);
xwhm=x.*whm';
Xwhm=fft(xwhm);
%figure;plot(f,abs(Xwhn),'b',f,abs(Xwhm),'r',f,abs(Xdft),'g');Xlabel('Hz');
%axis tight;title('DFT of windowed signals');
figure;plot(f,20*log10(abs(Xwhn)),'b',f,20*log10(abs(Xwhm)),'r',f,20*log10(abs(Xdft)),'g');
Xlabel('Hz');axis tight;title('DFT of windowed signals in dB');
%
% zero padding
Ne=4096; %zero padding to the length of Ne
Xdft=fft(x,Ne);
Xwhn=fft(xwhn,Ne);
Xwhm=fft(xwhm,Ne);
ff=[0:Ne-1]*fs/Ne;
%figure;plot(ff,abs(Xwhn),'b',ff,abs(Xwhm),'r',ff,abs(Xdft),'g');Xlabel('Hz');
%axis tight;title('DFT of windowed signals with zero padding');
figure;plot(ff,20*log10(abs(Xwhn)),'b',ff,20*log10(abs(Xwhm)),'r',ff,20*log10(abs(Xdft)),'g');
Xlabel('Hz');axis tight;title('DFT of windowed signals with zero padding in dB');



