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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fft.h"
static void
do_fft (complex_t *buffer, int n, complex_t principal, int is_inverse)
{
complex_t *temp, *even, *odd;
complex_t alpha;
complex_t p2;
int i;
double inv;
if (n == 1)
return;
temp = malloc (n * sizeof (complex_t));
even = temp;
odd = temp + n/2;
for (i = 0; i < n/2; ++i)
{
even[i] = buffer[2 * i];
odd[i] = buffer[2 * i + 1];
}
p2 = complex_mul (principal, principal);
do_fft (even, n/2, p2, FALSE);
do_fft (odd, n/2, p2, FALSE);
alpha.re = 1.0;
alpha.im = 0;
if (is_inverse)
inv = 1.0 / n;
for (i = 0; i < n/2; ++i)
{
buffer[i] = complex_add (even[i], complex_mul (alpha, odd[i]));
buffer[i + n/2] = complex_sub (even[i], complex_mul (alpha, odd[i]));
if (is_inverse)
{
buffer[i].re *= inv;
buffer[i].im *= inv;
buffer[i + n/2].re *= inv;
buffer[i + n/2].im *= inv;
}
alpha = complex_mul (alpha, principal);
}
free (temp);
}
void
fft (complex_t *buffer, int n)
{
complex_t p;
p.re = cos (2 * M_PI / n);
p.im = sin (2 * M_PI / n);
do_fft (buffer, n, p, FALSE);
}
void
ifft (complex_t *buffer, int n)
{
complex_t p;
p.re = cos (- (2 * M_PI / n));
p.im = sin (- (2 * M_PI / n));
do_fft (buffer, n, p, TRUE);
}
void
shift (complex_t *buffer, int n)
{
int i;
for (i = 0; i < n/2; ++i)
{
complex_t tmp = buffer[i];
buffer[i] = buffer[i + n/2];
buffer[i + n/2] = tmp;
}
}
typedef void (* fft_1d_t) (complex_t *buffer, int n);
static void
do_fft_2d (complex_t *buffer, int n, fft_1d_t func1d)
{
int i;
/* Transform all the rows */
for (i = 0; i < n; ++i)
{
complex_t *b = buffer + i * n;
func1d (b, n);
}
/* Then the columns */
for (i = 0; i < n; ++i)
{
int j;
complex_t *tmp = malloc (n * sizeof (complex_t));
for (j = 0; j < n; ++j)
tmp[j] = buffer[j * n + i];
func1d (tmp, n);
for (j = 0; j < n; ++j)
buffer[j * n + i] = tmp[j];
free (tmp);
}
}
void
fft_2d (complex_t *buffer, int n)
{
do_fft_2d (buffer, n, fft);
}
void
ifft_2d (complex_t *buffer, int n)
{
do_fft_2d (buffer, n, ifft);
}
void
shift_2d (complex_t *buffer, int n)
{
int i, j;
/* Swap quadrant 0 with quadrant 3 */
for (i = 0; i < n/2; ++i)
{
for (j = 0; j < n/2; ++j)
{
complex_t tmp;
tmp = buffer[i * n + j];
buffer[i * n + j] = buffer[((n/2) + i) * n + (j + n/2)];
buffer[((n/2) + i) * n + (j + n/2)] = tmp;
}
}
/* Swap quadrant 1 with quadrant 2 */
for (i = 0; i < n/2; ++i)
{
for (j = 0; j < n/2; ++j)
{
complex_t tmp;
tmp = buffer[i * n + j + n/2];
buffer[i * n + j + n/2] = buffer[(i + n/2) * n + j];
buffer[(i + n/2) * n + j] = tmp;
}
}
}
void
fft_shift_2d (complex_t *buffer, int n)
{
fft_2d (buffer, n);
shift_2d (buffer, n);
}
void
ifft_shift_2d (complex_t *buffer, int n)
{
shift_2d (buffer, n);
ifft_2d (buffer, n);
}
|