Trigonometric Fourier Series of Even Triangle Wave
M-file:
%% Filename: TrigFSEvenTriangleWave.m
% Description: m-file to plot Trigonometric Fourier Series
% representation of even triangle wave and its frequency
% (amplitude and phase) spectrum.
clear; clc; close all; % clear memory and command window, close all figures
%% Fourier Series representation of triangle wave for different numbers
% of terms in summation
t = -1.5:0.0001:3.5; % times as vector over which to plot FS
Nval = [1, 3, 5, 21]; % number of terms for n in four summations
figure(); % open figure in which to plot truncated Fourier Series
for in = 1:4, % Build four triangle waves using increasing number of terms
v = -1/2; % initialize sum to a_0 = -1/2
subplot(2,2,in);
% "brute force" plot of true triangle wave
plot([ -1.5, -1, 0, 1, 2, 3, 3.5],...
[ -0.5, -1, 0,-1, 0, -1, -0.5], ...
'k-.','LineWidth',2);
hold on;
for n = 1:2:Nval(in), % sum over odd values of n
harm = (4/(n*pi)^2)*cos(n*pi*t);
v = v + harm;
plot(t,harm,'b--','LineWidth',1); % plot each harmonic
end
plot(t,v,'r-','LineWidth',2); % plot sum of all terms/harmonics
grid; hold off;
xlabel('time, sec','FontSize',14); ylabel('FS Approx to v(t)','FontSize',14);
title(['Fourier Series Approx w/',num2str(Nval(in)),' Terms'],'FontSize',14);
end
%% Frequency spectrum of Fourier Series
figure(); % open a figure to plot frequency spectrum
n = 1:2:11; % vector of values for n odd noting omega = n x omega_0
a0 = -1/2; % DC offset
an = 4./(n*pi).^2; % coefficients of cosines for n odd
bn = 0; % coefficients of sines
A = sqrt(an.^2 + bn.^2); % amplitudes
phi = -atan2(bn,an)*180/pi; % phases
%% plot amplitude spectrum
subplot(2,1,1); stem([0, n*pi], [abs(a0), A], 'filled'); grid;
xlabel('\omega (rad/sec)'); ylabel('A_n'); title('Amplitude Spectrum');
%% plot phase spectrum
subplot(2,1,2); stem([0, n*pi], [angle(a0)*180/pi, phi], 'filled'); grid;
xlabel('\omega (rad/sec)'); ylabel('\phi_n (\circ)'); title('Phase Spectrum');
Plots generated: