Fourier Transform Motivation

To use this script:

Set T before running the script.
Try various values of T. I suggest the sequence
T = 2 3 4 9 15 30

Plot the time signal

% check if T has been defined
if isempty(find(strcmp(who,'T'),1))
    T = 2;
end
% period and fundamental frequency
fprintf('period T = %g\n',T);
f = 1/T;
tr = 5;
t = linspace(-tr,tr,201);
y = rect(t);
plot(t,y,'k','LineWidth',2);
xlabel('time');
ylabel('signal');
grid;
axis([-tr tr -0.2 1.2]);
period T = 3

Plot the Fourier series

% maximum harmonics to give kf = fmax
fmax = 5;
kmax = ceil(fmax/f);
k=1:kmax;
c = f*sinc(k*f);
ys = ones(size(t))*f;
for m = k
    ys = ys + 2*c(m)*cos(2*pi*k(m)*f*t);
end
plot(t,y,t,ys,'k','LineWidth',2);
grid;
axis([-tr tr -0.2 1.2]);

Plot the coefficients (discrete)

stem( [0 k]*f, [1 c/f],'k','LineWidth',2);
grid;
xlabel('frequency (\it{kf})');
ylabel('coefficient value');
text(2,0.5,sprintf('%g harmonics',kmax),'FontSize',20);
axis([-0.05 fmax -0.3 1.1]);

Plot the coefficients (continuous)

if kmax>50
    plot([0 fmax],[0 0], [0 k]*f, [1 c/f],'k','LineWidth',2);
    grid;
    xlabel('frequency (\it{kf})');
    ylabel('response');
    axis([0 fmax -0.3 1.1]);
end