Skip to content

Commit fdf5a81

Browse files
authored
Merge pull request #159 from Stanford-NavLab/daniel/dop_illconditioned
Safe square root to avoid the square root of negative numbers
2 parents 2fff3c5 + 2a25bbd commit fdf5a81

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

gnss_lib_py/utils/dop.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,35 @@ def parse_dop(dop_matrix):
277277

278278
dop = {}
279279
dop["dop_matrix"] = dop_matrix
280-
dop["GDOP"] = np.sqrt(np.trace(dop_matrix))
281-
dop["HDOP"] = np.sqrt(dop_matrix[0, 0] + dop_matrix[1, 1])
282-
dop["VDOP"] = np.sqrt(dop_matrix[2, 2])
283-
dop["PDOP"] = np.sqrt(dop_matrix[0, 0] + \
284-
dop_matrix[1, 1] + \
285-
dop_matrix[2, 2])
286-
dop["TDOP"] = np.sqrt(dop_matrix[3, 3])
280+
281+
dop["GDOP"] = _safe_sqrt(np.trace(dop_matrix))
282+
dop["HDOP"] = _safe_sqrt(dop_matrix[0, 0] + dop_matrix[1, 1])
283+
dop["VDOP"] = _safe_sqrt(dop_matrix[2, 2])
284+
dop["PDOP"] = _safe_sqrt(dop_matrix[0, 0] + \
285+
dop_matrix[1, 1] + \
286+
dop_matrix[2, 2])
287+
dop["TDOP"] = _safe_sqrt(dop_matrix[3, 3])
287288

288289
return dop
289290

290291

292+
def _safe_sqrt(x):
293+
"""
294+
Safe square root for DOP calculations.
295+
296+
Parameters
297+
----------
298+
x : float
299+
Value to take the square root of.
300+
301+
Returns
302+
-------
303+
y : float
304+
Square root of x, or NaN if x is negative.
305+
"""
306+
return np.sqrt(x) if x >= 0 else np.nan
307+
308+
291309
def calculate_dop(derived):
292310
"""
293311
Calculate the DOP from elevation and azimuth (ENU).

0 commit comments

Comments
 (0)