samer@0: /* samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.units; samer@0: import samer.maths.*; // uses ComplexVector and Vec samer@0: samer@0: /** samer@0: *
samer@0: * Mostly from my old C++ version, though I should credit the following samer@0: * FFT code for the idea of using a lookup table for the bit reversal samer@0: * and also a complete lookup table of sines: samer@0: * samer@0: *
samer@0: * > From the Unix Version 2.4 by Steve Sampson, Public Domain, samer@0: * > September 1988. samer@0: * > Adapted for Java by Ben Stoltzsamer@0: * samer@0: *, September 1997 samer@0: * > samer@0: * > Refer to http://www.neato.org/~ben/Fft.html for updates and related samer@0: * > resources. samer@0: *
samer@0: * My old version computed the bit-reversed indices on the fly - silly samer@0: * really - don't know why I didn't think of a lookup table myself. samer@0: * Also, it had a lookup table of sines and cosines of the form samer@0: * sin( 2*PI/k) with k going 1, 2, 4, 8 .. N. This version has complete samer@0: * tables for sin and cosine - which is a bit of a waste of memory, samer@0: * but frankly, who cares? samer@0: * samer@0: *
samer@0: * It is the user's responsibility to fill the arrays real samer@0: * and imag with data IN THE CORRECT BITREVERSED ORDER. This samer@0: * is easy enough using the bitrev lookup table - something like this samer@0: * would do the trick: samer@0: *
samer@0: * for (int i=0; isamer@0: * The arrays are used destructively, so it is also the user's samer@0: * responsibility, eg, to reset the imaginary parts to zero even samer@0: * if they are always zero. samer@0: * samer@0: * samer@0: * The results ends up in the arrays real and imag samer@0: */ samer@0: samer@0: samer@0: public class FFT samer@0: { samer@0: public int bitrev[]; // lookup table samer@0: public double H[]; // window - not yet used internally samer@0: public double real[]; samer@0: public double imag[]; samer@0: samer@0: protected ComplexVector W; // sines and cosines lookup table samer@0: protected int N; samer@0: protected double zeros[]; samer@0: samer@0: public FFT( int n) samer@0: { samer@0: N=n; samer@0: real = new double[N]; samer@0: imag = new double[N]; samer@0: W = new ComplexVector(N); samer@0: bitrev = new int[N]; samer@0: H = new double[N]; samer@0: samer@0: // calculate bit reversal lookup table samer@0: int m=N/2, j=0; samer@0: for (int i=0; i
1) && (j>=b); b>>=1) j-=b; samer@0: j+=b; samer@0: } samer@0: samer@0: // calculate sines and cosines lookup samer@0: double th=2*Math.PI/N; samer@0: for (int i=0; i >=1; samer@0: } samer@0: } samer@0: samer@0: // make everything conjugate - does inverse fft samer@0: public void invert() { samer@0: for (int i=0; i