forked from parallella/parallella-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fftw.c
257 lines (215 loc) · 5.4 KB
/
fftw.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
#include <string.h>
#include <stdlib.h>
#include <complex.h> /* Use C99 complex type */
#include <fftw3.h>
#include <float.h>
#include "demo.h"
#define NSIZE 128
/* Global state */
struct {
bool initialized;
/* Buffers */
fftwf_complex *ref;
fftwf_complex *B;
fftwf_complex *C;
fftwf_complex *ref_fft;
fftwf_complex *B_fft;
fftwf_complex *C_fft;
/* Plans */
fftwf_plan ref_fwd;
fftwf_plan B_fwd;
fftwf_plan C_inv;
} GLOB = {
.initialized = false,
/* Buffers */
.ref = NULL,
.B = NULL,
.C = NULL,
.ref_fft = NULL,
.B_fft = NULL,
.C_fft = NULL,
/* Plans */
.ref_fwd = NULL,
.B_fwd = NULL,
.C_inv = NULL,
};
void cleanup()
{
/* Free buffers */
if (GLOB.ref) {
fftwf_free(GLOB.ref);
GLOB.ref = NULL;
}
if (GLOB.B) {
fftwf_free(GLOB.B);
GLOB.B = NULL;
}
if (GLOB.C) {
fftwf_free(GLOB.C);
GLOB.C = NULL;
}
if (GLOB.ref_fft) {
fftwf_free(GLOB.ref_fft);
GLOB.ref_fft = NULL;
}
if (GLOB.B_fft) {
fftwf_free(GLOB.B_fft);
GLOB.B_fft = NULL;
}
if (GLOB.C_fft) {
fftwf_free(GLOB.C_fft);
GLOB.C_fft = NULL;
}
/* Destroy plans */
if (GLOB.ref_fwd) {
fftwf_free(GLOB.ref_fwd);
GLOB.ref_fwd = NULL;
}
if (GLOB.B_fwd) {
fftwf_free(GLOB.B_fwd);
GLOB.B_fwd = NULL;
}
if (GLOB.C_inv) {
fftwf_free(GLOB.C_inv);
GLOB.C_inv = NULL;
}
fftwf_cleanup();
}
void fftimpl_fini()
{
if (!GLOB.initialized)
return;
cleanup();
GLOB.initialized = false;
}
bool fftimpl_init()
{
const int fftw_flags = FFTW_MEASURE | FFTW_DESTROY_INPUT;
const size_t fftw_bufsize = sizeof(fftw_complex) * NSIZE * NSIZE;
/* Init bufs */
GLOB.ref = (fftwf_complex *) fftwf_malloc(fftw_bufsize);
GLOB.B = (fftwf_complex *) fftwf_malloc(fftw_bufsize);
GLOB.C = (fftwf_complex *) fftwf_malloc(fftw_bufsize);
GLOB.ref_fft = (fftwf_complex *) fftwf_malloc(fftw_bufsize);
GLOB.B_fft = (fftwf_complex *) fftwf_malloc(fftw_bufsize);
GLOB.C_fft = (fftwf_complex *) fftwf_malloc(fftw_bufsize);
if (!GLOB.ref || !GLOB.B || !GLOB.C ||
!GLOB.ref_fft || !GLOB.B_fft || !GLOB.C_fft)
goto fail;
/* Init plans */
GLOB.ref_fwd = fftwf_plan_dft_2d(NSIZE, NSIZE, GLOB.ref, GLOB.ref_fft, FFTW_FORWARD, fftw_flags);
GLOB.B_fwd = fftwf_plan_dft_2d(NSIZE, NSIZE, GLOB.B, GLOB.B_fft, FFTW_FORWARD, fftw_flags);
GLOB.C_inv = fftwf_plan_dft_2d(NSIZE, NSIZE, GLOB.C_fft, GLOB.C, FFTW_BACKWARD, fftw_flags);
if (!GLOB.ref_fwd || !GLOB.B_fwd || !GLOB.C_inv)
goto fail;
atexit(fftimpl_fini);
GLOB.initialized = true;
return true;
fail:
fprintf(stderr, "%s:%s(): ERROR: Failed allocating memory.\n",
__FILE__, __func__);
cleanup();
return false;
}
bool fftimpl_xcorr_one(uint8_t *B, int width, int height, float *out_corr)
{
int i, j;
float B_mean, B_sum, correlation;
/* Preallocated buffers can take up to 128x128 incl. zero padding */
if (width > NSIZE / 2 || height > NSIZE / 2) {
fprintf(stderr,
"ERROR: Input image dimensions must not exceed %dx%d\n",
NSIZE / 2, NSIZE / 2);
return false;
}
/* Convert to float */
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
GLOB.B[i * NSIZE + j] =
((float) B[i * width + j] / 255.0f);
}
}
/* Calculate means */
for (B_sum = 0, i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
B_sum += GLOB.B[i * NSIZE + j];
}
}
B_mean = B_sum / ((float) width * height);
/* Remove DC component */
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++)
GLOB.B[i * NSIZE + j] -= B_mean;
}
/* Calculate FFT for image B */
fftwf_execute(GLOB.B_fwd);
/* C_fft = Element wise ref_fft x B_fft(conjugate) (on host(!)) */
for (i = 0; i < NSIZE * NSIZE; i++)
GLOB.C_fft[i] = GLOB.ref_fft[i] * conjf(GLOB.B_fft[i]);
/* C = ifft(C_fft) */
fftwf_execute(GLOB.C_inv);
/* TODO: Is the max always @0 ??? */
for (i = 0, correlation = FLT_MIN; i < NSIZE * NSIZE; i++) {
if (crealf(GLOB.C[i]) > correlation)
correlation = crealf(GLOB.C[i]);
}
/* Normalize correlation */
correlation /= ((float) NSIZE * NSIZE);
*out_corr = correlation;
return true;
}
bool fftimpl_xcorr(uint8_t *ref_bmp, uint8_t *bmps, int nbmps,
int width, int height, float *out_corr)
{
int i, j, n;
float ref_mean, ref_sum;
float autocorr, tmp;
/* Preallocated buffers can take up to 128x128 incl. zero padding */
if (width > NSIZE / 2 || height > NSIZE / 2) {
fprintf(stderr,
"ERROR: Input image dimensions must not exceed %dx%d\n",
NSIZE / 2, NSIZE / 2);
return false;
}
/* Convert to float */
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
GLOB.ref[i * NSIZE + j] =
((float) ref_bmp[i * width + j] / 255.0f);
}
}
/* Calculate means */
for (ref_sum = 0, i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
ref_sum += GLOB.ref[i * NSIZE + j];
}
}
ref_mean = ref_sum / ((float) width * height);
/* Remove DC component */
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++)
GLOB.ref[i * NSIZE + j] -= ref_mean;
}
/* Calculate FFT for image A */
fftwf_execute(GLOB.ref_fwd);
/* Calculate autocorrelation */
if (!fftimpl_xcorr_one(ref_bmp, width, height, &autocorr)) {
fprintf(stderr,
"ERROR: fftimpl_xcorr_one_failed w ref img\n");
return false;
}
for (n = 0; n < nbmps; n++, out_corr++) {
if (!fftimpl_xcorr_one(&bmps[n * width * height],
width, height, &tmp)) {
fprintf(stderr,
"ERROR: fftimpl_xcorr_one_failed at bitmap %d\n",
n);
return false;
}
if (tmp > autocorr)
*out_corr = autocorr / tmp;
else
*out_corr = tmp / autocorr;
}
return true;
}