% computes exponential Fourier series for Example 4.2
% used to generate Figures 4.6 - 4.9

t = -3:6/1000:3;
N = input('Number of harmonics ');
c0 = 0.5;
w0 = pi;
xN = c0*ones(1,length(t));   % dc component
for n=1:2:N,                 % even harmonics are zero
  theta = ((-1)^((n-1)/2) - 1)*pi/2;
  xN = xN + 2/n/pi*cos(n*w0*t + theta);
end
plot(t,xN)
title(['Example 4.2, N = ',num2str(N)])
xlabel('Time (sec)')
ylabel(['x',num2str(N),'(t)'])

%  for direct exponential series calculation 
%   the following loop replaces the loop above

% for n=1:N,
%  cn = 1/n/pi*sin(n*pi/2);         
%  c_n = cn;
%  xN = xN + cn*exp(j*n*w0*t) + c_n*exp(-j*n*w0*t);
% end



