/** * A simple textbook implementation of the 1-dimensional Fast Fourier * Transform. * * ...I found this code on my hard drive... It's been so many year * since I touched this that I don't remember if this ever worked as * it should.. so beware. It's possible that this works better than * the other example codes that I've been hacking at a fast pace:) but * I didn't try this out. Looks like I gave it some thought back then, * though... * * This is for your convenience if you want to try the rotation- and * translation invariant shape descriptor features discussed on * Tuesdays lecture. * * (You'll have to implement the boundary tracking by yourself, and * distance-from-center computation. Something to learn by doing * instead of just watching ;)). */ /** * A simple textbook implementation of the Fast Fourier Transform. * * @author nieminen */ public class SimpleFFT { /** * @param args */ public static void main(String[] args) { System.out.println("Could have tests here, but so far there's none."); } /** * Bit-reverse operation. * * (reverses the lowest bits of an integer, and clears the rest.) * * example: bitrev(5,''10010'') is ''01001''. * example: bitrev(5,''100110010'') is ''01001''. * * @param nbits - interpret val to have this many bits * @param val - value to be bit-reversed * @return - bit-reversed value. */ private static int bitreverse(int nbits, int val) { int res = 0; for (int bit=0; bit>= 1; } return res; } /** * Fast Fourier transform for complex-valued vectors of size N=2^n. * * "By the book". Only for myself to finally learn how this is done. * Use a mature library in real applications! * * Normalizes the result by 1/sqrt(N). * * All arrays must have the same size, and the size must be 2^n for * integral n. The algorithm does the FFT essentially "in place", so you * may choose to pass arrays such that reSrc==reFt && imSrc==rmFt. * * @param reSrc Real parts of input data * @param imSrc Imaginary parts of input data * @param reFt Storage array for Re(ft(x[i])). Will be rewritten here; * @param imFt Storage array for Im(ft(x[i])). Will be rewritten here; */ public static void fft(double[] reSrc, double[] imSrc, double[] reFt, double[] imFt) { final int n = checkAndGiveMaxPowerOfTwo(reSrc, imSrc, reFt, imFt); final int flen = 1<i) continue; // Do each bit-reverse swap only once. double reTmp = reSrc[j]; double imTmp = imSrc[j]; reFt[j] = reSrc[i]; imFt[j] = imSrc[i]; reFt[i] = reTmp; imFt[i] = imTmp; } // The Cooley-Tukey FFT as found in many textbook examples: for (int m=1; m<=n; m++){ final int twoToM = 1 << m; final int twoToMMinus1 = 1 << (m-1); for(int k=0; k