#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); /************************************************************************************/ /* 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; double *A_re, *A_im, *W_re, *W_im; if (argc <= 2) { fprintf(stderr, "Usage: ./fft n outfile\n"); exit(-1); } n = atoi(argv[1]); 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); 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]); free(A_re); free(A_im); free(W_re); free(W_im); } /* 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; }