alexxx
alexxx

Reputation: 135

How to do FFT on MatrixXd in Eigen?

It seems the code below is correct:

#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;
    }
}

But this must specify the size of matrix in compile time, when I change the Matrix to MatrixXd, this has error when compiling. I want to know how could I do FFT on MatrixXd so I could specify the matrix size when it is running.

Upvotes: 2

Views: 1712

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

Change all your variables to Eigen::Dynamic size instead of hard coding them and it should work. Or, use the built-in types as such:

#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;
}

Upvotes: 5

Related Questions