来自特征矩阵Xcd的角度数据
angle data from eigen MatrixXcd
我正在使用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(...)
公式。
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 来自特征矩阵Xcd的角度数据