/************************************************ * FFT code from the book Numerical Recipes in C * * Visit www.nr.com for the licence. * ************************************************/ // The following line must be defined before including math.h to correctly define M_PI #define _USE_MATH_DEFINES #include #include #include #define PI M_PI /* pi to machine precision, defined in math.h */ #define TWOPI (2.0*PI) /* FFT/IFFT routine. (see pages 507-508 of Numerical Recipes in C) Inputs: data[] : array of complex* data points of size 2*NFFT+1. data[0] is unused, * the n'th complex number x(n), for 0 <= n <= length(x)-1, is stored as: data[2*n+1] = real(x(n)) data[2*n+2] = imag(x(n)) if length(Nx) < NFFT, the remainder of the array must be padded with zeros nn : FFT order NFFT. This MUST be a power of 2 and >= length(x). isign: if set to 1, computes the forward FFT if set to -1, computes Inverse FFT - in this case the output values have to be manually normalized by multiplying with 1/NFFT. Outputs: data[] : The FFT or IFFT results are stored in data, overwriting the input. */ void four1(double data[], int nn, int isign) { int n, mmax, m, j, istep, i; double wtemp, wr, wpr, wpi, wi, theta; double tempr, tempi; n = nn << 1; j = 1; clik_for (int i = 1; i < n; i += 2) { if (j > i) { tempr = data[j]; data[j] = data[i]; data[i] = tempr; tempr = data[j+1]; data[j+1] = data[i+1]; data[i+1] = tempr; } m = n >> 1; while (m >= 2 && j > m) { j -= m; m >>= 1; } j += m; } mmax = 2; while (n > mmax) { istep = 2*mmax; theta = TWOPI/(isign*mmax); wtemp = sin(0.5*theta); wpr = -2.0*wtemp*wtemp; wpi = sin(theta); wr = 1.0; wi = 0.0; clik_for (m = 1; m < mmax; m += 2) { clik_for (i = m; i <= n; i += istep) { j =i + mmax; tempr = wr*data[j] - wi*data[j+1]; tempi = wr*data[j+1] + wi*data[j]; data[j] = data[i] - tempr; data[j+1] = data[i+1] - tempi; data[i] += tempr; data[i+1] += tempi; } wr = (wtemp = wr)*wpr - wi*wpi + wr; wi = wi*wpr + wtemp*wpi + wi; } mmax = istep; } } /******************************************************** * The following is a test routine that generates a ramp * * with 10 elements, finds their FFT, and then finds the * * original sequence using inverse FFT * ********************************************************/ int main(int argc, char * argv[]) { int i; int Nx; int NFFT; double *x; double *X; /* generate a ramp with 10 numbers */ Nx = 10; printf("Nx = %d\n", Nx); x = (double *) malloc(Nx * sizeof(double)); for(i=0; i= Nx */ NFFT = (int)pow(2.0, ceil(log((double)Nx)/log(2.0))); printf("NFFT = %d\n", NFFT); /* allocate memory for NFFT complex numbers (note the +1) */ X = (double *) malloc((2*NFFT+1) * sizeof(double)); /* Storing x(n) in a complex array to make it work with four1. This is needed even though x(n) is purely real in this case. */ for(i=0; i