Opposite of numpy.unwrap

后端 未结 3 1366
Happy的楠姐
Happy的楠姐 2021-02-01 16:01

In Python numpy, there is an unwrap function that:

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

3条回答
  •  孤街浪徒
    2021-02-01 16:38

    import numpy as np
    phases = np.arctan2(np.sin(phases), np.cos(phases))
    

    This works because sin(phases)/cos(phases) == tan(phases). We get back phases (modulo 2π) by using the inverse-tangent function. Mathematically, the inverse-tangent function is multivalued, so in programming languages it is usually defined to return the phase in a fixed interval.

    The two-parameter arctangent function, i.e. np.arctan2(numerator, denominator), is the same as the regular arctangent function except that it keeps track of the signs of the numerator and denominator, and therefore is able to return the phase modulo 2π, instead of the regular np.arctan(numerator/denominator) function which is only able to return the phase modulo π. Numpy's implementation of the arctan2 function is defined to return the phase in the range [-π, +π], which is the range that the OP requested.

    Additional explanation: This arctan2 method follows directly from the complex representation, and is entirely mathematically equivalent to:

    phases = np.angle(np.exp(1j*phases))
    

    which may be more intuitive. And in fact, numpy's angle function uses arctan2 behind the scenes to separate the imaginary and real components of the exponential, i.e. the sine and cosine.

提交回复
热议问题