forked from ycshu/110_Fast_Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix2.c
More file actions
64 lines (57 loc) · 1013 Bytes
/
radix2.c
File metadata and controls
64 lines (57 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
time_t timer;
int main()
{
int j, k, p = 20, n, L, M, N = 1<<p;
complex *y, *x, t, w, u;
printf("N = %d\n",N);
x = (complex *) malloc(N*sizeof(complex));
y = (complex *) malloc(N*sizeof(complex));
for(k=0;k<N;++k){
x[k] = k;
}
timer = clock();
// bit-reverse swap
M = 1 << (p-1);
j = 0;
for(k=1;k<N-1;++k) {
L = M;
while(j>=L) {
j -= L;
L /= 2;
}
j += L;
if(j>k) {
t = x[k];
x[k] = x[j];
x[j] = t;
}
}
// Butterfly Radix
j = 1;
while(j<N) {
w = 1.0;
u = cexp(-I*M_PI/j);
for(n=0;n<j;n++) {
for(k=0;k<N;k+=2*j) {
// k+n+j --> multiply w
x[k+n+j] *= w;
// FFT2 for k+n and k+n+j
t = x[k+n];
x[k+n ] = x[k+n]+x[k+n+j];
x[k+n+j] = t-x[k+n+j];
}
w *= u;
}
j = j << 1;
}
timer = clock() - timer;
// for(k=0;k<N;++k){
// printf("%d:%f+%f i\n",k,creal(x[k]),cimag(x[k]));
// }
printf("cost time = %d ms\n", timer);
return 0;
}