-
Notifications
You must be signed in to change notification settings - Fork 1
/
fourier.c
260 lines (206 loc) · 5.95 KB
/
fourier.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#import <stdio.h>
#import <stdlib.h>
#include <complex.h>
#include <math.h>
#include "fourier.h"
/**
* Gets the column of an array representing a matrix.
*/
void _getColumn(float complex *y, float complex *col, int j, int n) {
for (int i = 0; i < n; i++) {
col[i] = y[i*n+j];
}
}
/**
* Sets the column of an array representing a matrix.
*/
void _setColumn(float complex *y, float complex *col, int j, int n) {
for (int i = 0; i < n; i++) {
y[i*n+j] = col[i];
}
}
/**
* Gets the row of an array representing a matrix.
*/
void _getRow(float complex *y, float complex *row, int i, int n) {
for (int j = 0; j < n; j++) {
row[j] = y[i*n+j];
}
}
/**
* Sets the row of an array representing a matrix.
*/
void _setRow(float complex *y, float complex *row, int i, int n) {
for (int j = 0; j < n; j++) {
y[i*n+j] = row[j];
}
}
/**
* Calculates the 1D fast Fourier transform of an array.
*/
void _fft1(float complex *y, float complex *yHat, int n) {
// Check if number is > 1 and power of 2
if (n < 2 || (n & (n-1))) {
printf("Error in FFT: Input vector must be of size n.\n");
return;
}
// Return if number is 2
if (n == 2) {
yHat[0] = y[0]+y[1];
yHat[1] = y[0]-y[1];
return;
}
// Halve the value of n
n = n/2;
// Setup even and odd arrays
float complex *yEven = malloc(n * sizeof(float complex));
float complex *yOdd = malloc(n * sizeof(float complex));
for (int i = 0; i < n; i++) {
yEven[i] = y[2*i];
yOdd[i] = y[2*i+1];
}
// Calculate c, d
float complex *c = malloc(n * sizeof(float complex));
_fft1(yEven, c, n);
free(yEven);
float complex *d = malloc(n * sizeof(float complex));
_fft1(yOdd, d, n);
free(yOdd);
// Correct d value
float pi = acos(-1.0);
for (int i = 0; i < n; i++) {
d[i] = cexp(-1.0*I*pi*i/n)*d[i];
}
// Combine the values again
for (int i = 0; i < 2*n; i++) {
if (i < n) {
yHat[i] = c[i]+d[i];
} else {
yHat[i] = c[i-n]-d[i-n];
}
}
free(c);
free(d);
}
/**
* Calculates the 1D inverse fast Fourier transform of an array.
*/
void _ifft1(float complex *yHat, float complex *y, int n) {
// Conjugate the whole array
for (int i = 0; i < n; i++) {
yHat[i] = conjf(yHat[i]);
}
// Calculate the FFT
_fft1(yHat, y, n);
// Conjugate the result
float h = 1.0/n;
for (int i = 0; i < n; i++) {
y[i] = h * conjf(y[i]);
}
}
/**
* Calculates the 1D convolution of two arrays.
*/
void _conv1(float complex *y1, float complex *y2, float complex *yConv, int n) {
// Calculate the FFT of both vectors
float complex *y1Hat = malloc(n * sizeof(float complex));
float complex *y2Hat = malloc(n * sizeof(float complex));
_fft1(y1, y1Hat, n);
_fft1(y2, y2Hat, n);
// Multiply in FFT space
float complex *yConvHat = malloc(n * sizeof(float complex));
for (int i = 0; i < n; i++) {
yConvHat[i] = y1Hat[i] * y2Hat[i];
}
free(y1Hat);
free(y2Hat);
// Transform back
_ifft1(yConvHat, yConv, n);
free(yConvHat);
}
/**
* Calculates the 2D fast Fourier transform of an array representing a matrix.
*/
void _fft2(float complex *y, float complex *yHat, int n) {
// Check if number is > 1 and power of 2
if (n < 2 || (n & (n-1))) {
printf("Error in FFT: Input matrix must be of size n*n.\n");
return;
}
// Go through each row
float complex *yHatTemp = malloc(n * n * sizeof(float complex));
for (int k = 0; k < n; k++) {
float complex *t = malloc(n * sizeof(float complex));
float complex *tHat = malloc(n * sizeof(float complex));
_getRow(y, t, k, n);
_fft1(&t[0], tHat, n);
free(t);
_setRow(yHatTemp, tHat, k, n);
free(tHat);
}
// Go through each col now
for (int k = 0; k < n; k++) {
float complex *t = malloc(n * sizeof(float complex));
float complex *tHat = malloc(n * sizeof(float complex));
_getColumn(yHatTemp, t, k, n);
_fft1(t, tHat, n);
free(t);
_setColumn(yHat, tHat, k, n);
free(tHat);
}
free(yHatTemp);
}
/**
* Calculates the 2D inverse fast Fourier transform of an array representing a matrix.
*/
void _ifft2(float complex *yHat, float complex *y, int n) {
// Conjugate the whole array
for (int i = 0; i < n*n; i++) {
yHat[i] = conjf(yHat[i]);
}
// Calculate the FFT
_fft2(yHat, y, n);
// Conjugate the result
float h = 1.0/(n*n);
for (int i = 0; i < n*n; i++) {
y[i] = h * conjf(y[i]);
}
}
/**
* Calculates the 2D convolution of two arrays representing a matrix.
*/
void _conv2(float complex *y1, float complex *y2, float complex *yConv, int n) {
// Calculate the FFT of both vectors
float complex *y1Hat = malloc(n * n * sizeof(float complex));
float complex *y2Hat = malloc(n * n * sizeof(float complex));
_fft2(y1, y1Hat, n);
_fft2(y2, y2Hat, n);
// Multiply in FFT space
float complex *yConvHat = malloc(n * n * sizeof(float complex));
for (int i = 0; i < n*n; i++) {
yConvHat[i] = y1Hat[i] * y2Hat[i];
}
free(y1Hat);
free(y2Hat);
// Transform back
_ifft2(yConvHat, yConv, n);
free(yConvHat);
}
/**
* Calculates the 2D convolution of two arrays representing a matrix,
* where the second array already is in Fourier space.
*/
void _conv2Hat(float complex *y1, float complex *y2Hat, float complex *yConv, int n) {
// Calculate the FFT of the first vector
float complex *y1Hat = malloc(n * n * sizeof(float complex));
_fft2(y1, y1Hat, n);
// Multiply in FFT space
float complex *yConvHat = malloc(n * n * sizeof(float complex));
for (int i = 0; i < n*n; i++) {
yConvHat[i] = y1Hat[i] * y2Hat[i];
}
free(y1Hat);
// Transform back
_ifft2(yConvHat, yConv, n);
free(yConvHat);
}