Wednesday, May 25, 2022

BASIC Fourier example

The header on this was left intact to give credit to the original author Hal Chamberlin, and Allen Murphey who ported it to the Tandy Color Computer.  I made significant optimizations to the code such as putting constants in variables, only printing the minimum text needed, adding the hardware multiply patch, math logic optimizations, etc... which lets the code run in about half the original time.  I also added an improved graph of the data that simply draws lines between the data points to make the waveform look better.  The original version is posted on facebook in the 'I'm a CoCO Nut' group about a year ago (April 2021). 

The code looks a bit cryptic after the optimizations, but converting constants from ASCII to floating point is time consuming, and the interpreter has to do it every time a constant is used.

This update flips the graph to account for the difference between the standard X/Y coordinate layout which starts in the lower left corner with Y increasing as it goes up, and the screen layout where coordinates start in the upper left corner with Y increasing as it goes down the screen.  The multiply patch code is now grouped with the data to make it easier to add to other programs.

This is the video I made last year that compared the original code to the optimized code.  It does not show the flip in the Y axes.




100  REM FIG. 13-7 PROGRAM TO OPTIMALLY FILL WAVEFORM TABLES
110  REM MUSICAL APPLICATIONS OF MICROPROCESSORS
120  REM COPYRIGHT 1985 BY HAL CHAMBERLIN AND HAYDEN BOOKS
130  REM SEE TABLE FILLING BY FOURIER SERIES, PAGE 432
140  REM HTTPS://ARCHIVE.ORG/DETAILS/MUSICALAPPLICATI00CHAM
150  REM COCO EXTENDED COLOR BASIC MODIFICATIONS
160  REM BY R. ALLEN MURPHEY
170  REM FOR HTTP://DISCORD.COCOTALK.LIVE #ORCH90-COCODAC-16 CHANNEL
180  REM YOU PROBABLY WANT THIS POKE &HFFD9,0
1000 REM PROGRAM TO UTILIZE THE FOURIER SERIES TO FILL A WAVEFORM TABLE
1010 REM ARRAY A HOLDS THE AMPLITUDES OF THE HARMONICS
1020 REM ARRAY P HOLDS THE PHASES OF THE HARMONICS
1030 REM THE ZEROTH ELEMENT OF A AND P CORRESPONDS TO THE DC COMPONENT
1040 REM VARIABLE N HOLDS THE NUMBER OF THE HIGHEST HARMONIC TO PROCESS
1050 REM VARIABLE M HOLDS THE SIZE OF THE TABLE TO GENERATE
1060 REM VARIABLE Q IF 0 SELECTS RANDOM PHASES, IF 1 USES ARRAY P
1070 REM ARRAY T IS THE GENERATED TABLE WITH ALL ENTRIES BETWEEN BUT NOT
1080 REM INCLUDING -1 AND +1

REM 1090 DIM T(256):I=10:J=I

1090 I=10:J=I
1140 N=I
1150 M=256
1160 Q=1
2 Z=0:O=1:J=Z:I=Z:DIM T(256):A1=Z:A2=Z:S=6.28318:G=.99999:V=96:H=32:P=13
1200 REM SET UP THE AMPLITUDE AND PHASE ARRAYS

1210 FOR I=Z TO N:READ A(I):NEXT

1240 FOR I=Z TO N
1250     IF Q=Z THEN P(O)=RND(O):NEXT:GOTO 1300
1260     READ P(I)
1280 NEXT

1300 REM MAIN LOOP TO COMPUTE PRELIMINARY TABLE CONTENTS
1301 REM AND SCAN RESULTING TABLE FOR MAXIMUM ABSOLUTE VALUE

1305 F=S/M
1306 PRINT@Z,"SIN GEN STEP:     OF 255";
1310 FOR I=Z TO M-O
1315     PRINT@P,I;:T(I)=Z:A1=I*F
1340     FOR J=Z TO N:T(I)=T(I)+A(J)*COS(J*A1+S*P(J)):NEXT
1365    E=ABS(T(I)):IF E > A2 THEN A2=E
1370 NEXT

1500 REM NORMALIZE THE TABLE
1505 F=O/A2*G:FOR I=Z TO M-O:T(I)=T(I)*F:NEXT

1600 REM ADD CODE HERE TO OUTPUT THE TABLE IN SUITABLE FORM
1700 PMODE 4,1:PCLS:SCREEN 1,1
REM A2-# CONVERTS FROM STANDARD COORDINATE LAYOUT TO SCREEN LAYOUT
1730 FOR I=O TO M-O:LINE(I,V+INT(A2-T(I-O)*H))-(I,V+INT(A2-T(I)*H)),PSET:NEXT

1760 A$=INKEY$
1770 IF A$="" THEN 1760
1800 END



REM COCO3 HARDWARE MULTIPLY PATCH BY JAMES DIFFENDAFFER.  
REM JUST ADD A GOSUB 10000 AT THE TOP OF YOUR CODE TO ADD IT
REM ADDRESS WE ARE STORING THE MULTIPLY PATCH IN RAM

0 POKE 65497,0:CLS:GOSUB10000

10000 AD=VAL("&HFA0C")
REM POKE THE MULTIPLY PATCH INTO RAM
10001 FORI=0 TO 64:READ B$:A=VAL("&H"+B$):POKE AD+I,A:NEXT
REM MAKE BASIC JUMP TO OUR MULTIPY INSTEAD OF USING IT'S CODE.  $BB00 JMP $FA0C
10002 POKE VAL("&HBB02"),VAL("&H7E"):POKE VAL("&HBB03"),VAL("&HFA"):POKE VAL("&HBB04"),VAL("&H0C")
10003 RETURN
10004 DATA 32,79,E7,60,96,60,3D,ED,63,E6,60,96,5E,3D,ED,61,E6,60,96,5D
10005 DATA 3D,ED,65,E6,60,96,5F,3D,E3,62,ED,62,EC,65,E9,61,89,00,ED,60
10006 DATA EC,63,D3,15,97,16,D7,63,EC,61,D9,14,99,13,DD,14,A6,60,89,00
10007 DATA 97,13,32,67,39


REM ORIGINAL DATA FROM THE TOP OF THE CODE MOVED
REM TO ACCOUNT FOR THE MULTIPLY PATCH DATA

11000 REM AMPLITUDE DATA
11010 DATA 0,.8,.6,.2,.55,1.0,.7,.3,.2,.1,.05
11020 REM PHASE DATA
11030 DATA 0,0,.2,.4,.7,.45,.1,.5,.85,.9,0

No comments:

Post a Comment