代码之家  ›  专栏  ›  技术社区  ›  serpentor

FFTW前后向收益率不同,原因何在?

  •  0
  • serpentor  · 技术社区  · 11 年前

    我正在尝试使用来自 http://www.fftw.org/ 基本上,我想先进行前向变换,然后进行后向变换,得到我选择的输入图像。然后我想用反向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 回复  |  直到 11 年前
        1
  •  0
  •   Claudi    11 年前

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

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

    例如,下面的行正在销毁数据,因为 result[i][j] 属于类型 双重的 (截至 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
    
        2
  •  0
  •   Paul R    11 年前

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

     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);