/
fft_support.c
459 lines (362 loc) · 13.1 KB
/
fft_support.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* fft_support.c */
/* collection of routines to prepare and manipulate complex data */
#include <math.h>
#include <float.h>
#include <fftw3.h>
#include "fft_support.h"
/* definitions to support fftw2 complex data type operations in fftw3 */
#define c_re(c) ((c)[0])
#define c_im(c) ((c)[1])
/* function prototypes */
VIO_Status fft_volume_1d(VIO_Volume data, int inverse_flg, int centre);
VIO_Status fft_volume_2d(VIO_Volume data, int inverse_flg, int centre);
VIO_Status fft_volume_3d(VIO_Volume data, int inverse_flg, int centre);
/* prepare a volume for FFT */
VIO_Status prep_volume(VIO_Volume *in_vol, VIO_Volume *out_vol, char *frequency_dimorder[]){
int i, j, k;
VIO_Real value;
VIO_progress_struct progress;
VIO_Real min, max;
int sizes[4];
VIO_Real starts[4];
VIO_Real separations[4];
VIO_Real tmp_dircos[4];
get_volume_sizes(*in_vol, sizes);
get_volume_starts(*in_vol, starts);
get_volume_separations(*in_vol, separations);
get_volume_real_range(*in_vol, &min, &max);
/* setup frequency dimension */
sizes[3] = 2;
starts[3] = 0;
separations[3] = 1;
/* define new out_vol VIO_Volume */
*out_vol = create_volume(4, frequency_dimorder, NC_FLOAT, TRUE, 0.0, 0.0);
set_volume_sizes(*out_vol, sizes);
set_volume_starts(*out_vol, starts);
set_volume_separations(*out_vol, separations);
set_volume_real_range(*out_vol, min, max);
/* copy over the direction cosines for x, y and z */
for(i = 0; i < 3; i++){
get_volume_direction_cosine(*in_vol, i, tmp_dircos);
set_volume_direction_cosine(*out_vol, i, tmp_dircos);
}
/* allocate space for out_vol */
alloc_volume_data(*out_vol);
initialize_progress_report(&progress, FALSE, sizes[0], "Prep VIO_Volume");
for(i = sizes[0]; i--;){
for(j = sizes[1]; j--;){
for(k = sizes[2]; k--;){
GET_VALUE_3D(value, *in_vol, i, j, k);
set_volume_real_value(*out_vol, i, j, k, 0, 0, value); /* real */
set_volume_real_value(*out_vol, i, j, k, 1, 0, 0.0); /* imag */
}
}
}
/* be tidy */
terminate_progress_report(&progress);
return (VIO_OK);
}
/* do projections from FFT'd data */
VIO_Status proj_volume(VIO_Volume *in_vol, VIO_Volume *out_vol, nc_type dtype, char *spatial_dimorder[], int job){
int i, j, k;
VIO_Real value, real, imag;
VIO_Real min, max;
int sizes[4];
VIO_Real starts[4];
VIO_Real separations[4];
VIO_Real tmp_dircos[4];
get_volume_sizes(*in_vol, sizes);
get_volume_starts(*in_vol, starts);
get_volume_separations(*in_vol, separations);
/* define new out_vol VIO_Volume */
*out_vol = create_volume(3, spatial_dimorder, dtype, TRUE, 0.0, 0.0);
set_volume_sizes(*out_vol, sizes);
set_volume_starts(*out_vol, starts);
set_volume_separations(*out_vol, separations);
/* copy over the direction cosines for x, y and z */
for(i = 0; i < 3; i++){
get_volume_direction_cosine(*in_vol, i, tmp_dircos);
set_volume_direction_cosine(*out_vol, i, tmp_dircos);
}
/* allocate space for out_vol */
alloc_volume_data(*out_vol);
min = DBL_MAX;
max = -DBL_MAX;
/* setup the required VIO_Volume */
for(i = sizes[0]; i--;){
for(j = sizes[1]; j--;){
for(k = sizes[2]; k--;){
real = get_volume_real_value(*in_vol, i, j, k, 0, 0);
imag = get_volume_real_value(*in_vol, i, j, k, 1, 0);
switch (job){
default:
case OUTPUT_MAGNITUDE:
value = sqrt((real * real) + (imag * imag));
break;
case OUTPUT_PHASE:
if(real != 0.0){
value = atan(imag / real);
}
else {
value = 0.0;
}
break;
case OUTPUT_MAGLN:
if(value > 0.1)
value = log(sqrt(real * real + imag * imag));
else
value = -2.3;
break;
case OUTPUT_MAG10:
if(value > 0.1)
value = log10(sqrt(real * real + imag * imag));
else
value = -1;
break;
case OUTPUT_POWER:
value = (real * real) + (imag * imag);
break;
case OUTPUT_REAL:
value = real;
break;
case OUTPUT_IMAG:
value = imag;
break;
}
if(value < min){
min = value;
}
if(value > max){
max = value;
}
set_volume_real_value(*out_vol, i, j, k, 0, 0, value);
}
}
}
set_volume_real_range(*out_vol, min, max);
return (VIO_OK);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : fft_volume.c
@INPUT : data - a pointer to a VIO_Volume_struct of data
inverse_flg = TRUE if inverse fft to be done.
@RETURNS : status variable - OK or ERROR.
*/
VIO_Status fft_volume(VIO_Volume data, int inverse_flg, int dim, int centre){
/* From the fftw FAQ */
/* 3.5 How can I make FFTW put the origin at the center of its output? */
/* */
/* For human viewing of a spectrum, it is often convenient to put the origin */
/* in frequency space at the center of the output array, rather than in the */
/* zero-th element (the default in FFTW). If all of the dimensions of your */
/* array are even, you can accomplish this by simply multiplying each element */
/* of the input array by (-1)^(i + j + ...) */
VIO_Status status;
switch (dim){
case 1:
status = fft_volume_1d(data, inverse_flg, centre);
break;
case 2:
status = fft_volume_2d(data, inverse_flg, centre);
break;
case 3:
status = fft_volume_3d(data, inverse_flg, centre);
break;
default:
fprintf(stderr, "Glark! I canna do %d dimensional FFT's yet!\n", dim);
status = VIO_ERROR;
break;
}
return status;
}
/* do a 1d fft on a 3d VIO_Volume (column by column) */
VIO_Status fft_volume_1d(VIO_Volume data, int inverse_flg, int centre){
int i, j, k;
int sizes[4];
VIO_Real value;
VIO_Real factor;
VIO_Real divisor;
VIO_progress_struct progress;
fftw_complex *fftw_data;
fftw_complex *fftw_data_ptr;
fftw_plan p;
get_volume_sizes(data, sizes);
/* check that sizes are even if shifting to centre */
if(centre && (sizes[2] % 2 != 0)){
fprintf(stderr,
"fft_volume_1d: length of first dimension (%d) must be even if using -centre\n\n",
sizes[2]);
exit(EXIT_FAILURE);
}
initialize_progress_report(&progress, FALSE, sizes[0], "FFT");
/* set up fftw data store */
fftw_data = (fftw_complex *) malloc(sizes[2] * sizeof(fftw_complex));
/* setup an FFT plan */
p = fftw_plan_dft_1d(sizes[2],
fftw_data, fftw_data,
(inverse_flg) ? FFTW_BACKWARD : FFTW_FORWARD,
FFTW_MEASURE);
/* for each slice */
for(i = sizes[0]; i--;){
for(j = sizes[1]; j--;){
/* do the super-funky shift to centre calculation if required */
fftw_data_ptr = fftw_data;
factor = 1.0;
for(k = sizes[2]; k--;){
if(centre){
factor = pow(-1.0, k);
}
GET_VOXEL_4D(value, data, i, j, k, 0);
c_re(*fftw_data_ptr) = (double)(value * factor);
GET_VOXEL_4D(value, data, i, j, k, 1);
c_im(*fftw_data_ptr) = (double)(value * factor);
fftw_data_ptr++;
}
/* do the FFT using the existing plan */
fftw_execute(p);
/* put the data back */
divisor = (inverse_flg) ? sizes[2] : 1.0;
fftw_data_ptr = fftw_data;
for(k = sizes[2]; k--;){
SET_VOXEL_4D(data, i, j, k, 0, (VIO_Real) c_re(*fftw_data_ptr) / divisor);
SET_VOXEL_4D(data, i, j, k, 1, (VIO_Real) c_im(*fftw_data_ptr) / divisor);
fftw_data_ptr++;
}
}
update_progress_report(&progress, sizes[0] - i);
}
/* be tidy */
fftw_destroy_plan(p);
free(fftw_data);
terminate_progress_report(&progress);
return (VIO_OK);
}
/* do a 2d fft on a 3d VIO_Volume (slice by slice) */
VIO_Status fft_volume_2d(VIO_Volume data, int inverse_flg, int centre)
{
int i, j, k;
int sizes[4];
VIO_Real value, factor, divisor;
VIO_progress_struct progress;
fftw_complex *fftw_data;
fftw_complex *fftw_data_ptr;
fftw_plan p;
get_volume_sizes(data, sizes);
/* check that sizes are even if shifting to centre */
if(centre && (sizes[1] % 2 != 0 || sizes[2] % 2 != 0)){
fprintf(stderr,
"fft_volume_2d: lengths of x (%d) and y (%d) must be even if using -centre\n\n",
sizes[2], sizes[1]);
exit(EXIT_FAILURE);
}
initialize_progress_report(&progress, FALSE, sizes[0], "FFT");
/* set up tmp data store */
fftw_data = (fftw_complex *) malloc(sizes[1] * sizes[2] * sizeof(fftw_complex));
/* setup an FFT plan */
p = fftw_plan_dft_2d(sizes[1], sizes[2],
fftw_data, fftw_data,
(inverse_flg) ? FFTW_BACKWARD : FFTW_FORWARD,
FFTW_MEASURE);
/* for each slice */
for(i = sizes[0]; i--;){
/* do the super-funky shift to centre calculation if required */
fftw_data_ptr = fftw_data;
factor = 1.0;
for(j = sizes[1]; j--;){
for(k = sizes[2]; k--;){
if(centre){
factor = pow(-1.0, j + k);
}
GET_VOXEL_4D(value, data, i, j, k, 0);
c_re(*fftw_data_ptr) = (double) (value * factor);
GET_VOXEL_4D(value, data, i, j, k, 1);
c_im(*fftw_data_ptr) = (double) (value * factor);
fftw_data_ptr++;
}
}
/* do the FFT using the existing plan */
fftw_execute(p);
/* put the data back */
divisor = (inverse_flg) ? sizes[1] * sizes[2] : 1.0;
fftw_data_ptr = fftw_data;
for(j = sizes[1]; j--;){
for(k = sizes[2]; k--;){
SET_VOXEL_4D(data, i, j, k, 0, (VIO_Real) c_re(*fftw_data_ptr) / divisor);
SET_VOXEL_4D(data, i, j, k, 1, (VIO_Real) c_im(*fftw_data_ptr) / divisor);
fftw_data_ptr++;
}
}
update_progress_report(&progress, sizes[0] - i);
}
/* be tidy */
fftw_destroy_plan(p);
free(fftw_data);
terminate_progress_report(&progress);
return (VIO_OK);
}
/* do a 3d fft on a 3d VIO_Volume */
VIO_Status fft_volume_3d(VIO_Volume data, int inverse_flg, int centre)
{
int i, j, k;
int sizes[4];
VIO_Real value, factor, divisor;
VIO_progress_struct progress;
fftw_complex *fftw_data;
fftw_complex *fftw_data_ptr;
fftw_plan p;
get_volume_sizes(data, sizes);
/* check that sizes are even if shifting to centre */
if(centre && (sizes[0] % 2 != 0 || sizes[1] % 2 != 0 || sizes[2] % 2 != 0)){
fprintf(stderr,
"fft_volume_2d: all lengths (%d,%d,%d) must be even if using -centre\n\n",
sizes[2], sizes[1], sizes[0]);
exit(EXIT_FAILURE);
}
initialize_progress_report(&progress, FALSE, sizes[0] * 3, "FFT");
/* set up tmp data store */
fftw_data =
(fftw_complex *) fftw_malloc(sizes[0] * sizes[1] * sizes[2] * sizeof(fftw_complex));
/* do the super-funky shift to centre calculation if required */
fftw_data_ptr = fftw_data;
factor = 1.0;
for(i = sizes[0]; i--;){
for(j = sizes[1]; j--;){
for(k = sizes[2]; k--;){
if(centre){
factor = pow(-1.0, i + j + k);
}
GET_VOXEL_4D(value, data, i, j, k, 0);
c_re(*fftw_data_ptr) = (double) (value * factor);
GET_VOXEL_4D(value, data, i, j, k, 1);
c_im(*fftw_data_ptr) = (double) (value * factor);
fftw_data_ptr++;
}
}
update_progress_report(&progress, sizes[0] - i);
}
/* do the FFT */
p = fftw_plan_dft_3d(sizes[0], sizes[1], sizes[2],
fftw_data, fftw_data,
(inverse_flg) ? FFTW_BACKWARD : FFTW_FORWARD,
FFTW_ESTIMATE);
fftw_execute(p);
update_progress_report(&progress, sizes[0] * 2);
/* put the data back */
divisor = (inverse_flg) ? sizes[0] * sizes[1] * sizes[2] : 1.0;
fftw_data_ptr = fftw_data;
for(i = sizes[0]; i--;){
for(j = sizes[1]; j--;){
for(k = sizes[2]; k--;){
SET_VOXEL_4D(data, i, j, k, 0, (VIO_Real) c_re(*fftw_data_ptr) / divisor);
SET_VOXEL_4D(data, i, j, k, 1, (VIO_Real) c_im(*fftw_data_ptr) / divisor);
fftw_data_ptr++;
}
}
update_progress_report(&progress, (sizes[0] * 3) - i);
}
/* be tidy */
fftw_destroy_plan(p);
fftw_free(fftw_data);
terminate_progress_report(&progress);
return (VIO_OK);
}