@@ -1335,6 +1335,94 @@ def test_physical_conversion(self):
13351335 atol = (0.05 / gain ),
13361336 )
13371337
1338+ def test_adc_gain_max_boundary (self ):
1339+ """
1340+ Exercise the MAX_I32 clamp path: use a tiny range around a huge negative signal
1341+ so the computed baseline would exceed MAX_I32. Verify we hit that branch and
1342+ that a write/read round-trip preserves the physical signal within expected
1343+ quantization and the formula produces the expected result.
1344+ """
1345+ # Tiny range around a large negative value forces baseline > MAX_I32
1346+ base_value = - 1e10
1347+ p_signal = np .array ([[base_value ], [base_value + 0.5 ], [base_value + 1.0 ], [base_value + 1.5 ]])
1348+
1349+ # Write the record
1350+ wfdb .wrsamp (
1351+ "test_negative_signal" ,
1352+ fs = 250 ,
1353+ sig_name = ["ECG" ],
1354+ units = ["mV" ],
1355+ p_signal = p_signal ,
1356+ fmt = ["16" ],
1357+ write_dir = self .temp_path ,
1358+ )
1359+
1360+ # Read it back
1361+ record = wfdb .rdrecord (
1362+ os .path .join (self .temp_path , "test_negative_signal" ),
1363+ physical = True ,
1364+ )
1365+
1366+ # Round-trip physical signal should match within quantization tolerance
1367+ np .testing .assert_allclose (
1368+ record .p_signal ,
1369+ p_signal ,
1370+ rtol = 1e-4 ,
1371+ atol = 1e4 , # Larger atol for large magnitude values
1372+ )
1373+
1374+ # Verify baseline was clamped to MAX_I32
1375+ self .assertEqual (record .baseline [0 ], 2147483647 ) # MAX_I32
1376+
1377+ # Confirm the formula is correct
1378+ expected_gain = (2147483647 - (- 32768 )) / abs (base_value )
1379+ np .testing .assert_allclose (record .adc_gain [0 ], expected_gain , rtol = 1e-3 )
1380+
1381+ def test_adc_gain_min_boundary (self ):
1382+ """
1383+ Exercise the MIN_I32 clamp path: use a tiny range around a huge positive signal
1384+ so the computed baseline would drop below MIN_I32. Verify we hit that branch and
1385+ that a write/read round-trip preserves the physical signal within expected
1386+ quantization and the formula produces the expected result..
1387+ """
1388+ # Tiny range around a large positive value forces baseline < MIN_I32
1389+ base_value = 1e10
1390+ p_signal = np .array (
1391+ [[base_value ], [base_value + 0.5 ], [base_value + 1.0 ], [base_value + 1.5 ]]
1392+ )
1393+
1394+ # Write the record
1395+ wfdb .wrsamp (
1396+ "test_positive_signal" ,
1397+ fs = 250 ,
1398+ sig_name = ["ECG" ],
1399+ units = ["mV" ],
1400+ p_signal = p_signal ,
1401+ fmt = ["16" ],
1402+ write_dir = self .temp_path ,
1403+ )
1404+
1405+ # Read it back
1406+ record = wfdb .rdrecord (
1407+ os .path .join (self .temp_path , "test_positive_signal" ),
1408+ physical = True ,
1409+ )
1410+
1411+ # Round-trip physical signal should match within quantization tolerance
1412+ np .testing .assert_allclose (
1413+ record .p_signal ,
1414+ p_signal ,
1415+ rtol = 1e-4 ,
1416+ atol = 1e4 ,
1417+ )
1418+
1419+ # Verify baseline was clamped to MIN_I32
1420+ self .assertEqual (record .baseline [0 ], - 2147483648 )
1421+
1422+ # Confirm the formula is correct
1423+ expected_gain = (32767 - (- 2147483648 )) / (base_value + 1.5 )
1424+ np .testing .assert_allclose (record .adc_gain [0 ], expected_gain , rtol = 1e-3 )
1425+
13381426 @classmethod
13391427 def setUpClass (cls ):
13401428 cls .temp_directory = tempfile .TemporaryDirectory ()
0 commit comments