本征FFT库 [英] Eigen FFT library
问题描述
我正在尝试通过FFTW后端使用Eigen不支持的FFT库.具体来说,我想进行2D FFT.这是我的代码:
I am trying to use Eigen unsupported FFT library using FFTW backend. Specifically I am want to do a 2D FFT. Here's my code :
void fft2(Eigen::MatrixXf * matIn,Eigen::MatrixXcf * matOut)
{
const int nRows = matIn->rows();
const int nCols = matIn->cols();
Eigen::FFT< float > fft;
for (int k = 0; k < nRows; ++k) {
Eigen::VectorXcf tmpOut(nRows);
fft.fwd(tmpOut, matIn->row(k));
matOut->row(k) = tmpOut;
}
for (int k = 0; k < nCols; ++k) {
Eigen::VectorXcf tmpOut(nCols);
fft.fwd(tmpOut, matOut->col(k));
matOut->col(k) = tmpOut;
}
}
我有2个问题:
-
首先,在某些矩阵上使用此代码时遇到分段错误.并非所有矩阵都将发生此错误.我想这与对齐错误有关.我通过以下方式使用这些功能:
First, I get a segmentation fault when using this code on some matrix. This error doesn't happen for all matrixes. I guess it's related to an alignment error. I use the functions in the following way :
Eigen :: MatrixXcf matFFT(mat.rows(),mat.cols());fft2(& matFloat,& matFFT);
Eigen::MatrixXcf matFFT(mat.rows(),mat.cols()); fft2(&matFloat,&matFFT);
其中mat可以是任何矩阵.有趣的是,仅当我在第二维上计算FFT时才植入代码,而在第一维上却没有.KissFFT后端不会发生这种情况.
where mat can be any matrix. Funnily, the code plants only when I compute the FFT over the 2nd dimension, never on the first one. This doesn't happen with kissFFT backend.
- 第二,当函数正常工作时,我得到的结果与Matlab(使用FFTW)不同.例如:
输入矩阵:
[2, 1, 2]
[3, 2, 1]
[1, 2, 3]
本征给出:
[ (0,5), (0.5,0.86603), (0,0.5)]
[ (-4.3301,-2.5), (-1,-1.7321), (0.31699,-1.549)]
[ (-1.5,-0.86603), (2,3.4641), (2,3.4641)]
Matlab给出了:
Matlab gives :
17 + 0i 0.5 + 0.86603i 0.5 - 0.86603i
-1 + 0i -1 - 1.7321i 2 - 3.4641i
-1 + 0i 2 + 3.4641i -1 + 1.7321i
只有中央部分是相同的.
Only the central part is the same.
任何帮助都将受到欢迎.
Any help would be welcome.
推荐答案
在我的第一个解决方案中,我未能激活EIGEN_FFTW_DEFAULT,激活它揭示了在fftw支持的Eigen实现中出现错误.以下作品:
I failed to activate EIGEN_FFTW_DEFAULT in my first solution, activating it reveals an error in the fftw-support implementation of Eigen. The following works:
#define EIGEN_FFTW_DEFAULT
#include <iostream>
#include <unsupported/Eigen/FFT>
int main(int argc, char *argv[])
{
Eigen::MatrixXf A(3,3);
A << 2,1,2, 3,2,1, 1,2,3;
const int nRows = A.rows();
const int nCols = A.cols();
std::cout << A << "\n\n";
Eigen::MatrixXcf B(3,3);
Eigen::FFT< float > fft;
for (int k = 0; k < nRows; ++k) {
Eigen::VectorXcf tmpOut(nRows);
fft.fwd(tmpOut, A.row(k));
B.row(k) = tmpOut;
}
std::cout << B << "\n\n";
Eigen::FFT< float > fft2; // Workaround: Using the same FFT object for a real and a complex FFT seems not to work with FFTW
for (int k = 0; k < nCols; ++k) {
Eigen::VectorXcf tmpOut(nCols);
fft2.fwd(tmpOut, B.col(k));
B.col(k) = tmpOut;
}
std::cout << B << '\n';
}
我得到以下输出:
2 1 2
3 2 1
1 2 3
(17,0) (0.5,0.866025) (0.5,-0.866025)
(-1,0) (-1,-1.73205) (2,-3.4641)
(-1,0) (2,3.4641) (-1,1.73205)
与您的Matlab结果相同.
Which is the same as your Matlab result.
N.B.:FFTW似乎本机支持2D实数->复杂FFT(无需使用单独的FFT).这可能会更有效.
N.B.: FFTW seems to support 2D real->complex FFT natively (without using individual FFTs). This is likely more efficient.
fftwf_plan fftwf_plan_dft_r2c_2d(int n0, int n1,
float *in, fftwf_complex *out, unsigned flags);
这篇关于本征FFT库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!