[Numpy-discussion] phase unwrapping (1d)

Pierre Haessig pierre.haessig at crans.org
Thu Jan 17 10:48:36 EST 2013


Hi Neal,

Le 14/01/2013 15:39, Neal Becker a écrit :
> This code should explain all:
> --------------------------------
> import numpy as np
> arg = np.angle
>
> def nint (x):
>     return int (x + 0.5) if x >= 0 else int (x - 0.5)
>
> def unwrap (inp, y=np.pi, init=0, cnt=0):
>     o = np.empty_like (inp)
>     prev_o = init
>     for i in range (len (inp)):
>         o[i] = cnt * 2 * y + inp[i]
>         delta = o[i] - prev_o
>
>         if delta / y > 1 or delta / y < -1:
>             n = nint (delta / (2*y))
>             o[i] -= 2*y*n
>             cnt -= n
>
>         prev_o = o[i]
>
>     return o
>             
>
> u = np.linspace (0, 400, 100) * np.pi/100
> v = np.cos (u) + 1j * np.sin (u)
> plot (arg(v))
> plot (arg(v) + arg (v))
> plot (unwrap (arg (v)))
> plot (unwrap (arg (v) + arg (v)))
I think your code does the job.

I tried the following simplification, without the use of nint (which by
the way could be replaced by int(floor(x)) I think) :

def unwrap (inp, y=np.pi, init=0, cnt=0):
    o = np.empty_like (inp)
    prev_o = init
    for i in range (len (inp)):
        o[i] = cnt * 2 * y + inp[i]
        delta = o[i] - prev_o

        if delta / y > 1:
             o[i] -= 2*y
            cnt -= 1
        elif delta / y < -1:
            o[i] += 2*y
            cnt += 1

        prev_o = o[i]

    return o

And now I understand the issue you described of "phase changes of more
than 2pi" because the above indeed fail to unwrap (arg (v) + arg (v)).
On the other hand np.unwrap handles it correctly.

(I still don't know for the speed issue).

Best,
Pierre




-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 900 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130117/c0836328/attachment.sig>


More information about the NumPy-Discussion mailing list