% Fourier Series and Fourier Transform example
% DRB 8-Apr-2010
% x(t) = rect(t)
% =================================================================
t = -10:0.001:10;               % time vector
T0 = 2;                         % fundamental period (increase this and see what happens)
% =================================================================

w0 = 2*pi/T0;

% create aperiodic version of the signal
x = (t<=1/2).*(t>=-1/2);

% create periodic version of the signal
xT0 = zeros(1,length(t));
for n=-5:5,
    xT0 = xT0 + (t<=(T0*n+1/2)).*(t>=(T0*n-1/2));
end

% compute Fourier transform of x
omega = -60:0.001:60;           % frequency vector
X = sinc(omega/2/pi);           % Matlab defn of sinc is different than Lathi

% compute Fourier series of xT0
n = -floor(60/w0):floor(60/w0);
Dn = (1/T0)*sinc(n*w0/2/pi);    % FS coefficients

% plot time domain representation of signals
subplot(2,1,1);
plot(t,x,t,xT0,'--');
axis([min(t) max(t) -1 2]);
legend('x(t)','xT0(t)');
grid on
title(['T0 = ',num2str(T0)]);

% plot freq domain representation of signals
subplot(2,1,2);
plot(omega,X);
hold on
stem(n*w0,T0*Dn);
hold off
legend('X(\omega)','T0*Dn');
grid on
