% Amplitude spectrum of harmonic signals %emol
% (complex Fourier coefficients) %emol

clc %emol
clearvars %emol
close all %emol

f1=10; f2=20; f3=21; fs=60; 
T=0.6; % try different values: 0.6, 0.8, 1.0, 1.5, 2, 2.5, 3, 4

t=0:(1/fs):T-1/fs; % why 1/fs? what is it? %emol
x=2*sin(2*pi*f1*t)+2*sin(2*pi*f2*t)+2*sin(2*pi*f3*t);
N=length(x);
X=fft(x);
f=(fs/N)*(0:N-1); % why fs/N? what is it? %emol

Xz=fft([x zeros(1,2000-N)]); % zero padding
Nz=length(Xz);
fz=(fs/Nz)*(0:Nz-1); 
% zero padding does not improve 'true' resolution  (ability to to
% distinguish closely spaced frequencies), just interpolates data
% in the frequency domain
% (see, for instance, T=0.6 and T=4, as Nz is constant) %emol

figure(1)
stem(f(1:N/2+1),abs(X(1:N/2+1)/(fs*T)),'r:') % why fs*T? what is it? %emol
axis([0 30 0 1.2])
xlabel('Frequency (Hz)'); ylabel('Modulus')
hold on
plot(fz(1:Nz/2+1),abs(Xz(1:Nz/2+1)/(fs*T)),'b-') %emol
hold off; 
grid on