'this prog. plots out a sawtooth using the Fourier Series summation.
'Note: a sawtooth only has odd and even harmonic sinewaves (no cosines).
'This analysis is for a sawtooth with a dc offset such that the bottom
'of the wave rests on the horizontal axis.
'The screen will show two periods of the sawtooth.  (two teeth)



SCREEN (9)



' this sets up the screen
' left to right is 1 to 640
' top to bottom is 1 to -1
CLS
SCREEN 12
VIEW (0, 0)-(639, 400)
WINDOW (1, 1.001)-(640, -1.001)
VIEW PRINT 26 TO 30
LINE (1, 0)-(640, 0)
LINE (1, 1)-(1, -1)




'First, this program will show each harmonic sine wave.  Meanwhile, it
'will keep a sum of the instantaneous levels of each harmonic as it is
'displayed.
'The sums will be stored and shown later as the final sawtooth.
'n is the number of the harmonic being shown.
'since the screen is only 640 pixels wide, and one pixel is one sample,
'and the screen will show two periods, the maximum n allowed is 160.
'(640 divided by two periods = 320 dots per period is the sampling rate.
'Maximum frequency is 1/2 the sampling rate. 320 divided by 2 = 160)

'For ease of explaining, this program will use period T as 1second.
'Therefore, the fundamental frequency f is 1Hz.
'The screen will show 2 seconds.  t is time in seconds.
'wo is the frequency in rads/sec of the fundamental.
'wo=2*pi*f
'wo = 6.2831853
'The sampling rate is 320 Hz.
'The peak to peak amplitude of the sawtooth is 1volt.
'The dc offset is inputted in volts.
'bn is the amplitude of the nth harmonic.
'v is the instantaneous voltage for a given harmonic at time t.
'each element of the array "vsum" is the sum of instantaneous
'voltages of the harmonics (1 to n) at time t.
'bn is the amplitude of the nth harmonic.
'pi is 3.1415927

CONST pi = 3.1415927#
CONST wo = 6.2831853#

DIM t AS SINGLE
DIM sample AS INTEGER

DIM vsum(0 TO 640) AS SINGLE
DIM v AS SINGLE

DIM col AS INTEGER

PRINT "This program demonstrates the Fourier Series summation"
PRINT "of a steady state sawtooth wave."




'This section adds the dc portion to the stored sums.
INPUT "Enter the desired dc offset (-1 to 1)"; dc
FOR sample = 0 TO 640
vsum(sample) = dc
NEXT sample

CLS 1
COLOR 15
LINE (1, 0)-(640, 0)
LINE (1, 1)-(1, -1)

CLS 2
PRINT "This is the dc offset"
COLOR 12

LINE (0, dc)-(640, dc)
FOR screendelay = 1 TO 200000
NEXT screendelay






'This loop calculates bn for each n

INPUT "Enter the number of harmonics (integer 1 to 160)"; harms%
FOR n = 1 TO harms%
PRINT n;
'The formula for sawtooth bn is -Vm/pi/n
bn = -1 / pi / n

CLS 2
COLOR 15
PRINT "n ="; n
IF n > 1 THEN PRINT "this is harmonic #"; n
IF n = 1 THEN PRINT "This is the fundamental frequency. "
PRINT "The amplitude of this wave is bn = "; bn

CLS 1
COLOR 15
LINE (1, 0)-(640, 0)
LINE (1, 1)-(1, -1)

COLOR 14







'This loop calculates and plots the instantaneous v for each
'time sample for the current nth harmonic.
'Then it adds it to the vsum for each time sample.
'Each sample is 1/320th of a second.
'There are two seconds worth of samples.
pv = 0
FOR sample = 1 TO 640
't is in seconds
t = sample / 320

v = bn * SIN(n * wo * t)

'pv is previous (v at sample - 1)
LINE (sample - 1, pv)-(sample, v)
pv = v

vsum(sample) = vsum(sample) + v

NEXT sample

INPUT "Hit Enter to continue"; z

NEXT n





CLS 1
COLOR 15
LINE (1, 0)-(640, 0)
LINE (1, 1)-(1, -1)

CLS 2
PRINT "This is the final summation of the truncated Fourier series."
COLOR 13
FOR sample = 1 TO 640

LINE (sample - 1, vsum(sample - 1))-(sample, vsum(sample))

NEXT sample

