% Question 1
clear all;
clc;
t=-100:0.0001:100;
w=-10*pi:pi/100:10*pi;

figure(1)
tau=1e-8;
xt=(t<tau).*(t>-tau);
xw=(2./w).*(sin(w.*tau));
subplot (2,1,1)
plot(t, xt)
title('tau=10^{-8}, x(t), original function')
subplot (2,1,2)
plot (w,xw)
title('tau=10^{-8}, x(w), fourier transform')

figure(2)
tau=1;
xt=(t<tau).*(t>-tau);
xw=(2./w).*(sin(w.*tau));
subplot (2,1,1)
plot(t,xt)
title('tau=1, x(t), original function')
subplot(2,1,2)
plot (w, xw)
title('tau=1, x(w), fourier transform')

figure(3)
tau=10;
xt=(t<tau).*(t>-tau);
xw=(2./w).*(sin(w.*tau));
subplot (2,1,1)
plot(t,xt)
title('tau=10, x(t), original function')
subplot (2,1,2)
plot (w,xw)
title('tau=10, x(w), fourier transform')

figure(4)
tau=40;
xt=(t<tau).*(t>-tau);
xw=(2./w).*(sin(w.*tau));
subplot (2,1,1)
plot(t,xt)
title('tau=40, x(t), original function')
subplot (2,1,2)
plot(w,xw)
title('tau=40, x(w), fourier transform')



clear all
u=@(t)(t>=0);
r=@(t)t.*(t>=0);

xt=@(t)u(t+1)-u(t-1);
yt=@(t)r(t+1)-2*r(t)+r(t-1);
zt=@(t)(u(t+1)-u(t-1)).*cos(2*pi/4*t);

t=-50:0.01:50;

subplot(3,2,1)
plot(t,xt(t))
axis([-3 3 -0.3 1.3])
subplot(3,2,3)
plot(t,yt(t))
axis([-3 3 -0.3 1.3])
subplot(3,2,5)
plot(t,zt(t))
axis([-3 3 -0.3 1.3])
w=-20:0.01:20;
xw=[];
yw=[];
zw=[];
for i=w
    int=xt(t).*exp(j*i*t);
xw=[xw trapz(t,int)];
end

for i=w
    int=yt(t).*exp(j*i*t);
yw=[yw trapz(t,int)];
end

for i=w
    int=zt(t).*exp(j*i*t);
zw=[zw trapz(t,int)];
end
subplot(3,2,2)
plot(w,(1/(2*pi)*xw))
subplot(3,2,4)
plot(w,(1/(2*pi)*yw))
subplot(3,2,6)
plot(w,(1/(2*pi)*zw))

% Question2
clear all;
clc;

figure(1)
tau=10;
t=-50:0.13:50;
w=-10*pi:pi/100:10*pi;
xt=(1./(pi*t)).*(sin(t.*tau));
xw=zeros(1,length(w));
for i=1:length(w)
    xw(i)=(1/(2*pi))*trapz(t,(xt.*exp(j*w(i).*t)));
end
subplot(1,2,1)
plot(t,xt)
title('tau=10, x(t), original function')
subplot(1,2,2)
plot(w,xw)
title('tau=10, x(w), fourier transform')

figure(2)
tau=1;
t=-50:0.13:50;
w=-10*pi:pi/100:10*pi;
xt=(1./(pi*t)).*(sin(t.*tau));
xw=zeros(1,length(w));
for i=1:length(w)
    xw(i)=(1/(2*pi))*trapz(t,(xt.*exp(j*w(i).*t)));
end
subplot(1,2,1)
plot(t,xt)
title('tau=1, x(t), original function')
subplot(1,2,2)
plot(w,xw)
title('tau=1, x(w), fourier transform')
