Opposite of numpy.unwrap
This answer is a slight variation to sega_sai answer which is:
phases = ( phases + np.pi) % (2 * np.pi ) - np.pi
This maps phases to [-pi, pi) -> which means pi is mapped to -pi
Shown here:
In [27]: phases = np.pi
In [28]: phases = ( phases + np.pi) % (2 * np.pi ) - np.pi
In [29]: print phases
-3.14159265359
Which is perfectly legitimate but if you want a mapping of (-pi, pi] then
Times the input and output of the operation buy -1. Like so:
phases = (( -phases + np.pi) % (2.0 * np.pi ) - np.pi) * -1.0
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.
phases = (phases + np.pi) % (2 * np.pi) - np.pi