Python numpy unwrap function

10,143

Solution 1

Although the accepted answer gives you the result you want, I don't think it gets to the heart of the problem which, if I'm interpreting your question correctly, is that you actually want wrap your phase, not unwrap it.

The reason np.unwrap works in this case, with small changes to your data, is actually a consequence of the naive way that np.unwrap computes its result; it simply looks for local discontinuities in your data and adjusts accordingly. Getting the result you're looking for in this way is a result of sampling errors. In other words, if you improve your sampling by interpolating to get a = np.array([np.pi, 3*np.pi/2, 2*np.pi, 5*np.pi/2, 3*np.pi]), adjusting the data won't work any more.

A more sophisticated method of phase unwrapping, such as a Fourier transform method, will leave your data unwrapped, even if the sampling is poor.

If you really want to constrain your data to [0, 2*pi), np.unwrap is the inverse of what you want. The simplest way I can think of to wrap your phase is with the modulo operator:

import numpy as np

a = np.array([np.pi, 2 * np.pi, 3 * np.pi])
a_wrapped = a % (2 * np.pi)
print (a_wrapped)

Of course, because of the sampling errors, np.unwrap(a_wrapped) does not return your original a, so it may not be clear that this is the inverse. However, if you improve your sampling, it does indeed return the original a:

import numpy as np

a = np.arange(0, 4 * np.pi, np.pi/10)
print (a)
a_wrapped = a % (2 * np.pi)
print (a_wrapped)
a = np.unwrap(a_wrapped)
print (a)

Solution 2

Taken from np.unwrap docs:

Unwrap radian phase p by changing absolute jumps greater than discont to their 2*pi complement along the given axis.

Where discont = np.pi (by default). When

a = np.array([np.pi, 2*np.pi, 3*np.pi])

The jumps a[1] - a[0] = np.pi and a[2] - a[1] = np.pi are not greater than np.pi and therefore not 'unwraped'. However, if

a = np.array([3.14159265,  6.28318531,  9.42477796])

you have a[1] - a[0] = 3.1415926600000001 greater than np.pi, thus the function unwraps the values.

Solution 3

There seems to be a rounding issue. The two test cases are not the same.

a = np.array([np.pi, 2*np.pi, 3*np.pi])
a1 = np.array([3.14159265,  6.28318531,  9.42477796])
print('a ', ', '.join([str(i) for i in a]))
print('a1', ', '.join([str(i) for i in a1]))

a  3.14159265359, 6.28318530718, 9.42477796077   
a1 3.14159265, 6.28318531, 9.42477796
Share:
10,143
SunnyIsaLearner
Author by

SunnyIsaLearner

Learning programming every day :)

Updated on August 15, 2022

Comments

  • SunnyIsaLearner
    SunnyIsaLearner almost 2 years

    I am hoping to convert a array of radians into range [0, 2*pi) and numpy unwrap function is exactly what I need

    However, when I run the following code to input a = [pi, 2*pi, 3*pi]:

    import numpy as np
    
    a = np.array([np.pi, 2*np.pi, 3*np.pi])
    np.unwrap(a)
    

    I expect the results to be close to [pi, 0, pi]. However, the output is still:

    array([ 3.14159265,  6.28318531,  9.42477796])
    

    It is not unwrapped. However, if I instead run the following without using the numpy.pi

    a = np.array([3.14159265,  6.28318531,  9.42477796])
    np.unwrap(a)
    

    The output is correct:

    array([  3.14159265e+00,   2.82041412e-09,   3.14159265e+00])
    

    what is going on?

  • Zac-K
    Zac-K almost 7 years
    This only works because of sampling errors (i.e., the data is sparse), and is not really a solution to the actual problem (which I believe the OP has misstated). Your answer contains the same semantic mistake that OP made, so I'm guessing you probably don't know the topic and just found what seemed to be a logical solution from the information available to you. See my answer for details.