# matlab unwrap函数的java实现

## matlab unwrap文档

`Q = unwrap(P)`展开向量 P 中的弧度相位角。每当连续相位角之间的跳跃大于或等于 π 弧度时，unwrap 就会通过增加 ±2π 的整数倍来平移相位角，直到跳跃小于 π。如果 P 是矩阵，unwrap 将按列运算。如果 P 是多维数组，unwrap 将对大小大于 1 的第一个维度进行运算。

## java实现

``````    /**
* matlab unwrap
*
* @param data_in
*/
public double[] unwrap(double[] data_in) {
int n = data_in.length;

double[] data_out = new double[n];

data_out[0] = data_in[0];

for (int i = 1; i < n; i++) {
double diff = Math.abs(data_in[i] - data_out[i - 1]);
if (diff < Math.PI) {
data_out[i] = data_in[i];
} else {
//连续相位角之间的跳跃大于等于Math.PI,执行增加 ±2π操作
double multiple = diff / (2 * Math.PI);
double remainder = diff % (2 * Math.PI);
int move = (int) Math.floor(multiple);
if (remainder > Math.PI) {
move++;
}

if (data_in[i] > data_out[i - 1]) {
data_out[i] = data_in[i] - move * 2 * Math.PI;

} else {
data_out[i] = data_in[i] + move * 2 * Math.PI;

}

//                System.out.println("i:" + i +";multiple:"+multiple+";remainder"+remainder
//                    + ";move:" + move + ";diff:" + diff
//                    + ";data_in[i]:" + data_in[i] + "; data_out[i - 1]:" + data_out[i - 1]
//                    + ";data_out[i]:" + data_out[i]
//                );
}
}

return data_out;
}
``````