% Modified from file ex4_2.m from Kamen/Heck text to plot
% truncated exponential Fourier series of the square wave
% considered in class.
%
% ELEC 320, Rich Kozick, Fall 1997
%
% 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 ');
T = 1;		% Period
w0 = 2*pi/T;	% Fundamental frequency

c0 = 0.0;
xN = c0*ones(1,length(t));   % dc component

% Compute truncated exponential Fourier series

for n=1:N
  if (rem(n,2) == 0)	% Even harmonic
    cn = 0;
    c_n = 0;
  else			% Odd harmonic
    cn = 2 / (j*pi*n);
    c_n = -cn;
  end
  xN = xN + cn*exp(j*n*w0*t) + c_n*exp(-j*n*w0*t);
end

plot(t,xN)
title(['Square wave with harmonics <= ',num2str(N)])
xlabel('Time (sec)')
ylabel(['x',num2str(N),'(t)'])

