%% Author Roberto Rosas-Romero
%% Parameters of Periodic Signal
% Period
T = pi;
% Fundamental Frequency
w = 2 * pi / T;
% Pulse width for the case of a pulse train
% u = 1.5;
%% Integration to compute Fourier Coefficients
% Integration Limits
a = 0;
b = pi;
% Integration to estimate Exponential Fourier Coefficients
syms t;
syms n;
integral = (1/T) * int(exp(-t/2) * exp(-i* n * w* t), t, a, b);
% Defining Dn functions of n
Dn = inline(integral, 'n');
%% Approximation of the original signal by using the Fourier Series
% Time Interval over which the periodic signal is approximated
t = - 2*pi : 0.01 : 2*pi;
% Number of Harmonics used in the approximation
Harmonics = 50;
% Initializing approximation
x = 0;
% Iterative addition of harmonics
for n = -Harmonics : Harmonics
x = x + Dn(n) * exp(i * n * w* t);
end;
%% Plot of the Approximation
subplot(3, 1, 1), plot(t, x, 'r');
%% Computation and Plot of Magnitude Spectrum
% Initialization of vector of frequencies
VectorOfFrequencies = [];
% Initialization of vector of coefficients
VectorOfCoefficients = [];
% Initialization of Vector of Phase
VectorOfPhase = [];
% Initialization of the Power of the Approximation
P = 0;
% Number of Harmonics used in the approximation
Harmonics = 50;
for n = -Harmonics : Harmonics
VectorOfFrequencies = [VectorOfFrequencies; n*w];
VectorOfCoefficients = [VectorOfCoefficients; abs(Dn(n))];
VectorOfPhase = [VectorOfPhase; phase(Dn(n))];
P = P + abs(Dn(n))^2;
end;
% Power in the original signal
Poriginal = (1 - exp(-pi)) / pi;
% Percentage of Power contained in the approximation
Percentage = double(100 * P / Poriginal);
% Plot of the Magnitude Spectrum
hold on; subplot(3, 1, 2), plot(VectorOfFrequencies, VectorOfCoefficients, 'o');
% Plot of the Phase Spectrum
subplot(3, 1, 3), plot(VectorOfFrequencies, VectorOfPhase, 'o');