How to use Eigen FFT with MatrixXf?

北慕城南 提交于 2019-12-07 16:54:01

问题


I am new to the Eigen library. I would like to compute FFT of Eigen Matrices. However, my attempts to do so indicate that the unsupported Eigen FFT module can't be used with MatrixXf. I want to pull off something like:

#include <eigen3/unsupported/Eigen/FFT>
#include<Eigen/Dense>
#include<iostream>
using namespace std;
using namespace Eigen;
int main(){
    MatrixXf A = MatrixXf::Random(3,10);
    FFT<float> fft;
    MatrixXf B;
    fft.fwd(B,A);
}

Is this achievable? Any other suggestions are welcome. It took me a great deal of self persuasion to migrate to Eigen from matlab, and I would prefer not using a different library unless it's inevitable. Thanks.


回答1:


This is a reasonable expectation. Unfortunately in its current form, FFT does not support that exactly.

MatrixXcf B(3,10);  // note the change from real to complex
//fft.fwd(B,A); // It is natural to want to do this, unfortunately it is not yet supported

// it works to iterate over the columns
for (int k=0;k<A.cols();++k)
    B.col(k) = fft.fwd( A.col(k) );



回答2:


Unfortunately it is not correct;

1) you have to iterate on the rows of the input matrix (real)

2) then iterate over the columns of the output matrix (complex)

FFT<float> fft;
Eigen::Matrix<float, dim_x, dim_y> in = setMatrix();
Eigen::Matrix<complex<float>, dim_x, dim_y> out;

for (int k = 0; k < in.rows(); k++) {
    Eigen::Matrix<complex<float>, dim_x, 1> tmpOut;
    fft.fwd(tmpOut, in.row(k));
    out.row(k) = tmpOut;
}

for (int k = 0; k < in.cols(); k++) {
    Eigen::Matrix<complex<float>, 1, dim_y> tmpOut;
    fft.fwd(tmpOut, out.col(k));
    out.col(k) = tmpOut;
}



回答3:


I am posting my answer that is based on Saba's.

std::shared_ptr< Eigen::MatrixXcf > Util::fft2(std::shared_ptr< Eigen::MatrixXf > matIn)
{
    const int nRows = matIn->rows();
    const int nCols = matIn->cols();

    Eigen::FFT< float > fft;
    std::shared_ptr< Eigen::MatrixXcf > matOut = std::make_shared< Eigen::MatrixXcf > (nRows, nCols);

    for (int k = 0; k < nRows; ++k) {
        Eigen::VectorXcf tmpOut(nCols);
        fft.fwd(tmpOut, matIn->row(k));
        matOut->row(k) = tmpOut;
    }

    for (int k = 0; k < matOut->cols(); ++k) {
        Eigen::VectorXcf tmpOut(nRows);
        fft.fwd(tmpOut, matOut->col(k));
        matOut->col(k) = tmpOut;
    }

    return matOut;
}


来源:https://stackoverflow.com/questions/17194451/how-to-use-eigen-fft-with-matrixxf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!