将Armadillo与FFTW连接 [英] Interfacing Armadillo with FFTW

查看:95
本文介绍了将Armadillo与FFTW连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Armadillo软件包的矩阵中使用FFTW.我需要在2D矩阵上执行N个独立的FFT.遵循FFTW和其他在线资源的手册,我有以下代码:

I am trying to use FFTW in the matrix of Armadillo package. I need to perform N independent FFT on a 2D matrix. Following the manual of FFTW and other resource online, I have the following code:

#include <armadillo>
#include <iostream>
#include "fftw3.h"

using namespace std;
using namespace arma;

fftw_plan fplan;

int main(int argc, char *argv[])
{
  cx_mat psi;
  int NCOL=2, NROW=4;
  int frank=1;
  int howmany=NCOL;
  int n[]={NROW};         
  int idist=NROW, odist=NROW; 
  int istride=1, ostride=1;
  int *inembed=n, *onembed=n;

  psi.resize(NROW, NCOL);
  psi(0,0)=cx_double(1,1); psi(0,1)=cx_double(1,1);
  psi(1,0)=cx_double(2,1); psi(1,1)=cx_double(1,2);
  psi(2,0)=cx_double(3,1); psi(2,1)=cx_double(1,3);
  psi(3,0)=cx_double(4,1); psi(3,1)=cx_double(1,4);
  cout << psi << endl; // the output is correct at here

  fftw_complex* in = reinterpret_cast<fftw_complex*>(psi.memptr());

  fplan = fftw_plan_many_dft(frank, n, howmany,
                             in, inembed, istride, idist,
                             in, onembed, ostride, odist,
                             FFTW_FORWARD, FFTW_MEASURE);

  cout << endl << "after fftw plan: " << endl << psi << endl; // all zeros

  // fftw_execute(fplan); // output zeros again if execute
  cx_double* A_mem=psi.memptr();
  cout << endl << "by reference: " << endl << *A_mem << " " << *(A_mem+1) << endl;

  return 0;
}

代码编译没有任何错误.但是,如果我运行代码,则在fftw_plan_many_dft之后,它将输出全零.如果删除fftw_plan_many_dft,则输出正确.我什至没有执行fftw计划,那么为什么只通过设置计划来清理我的数据呢?

The code compiles without any error. But if I run the code, after fftw_plan_many_dft, it outputs all zeroes instead. If I remove fftw_plan_many_dft, the output is correct. I don't even execute the fftw plan so why it clean my data by just setting up the plan?

推荐答案

通过 FFTW_MEASURE 标志,缓冲区实际上用于调整FFT算法.您应该首先创建计划,然后初始化输入数据.

With the FFTW_MEASURE flag the buffers are actually used to tune the FFT algorithm. You should create the plan first, and then initialise the input data.

请注意, FFTW_ESTIMATE 不会没有表现出这种行为,因此您也可以在当前代码中更改此标志以解决此问题,但是您的代码可能会运行慢一点.

Note that FFTW_ESTIMATE does not exhibit this behaviour, so you could also just change this flag in your current code to fix the problem, but then your code would probably run slower.

这篇关于将Armadillo与FFTW连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆