FFTW前进和后退产量的结果不同为什么?

时间:2014-08-25 09:13:21

标签: c++ fftw

我正在尝试使用http://www.fftw.org/中的库来FFT图像。基本上我想做一个正向变换,然后是后向变换来获得我选择的输入图像。然后我想用反向FFT返回我的输入,但它不起作用。这是我的代码:

double n[w][h][2];
double im[w][h][2];

const int Lx = w;
 const int Lt = h;
 int var_x;
 int var_t;


 fftw_complex *in, *out, *result;
 fftw_plan p;
 fftw_plan inv_p;

 in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*Lx*Lt);
 out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*Lx*Lt);
 result = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) *Lx *Lt);

 p = fftw_plan_dft_2d(Lx, Lt, in, out, FFTW_FORWARD, FFTW_MEASURE);



 for (int x = 0; x < Lx; x++)
 {
     for (int t = 0; t < Lt; t++)
     {
         in[t + Lt*x][0] = n[x][t][0];
         in[t + Lt*x][1] = 0;
     }
 }

 fftw_execute(p);

 for (int x = 0; x < Lx; x++)
 {
     for (int t = 0; t < Lt; t++)
     {
         n[x][t][0] = out[t + Lt*x][0];
         n[x][t][1] = out[t + Lt*x][1];
     }
 }



 inv_p = fftw_plan_dft_2d(Lx, Lt, out, result, FFTW_BACKWARD, FFTW_MEASURE);

 fftw_execute(inv_p);

 for (int x = 0; x < Lx; x++)
 {
     for (int t = 0; t < Lt; t++)
     {
         im[x][t][0] = result[t + Lt*x][0];
         im[x][t][1] = result[t + Lt*x][1];
         std::cout<<im[x][t][0]<<std::endl;
     }
 }

 fftw_destroy_plan(p);
 fftw_free(in);
 fftw_free(out);

如您所见,我只是尝试执行正常的FFT,然后将其反转。问题是我的输出'im'只是0,而不是1和0 ...

我的代码出了什么问题?

谢谢:)

2 个答案:

答案 0 :(得分:0)

原始图像和转换矩阵声明为int。尝试将它们定义为double

double n[w][h][2];
double im[w][h][2];

例如,下面的行正在销毁数据,因为result[i][j]的类型为double(截至fftw_complex definition)。因此,如果result[i][j] == 0.99,它将被转换为0。

im[x][t][0] = result[t + Lt*x][0]; //A value of 0.99 may be converted to 0

答案 1 :(得分:0)

这是现在正在运行的代码的更正版本 - 感谢所有帮助我解决所有问题的人。

 double n[w][h][2];
 double im[w][h][2];

 const int Lx = w;
 const int Lt = h;
 int var_x;
 int var_t;

 fftw_complex *in, *out, *result;
 fftw_plan p;
 fftw_plan inv_p;

 in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*Lx*Lt);
 out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*Lx*Lt);
 result = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) *Lx *Lt);

 p = fftw_plan_dft_2d(Lx, Lt, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

 for (int x = 0; x < Lx; x++)
 {
     for (int t = 0; t < Lt; t++)
     {
         in[t + Lt*x][0] = n[x][t][0];
         in[t + Lt*x][1] = 0;
     }
 }

 fftw_execute(p);

 for (int x = 0; x < Lx; x++)
 {
     for (int t = 0; t < Lt; t++)
     {
         n[x][t][0] = out[t + Lt*x][0];
         n[x][t][1] = out[t + Lt*x][1];
     }
 }

 inv_p = fftw_plan_dft_2d(Lx, Lt, out, result, FFTW_BACKWARD, FFTW_ESTIMATE);

 fftw_execute(inv_p);

 for (int x = 0; x < Lx; x++)
 {
     for (int t = 0; t < Lt; t++)
     {
         im[x][t][0] = result[t + Lt*x][0];
         im[x][t][1] = result[t + Lt*x][1];
         std::cout<<im[x][t][0]<<std::endl;
     }
 }

 fftw_destroy_plan(p);
 fftw_destroy_plan(inv_p);
 fftw_free(in);
 fftw_free(out);
 fftw_free(result);
相关问题