Fourier Series Example: Matlab Code
Fourier Series Example: Matlab Code
%FOURIER_SERIES_01_MAT
%
fig_size = [232 84 774 624];
x = [0.1 0.9 0.1]; % 1 period of x(t)
x = [x x x x]; % 4 periods of x(t)
tx = [-2 -1 0 0 1 2 2 3 4 4 5 6]; % time points for x(t)
figure(1),plot(tx,x),grid,xlabel('Time (s)'),ylabel('Amplitude'),...
title('Periodic Signal x(t)'),axis([-2 6 0 1]),...
set(gcf,'Position',fig_size)
%
a0 = 0.5; % DC component of Fourier Series
ph0 = 0;
n = [1 3 5 7 9]; % Values of n to be evaluated
an = -3.2 ./ (pi * n).^2; % Fourier Series coefficients
mag_an = abs(an);
ph_an = -180 * ones(1,length(n));
%
n = [0 n];
mag_an = [a0 mag_an]; % Including a0 with a_n
ph_an = [ph0 ph_an];
%
figure(2),clf,subplot(211),plot(n,mag_an,'o'),grid,xlabel('Harmonic Number'),...
ylabel('Magnitude'),title('Fourier Series Magnitude'),axis([0 10 0 0.6]),...
set(gcf,'Position',fig_size)
%
subplot(212),plot(n,ph_an,'o'),grid,xlabel('Harmonic Number'),...
ylabel('Phase (deg)'),title('Fourier Series Phase'),axis([0 10 -200 0]),...
set(gcf,'Position',fig_size)
%
w0 = pi; % Fundamental Frequency
t = [-2:0.002:6]; % time vector for approximations
%
x1 = 0; % approximation with DC + 1 term
for i = 1:2
x1 = x1 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);
end
%
x2 = x1; % approximation with DC + 2 terms
i = 3;
x2 = x2 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);
%
x3 = x2; % approximation with DC + 3 terms
i = 4;
x3 = x3 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);
bass.gmu.edu/~gbeale/ece_220/fourier_series_01_mat.html
1/2
27/12/13
%
x4 = x3; % approximation with DC + 5 terms
for i = 5:6
x4 = x4 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);
end
%
figure(3),subplot(221),plot(t,x1),grid,xlabel('Time (s)'),...
ylabel('Amplitude'),title('DC + 1 Term'),axis([-2 6 0 1]),...
subplot(222),plot(t,x2),grid,xlabel('Time (s)'),...
ylabel('Amplitude'),title('DC + 2 Terms'),axis([-2 6 0 1]),...
subplot(223),plot(t,x3),grid,xlabel('Time (s)'),...
ylabel('Amplitude'),title('DC + 3 Terms'),axis([-2 6 0 1]),...
subplot(224),plot(t,x4),grid,xlabel('Time (s)'),...
ylabel('Amplitude'),title('DC + 5 Terms'),axis([-2 6 0 1]),...
set(gcf,'Position',fig_size)
%
%
%
bass.gmu.edu/~gbeale/ece_220/fourier_series_01_mat.html
2/2