#include #include #include /************************************************************************************/ void init_array(int n, double *A_re, double *A_im); void compute_W(int n, double *W_re, double *W_im); void output_array(int n, double *A_re, double *A_im, char *outfile); void permute_bitrev(int n, double *A_re, double *A_im); int bitrev(int inp, int numbits); int log_2(int n); void fft(int n, double *A_re, double *A_im, double *W_re, double *W_im); /************************************************************************************/ /* gets no. of points from the user, initialize the points and roots of unity lookup table * and lets fft go. finally bit-reverses the results and outputs them into a file. * n should be a power of 2. */ int main(int argc, char *argv[]) { int n; int i; double *A_re, *A_im, *W_re, *W_im; n = 64; A_re = (double*)malloc(sizeof(double)*n); A_im = (double*)malloc(sizeof(double)*n); W_re = (double*)malloc(sizeof(double)*n/2); W_im = (double*)malloc(sizeof(double)*n/2); assert(A_re != NULL && A_im != NULL && W_re != NULL && W_im != NULL); for (i=0; i<3; i++) { init_array(n, A_re, A_im); compute_W(n, W_re, W_im); fft(n, A_re, A_im, W_re, W_im); permute_bitrev(n, A_re, A_im); //output_array(n, A_re, A_im, argv[2]); print_string("done"); } free(A_re); free(A_im); free(W_re); free(W_im); exit(0); } /* initializes the array with some function of n */ void init_array(int n, double *A_re, double *A_im) { int NumPoints, i; NumPoints = 0; #ifdef COMMENT_ONLY for(i=0; i < n*2 ; i+=2) { A_re[NumPoints] = (double)input_buf[i]; A_im[NumPoints] = (double)input_buf[i+1]; /* printf("%d,%d -> %g,%g\n", input_buf[i], input_buf[i+1], A_re[NumPoints], A_im[NumPoints]); */ /* printf("%g %g\n", A_re[NumPoints], A_im[NumPoints]); */ NumPoints++; } #endif for (i=0; i>= 1; } return rev; } /* returns log n (to the base 2), if n is positive and power of 2 */ int log_2(int n) { int res; for (res=0; n >= 2; res++) n = n >> 1; return res; } /* fft on a set of n points given by A_re and A_im. Bit-reversal permuted roots-of-unity lookup table * is given by W_re and W_im. More specifically, W is the array of first n/2 nth roots of unity stored * in a permuted bitreversal order. * * FFT - Decimation In Time FFT with input array in correct order and output array in bit-reversed order. * * REQ: n should be a power of 2 to work. * * Note: - See www.cs.berkeley.edu/~randit for her thesis on VIRAM FFTs and other details about VHALF section of the algo * (thesis link - http://www.cs.berkeley.edu/~randit/papers/csd-00-1106.pdf) * - See the foll. CS267 website for details of the Decimation In Time FFT implemented here. * (www.cs.berkeley.edu/~demmel/cs267/lecture24/lecture24.html) * - Also, look "Cormen Leicester Rivest [CLR] - Introduction to Algorithms" book for another variant of Iterative-FFT */ void fft(int n, double *A_re, double *A_im, double *W_re, double *W_im) { double w_re, w_im, u_re, u_im, t_re, t_im; int m, g, b; int i, mt, k; /* for each stage */ for (m=n; m>=2; m=m>>1) { /* m = n/2^s; mt = m/2; */ mt = m >> 1; /* for each group of butterfly */ for (g=0,k=0; g