DECLARE SUB dc ()
DECLARE SUB radsec ()
DECLARE SUB degsec ()
DECLARE SUB hertz ()
DECLARE SUB summate ()
DECLARE SUB seconds ()
DECLARE SUB radians ()
DECLARE SUB degrees ()
DECLARE SUB clearscreen ()
DECLARE SUB clearmem ()
DECLARE SUB newsin ()
DECLARE SUB showpeak ()

' this sets up the screen
CLS
SCREEN 12
VIEW (0, 0)-(639, 400)
WINDOW (0, 10.1)-(639, -10.1)
VIEW PRINT 26 TO 30
LINE (0, 0)-(640, 0)
LINE (0, 10)-(0, -10)


'this sets up the memory
DIM SHARED sum(0 TO 640)  AS SINGLE
'shared means this array is shared by all subroutines
'single means precision of each entry is 32 bits long and floating decimal point

DIM SHARED peak(0 TO 640) AS SINGLE

DIM SHARED pi AS SINGLE
pi = 3.141593

DIM SHARED frq  AS SINGLE

DIM SHARED dly   AS SINGLE
                 
DIM SHARED dcv  AS SINGLE

DIM SHARED amp   AS SINGLE

DIM SHARED x     AS SINGLE

DIM SHARED y     AS SINGLE

DIM SHARED col
col = 9


PRINT "This program is modified from sinesum.bas."
PRINT "It sums sinewaves as harmonics to a fundamental to reconstruct the fundamental."
PRINT "This program does not allow for cosines and is therefore incomplete."
FOR screendelay = 1 TO 900000
NEXT screendelay

'this is the command handling
100
CLS 2
COLOR 15



CALL newsin
CLS 2
CALL clearscreen
CALL summate
INPUT "Hit enter"; q
CALL clearmem
CALL clearscreen
GOTO 100









GOTO 100

SUB clearmem
ERASE peak
ERASE sum
END SUB

SUB clearscreen
CLS
COLOR 15
LINE (0, 0)-(639, 0)
LINE (0, 10)-(0, -10)

END SUB

SUB dc
CLS 2
COLOR 15

INPUT "Enter a dc value: "; dcv

col = col + 1
IF col > 14 THEN col = 9
COLOR col

FOR tic = 0 TO 639

   
    x = tic
   
    y = dcv
   
    sum(tic) = sum(tic) + y
    IF y > peak(tic) THEN peak(tic) = y


    LINE (x, y)-(x, y)
NEXT tic
COLOR 15
END SUB

SUB degrees
CLS 2
COLOR 15
INPUT "Enter delay in degrees: ", dly
dly = dly * 2! * pi / 360!
' converts delay degrees into radians
' ! is to designate data type 'single' to prevent overflow during calculations


END SUB

SUB degsec
120
CLS 2
COLOR 15
INPUT "Enter the frequency in degrees/second: ", frq
IF frq < 0 GOTO 120
frq = frq * 2! * pi / (640! * 360!)

' converts freq to rad/tic
END SUB

SUB hertz
140
CLS 2
COLOR 15
frq = 1
IF frq < 0 GOTO 140
frq = frq * 2! * pi / 640!

'converts freq to rad/tic


END SUB

SUB newsin
CLS 2
COLOR 15





CLS 2
COLOR 15
frq = 0
IF freq$ = "h" THEN CALL hertz
IF freq$ = "r" THEN CALL radsec
IF freq$ = "d" THEN CALL degsec




dly = 0
IF del$ = "s" THEN CALL seconds
IF del$ = "r" THEN CALL radians
IF del$ = "d" THEN CALL degrees


CLS 2
IF c$ = "c" THEN dly = dly - (pi / 2!)

INPUT "enter number of fundamental cycles to view"; cycles


50
CLS 2
INPUT "enter harmonic number (1 = Fundamental) or enter to sum"; harm%
IF harm% = 0 THEN GOTO 1000

INPUT "enter amplitude of the harmonic (0 to 100)"; amp


amp = amp / 10



frq = harm% * cycles
frq = frq * 2! * pi / 640

IF frq > 2.9 THEN frq = 0
IF frq = 0 THEN amp = 0


CLS 2
col = col + 1
IF col > 14 THEN col = 9
COLOR col

FOR tic = 0 TO 639
    a = (frq * tic) - dly
    a1 = (frq * (tic + 1)) - dly

    x = tic
    x1 = tic + 1

    y = amp * SIN(a)
    y1 = amp * SIN(a1)

    IF r$ = "r" THEN y = ABS(y)
    IF r$ = "r" THEN y1 = ABS(y1)

    sum(tic) = sum(tic) + y
    IF y > peak(tic) THEN peak(tic) = y


    LINE (x, y)-(x1, y1)
NEXT tic
GOTO 50


1000
COLOR 15
END SUB

SUB radians
CLS 2
COLOR 15
INPUT "Enter delay in radians: "; dly

END SUB

SUB radsec
160
CLS 2
COLOR 15
INPUT "Enter the frequency in rad/sec: ", frq
IF frq < 0 GOTO 160
frq = frq / 640!
'converts frq to rad/tic
END SUB

SUB seconds
CLS 2
COLOR 15
INPUT "Enter delay in seconds: "; dly

dly = dly * frq * 640!

'converts delay into rads
END SUB

SUB showpeak
COLOR 15
FOR tic = 0 TO 639
    x = tic
    x1 = tic + 1
    y = peak(tic)
    y1 = peak(tic + 1)
    LINE (x, y)-(x1, y1)
NEXT tic

END SUB

SUB summate
COLOR 15
FOR tic = 0 TO 639
    x = tic
    x1 = tic + 1
    y = sum(tic)
    y1 = sum(tic + 1)
    LINE (x, y)-(x1, y1)
NEXT tic

END SUB

