Skip to content

Commit

Permalink
Safe sqrt to avoid sqrt of negative numbers
Browse files Browse the repository at this point in the history
This can occur when DOP matrices are ill-conditioned.
  • Loading branch information
danineamati committed Feb 14, 2024
1 parent 2fff3c5 commit 2a25bbd
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions gnss_lib_py/utils/dop.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,35 @@ def parse_dop(dop_matrix):

dop = {}
dop["dop_matrix"] = dop_matrix
dop["GDOP"] = np.sqrt(np.trace(dop_matrix))
dop["HDOP"] = np.sqrt(dop_matrix[0, 0] + dop_matrix[1, 1])
dop["VDOP"] = np.sqrt(dop_matrix[2, 2])
dop["PDOP"] = np.sqrt(dop_matrix[0, 0] + \
dop_matrix[1, 1] + \
dop_matrix[2, 2])
dop["TDOP"] = np.sqrt(dop_matrix[3, 3])

dop["GDOP"] = _safe_sqrt(np.trace(dop_matrix))
dop["HDOP"] = _safe_sqrt(dop_matrix[0, 0] + dop_matrix[1, 1])
dop["VDOP"] = _safe_sqrt(dop_matrix[2, 2])
dop["PDOP"] = _safe_sqrt(dop_matrix[0, 0] + \
dop_matrix[1, 1] + \
dop_matrix[2, 2])
dop["TDOP"] = _safe_sqrt(dop_matrix[3, 3])

return dop


def _safe_sqrt(x):
"""
Safe square root for DOP calculations.
Parameters
----------
x : float
Value to take the square root of.
Returns
-------
y : float
Square root of x, or NaN if x is negative.
"""
return np.sqrt(x) if x >= 0 else np.nan


def calculate_dop(derived):
"""
Calculate the DOP from elevation and azimuth (ENU).
Expand Down

0 comments on commit 2a25bbd

Please sign in to comment.