home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 3
/
FREEWARE.BIN
/
oh_fm
/
fft
/
fft.bas
next >
Wrap
BASIC Source File
|
1980-01-02
|
7KB
|
193 lines
1000 '
1010 ' SPECTRUM ANALYSER with FFT
1020 ' 1989/12/20 by Teckey ZeroOne
1030 '
1040 CLEAR ,,8192:DEFINT A-Z :TESTF=0
1050 BIT=8:N=2^BIT:P!=6.28318!/N
1060 MOUSE 0:ON MOUSE (4) GOSUB *MOUSE4
1070 ON MOUSE(2) GOSUB *MOUSE2
1080 HAMF=1:SLOG=1:FAUTO=1:SFREQ=9600:BFREQ!=SFREQ/N
1090 GOSUB *SINIT:GOSUB *VSCALE:GOSUB *HSCALE:GOSUB *COND
1100 GOSUB *INIT
1110 MOUSE(2) ON:MOUSE(4) ON
1120 *MLOOP:LINE (17,78)-(72,97),XOR,7,BF
1130 IF FAUTO<>1 THEN A$=INKEY$:IF A$=""THEN 1130
1140 LINE (17,78)-(72,97),XOR,7,BF
1150 IF TESTF=0 THEN GOSUB *RECORD ELSE GOSUB *SETDATA
1160 GOSUB *SCREEN
1170 IF HAMF=1 THEN GOSUB *HAMMING
1180 GOSUB *FFT
1190 GOSUB *POW
1200 GOSUB *PLOT
1210 GOTO *MLOOP
1220 *SETDATA:'SETUP DATA FOR TEST
1230 FOR I=0 TO N-1:IMAG!(I)=0
1240 REAL!(I)=(I>N/32)+1
1250 'REAL!(I)=SIN(P!*N*SIN(P!*I*2.1!))
1260 NEXT I
1270 HAMF=0:FAUTO=0:GOSUB *COND:RETURN
1280 *INIT
1290 DIM REAL!(N-1),IMAG!(N-1),POW!(N-1),SAMPLE(N/2+32)
1300 DIM TSIN!(N-1),TCOS!(N-1),TREV(N-1)
1310 LINE (19,58)-(69,77),XOR,7,BF
1320 FOR I=0 TO N-1
1330 TCOS!(I)=COS(I*P!):TSIN!(I)=SIN(I*P!)
1340 REV=0:K=I
1350 FOR J=0 TO BIT - 1
1360 REV = REV + REV
1370 IF (K AND 1)=1 THEN REV = REV + 1
1380 K = K \ 2
1390 NEXT J
1400 TREV(I)=REV
1410 NEXT I:LINE (19,58)-(69,77),XOR,7,BF
1420 RETURN
1430 *SINIT
1440 SCREEN@ 0:VIEW:WINDOW:CLS
1450 PALETTE 0,[80,80,90]:PALETTE 15,[210,40,60]:PALETTE 13,[180,10,80]
1460 PALETTE 12,[240,120,120]:PALETTE 14,[200,250,90]
1470 PALETTE 10,[50,250,50]:PALETTE 11,[200,200,50]
1480 SYMBOL (0,8),"FFT",2,2,4,0,PSET,1+2+4
1490 LINE(116,1)-(629,203),PSET,1,B:LINE(116,212)-(629,453),PSET,1,B
1500 *SYMINIT:VIEW:WINDOW:LINE (16,58)-(72,179),PSET,0,BF
1510 SYMBOL (20,60), " INIT ",1,1,3,0,PSET,1
1520 SYMBOL (16,80), "STANDBY",1,1,3,0,PSET,1
1530 SYMBOL (20,100), "RECORD",1,1,3,0,PSET,1
1540 SYMBOL (16,120)," SETUP",1,1,3,0,PSET,1
1550 SYMBOL (16,140)," FFT ",1,1,3,0,PSET,1
1560 SYMBOL (16,160)," POWER",1,1,3,0,PSET,1
1570 RETURN
1580 *FFT
1590 LINE (19,138)-(69,157),XOR,7,BF
1600 N1=1:N2=N:LOCATE 2,9
1610 *LOOP1
1620 N2=N2/2 : K1=0
1630 *LOOP2
1640 X=0:K2=K1+N2-1
1650 FOR I=K1 TO K2
1660 H=I+N2
1670 V!=TCOS!(X):W!=TSIN!(X)
1680 R!=REAL!(I):I!=IMAG!(I)
1690 REAL!(I)=R!+REAL!(H):IMAG!(I)=I!+IMAG!(H)
1700 Q!=(R!-REAL!(H))*V! + (I!-IMAG!(H))*W!
1710 IMAG!(H)=(I!-IMAG!(H))*V! - (R!-REAL!(H))*W!
1720 REAL!(H)=Q!
1730 X=X+N1
1740 NEXT I
1750 K1=K1+N2+N2
1760 IF K1 < N THEN *LOOP2
1770 IF N2=1 THEN *FFTEND
1780 N1=N1+N1
1790 PRINT "*";:GOTO *LOOP1
1800 *FFTEND
1810 LINE (19,138)-(69,157),XOR,7,BF
1820 RETURN
1830 *POW:MAXPOW!=0
1840 LINE (19,158)-(69,177),XOR,7,BF
1850 FOR I=0 TO N/2
1860 RE!=REAL!(TREV(I)):IM!=IMAG!(TREV(I))
1870 POW!(I)=SQR(RE!*RE!+IM!*IM!)
1880 IF POW!(I)>MAXPOW! THEN MAXPOW!=POW!(I)
1890 NEXT I
1900 IF SLOG<>1 THEN *POWEND
1910 FOR I=0 TO N/2
1920 IF POW!(I)<>0:POW!(I)=100+20*LOG(POW!(I)/MAXPOW!):ELSE POW!(I)=0
1930 NEXT I:MAXPOW!=100
1940 *POWEND:LINE (19,158)-(69,177),XOR,7,BF
1950 RETURN
1960 *HAMMING
1970 FOR I=0 TO N-1
1980 REAL!(I)=REAL!(I)*(.54!-.46!*COS(P!*I))
1990 NEXT I
2000 RETURN
2010 *SCREEN
2020 VIEW (117,2)-(628,202):CLS
2030 WINDOW(0,1)-(N-1,-1)
2040 LINE(0,0)-(N-1,0),PSET,1
2050 PSET(0,REAL!(0))
2060 FOR I=1 TO N-1
2070 LINE -(I,REAL!(I)),PSET
2080 NEXT I
2090 WINDOW:VIEW:RETURN
2100 *PLOT
2110 VIEW (117,213)-(628,452):CLS
2120 WINDOW(0,MAXPOW!)-((N-1)/2,0):DEF PEN 0,2
2130 FOR I=0 TO N/2-1
2140 IF (I MOD 10) = 0 THEN PCOL = 5 ELSE PCOL = 7
2150 LINE (I,POW!(I))-(I,0),PSET,PCOL
2160 NEXT I
2170 LINE(1,POW!(1))-(1,0),PSET,2
2180 DEF PEN 0 :WINDOW:VIEW:RETURN
2190 *RECORD
2200 LINE (19,98)-(69,117),XOR,7,BF
2210 PCMREC SAMPLE,SFREQ
2220 LINE (19,98)-(69,117),XOR,7,BF
2230 *SETSAMPLE:PTR&=VARPTR(SAMPLE(30))
2240 LINE (19,118)-(69,137),XOR,7,BF
2250 FOR I=0 TO N-1
2260 DAT=PEEK(PTR&,1):PTR&=PTR&+1
2270 IF (DAT AND 128)<>0 THEN SIGN=1 ELSE SIGN=-1
2280 REAL!(I)=SIGN*(DAT AND 127)/127:IMAG!(I)=0
2290 NEXT I
2300 LINE (19,118)-(69,137),XOR,7,BF:RETURN
2310 *MOUSE2
2320 GOSUB *SYMINIT
2330 RETURN *MLOOP
2340 *MOUSE4
2350 SF=0:GOSUB *SYMINIT:MOUSE 1,10,240,1:MOUSE(2)OFF
2360 *MOSLOOP:WHILE MOUSE(2,0)<>-1:WEND
2370 MX=MOUSE(0):MY=MOUSE(1):IF MX<5 OR MX>85 THEN *MOSLOOP
2380 IF MY>450 AND MY<466 THEN *M4_END
2390 IF MY>370 AND MY<386 THEN FAUTO=FAUTO XOR 1:GOSUB *COND
2400 IF MY>320 AND MY<336 THEN SLOG=SLOG XOR 1:GOSUB *VSCALE:GOSUB *COND
2410 IF MY>420 AND MY<436 THEN HAMF=HAMF XOR 1:GOSUB *COND
2420 IF MY>220 AND MY<236 THEN GOSUB *M_SAMPLE:GOSUB *HSCALE:GOSUB *COND
2430 IF MY>270 AND MY<286 THEN GOSUB *M_FREQ:GOSUB *HSCALE:GOSUB *COND
2440 GOTO *MOSLOOP
2450 *M4_END
2460 IF SF=1 THEN GOSUB *DINIT
2470 MOUSE 1,,,0:MOUSE(4)ON:MOUSE(2)ON:RETURN *MLOOP
2480 *DINIT
2490 P!=6.28318!/N
2500 ERASE REAL!,IMAG!,POW!,SAMPLE,TSIN!,TCOS!,TREV:GOSUB *INIT
2510 RETURN
2520 *M_SAMPLE
2530 BIT=BIT+1:IF BIT>9 THEN BIT=7
2540 N=2^BIT:BFREQ!=SFREQ/N:SF=1
2550 RETURN
2560 *M_FREQ
2570 IF SFREQ=19200 THEN SFREQ=4800 ELSE SFREQ=SFREQ*2
2580 BFREQ!=SFREQ/N:RETURN
2590 *VSCALE
2600 VIEW:WINDOW:LINE(88,210)-(112,479),PSET,0,BF
2610 IF SLOG=1 THEN RESTORE *D_LOG ELSE RESTORE *D_LIN
2620 FOR Y=213 TO 453 STEP 24
2630 LINE(113,Y)-(115,Y),PSET,1
2640 READ S$
2650 SYMBOL (88,Y-4),S$,1,.6!,5
2660 NEXT Y
2670 READ S$:SYMBOL(90,463),S$,1,1,5
2680 RETURN
2690 *D_LIN DATA 1.0,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1," 0",""
2700 *D_LOG DATA " 0",-10,-20,-30,-40,-50,-60,-70,-80,-90,-∞, db
2710 *HSCALE
2720 LINE(119,454)-(400,479),PSET,0,BF
2730 SYMBOL(117,455),STR$(BFREQ!)+"(Hz)",1,1,5
2740 RETURN
2750 *COND:LINE (10,200)-(80,440),PSET,0,BF
2760 SYMBOL (10,200),"SAMPLE",1,1,6,0,PSET,1
2770 SYMBOL (10,250),"FREQ(Hz)",1,1,6,0,PSET,1
2780 SYMBOL (10,300),"SCALE",1,1,6,0,PSET,1
2790 SYMBOL (10,350),"MODE",1,1,6,0,PSET,1
2800 SYMBOL (10,400),"WINDOW",1,1,6,0,PSET,1
2810 SYMBOL (5,450),"START",2,1,2,0,PSET,1
2820 *CONDSET
2830 IF SLOG=1 THEN S$="LOG(db)" ELSE S$="LINEAR "
2840 SYMBOL (20,220),STR$(N),1,1,4,0,PSET,1
2850 SYMBOL (20,270),STR$(SFREQ),1,1,4,0,PSET,1
2860 SYMBOL (20,320),S$,1,1,4,0,PSET,1
2870 IF FAUTO=1 THEN S$="AUTO" ELSE S$="ONCE"
2880 SYMBOL (20,370),S$,1,1,4,0,PSET,1
2890 IF HAMF=1 THEN S$="ON " ELSE S$="OFF"
2900 SYMBOL (20,420),S$,1,1,4,0,PSET,1
2910 RETURN