fftw_plan_dft_2d异常 使用技巧

举报
风吹稻花香 发表于 2021/06/05 00:24:03 2021/06/05
【摘要】 fftw用着用着就异常了,前一个还挺好的,后一个就不行,程序也不报错,就是不能正常计算。 后来发现,fftw_plan_dft_2d有两个参数,一个代表傅里叶变换,一个代表傅里叶反变换。 一般要成对出现,不能连续创建两个傅里叶变换,然后创建一个傅里叶反变换,这样就有问题了。 改正后,都正常了     还发现,fftw_plan_dft_2d创建...

fftw用着用着就异常了,前一个还挺好的,后一个就不行,程序也不报错,就是不能正常计算。

后来发现,fftw_plan_dft_2d有两个参数,一个代表傅里叶变换,一个代表傅里叶反变换。

一般要成对出现,不能连续创建两个傅里叶变换,然后创建一个傅里叶反变换,这样就有问题了。

改正后,都正常了

 

 

还发现,fftw_plan_dft_2d创建一次,后面更换数据源,可以反复计算。

 

fftw_plan_dft_2d创建以后,把输入数据换掉(不是重新实例化,把数据内容更新),重新执行后,结果也会更新,

这样,输入输出的数组大小类型不变,就可以反复使用,不用每次执行时创建与释放,效率能提高好几倍乃至好几十倍。

 

加上参数FFTW_WISDOM_ONLY,分配内存为空。

 fftPlan  = fftwf_plan_dft_r2c_2d(row, col, (float *) xtv[0].data, 
                    (fftwf_complex *) xtfv[0].data, FFTW_WISDOM_ONLY|FFTW_PATIENT);


      #include "fftw3.h"
      #include <iostream>
      #include <string>
      #include <cassert>
      using namespace std;
      int main(int argc,char * argv[]) {
      int row=10;
      int col;
      float * realInput;
      fftwf_complex * complexOutput;
      fftwf_complex * complexInput;
      float * realOutput;
      for(int i= 1;i<argc;i++){
      row = col;
      realInput = (float *) fftwf_malloc(sizeof (float) * row * col );
      assert(realInput!=nullptr);
      complexOutput = (fftwf_complex *) fftwf_malloc(sizeof (fftwf_complex) * row * (col/2+1));
      assert(complexOutput!=nullptr);
      fftwf_plan r2c = fftwf_plan_dft_r2c_2d(row, col, realInput, complexOutput, FFTW_PATIENT);
      if(r2c == nullptr ){
      cout << "fftwf create r2c plan failed!" << endl;
      cout << "plan row: " << row << " col: " << col << endl;
      exit(1);
      } else {
      cout << "fftwf success to create fft r2c!" << endl;
      cout << "plan row: " << row << " col: " << col << endl;
      }
      complexInput = (fftwf_complex *) fftwf_malloc(sizeof (fftwf_complex) * row * (col/2+1) );
      assert(complexInput!=nullptr);
      realOutput = (float *) fftwf_malloc(sizeof (float) * row * col);
      assert(realOutput!=nullptr);
      fftwf_plan c2r = fftwf_plan_dft_c2r_2d(row, col, complexInput, realOutput, FFTW_PATIENT);
      if(c2r == nullptr){
      cout << "fftwf create c2r failed!" << endl;
      cout << "plan row: " << row << " col: " << col << endl;
      exit(1);
      } else {
      cout << "fftwf success to create c2r!" << endl;
      cout << "plan row: " << row << " col: " << col << endl;
      }
      }
      string wisdomFile = "wisdom";
      if(1==fftwf_export_wisdom_to_filename(wisdomFile.c_str()))
      cout << "fftwf_export_wisdom_to_filename wisdom success" << endl;
      else
      cout << "fftwf_export_wisdom_to_filename wisdom fail" << endl;
      }
  
 

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/64975458

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。