%Fourier example
T=1.0;
dt=0.01;
t=0:dt:T;
wo=2*pi/T;
sig=ones(size(t));
myind=find(t>0.5);
sig(myind)=-1;
sig(1)=0;
sig(end)=0;
si=find(t==0.5);
sig(si)=0;

figure(1)
plot(t,sig)
ylim([-1.1,1.1])	

%[A1,B1]=fourier_coeffs(1,sig,T)
%[A2,B2]=fourier_coeffs(2,sig,T)
[A1,B1,cos1,sin1]=fourier_coeffs(1,sig,T)
[A2,B2,cos2,sin2]=fourier_coeffs(2,sig,T)

f1=A1*cos(wo*t)+B1*sin(wo*t);
f2=A2*cos(wo*t)+B2*sin(wo*t);

figure(2)
plot(t,sig,t,f1,t,f2,t,f1+f2)

figure(3)
plot(t,cos1,t,sin1)
legend('cos1','sin1')
title('n=1')

figure(4)
plot(t,cos2,t,sin2)
legend('cos2','sin2')
title('n=2')

N=30;
nvect=1:N;
A=zeros(size(nvect));
B=zeros(size(nvect));
f=zeros(length(t),N);
legvect={};
legvect{1}='signal';

for n=1:length(nvect),
	[A(n),B(n)]=fourier_coeffs(n,sig,T)
 	f(:,n)=A(n)*cos(n*wo*t)+B(n)*sin(n*wo*t);
 	legvect{n+1}=['n=',num2str(n)];
end
f=f';
legvect{length(legvect)+1}='total f';
totalf=sum(f);

figure(5)
%plot(t,sig,t,f,t,totalf)
plot(t,sig,t,totalf)
%legend(legvect)