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


  
  1. #include "fftw3.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cassert>
  5. using namespace std;
  6. int main(int argc,char * argv[]) {
  7. int row=10;
  8. int col;
  9. float * realInput;
  10. fftwf_complex * complexOutput;
  11. fftwf_complex * complexInput;
  12. float * realOutput;
  13. for(int i= 1;i<argc;i++){
  14. row = col;
  15. realInput = (float *) fftwf_malloc(sizeof (float) * row * col );
  16. assert(realInput!=nullptr);
  17. complexOutput = (fftwf_complex *) fftwf_malloc(sizeof (fftwf_complex) * row * (col/2+1));
  18. assert(complexOutput!=nullptr);
  19. fftwf_plan r2c = fftwf_plan_dft_r2c_2d(row, col, realInput, complexOutput, FFTW_PATIENT);
  20. if(r2c == nullptr ){
  21. cout << "fftwf create r2c plan failed!" << endl;
  22. cout << "plan row: " << row << " col: " << col << endl;
  23. exit(1);
  24. } else {
  25. cout << "fftwf success to create fft r2c!" << endl;
  26. cout << "plan row: " << row << " col: " << col << endl;
  27. }
  28. complexInput = (fftwf_complex *) fftwf_malloc(sizeof (fftwf_complex) * row * (col/2+1) );
  29. assert(complexInput!=nullptr);
  30. realOutput = (float *) fftwf_malloc(sizeof (float) * row * col);
  31. assert(realOutput!=nullptr);
  32. fftwf_plan c2r = fftwf_plan_dft_c2r_2d(row, col, complexInput, realOutput, FFTW_PATIENT);
  33. if(c2r == nullptr){
  34. cout << "fftwf create c2r failed!" << endl;
  35. cout << "plan row: " << row << " col: " << col << endl;
  36. exit(1);
  37. } else {
  38. cout << "fftwf success to create c2r!" << endl;
  39. cout << "plan row: " << row << " col: " << col << endl;
  40. }
  41. }
  42. string wisdomFile = "wisdom";
  43. if(1==fftwf_export_wisdom_to_filename(wisdomFile.c_str()))
  44. cout << "fftwf_export_wisdom_to_filename wisdom success" << endl;
  45. else
  46. cout << "fftwf_export_wisdom_to_filename wisdom fail" << endl;
  47. }

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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