-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfft_2d.c
197 lines (174 loc) · 4.63 KB
/
fft_2d.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Devin Bidstrup 03/29/21-05/07/21
Must compile with:
gcc fft_2d.c -o fft_2d -lm -std=c99
Code based on a number of sources:
* C language with recursion:
https://rosettacode.org/wiki/Fast_Fourier_transform#C
* C++ in place algorithm:
https://cp-algorithms.com/algebra/fft.html
* Helpful visualization of the algorithm:
https://towardsdatascience.com/fast-fourier-transform-937926e591cb
*/
#define _USE_MATH_DEFINES
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <math.h>
#include <complex.h>
#define PI 3.1415926535897932384
typedef double complex cplx;
/* Performs in place FFT on buf of size n*/
void fft(cplx buf[], int n)
{
//Rearrange the array such that it can be iterated upon in the correct order
//This is called decimination-in-time or Cooley-Turkey algorithm to rearrange it first, then do nlogn iterations
int i, j, len;
for (i = 1, j = 0; i < n; i++)
{
int bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
//swap(buf[i], buf[j]);
cplx temp;
if (i < j)
{
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
/*Compute the FFT for the array*/
cplx w, u, v;
// len goes 2, 4, ... n/2, n
// len iterates over the array log2(n) times
for (len = 2; len <= n; len <<= 1)
{
double ang = 2 * PI / len;
/* i goes from 0 to n with stride len
j goes from 0 to len/2 in stride 1
The sum of i+j is used to index into the buffer
and determine the correct indexes at which to perform the DFT.
For example if n = 8:
For the first iteration len = 2, i = 0,2,4,8, j = 0 so that i + j = 0,2,4,8.
For the second iteration len = 4, i = 0,4, j = 0,1 so that i + j = 0,1,4,5.
For the final iteration len = 8, i = 0, j = 0,1,2,3 so that i + j = 0,1,2,3.
This allows us to DFT properly for each index based on the conceptual algorithm.
For each iteration of there are n/2 iterations as shown above,
*/
for (i = 0; i < n; i += len)
{
for (j = 0; j < (len / 2); j++)
{
//Compute the DFT on the correct elements
w = cexp(-I * ang * j);
u = buf[i+j];
v = buf[i+j+(len/2)] * w;
buf[i+j] = u + v;
buf[i+j+(len/2)] = u - v;
}
}
}
}
/* Performs in place FFT on buf of size n*/
void fft_wlen(cplx buf[], int n)
{
//Rearrange the array such that it can be iterated upon in the correct order
//This is called decimination-in-time or Cooley-Turkey algorithm to rearrange it first, then do nlogn iterations
int i, j, len;
for (i = 1, j = 0; i < n; i++)
{
int bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
//swap(buf[i], buf[j]);
cplx temp;
if (i < j)
{
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
/*Compute the FFT for the array*/
cplx wlen, w, u, v;
// len goes 2, 4, ... n/2, n
// len iterates over the array log2(n) times
for (len = 2; len <= n; len <<= 1)
{
double ang = 2 * PI / len;
wlen = cexp(-I * ang);
/* i goes from 0 to n with stride len
j goes from 0 to len/2 in stride 1
The sum of i+j is used to index into the buffer
and determine the correct indexes at which to perform the DFT.
For example if n = 8:
For the first iteration len = 2, i = 0,2,4,8, j = 0 so that i + j = 0,2,4,8.
For the second iteration len = 4, i = 0,4, j = 0,1 so that i + j = 0,1,4,5.
For the final iteration len = 8, i = 0, j = 0,1,2,3 so that i + j = 0,1,2,3.
This allows us to DFT properly for each index based on the conceptual algorithm.
For each iteration of there are n/2 iterations as shown above,
*/
for (i = 0; i < n; i += len)
{
w = 1;
for (j = 0; j < (len / 2); j++)
{
//Compute the DFT on the correct elements
u = buf[i+j];
v = buf[i+j+(len/2)] * w;
buf[i+j] = u + v;
buf[i+j+(len/2)] = u - v;
w *= wlen;
}
}
}
}
/* Transpose the matrix */
void transpose(cplx buf[], int rowLen)
{
int i, j;
cplx temp;
for (i = 0; i < rowLen; i++)
{
for (j = i+1; j < rowLen; j++)
{
temp = buf[i*rowLen + j];
buf[i*rowLen + j] = buf[j*rowLen + i];
buf[j*rowLen + i] = temp;
}
}
}
/* Orchestrates the row-column 2D FFT algorithm */
void fft_2d(cplx buf[], int rowLen, int n)
{
// Do rows
int i;
for(i = 0; i < n; i += rowLen)
{
fft(buf+i, rowLen);
}
// Transpose the matrix
transpose(buf, rowLen);
// Do columns
for(i = 0; i < n; i += rowLen)
{
fft(buf+i, rowLen);
}
// Transpose back
transpose(buf, rowLen);
}
//Print the complex arrays before and after FFT
void show_buffer(cplx buf[], int rowLen, int n) {
int i;
for (i = 0; i < n; i++)
{
if (i%rowLen == 0)
printf("\n");
if (!cimag(buf[i]))
printf("%g ", creal(buf[i]));
else
printf("(%g,%g) ", creal(buf[i]), cimag(buf[i]));
}
printf("\n\n");
}