%
% Half-wave rectified sinusoid
%
t=[0:.001:2];
n = [0:50];
bn = zeros(1,length(n));
bn(2) = 1/2;
an = -(2/pi)*ones(1,length(n))./(n.*n-1); an(1) = 1/pi;an(2:2:length(n)) = 0;
hwrs = cumsum((bn'*ones(1,length(t)).*sin(2*pi*n'*t))...
   + (an'*ones(1,length(t)).*cos(2*pi*n'*t)));
Nterms = sum(an~=0);
M = moviein(Nterms,gcf);
for N = 1:2:length(n)
		clf
		subplot(211);
        plot(t,hwrs(N,:));
        axis([0 2 -.5 1.5]);
        h = text(1.7,1.2,sprintf('N: %3d',N-1));
        set(h,'fontsize',18);
        h = xlabel('Time'); set(h,'fontsize',18);
        h = ylabel('Amplitude'); set(h,'fontsize',18);
        set(gca, 'fontsize',18);
		subplot(212)
		stem(n(1:N),an(1:N),'filled','r');
		hold on
		if N>1
			stem(1,bn(2),'filled','b');
		end
		axis([min(n) max(n) -.5 .5]);
		h = xlabel('Fourier Index'); set(h,'fontsize',18);
		h = ylabel('Spectral amplitude'); set(h,'fontsize',18);
		set(gca, 'fontsize',18);
		grid
        M(:,(N+1)/2) = getframe(gcf);
end
% Sine wave display
figure
plot(t, (ones(length(t),1)*bn.*sin(2*pi*t'*n))+(ones(length(t),1)*an.*cos(2*pi*t'*n)))
%
% Square Wave
%
figure('Units','normalized','Position',[.25,.25,.7,.7])
t=[0:.001:2];
n = [0:100];
an = zeros(size(n));
bn = ones(1,length(n))./n; bn(1) = 0; bn(3:2:length(n)) = 0;
sq = cumsum((4/pi)*(bn'*ones(1,length(t)).*sin(2*pi*n'*t)));
Nterms = sum(bn~=0);
M = moviein(Nterms, gcf);
for N = 2:2:length(n)
		clf
		subplot(211);
        plot(t,sq(N,:));
        axis([0 2 -1.5 1.5]);
        h = text(1.7,1.2,sprintf('N: %3d',N-1));
        set(h,'fontsize',18);
        h = xlabel('Time'); set(h,'fontsize',18);
        h = ylabel('Amplitude'); set(h,'fontsize',18);
        set(gca, 'fontsize',18);
		subplot(212)
		stem(n(2:N),bn(2:N),'filled','r');
		axis([min(n) max(n) 0 1]);
		h = xlabel('Fourier Index'); set(h,'fontsize',18);
		h = ylabel('Spectral amplitude'); set(h,'fontsize',18);
		set(gca, 'fontsize',18);
		grid
        M(:,N/2) = getframe(gcf);
end
%
figure
plot(t, (ones(length(t),1)*bn.*sin(2*pi*t'*n))+(ones(length(t),1)*an.*cos(2*pi*t'*n)))
