问题
I'm using the following code to perform the COMPLEX IFFT of an array of complex numbers (I must get complex results):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
int main(void)
{
fftw_complex *in;
fftw_complex *out;
double re,im;
int size;
int i=0;
FILE *file;
fftw_plan ifft;
printf("Insert size");
if (scanf("%d", &size) != 1 || size < 1)
return 1;
in = fftw_malloc(sizeof(*in)*size);
out = fftw_malloc(sizeof(*out)*size);
file = fopen("numbers.txt", "r");
for (i = 0; i < size && fscanf(file, "%lf+%lf*I\n", &re, &im) == 2; i++)
{
in[i]= re+im*I;
}
fclose(file);
// Error if i != size?
ifft = fftw_plan_dft_1d(size, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(ifft);
printf("Input: \tOutput:\n");
for(i=0; i<size; i++)
{
printf("%lf+%lf*I\t%lf+%lf*I\n", creal(in[i]), cimag(in[i]), creal(out[i]), cimag(out[i]));
}
fftw_destroy_plan(ifft);
fftw_free(in);
fftw_free(out);
//free(out);
return 0;
}
The problem is that I always get a real-valued transformed array (imaginary part = 0). For example:
Input: Output:
0.000000+0.000000*I -3.018122+0.000000*I
0.734204+-1.072180*I -2.106427+0.000000*I
-0.055891+1.938470*I 0.808331+0.000000*I
1.117910+0.471070*I -0.171778+0.000000*I
-1.916920+1.203730*I -0.184422+0.000000*I
-0.114476+0.635334*I 3.623205+0.000000*I
-1.151660+0.278201*I 10.225375+0.000000*I
-0.712223+-0.880753*I 1.746433+0.000000*I
1.179990+0.000000*I -7.119782+0.000000*I
-0.712223+0.880753*I -8.238863+0.000000*I
-1.151660+-0.278201*I 2.578253+0.000000*I
-0.114476+-0.635334*I -6.742277+0.000000*I
-1.916920+-1.203730*I -0.293074+0.000000*I
1.117910+-0.471070*I -7.627715+0.000000*I
-0.055891+-1.938470*I 6.443361+0.000000*I
0.734204+1.072180*I 10.077501+0.000000*I
How can I fix it, so to get COMPLEX results?
回答1:
It happens that your input signal has the following property :
Inputs of corresponding frequencies are conjugates from one another. Hence, it is the FFT of a real signal : no need to worry about the imaginary part of your output being null. It is perfectly normal.
Change one line of your input file and things should change.
来源:https://stackoverflow.com/questions/27740255/fftw-producing-real-instead-of-complex-output