clear
hold off
format long e
N = 1024; %No. of FFT samples
fmax = 20; 
sampling_rate = 2*fmax;
tstep = 1/sampling_rate;
tmax = N*tstep/2;
tmin = -tmax;
tt = tmin:tstep:tmax-tstep;
T = 0.25;
fmin = -fmax;
fstep = (fmax-fmin)/N;
freq = fmin:fstep:fmax-fstep;
%xt = 5*cos(2*pi*5*tt) + 2*sin(2*pi*10*tt);
xt = 10*exp(-tt.*tt/(T*T));
Hp1 = plot(tt,xt)
axis([-1 1 0 10])
set(Hp1,'LineWidth',2)
Ha = gca;
set(Ha,'Fontsize',16)
Hx=xlabel('Time (sec) ');
set(Hx,'FontWeight','bold','Fontsize',16)
Hx=ylabel('Input signal x(t)  (Volt)');
set(Hx,'FontWeight','bold','Fontsize',16)
title('Input signal x(t) : Time domain');
pause
axis auto
Xf1 = fft(xt);
Xf = fftshift(Xf1);
Hp1=plot(freq,abs(Xf))
set(Hp1,'LineWidth',2)
Ha = gca;
set(Ha,'Fontsize',16)
Hx=xlabel('Frequency (Hz) ');
set(Hx,'FontWeight','bold','Fontsize',16)
Hx=ylabel('|X(f)|');
set(Hx,'FontWeight','bold','Fontsize',16)
title('Input spectrum X(f)');
axis([-5 5 0 200])




pause
%Ideal low pass filter
B = 0.5; Cut-off frequency
n=1;
for f = fmin:fstep:fmax-fstep
 if(abs(f) < B)
     Hf(n) = 1.0;
 else
     Hf(n) = 0.0;
 end
 n=n+1;
end
Yf = Hf .* Xf;
Hp1=plot(freq,abs(Yf))
set(Hp1,'LineWidth',2)
Ha = gca;
set(Ha,'Fontsize',16)
Hx=xlabel('Frequency (Hz) ');
set(Hx,'FontWeight','bold','Fontsize',16)
Hx=ylabel('|Y(f)|');
set(Hx,'FontWeight','bold','Fontsize',16)
title('Output spectrum Y(f)');
axis([-5 5 0 200])
%axis([-10 10 0 200])
pause

Yf1 = fftshift(Yf);
yt = ifft(Yf1);
Hp1=plot(tt,real(yt),'r');
%axis([-1 1 -6 6]);
axis([-1 1 0 10])
set(Hp1,'LineWidth',2)
Ha = gca;
set(Ha,'Fontsize',16)
Hx=xlabel('Time (sec) ');
set(Hx,'FontWeight','bold','Fontsize',16)
Hx=ylabel('Output signal y(t) (Volt)');
set(Hx,'FontWeight','bold','Fontsize',16)
title('Output signal y(t)');
pause
hold on
Hp2 = plot(tt,xt)
%Hp2=plot(tt,5*cos(2*pi*5*tt),'r:')
set(Hp2,'LineWidth',2)

     
