4
4
#include <stdlib.h>
5
5
#include <time.h>
6
6
7
- #define PI 3.1415926535897932384626
8
-
9
7
void dft (double complex * X , const size_t N ) {
10
8
double complex tmp [N ];
11
9
for (size_t i = 0 ; i < N ; ++ i ) {
12
10
tmp [i ] = 0 ;
13
11
for (size_t j = 0 ; j < N ; ++ j ) {
14
- tmp [i ] += X [j ] * cexp (-2.0 * PI * I * j * i / N );
12
+ tmp [i ] += X [j ] * cexp (-2.0 * M_PI * I * j * i / N );
15
13
}
16
14
}
17
15
@@ -35,27 +33,30 @@ void cooley_tukey(double complex *X, const size_t N) {
35
33
cooley_tukey (X + N / 2 , N / 2 );
36
34
37
35
for (size_t i = 0 ; i < N / 2 ; ++ i ) {
38
- X [i + N / 2 ] = X [i ] - cexp (-2.0 * I * PI * i / N ) * X [i + N / 2 ];
36
+ X [i + N / 2 ] = X [i ] - cexp (-2.0 * I * M_PI * i / N ) * X [i + N / 2 ];
39
37
X [i ] -= (X [i + N / 2 ]- X [i ]);
40
38
}
41
39
}
42
40
}
43
41
44
42
void bit_reverse (double complex * X , size_t N ) {
45
- double complex temp ;
46
- unsigned int b ;
43
+ for (int i = 0 ; i < N ; ++ i ) {
44
+ int n = i ;
45
+ int a = i ;
46
+ int count = (int )log2 ((double )N ) - 1 ;
47
+
48
+ n >>= 1 ;
49
+ while (n > 0 ) {
50
+ a = (a << 1 ) | (n & 1 );
51
+ count -- ;
52
+ n >>= 1 ;
53
+ }
54
+ n = (a << count ) & ((1 << (int )log2 ((double )N )) - 1 );
47
55
48
- for (unsigned int i = 0 ; i < N ; ++ i ) {
49
- b = i ;
50
- b = (((b & 0xaaaaaaaa ) >> 1 ) | ((b & 0x55555555 ) << 1 ));
51
- b = (((b & 0xcccccccc ) >> 2 ) | ((b & 0x33333333 ) << 2 ));
52
- b = (((b & 0xf0f0f0f0 ) >> 4 ) | ((b & 0x0f0f0f0f ) << 4 ));
53
- b = (((b & 0xff00ff00 ) >> 8 ) | ((b & 0x00ff00ff ) << 8 ));
54
- b = ((b >> 16 ) | (b << 16 )) >> (32 - (unsigned int ) log2 ((double )N ));
55
- if (b > i ) {
56
- temp = X [b ];
57
- X [b ] = X [i ];
58
- X [i ] = temp ;
56
+ if (n > i ) {
57
+ double complex tmp = X [i ];
58
+ X [i ] = X [n ];
59
+ X [n ] = tmp ;
59
60
}
60
61
}
61
62
}
@@ -65,7 +66,7 @@ void iterative_cooley_tukey(double complex *X, size_t N) {
65
66
66
67
for (int i = 1 ; i <= log2 ((double )N ); ++ i ) {
67
68
int stride = pow (2 , i );
68
- double complex w = cexp (-2.0 * I * PI / stride );
69
+ double complex w = cexp (-2.0 * I * M_PI / stride );
69
70
for (size_t j = 0 ; j < N ; j += stride ) {
70
71
double complex v = 1.0 ;
71
72
for (size_t k = 0 ; k < stride / 2 ; ++ k ) {
@@ -93,8 +94,7 @@ int main() {
93
94
}
94
95
95
96
cooley_tukey (y , 64 );
96
- //iterative_cooley_tukey(z, 64);
97
- dft (z , 64 );
97
+ iterative_cooley_tukey (z , 64 );
98
98
99
99
approx (y , z , 64 );
100
100
0 commit comments