Source: Write MP3 decoder in Java--gui
/** Fft.java * Fast Fourier transform for spectral display *http://jmp123.sf.net/ */ Public classFFT { Public Static Final intFft_n_log = 9;//Fft_n_log <= Public Static Final intFft_n = 1 <<Fft_n_log; Private Static Final floatMiny = (float) ((Fft_n << 2) * MATH.SQRT (2));//(*) Private float[] real, imag, sintable, costable; Private int[] bitreverse; PublicFFT () {Real=New float[Fft_n]; Imag=New float[Fft_n]; Sintable=New float[Fft_n >> 1]; Costable=New float[Fft_n >> 1]; Bitreverse=New int[Fft_n]; intI, J, K, Reve; for(i = 0; i < fft_n; i++) {k=i; for(j = 0, Reve = 0; J! = Fft_n_log; j + +)) {Reve<<= 1; Reve|= (K & 1); K>>>= 1; } Bitreverse[i]=Reve; } Doubletheta, dt = 2 * 3.14159265358979323846/Fft_n; for(i = 0; i < (fft_n >> 1); i++) {theta= i *DT; Costable[i]= (float) Math.Cos (theta); Sintable[i]= (float) Math.sin (theta); } } /*** Fast Fourier transform for spectral display *@paramThe realio input fft_n A real number, and it also uses it to hold the FFT_N/2 output value of the FFT (the square of the complex modulus). */ Public voidCalculatefloat[] realio) { intI, J, K, ir, exchanges = 1, idx = fft_n_log-1; floatCosv, SINV, TMPR, Tmpi; for(i = 0; I! = Fft_n; i++) {Real[i]=Realio[bitreverse[i]]; Imag[i]= 0; } for(i = fft_n_log; I! = 0; i--) { for(j = 0; J! = Exchanges; J + +)) {COSV= Costable[j <<IDX]; SINV= Sintable[j <<IDX]; for(k = j; k < Fft_n; k + + exchanges << 1) {IR= k +exchanges; TMPR= COSV * Real[ir]-SINV *Imag[ir]; Tmpi= COSV * Imag[ir] + SINV *Real[ir]; Real[ir]= Real[k]-TMPR; Imag[ir]= Imag[k]-Tmpi; REAL[K]+=TMPR; IMAG[K]+=Tmpi; }} Exchanges<<= 1; IDX--; } J= Fft_n >> 1; /** Output modulus squared (fft_n times): * for (i = 1; I <= J; i++) * realio[i-1] = real[i] * Real[i] + imag [i] * imag[i]; * * If the FFT is only used for spectrum display, it can reduce the floating-point multiplication by "phasing out" the amplitude smaller. The values of Miny * and spectrum.y0,spectrum.logy0 correspond. */SINV=Miny; COSV= -Miny; for(i = j; I! = 0; i--) {TMPR=Real[i]; Tmpi=Imag[i]; if(Tmpr > Cosv && tmpr < SINV && tmpi > Cosv && tmpi <SINV) Realio[i-1] = 0; ElseRealio[i-1] = TMPR * TMPR + tmpi *Tmpi; } }}
Write MP3 decoder--gui (FFT) in Java (GO)