fftw_plan_dft_2d异常 使用技巧

本文探讨了FFTW库中fftw_plan_dft_2d函数的正确使用方法,强调了正反变换计划需成对出现的重要性,并分享了如何通过更新数据源而非重新创建计划来提升计算效率的经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值