|
6 | 6 |
|
7 | 7 | #include <fftw3.h> |
8 | 8 |
|
9 | | -// For simplicity, just use the dft for complex by adding 0 to the input complex part |
10 | | -// We will use the special version for computing dft real later |
11 | | -void dft_1d(int N, double *in, double *out, int forward_or_backward) { |
12 | | - fftw_complex *aux; |
13 | | - fftw_plan p; |
14 | | - int i; |
15 | | - aux = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); |
16 | | - p = fftw_plan_dft_1d(N, aux, aux, forward_or_backward, FFTW_MEASURE); |
17 | | - for (i = 0; i < N; i++) { |
18 | | - aux[i][0] = in[i]; |
19 | | - aux[i][1] = in[i + N]; |
20 | | - } |
21 | | - fftw_execute(p); /* repeat as needed */ |
22 | | - |
23 | | - |
24 | | - if (forward_or_backward == FFTW_FORWARD) { |
25 | | - for (i = 0; i < N; i++) { |
26 | | - out[i] = aux[i][0]; |
27 | | - out[i + N] = aux[i][1]; |
28 | | - } |
29 | | - } else { |
| 9 | +void dft_1d(int N, double complex *in, double complex *out, int forward_or_backward) { |
| 10 | + fftw_plan p = fftw_plan_dft_1d(N, in, out, forward_or_backward, FFTW_ESTIMATE); |
| 11 | + fftw_execute(p); |
| 12 | + if (forward_or_backward == FFTW_BACKWARD) { |
| 13 | + int i; |
30 | 14 | for (i = 0; i < N; i++) { |
31 | | - out[i] = aux[i][0] / N; |
32 | | - out[i + N] = aux[i][1] / N; |
| 15 | + out[i] = out[i] / N; |
33 | 16 | } |
34 | 17 | } |
35 | 18 |
|
36 | 19 | fftw_destroy_plan(p); |
37 | | - fftw_free(aux); |
38 | 20 | } |
39 | 21 |
|
40 | | -// For simplicity, just use the dft for complex by adding 0 to the input complex part |
41 | | -// We will use the special version for computing dft real later |
42 | | -void dft_2d(int ROW, int COLUMN, double *in, double *out, int forward_or_backward) { |
43 | | - fftw_complex *aux; |
44 | | - fftw_plan p; |
45 | | - int i, j; |
| 22 | +void dft_2d(int ROW, int COLUMN, double complex *in, double complex *out, int forward_or_backward) { |
46 | 23 | int size = ROW * COLUMN; |
47 | | - aux = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * size); |
48 | | - p = fftw_plan_dft_2d(ROW, COLUMN, aux, aux, forward_or_backward, FFTW_MEASURE); |
49 | | - for (i = 0; i < ROW; i++) { |
50 | | - for (j = 0; j < COLUMN; j++) { |
51 | | - aux[i * COLUMN + j][0] = in[i * COLUMN + j]; |
52 | | - aux[i * COLUMN + j][1] = in[i * COLUMN + j + size]; |
53 | | - } |
54 | | - } |
55 | | - fftw_execute(p); /* repeat as needed */ |
56 | | - |
57 | | - if (forward_or_backward == FFTW_FORWARD) { |
58 | | - for (i = 0; i < ROW; i++) { |
59 | | - for (j = 0; j < COLUMN; j++) { |
60 | | - out[i * COLUMN + j] = aux[i * COLUMN + j][0]; |
61 | | - out[i * COLUMN + j + size] = aux[i * COLUMN + j][1]; |
62 | | - } |
63 | | - } |
64 | | - } else { |
65 | | - for (i = 0; i < ROW; i++) { |
66 | | - for (j = 0; j < COLUMN; j++) { |
67 | | - out[i * COLUMN + j] = aux[i * COLUMN + j][0] / size; |
68 | | - out[i * COLUMN + j + size] = aux[i * COLUMN + j][1] / size; |
69 | | - } |
| 24 | + fftw_plan p = fftw_plan_dft_2d(ROW, COLUMN, in, out, forward_or_backward, FFTW_ESTIMATE); |
| 25 | + fftw_execute(p); |
| 26 | + if (forward_or_backward == FFTW_BACKWARD) { |
| 27 | + int i; |
| 28 | + for (i = 0; i < size; i++) { |
| 29 | + out[i] = out[i] / size; |
70 | 30 | } |
71 | 31 | } |
72 | 32 |
|
73 | 33 | fftw_destroy_plan(p); |
74 | | - fftw_free(aux); |
75 | 34 | } |
76 | 35 |
|
77 | 36 | /** |
|
0 commit comments