numpy.unwrap()调用方法:

numpy.unwrap(p, discont=3.141592653589793, axis=-1)

各个参数意义:
p:输入数组。
discont:值之间最大的不连续的大小,默认为pi
axis:拆开沿着的坐标轴,默认为最后一个坐标轴
返回值:对数组中的元素间隔大于discont的进行拆分2pi的操作并返回。

为了解释这个问题,首先我们需要理解angle(角度)radian phase(弧度)的不同之处。

代码如下:

import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        # angle角度值
        angle = np.array([0, 60, 120, 180, 240, 300, 360])
        # radian
        radian = angle / 360 * 2 * np.pi
        print('角度值为: ')
        print(angle)
        print('弧度制为: ')
        print(radian)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
角度值为: 
[  0  60 120 180 240 300 360]
弧度制为: 
[0.         1.04719755 2.0943951  3.14159265 4.1887902  5.23598776
 6.28318531]
"""

我们可以看到,实际上角度的360°对应弧度的2pi,因此我们可以得到其他角度对应的弧度值通过x / 360 * 2 * np.pi

代码如下:

import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        phase = np.linspace(0, np.pi, num=5)
        phase[3:] += np.pi
        print('The value of phase is: ')
        print(phase)
        array = np.unwrap(phase)
        print('The value of array is: ')
        print(array)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
The value of phase is: 
[0.         0.78539816 1.57079633 5.49778714 6.28318531]
The value of array is: 
[ 0.          0.78539816  1.57079633 -0.78539816  0.        ]
"""

我们可以看到,原始我们创建了一个一维数组,有五个元素,值位于0pi之间。然后我们将后两个值加了pi。当我们使用np.unwrap()的时候自动将大于pi的值减去了2pi。因此np.unwrap()相当于拆分了数组中的元素,给相位元素减去了2pi值。

但是会有如下的特殊情况出现,请看代码:

import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        phase = np.linspace(0, 2 * np.pi, num=3)
        # phase[2] = phase[2] + 0.1
        print('The value of phase is: ')
        print(phase)
        array = np.unwrap(phase)
        print('The value of array is: ')
        print(array)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
The value of phase is: 
[0.         3.14159265 6.28318531]
The value of array is: 
[0.         3.14159265 6.28318531]
"""

我们发现并没有出现任何变化,因为这时候数组元素之间的差值是pipi并不大于pi,所以并不会进行拆分操作。

如果我们对discont的值稍作改变。

import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        phase = np.linspace(0, 2 * np.pi, num=3)
        phase[2] = phase[2] + 0.1
        print('The value of phase is: ')
        print(phase)
        array = np.unwrap(phase)
        print('The value of array is: ')
        print(array)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
The value of phase is: 
[0.         3.14159265 6.38318531]
The value of array is: 
[0.         3.14159265 0.1       ]
"""

当我们给最后一个元素增加0.1后,phase数组中的第二个元素和第一个元素之间的差值大于了pi,因此此时对第二个元素进行了2pi拆分。得到了0.1

然而很神奇的是实际中对discont参数进行操作时无论如何设定discont值都没有作用。后续再做补充吧。。。

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐