Trigonometric Fourier Series of Even Square Wave
M-file:
% Filename: TrigFSEvenSquareWave.m
% Description: m-file to plot Trigonometric Fourier Series
% representation of even square wave.
clear; clc; close all; % clear memory and command window, close all figures
t = -3:0.001:3; % times as vector over which to plot FS
Nval = [1, 3, 6, 250]; % number of terms for n in four summations
figure(); % open figure in which to plot truncated Fourier Series
for in = 1:4, % % Build four square 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 square wave
plot([-3,-5/2,-5/2,-3/2,-3/2,-1/2,-1/2,1/2,1/2,3/2,3/2,5/2,5/2,3], ...
[ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,0], ...
'k-.','LineWidth',2);
hold on;
for n = 1:Nval(in),
harm = 2*(sin(n*pi/2)/(n*pi))*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
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
Figure generated: