如何在Eigen中对MatrixXd进行FFT?

时间:2016-04-19 13:06:04

标签: c++ eigen eigen3

以下代码似乎是正确的:

#include <Eigen/Core>
#include <unsupported/Eigen/FFT>

int main ()
{
    Eigen::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;
    }
}

但是这必须在编译时指定矩阵的大小,当我将Matrix更改为MatrixXd时,编译时会出错。我想知道如何在MatrixXd上进行FFT,这样我就可以在运行时指定矩阵大小。

1 个答案:

答案 0 :(得分:5)

将所有变量更改为Eigen::Dynamic大小而不是对其进行硬编码,它应该可以正常工作。或者,使用内置类型:

#include <Eigen/Core>
#include <unsupported/Eigen/FFT>

int main ()
{
    size_t dim_x = 28, dim_y = 126;
    Eigen::FFT<float> fft;
    Eigen::MatrixXf in = Eigen::MatrixXf::Random(dim_x, dim_y);
    Eigen::MatrixXcf out;
    out.setZero(dim_x, dim_y);

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

    for (int k = 0; k < in.cols(); k++) {
        Eigen::VectorXcf tmpOut(dim_y);
        fft.fwd(tmpOut, out.col(k));
        out.col(k) = tmpOut;
    }
    return 0;
}