Skip to content

Commit 1ea12f7

Browse files
committed
add angle wrapping
1 parent b350167 commit 1ea12f7

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

gnss_lib_py/utils/coordinates.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,26 @@ def add_el_az(navdata, receiver_state, inplace=False):
547547
data_el_az["az_sv_deg"] = sv_el_az[1,:]
548548

549549
return data_el_az
550+
551+
def wrap_0_to_2pi(angles):
552+
"""Wraps an arbitrary radian between [0, 2pi).
553+
554+
Angles must be in radians.
555+
556+
Parameters
557+
----------
558+
angles : np.ndarray
559+
Array of angles in radians to wrap between 0 and 2pi.
560+
561+
Returns
562+
-------
563+
angles : np.ndarray
564+
Angles wrapped between 0 and 2pi in radians.
565+
566+
"""
567+
while np.any(angles < 0.):
568+
angles = np.where(angles<0. , 2.*np.pi+angles, angles)
569+
while np.any(angles >= 2*np.pi):
570+
angles = np.where(angles>= 2*np.pi , angles-2.*np.pi, angles)
571+
572+
return angles

tests/utils/test_coordinates.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from gnss_lib_py.utils.coordinates import ecef_to_el_az, add_el_az
1717
from gnss_lib_py.utils.coordinates import geodetic_to_ecef
1818
from gnss_lib_py.utils.coordinates import ecef_to_geodetic, LocalCoord
19+
from gnss_lib_py.utils.coordinates import wrap_0_to_2pi
1920

2021

2122
@pytest.fixture(name="local_ecef")
@@ -424,3 +425,35 @@ def test_add_el_az(navdata):
424425
navdata["el_sv_deg"])
425426
np.testing.assert_array_almost_equal(data_el_az["az_sv_deg"],
426427
navdata["az_sv_deg"])
428+
429+
430+
def test_wrap_0_to_2pi():
431+
"""Test wrapping.
432+
433+
"""
434+
# nothing should change
435+
angles_in = np.linspace(0,7*np.pi/4.,8)
436+
angles_out = np.linspace(0,7*np.pi/4.,8)
437+
np.testing.assert_array_equal(wrap_0_to_2pi(angles_in),angles_out)
438+
439+
# test greater than loop
440+
angles_in = np.linspace(0,7*np.pi/4.,8) + 16*np.pi
441+
angles_out = np.linspace(0,7*np.pi/4.,8)
442+
np.testing.assert_array_almost_equal(wrap_0_to_2pi(angles_in),angles_out)
443+
444+
# test greater than loop
445+
angles_in = np.linspace(0,7*np.pi/4.,8) - 12*np.pi
446+
angles_out = np.linspace(0,7*np.pi/4.,8)
447+
np.testing.assert_array_almost_equal(wrap_0_to_2pi(angles_in),angles_out)
448+
449+
# test positive offset loop
450+
angles_in = np.linspace(0,7*np.pi/4.,8) + 7*np.pi
451+
angles_out = np.concatenate((np.linspace(np.pi,7*np.pi/4.,4),
452+
np.linspace(0,3*np.pi/4.,4)))
453+
np.testing.assert_array_almost_equal(wrap_0_to_2pi(angles_in),angles_out)
454+
455+
# test positive offset loop
456+
angles_in = np.linspace(0,7*np.pi/4.,8) - 11*np.pi
457+
angles_out = np.concatenate((np.linspace(np.pi,7*np.pi/4.,4),
458+
np.linspace(0,3*np.pi/4.,4)))
459+
np.testing.assert_array_almost_equal(wrap_0_to_2pi(angles_in),angles_out)

0 commit comments

Comments
 (0)