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.
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) );
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;
}
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;
}