来自特征矩阵Xcd的角度数据

angle data from eigen MatrixXcd

本文关键字:数据 Xcd 特征      更新时间:2023-10-16

我正在使用EIGEN 3.2 c++ Matrix库。 我有一个问题,需要我从 Eigen::MatrixXcd 类型的矩阵中提取相位或角度信息。 问题涉及我有一个复数矩阵,它是我的代码中计算的结果。 我有 nsamp 维度 nsamp 的结果 M,其中 nsamp 是大小为 256 的整数(例如)。

因此,MatrixXcd M(nsamp, nsamp);

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

MatrixXcd ratio = M.imag().array().sin()/M.real().array().cos();MatrixXd phase = M.real().array().atan();

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

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

数学是可靠的,但我得到的答案不正确。 我看过虚部,即

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

并得到"更正确"的答案,这没有意义。

社区中是否有人以前处理过这个问题,您的解决方案是什么?

非常感谢,

罗伯特

嗯。 对于任何看到这个的人。 我想出了我自己问题的答案。 为了计算相位贡献,我们需要使用 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;
}

提出问题时,该函数很可能不存在,但最简单的解决方案是调用arg()函数。

Eigen::MatrixXcd mat = ...;
Eigen::MatrixXd phase = mat.array().arg(); // needs .array() since this works per element

如果您需要手动计算,最好使用atan2(imag, real)而不是复杂的2*atan(...)公式。