Fourier Series Example Script

From Class Wiki
Revision as of 14:26, 29 September 2010 by Frohro (talk | contribs)
Jump to navigation Jump to search

This is a script that plots a few terms of the Fourier series of a square wave and then plots some samples of it in red *'s. You can copy this and paste it into your editor and run it from octave or just paste it into an octave window to see the plot.

% This script plots a Fourier Series for a Square Wave
clf;
t=0:.01:10;
T=2.5
M=50    
sum1=0;
for m=1:2:M,
    sum1 = sum1+4/m/pi*sin(m*pi/2)*cos(2*pi*m*t/T);
end
plot(t,sum1,'b-',t(1:10:end),sum1(1:10:end),'r*')
title('Fourier Series Representation of a Square Wave')
xlabel('time (seconds)')
ylabel('Function')
grid on;
axis([0,10,-2,2])
legend('Five Terms','Five Terms Sampled')
print("squarewave.png","-dpng")  % Prints the plot to a png file called squarewave.png