No BSD License  

Highlights from
Signals & Systems: Continuous and Discrete, 4e Companion Software

from Signals & Systems: Continuous and Discrete, 4e Companion Software by Rodger Ziemer
Companion Software for Signals & Systems: Continuous and Discrete, 4e

c3ex6.m
%	Program to give partial complex exponential Fourier sums of an 
%	half rectified sine wave of unit amplitude
%
n_max = input('Enter vector of highest harmonic values desired (even) ');
N = length(n_max);
t = 0:.005:1;
omega_0 = 2*pi;
for k = 1:N
	n = [];
	n = [-n_max(k):n_max(k)];
	L_n = length(n);
	X_n = zeros(1, L_n);		% Form vector of Fourier sine-coefficients; all
	X_n((L_n+1)/2+1) = -0.25*j;	% odd-order terms are zero except X(1) and X(-1) 
	X_n((L_n+1)/2-1) = 0.25*j;	% so define coefficient array as a zero array and
	for i = 1:2:L_n			% then fill in nonzero values
		X_n(i)=1/(pi*(1-n(i)^2));% Form vector of Fourier sine-coefficients
	end
	x = X_n*exp(j*omega_0*n'*t);	% Rows of exponential matrix are versus time; 
					% columns are versus n; matrix multiply sums over n
% 	Plot real part of x to get rid of small imaginary part due to computational error
	subplot(N,1,k),plot(t, real(x)), xlabel('t'), ylabel('partial sum'),...
	axis([0 1 -0.5 1.5]), text(.05,-.25, ['max. har. = ', num2str(n_max(k))])
end

Contact us at files@mathworks.com