Skip to content

Instantly share code, notes, and snippets.

@Veilkrand
Created February 12, 2019 02:11
Show Gist options
  • Save Veilkrand/23c3859c45e216b3ac7319321ef5ce1b to your computer and use it in GitHub Desktop.
Save Veilkrand/23c3859c45e216b3ac7319321ef5ce1b to your computer and use it in GitHub Desktop.
Unwrapping angles to [-pi,pi]
## Using Numpy functions result should be [-pi,pi]
import numpy as np
phases = np.arctan2(np.sin(phases), np.cos(phases))
## equivalen to:
phases = np.angle(np.exp(1j*phases))
## Modulo approach
phases = (( -phases + np.pi) % (2.0 * np.pi ) - np.pi) * -1.0
## Programmatic approach in C++
double constrainAngle(double x){
x = fmod(x + 180,360);
if (x < 0)
x += 360;
return x - 180;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment