Fix Phase Deployment Errors in Numpy

I have a series of unfolded phases with some pivot errors that consist of a +/- fold Pi jump:

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

In this example, there is a first jump in 2 cycles between 2.4 and 10.3 and a -1 cycle jump between 7.6 and 3.2. I want to remove the jumps. The trick is that when you delete a jump, you need to increase or decrease the rest of the series, and not just the value in which the transition occurs.

Is there a cleaner way (no / fewer cycles, faster) to do this:

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
+5
source share
2 answers

NumPy offers a function numpy.unwrap()for unfolding phases. With default parameter values, it will correct the phase array modulo 2π, so that all jumps are less than or equal to π:

>>> 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       ])
+8

:

import numpy as np 
a = np.array([0.5, 1.3, 2.4, 10.3, 10.8, 10.2, 7.6, 3.2, 2.9])
d = np.diff(a)/np.pi
b = np.empty_like(a)
b[0] = a[0]
b[1:] = a[1:]-(np.floor(np.abs(d))*np.sign(d)).cumsum()*np.pi

:

In [40]: print a
[  0.5   1.3   2.4  10.3  10.8  10.2   7.6   3.2   2.9]

In [41]: print b
[ 0.5         1.3         2.4         4.01681469  4.51681469  3.91681469
  1.31681469  0.05840735 -0.24159265]

d "", "" - mulitple of pi, / .

, ?

+2

All Articles