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
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