Here is an example. It does two things. First, it prepares an input array in[N]
as a cosine wave, whose frequency is 3 and magnitude is 1.0, and Fourier transforms it. So, in the output, you should see a peak at out[3]
and and another at out[N-3]
. Since the magnitude of the cosine wave is 1.0, you get N/2 at out[3]
and out[N-3]
.
Second, it inverse Fourier transforms the array out[N]
back to in2[N]
. And after a proper normalization, you can see that in2[N]
is identical to in[N]
.
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
#define N 16
int main(void) {
fftw_complex in[N], out[N], in2[N]; /* double [2] */
fftw_plan p, q;
int i;
/* prepare a cosine wave */
for (i = 0; i < N; i++) {
in[i][0] = cos(3 * 2*M_PI*i/N);
in[i][1] = 0;
}
/* forward Fourier transform, save the result in 'out' */
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(p);
for (i = 0; i < N; i++)
printf("freq: %3d %+9.5f %+9.5f I\n", i, out[i][0], out[i][1]);
fftw_destroy_plan(p);
/* backward Fourier transform, save the result in 'in2' */
printf("\nInverse transform:\n");
q = fftw_plan_dft_1d(N, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(q);
/* normalize */
for (i = 0; i < N; i++) {
in2[i][0] *= 1./N;
in2[i][1] *= 1./N;
}
for (i = 0; i < N; i++)
printf("recover: %3d %+9.5f %+9.5f I vs. %+9.5f %+9.5f I\n",
i, in[i][0], in[i][1], in2[i][0], in2[i][1]);
fftw_destroy_plan(q);
fftw_cleanup();
return 0;
}
This is the output:
freq: 0 -0.00000 +0.00000 I
freq: 1 +0.00000 +0.00000 I
freq: 2 -0.00000 +0.00000 I
freq: 3 +8.00000 -0.00000 I
freq: 4 +0.00000 +0.00000 I
freq: 5 -0.00000 +0.00000 I
freq: 6 +0.00000 -0.00000 I
freq: 7 -0.00000 +0.00000 I
freq: 8 +0.00000 +0.00000 I
freq: 9 -0.00000 -0.00000 I
freq: 10 +0.00000 +0.00000 I
freq: 11 -0.00000 -0.00000 I
freq: 12 +0.00000 -0.00000 I
freq: 13 +8.00000 +0.00000 I
freq: 14 -0.00000 -0.00000 I
freq: 15 +0.00000 -0.00000 I
Inverse transform:
recover: 0 +1.00000 +0.00000 I vs. +1.00000 +0.00000 I
recover: 1 +0.38268 +0.00000 I vs. +0.38268 +0.00000 I
recover: 2 -0.70711 +0.00000 I vs. -0.70711 +0.00000 I
recover: 3 -0.92388 +0.00000 I vs. -0.92388 +0.00000 I
recover: 4 -0.00000 +0.00000 I vs. -0.00000 +0.00000 I
recover: 5 +0.92388 +0.00000 I vs. +0.92388 +0.00000 I
recover: 6 +0.70711 +0.00000 I vs. +0.70711 +0.00000 I
recover: 7 -0.38268 +0.00000 I vs. -0.38268 +0.00000 I
recover: 8 -1.00000 +0.00000 I vs. -1.00000 +0.00000 I
recover: 9 -0.38268 +0.00000 I vs. -0.38268 +0.00000 I
recover: 10 +0.70711 +0.00000 I vs. +0.70711 +0.00000 I
recover: 11 +0.92388 +0.00000 I vs. +0.92388 +0.00000 I
recover: 12 +0.00000 +0.00000 I vs. +0.00000 +0.00000 I
recover: 13 -0.92388 +0.00000 I vs. -0.92388 +0.00000 I
recover: 14 -0.70711 +0.00000 I vs. -0.70711 +0.00000 I
recover: 15 +0.38268 +0.00000 I vs. +0.38268 +0.00000 I