Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Eigen FFT with MatrixXf?

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.

like image 316
John Doe Avatar asked Mar 24 '23 12:03

John Doe


2 Answers

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;
}
like image 78
Sab Avatar answered Mar 26 '23 02:03

Sab


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;
}
like image 36
wsw Avatar answered Mar 26 '23 03:03

wsw