clc
clear all
close all
f0=3;
fs=3*60000;
t=[0:1/fs:1];
x0=1/2;
n=1;
x1=(2/pi)*sin((2*n-1)*2*pi*f0*t)/(2*n-1);
n=2;
x2=(2/pi)*sin((2*n-1)*2*pi*f0*t)/(2*n-1);
n=3;
x3=(2/pi)*sin((2*n-1)*2*pi*f0*t)/(2*n-1);
n=4;
x4=(2/pi)*sin((2*n-1)*2*pi*f0*t)/(2*n-1);
figure(1)
figure('color', [1,1,1])
subplot(4,1,1)
plot(t,x0+x1,'LineWidth',2); box off; axis tight;
title('Square wave as a linear combination of sinusoids')
ylabel('1 sinusoid')
subplot(4,1,2)
plot(t,x0+x1+x2,'LineWidth',2); box off; axis tight;
ylabel('2 sinusoids')
subplot(4,1,3)
plot(t,x0+x1+x2+x3,'LineWidth',2); box off; axis tight;
ylabel('3 sinusoids')
figure(2)
subplot(4,1,4)
plot(t,x0+x1+x2+x3+x4,'LineWidth',2); box off; axis tight;
ylabel('4 sinusoids')
N=1000;
x=1/2;
for n=1:N
y=(2/pi)*sin((2*n-1)*2*pi*f0*t)/(2*n-1);
x=x+y;
end
figure('color',[1,1,1]');
plot(t,x,'LineWidth',2);
xlabel('time (s)','fontsize',18); ylabel('Square wave','fontsize',18);
title('Square wave as a linear combination of 1000 sinusoids','fontsize',18)
set(gca,'FontSize',18)
grid on
box off
axis tight