%Experiment No.4
%Aim: To study and perform fourier series of DT signal
%Name: Anurag R Kados
%Roll NO.: EC2162
%Batch No.: A-5
%Class: SY-A

%Code-2
fs=100;
T=2;
w0=0:1/fs:10;
k=0.1/fs:1/fs:10;
y=square(2*pi*k,50);
figure
plot(k,y);
title('Squre Wave');
xlabel('Time (s)');
ylabel('Amplitude');

syms t
N=5;
n=1:N;
a0=(1/T)*(int(1,t,0,1)+int(-1,t,1,2));
an=sym(zeros(1,N));
bn=sym(zeros(1,N));
for i=1:N
    an(i)=(2/T)*(int(1*cos(i*2*pi/T*t),t,0,1)+int(-1*cos(i*2*pi/T*t),t,1,2));
    bn(i)=(2/T)*(int(1*sin(i*2*pi/T*t),t,0,1)+int(-1*sin(i*2*pi/T*t),t,1,2));
end

F=double(a0)*ones(size(k));

for i=1:N
    F=F+double(an(i))*cos(i*2*pi/T*k)+double(bn(i))*sin(i*2*pi/T*k);
end

figure
plot(k,F);
title('Fourier Series Approximation');
xlabel('Time(s)');
ylabel('Amplitude');