如何在 Eigen 中对 MatrixXd 进行 FFT?
How to do FFT on MatrixXd in Eigen?
看来下面的代码是正确的:
#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的大小,当我把Matrix改成MatrixXd的时候,这样编译就报错了。我想知道如何在 MatrixXd 上进行 FFT,以便在 运行.
时指定矩阵大小
将所有变量更改为 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;
}
看来下面的代码是正确的:
#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的大小,当我把Matrix改成MatrixXd的时候,这样编译就报错了。我想知道如何在 MatrixXd 上进行 FFT,以便在 运行.
时指定矩阵大小将所有变量更改为 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;
}