修复Numpy中的相位展开错误

Ben*_*min 5 python numpy phase

我有一系列未包装的阶段,有一些解缠错误,包括+/-倍数Pi的跳跃:

import numpy
a = numpy.array([0.5, 1.3, 2.4, 10.3, 10.8, 10.2, 7.6, 3.2, 2.9])
Run Code Online (Sandbox Code Playgroud)

在这个例子中,在2.4和10.3之间有2个循环的第一次跳跃,在7.6和3.2之间跳跃-1循环.我想删除跳转.问题在于,当您移除跳转时,您需要相应地增加或减少系列的其余部分,而不仅仅是跳转发生的值.

这样做是否有一种更清洁的方式(没有/更少循环,更快):

jumpsexist = 1
while jumpsexist:
    # Look for absolute differences greater than Pi
    jump = numpy.abs((numpy.roll(a,-1) -a)) > numpy.pi
    if jump[:-1].any():
        # Find the index of the first jump
        jumpind = numpy.argmax(jump) + 1
        # Calculate the number of cycles in that jump
        cycles = ((a[jumpind] - a[jumpind- 1]) / numpy.pi).astype("Int8")
        # Remove the cycles
        a[jumpind:] -= cycles * numpy.pi
    else:
        break
Run Code Online (Sandbox Code Playgroud)

Sve*_*ach 8

NumPy提供numpy.unwrap()相位展开功能.使用默认参数值,它将校正以2π为模的相位数组,使得所有跳跃都小于或等于π:

>>> a = numpy.array([0.5, 1.3, 2.4, 10.3, 10.8, 10.2, 7.6, 3.2, 2.9])
>>> numpy.unwrap(a)
array([ 0.5       ,  1.3       ,  2.4       ,  4.01681469,  4.51681469,
        3.91681469,  1.31681469,  3.2       ,  2.9       ])
Run Code Online (Sandbox Code Playgroud)