% Plot N hormonic components of a periodic signal x(t) with period
% 2, and x(t)= exp(-t), for  -1 <= t < 1 . for N = 1, 5, 10, 50, 100.

% We can shown that the Fourier series coefficient sequence is
% Ck = (-1)^k*(exp(1)-exp(-1))/( 2*(1+j*k*pi) )  for all k.
% T = 2, and  w0 = pi.

% Example:  demo_FS(10)

function demo_FS(Nharmonic)
syms t
xhat = 0;
for k = -Nharmonic : Nharmonic
    Ck = (-1)^k*(exp(1)-exp(-1))/( 2*(1+j*k*pi) );
    xhat = xhat + Ck * exp( j * k * pi * t );
end

figure;
ezplot( xhat, [-2, 2] );   % plot the function in -2 < t < 2
title(sprintf('N = %d', Nharmonic));

% test the convergence at discontinuity point as N --> inf 
xhatdisc = subs(xhat, -1)
half_real_disc = ( exp(1) + exp(-1) )/2
