%Question 1
clear all;
clc;

figure(1)
N=2;
n=-1000:1000;
xn=(n>=-N) .* (n<=N) ;
w=-20*pi:pi/100:20*pi;
xw=zeros (1,length(w)) ;
for i=1:length(w)
 xw(i)=sum(xn.*exp(-j*w(i).*n)) ;
end
subplot(4,1,1)
stem (w, abs (xw))
title('N=2, Magnitude of the function')
subplot(4,1,2)
stem(w,angle(xw))
title('N=2, Phase of the function')
subplot(4,1,3)
stem(w,xw)
title('N=2, Fourier Transform of the original function')
subplot(4,1,4)
stem(n,xn)
title('N=2, Original function')

figure(2)
N=5;
n=-1000:1000;
xn= (n>=-N) .* (n<=N) ;
w=-20*pi:pi/100:20*pi;
xw=zeros (1, length(w)) ;
for i=1:length(w)
 xw(i) =sum(xn.*exp(-j*w(i).*n)) ;
end
subplot (4,1,1)
stem (w, abs(xw))
title('N=5, Magnitude of the function')
subplot (4,1,2)
stem(w, angle(xw))
title('N=5, Phase of the function')
subplot (4,1,3)
stem (w, xw)
title('N=5, Fourier Transform of the original function')
subplot(4,1,4)
stem(n, xn)
title('N=5, Original function')

figure(3)
N=20;
n=-1000:1000;
xn= (n>=-N) .* (n<=N);
w=-20*pi:pi/100:20*pi;
xw= zeros(1, length(w));
for i=1:length(w)
xw(i)=sum(xn.*exp(-j*w(i).*n));
end
subplot(4,1,1)
stem(w, abs(xw))
title ('N=20, Magnitude of the function')
subplot (4,1,2)
stem(w, angle(xw))
title('N=20, Phase of the function')
subplot (4,1,3)
stem (w, xw)
title('N=20, Fourier Transform of the original function')
subplot(4,1,4)
stem(n, xn)
title('N=20, Original function')

figure(4)
N=50;
n=-1000:1000;
xn=(n>=-N) .* (n<=N);
W=-20*pi:pi/100:20*pi;
xw=zeros (1, length(w));
for i=1: length (w)
 xw(i)= sum(xn.*exp(-j*w(i) .*n));
end
subplot (4,1,1)
stem(w, abs(xw))
title('N-50, Magnitude of the function')
subplot(4,1,2)
stem(w, angle(xw))
title('N=50, Phase of the function')
subplot (4,1,3)
stem(w, xw)
title('N=50, Fourier Transform of the original function')
subplot(4,1,4)
stem(n, xn)
title('N=50, Original function')
--------------------------------------------------------------------------------
%Question 2:
clear all;
clc;
% Part (a)
figure(1)
t=-10*pi:pi/100:10*pi;
w=-50:0.01:50;
xt=cos(2*t);
xw=zeros(1,length(w)) ;
for i=1:length(w)
xw(i)=trapz(t,xt.*exp(-j*w(i)*t));
end
subplot (3,1,1)
plot (w,xw)
title( 'Fourier Transform of cos (2t)')
subplot (3,1,2)
plot (w, abs(xw))
title ('Magnitude of the Fourier Transform of cos (2t)')
subplot (3,1,3)
plot(w,angle(xw))
title ('Phase of the Fourier Transform of cos (2t)')
% Part (b)
figure(2)
t=-10*pi:pi/100:10*pi;
w=-50:0.01:50;
xt=sin(2*t);
xw=zeros (1,length(w)) ;
for i=1:length(w)
xw(i)=trapz(t,xt.*exp(-j*w(i)*t));
end
subplot (3,1,1)
plot (w,imag(xw) )% Since Fourier Transform of a sine function is imaginary!
title( 'Fourier Transform of sin(2t)')
subplot (3,1,2)
plot (w,abs(xw))
title('Magnitude of the Fourier Transform of sin(2t)')
subplot(3,1,3)
plot (w,angle(xw))
title('Phase of the Fourier Transform of sin(2t)')
---------------------------------------------------------------------
Question 3

imp=@(t)(t==0)
w=-10*pi:pi/100:10*pi;
t=-50:0.01:50;

ft=imp(t-1);
fw=zeros(1,length(w));

for i=1:length(w)
    fw(i)=trapz(t,ft.*exp(-j*w(i)*t));
end

gt=imp(t-2);
gw=zeros(1,length(w));

for i=1:length(w)
   gw(i)=trapz(t,gt.*exp(-j*w(i)*t));
end

ht=imp(t-3);
hw=zeros(1,length(w));

for i=1:length(w)
   hw(i)=trapz(t,ht.*exp(-j*w(i)*t));
end

subplot(4,1,1)
plot(w,fw)
title('Fourier Transform of f(t)')
subplot(4,1,2)
plot(w,gw)
title('Fourier Transform of g(t)')
subplot(4,1,3)
plot(w,hw)
title('Fourier Transform of h(t)')
subplot(4,1,4)
plot(w,sqrt(2*pi).*fw.*gw)
title('Fourier Transform of sqrt(2*pi).*f(w).*g(w)')
