| 1 |
/* FFT - Fast Fourier Transform */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <math.h> |
| 5 |
|
| 6 |
#define FLOAT double |
| 7 |
|
| 8 |
#define pi2 6.28318530717959 |
| 9 |
|
| 10 |
#define SWAP(a,b) { FLOAT temp = a; a = b; b = temp; } |
| 11 |
|
| 12 |
static void four1 (FLOAT *data, int n) |
| 13 |
{ |
| 14 |
int i, j, m, mmax; |
| 15 |
|
| 16 |
i = 0; |
| 17 |
j = 0; |
| 18 |
while (i < n) |
| 19 |
{ |
| 20 |
if (i < j) |
| 21 |
{ |
| 22 |
SWAP(data[i], data[j]); |
| 23 |
SWAP(data[i+1], data[j+1]); |
| 24 |
} |
| 25 |
m = n/2; |
| 26 |
while (m >= 2 && j >= m) |
| 27 |
{ |
| 28 |
j = j-m; |
| 29 |
m = m/2; |
| 30 |
} |
| 31 |
i += 2; |
| 32 |
j += m; |
| 33 |
} |
| 34 |
|
| 35 |
mmax = 2; |
| 36 |
while (mmax < n) |
| 37 |
{ |
| 38 |
FLOAT theta = pi2/mmax; |
| 39 |
FLOAT x = sin (0.5*theta); |
| 40 |
FLOAT wpr = -2.0*x*x; |
| 41 |
FLOAT wpi= sin (theta); |
| 42 |
FLOAT wr = 1.0; |
| 43 |
FLOAT wi = 0.0; |
| 44 |
FLOAT temp, tempr, tempi; |
| 45 |
|
| 46 |
m = 0; |
| 47 |
while (m < mmax) |
| 48 |
{ |
| 49 |
i = m; |
| 50 |
while (i < n) |
| 51 |
{ |
| 52 |
j = i+mmax; |
| 53 |
tempr = wr*data[j]-wi*data[j+1]; |
| 54 |
tempi = wi*data[j]+wr*data[j+1]; |
| 55 |
data[j] = data[i]-tempr; |
| 56 |
data[j+1] = data[i+1]-tempi; |
| 57 |
data[i] = data[i]+tempr; |
| 58 |
data[i+1] = data[i+1]+tempi; |
| 59 |
i = j+mmax; |
| 60 |
} |
| 61 |
temp = wr; |
| 62 |
wr = temp*wpr-wi*wpi+wr; |
| 63 |
wi = wi*wpr+temp*wpi+wi; |
| 64 |
m += 2; |
| 65 |
} |
| 66 |
mmax *= 2; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
#define N 1024 |
| 71 |
|
| 72 |
FLOAT data[N]; |
| 73 |
|
| 74 |
static FLOAT test () |
| 75 |
{ |
| 76 |
four1 (data, N); |
| 77 |
return data[0]; |
| 78 |
} |
| 79 |
|
| 80 |
int main (int argc, char *argv[]) |
| 81 |
{ |
| 82 |
int i; |
| 83 |
FLOAT result; |
| 84 |
|
| 85 |
for (i=0; i<N; i++) |
| 86 |
data[i] = 0.0; |
| 87 |
|
| 88 |
for (i=0; i<2000; i++) |
| 89 |
result = test (); |
| 90 |
|
| 91 |
if (result != 0.) |
| 92 |
printf ("*** wrong result ***\n"); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |