2014-11-14 54 views
1

我正在使用EIGEN 3.2 C++矩阵库。我有一个问题需要我从Eigen :: MatrixXcd类型的矩阵中提取相位或角度信息。这个问题涉及到我的代码中有一个复数的矩阵,它是计算的结果。我有nsamp的尺寸为nsamp的结果M,其中nsamp是256(例如)的整数。因此,MatrixXcd M(nsamp,nsamp);来自特征MatrixXcd的角度数据

现在我想提取从M.相位(或角度信息)我知道,这样做的复杂的分析方法是,

MatrixXcd比= M.imag()。阵列()。罪( )/M.real()数组()COS()。; MatrixXd phase = M.real()。array()。atan();

但是,Eigen库中没有atan方法。因此,作为我正在使用的解决方案

MatrixXcd cosPhase = M.real()。array()。cos()/ M.array()。abs(); MatrixXd phase = M.real()。array()。acos();

数学很稳固,但我得到的答案不正确。我已经看过虚构部分,即

MatrixXd phase = M.imag()。array()。acos();

并获得“更正确”的答案,这是没有意义的。

社区里有没有人处理过这个问题,你的解决方案是什么?

非常感谢,

罗伯特

回答

2

嘛。任何人看到这一点。我想出了自己问题的答案。为了计算相位贡献,我们需要使用2 * atan(imag /(sqrt(real^2 + imag^2)+ real))算法来计算相位。
这是使用犰狳库

#include <iostream> 
 
#include <armadillo> 
 

 
using namespace std; 
 
using namespace arma; 
 

 
int main(int argc, const char * argv[]) { 
 
    // calculate the phase content resulting from a complex field 
 
    // matrix of type Eigen::MatrixXcd 
 
    double pi = acos(-1); 
 
    mat phase(2,2); 
 
    phase << pi/2 << pi/2 << endr 
 
      pi/2 << pi/2 << endr; 
 
    
 
    // form the complex field 
 
    cx_mat complexField = cx_mat(cos(phase), sin(phase)); 
 
    
 
    // calculate the phase using the tan2 identity 
 
    mat REAL = real(complexField); 
 
    mat IMAG = imag(complexField); 
 

 
    // calculate the phase using real component of complexField 
 
    mat phaseResult = 2*atan(IMAG/(sqrt(REAL%REAL+IMAG%IMAG)+REAL)); 
 
    cout << phaseResult << "\n" << endl; 
 
    
 
    return 0; 
 
}

包括一些简单的测试码