% Solution to problem on Quiz 3
%
% 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 = -10:20/1000:10;
N = input('Number of harmonics ');
T = 10;		% Period
w0 = 2*pi/T;	% Fundamental frequency

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

% Compute truncated exponential Fourier series

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

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

