% Matlab Example for DFT Slides
%
%  EE322 @ Kefu Xue, Ph.D. July 2006
%
% aliasing error
fs1=360;T1=1/fs1;
fs2=2880;T2=1/fs2;
N1=72; %plot 3 periods
N2=fs2/fs1*N1; % also in the same duration
t1=(0:N1-1)*T1;
t2=(0:N2-1)*T2;
x1=2*cos(300*pi*t1-pi/3)+sin(1170*pi*t1)+0.5*cos(720*pi*t1-pi/4)-sin(450*pi*t1);
x2=2*cos(300*pi*t2-pi/3)+sin(1170*pi*t2)+0.5*cos(720*pi*t2-pi/4)-sin(450*pi*t2);
x3=0.5*cos(pi/4)+2*cos(300*pi*t1-pi/3); % overwrite the red plot x1=x3
figure;plot(t1,x1,'r',t2,x2,'b',t1,x3,'g');axis tight;
legend('aliased signal','not aliased signal', 'alised signal (analytical)',0);
xlabel('time in second');
title('Alising error coused by under sampling')
%  DFT sample
%syms t Omega; % need sympolic tool box
% Effective bandwidth of time
%funct=500^2*t^2*exp(-2000*abs(t))/250;
%Dt=sqrt(int(funct,t,-inf,inf));
% Effective bandwidth of frequency
%funcf=(Omega^2*10^12)/(10^6+Omega^2)^2/250; 
%Bo=sqrt(int(funcf,Omega, -inf, inf));
%DtBo=Dt*Bo;
% sampling and FFT
Fs=32000;
D=0.005;
t=-D/2:1/Fs:D/2-1/Fs;
N=length(t);
xa=500*exp(-1000*abs(t));
figure;subplot(2,1,1);plot(t,xa);axis tight;
title('time domain signal xa(t)');
Xa=20*log10(abs(fft(xa))/Fs);
f=(0:N-1)*Fs/N;
Xaa=20*log10(10^6./(10^6+(2*pi*f).^2));
subplot(2,1,2);plot(f,Xa,f,Xaa);axis tight;
legend('sampled (DFT)','original (FT)',0);
title('Comparison of Fourier spectra of xa(t) and its sampled signal');
ylabel('Magnitude in dB');xlabel('Hz');
%
% Window functions plot 
whm=hamming(N);
whn=hanning(N);
figure;plot(whm,'b');axis tight;hold on; plot(whn, 'g-');axis tight;
xlabel('Blue solid: Hamming window and Green dashed: Hanning window')
%
% apply window
wxa=xa.*whm';
Xwxa=20*log10(abs(fft(wxa))/Fs);
figure;plot(f,Xa,f,Xaa,f,Xwxa);axis tight;
xlabel('DFT spectrum (red) after applying Hamming window')

% end of file


   