restart:# FAST FOURIER TRANSFORM ALGORITHM 8.3## To compute the coefficients in the discrete approximation# for the data (x(J),y(J)), 0<=J<=2m-1 where m=2^p and# x(J)=-pi+J*pi/m for 0<=J<=2m-1.## INPUT: m: y(0),y(1),...y(2m-1).## OUTPUT: complex numbers c(0),...,c(2m-1): real numbers# a(0),...,a(m): b(1),...,b(m-1).IBR := proc(J,NU) local J1, K, JJ, J2:J1 := J:K := 0:for JJ from 1 to NU doJ2 := floor(J1/2):K := 2*K+J1-2*J2:J1 := J2:od:RETURN(K): end:print(`This is the Fast Fourier Transform.`):print(`The user must make provisions if the`):print(`interval is not [-pi,pi].`):OK := FALSE:while OK = FALSE doprint(`Choice of input method:`):print(`1. Input entry by entry from keyboard`):print(`2. Input data from a text file`):print(`3. Generate data using a function F`):print(`Choose 1, 2, or 3 please`):FLAG := scanf(`%d`)[1]:print(`Input is `):print(FLAG):if FLAG = 1 or FLAG = 2 or FLAG = 3 thenOK := TRUE:fi:od:if FLAG = 1 thenOK := FALSE:while OK = FALSE doprint(`Input m\134n`):M := scanf(`%d`)[1]:print(`m = `):print(M):if M > 0 thenOK := TRUE:N := 2*M:for JJ from 1 to N doJ := JJ-1: print(`Input y(I) for I = `):print(J):Y[JJ-1] := scanf(`%f`)[1]: print(`Input is `):print(Y[JJ-1]):od:elseprint(`Number must be a positive integer.\134n`):fi:od:fi:if FLAG = 2 thenprint(`Has a text file been created with the `):print(`entries y(0),...,y(2m-1)\134n`):print(`separated by a blank?\134n`):print(`Enter 1 for yes and 2 for no`):A := scanf(`%d`)[1]:print(`Input is `):print(A):if A = 1 thenprint(`Input the file name in the form - `):print(`drive:\134\134name.ext\134n`):print(`for example: A:\134\134DATA.DTA\134n`):NAME := scanf(`%s`)[1]:INP := fopen(NAME,READ,TEXT):OK := FALSE:while OK = FALSE doprint(`Input number m.\134n`):M := scanf(`%d`)[1]:print(`Input is `):print(M):N := 2*M:if N > 0 thenfor JJ from 1 to N doY[JJ-1] := fscanf(INP, `%f`)[1]:od:fclose(INP):OK := TRUE:else print(`Number must be a positive integer.\134n`):fi:od:elseprint(`The program will end so the input file can `):print(`be created.\134n`):OK := FALSE:fi:fi:if FLAG = 3 thenprint(`Input the function F(x) in terms of x\134n`):print(`for example: cos(x)\134n`):F := scanf(`%a`)[1]:print(`Input is F(x) = `):print(F):F := unapply(F,x):OK := FALSE:while OK = FALSEdo print(`Input the number m.\134n`):M := scanf(`%d`)[1]:print(`Input is `):print(M):N := 2*M:if N > 0 thenfor JJ from 1 to N doZ := -Pi+(JJ-1)*Pi/M:Y[JJ-1] := evalf(F(Z)):od:OK := TRUE:else print(`Number must be a postive integer.\134n`):fi:od: fi:if OK = TRUE thenprint(`Choice of output method:\134n`):print(`1. Output to screen\134n`):print(`2. Output to text file\134n`):print(`Please enter 1 or 2.\134n`):FLAG := scanf(`%d`)[1]:print(`Input is `):print(FLAG):if FLAG = 2 thenprint(`Input the file name in the form - drive:\134\134name.ext\134n`):print(`for example: A:\134\134OUTPUT.DTA\134n`):NAME := scanf(`%s`)[1]:print(`Input file is `):print(NAME):OUP := fopen(NAME, WRITE,TEXT):elseOUP := default:fi:fprintf(OUP, `FAST FOURIER TRANSFORM\134n\134n`):TW := ln(2):# Step 1# Use N2 for m, NG for p, NU1 for q, WW for zetaN2 := floor(N/2):# Step 2for JJ from 1 to N doC[JJ-1] := Y[JJ-1]:od:Z := N:NG := round(ln(Z)/TW):NU1 := NG-1:YY := 2*Pi*I/N:WW := exp(YY):# Step 3for JJ from 1 to N2 doX := 1:YY := 1:for J from 1 to JJ doYY := X*WW:X := YY:od:W[JJ-1] := X:W[N2+JJ-1] := -X:od:# Step 4K := 0:W[-1] := 1:# Step 5for L from 1 to NG do# Step 6while K < N-1 do# Step 7for JJ from 1 to N2 do# Step 8Z := exp(NU1*TW):M1 := round(Z):# IBR does the bit reversalM1 := floor(K/M1):NP := IBR(M1,NG):# T1 is used in place of etaT1 := C[K+N2]:# Step 9if NP <> 0 thenX := T1:T1 := X*W[NP-1]:fi:C[K+N2] := C[K]-T1:C[K] := C[K]+T1:# Step 10K := K+1:od:# Step 11K := K+N2:od:# Step 12K := 0:N2 := floor(N2/2):NU1 := NU1-1:od:# Step 13while K < N-1 do# Step 14JJ := IBR(K,NG):# Step 15if JJ > K thenT3 := C[K]:C[K] := C[JJ]:C[JJ] := T3:fi:# Step 16K := K+1:od:# Steps 17 and 18fprintf(OUP, `Coefficients c(0), ... , c(2m-1)\134n\134n`):for JJ from 1 to N doYY := -(JJ-1)*Pi*I:X := exp(YY):YY := X*C[JJ-1]:C[JJ-1] := evalc(evalf(YY/(0.5*N))):K := JJ-1:fprintf(OUP, `%3d %a\134n`, K, C[JJ-1]):od:fprintf(OUP, `\134nCoefficients a(0), ..., a(m)\134n\134n`):for JJ from 1 to M+1 dofprintf(OUP, `%.8f\134n`, Re(C[JJ-1])):od:fprintf(OUP, `\134nCoefficients b(1), ..., b(m-1)\134n\134n`):for JJ from 2 to M do fprintf(OUP, `%.8f\134n`, Im(C[JJ-1])): od:if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:JSFH