From eef13cd8798237de40cb654a3c1c2414099ab2f2 Mon Sep 17 00:00:00 2001 From: Jose Alejandro Bolanos Arroyave Date: Wed, 9 Oct 2024 11:20:31 -0500 Subject: [PATCH] fix(auto phase): avoid NaN phase correction value (#262) * fix(auto phase): avoid NaN phase correction value * chore: fix eslint * chore: read json instead of import --- .../__tests__/data/onlyOneBaselineZone.json | 87 +++++++++++++++++++ .../__tests__/reimAutoPhaseCorrection.test.ts | 27 ++++++ src/reim/reimAutoPhaseCorrection.ts | 5 ++ 3 files changed, 119 insertions(+) create mode 100644 src/reim/__tests__/data/onlyOneBaselineZone.json diff --git a/src/reim/__tests__/data/onlyOneBaselineZone.json b/src/reim/__tests__/data/onlyOneBaselineZone.json new file mode 100644 index 00000000..9a40217c --- /dev/null +++ b/src/reim/__tests__/data/onlyOneBaselineZone.json @@ -0,0 +1,87 @@ +{ + "re": [ + -0.04707022036698495, -0.35057844429928764, -1.4718936607762938, + -4.027040673448458, -8.766460978553244, -15.98828586975431, + -25.704251252040017, -37.30825242176001, -49.379393983215664, + -60.46947647735135, -69.66901415617662, -76.02055866210512, + -80.8103905423442, -83.70964700574312, -84.53939368857705, + -86.46293753035977, -88.75661438987953, -90.22235474334607, + -92.63459403410555, -94.91826420593382, -96.95782318934468, + -99.59314025835775, -101.3414047882871, -104.10888330109368, + -106.73576248622528, -109.95253184334393, -112.27762725715039, + -115.38847283375183, -118.9940389804313, -122.14176082378577, + -126.0019916297863, -130.66302064627584, -135.79902654476376, + -140.53861655075661, -146.9872872761892, -152.28236577822295, + -150.48831767626973, -168.56244690469543, -161.8584196967421, + -153.22693124824355, -168.55636120062795, -176.7781607987559, + -188.88933397079407, -203.7580785763408, -215.0371716782741, + -224.77206372882097, -249.9942212387989, -274.4455152001884, + -308.5453437080523, -358.8497658911934, -454.3287576623095, + -748.4086661375766, 4182.743156595546, 132.648333037672, -41.44953569468161, + -93.15702944321731, -240.75689505274505, -313.38369392594683, + -391.725690200762, -504.12090222813515, -693.3234632711261, + -1073.591583419653, -3426.284361414097, 1984.4115904770952, + 587.2698054865549, 239.04125694439068, 61.74362314271867, + -72.47291388160158, -196.29809944662554, -343.40607677320014, + -592.2750796685345, -2162.810877920601, -510.65005756800895, + -5066.67817000593, -23.582459776473844, -3083.7898511125463, + 1112.10237102911, 1368.1197772192425, 326.7730174576001, + -2859.8483940562746, 1333.571600878023, 836.2443224728014, 643.167083153868, + 536.2284769066052, 463.3491302880647, 412.6001915476077, 372.4688868987695, + 339.98445744260977, 313.8775282933668, 290.0637394990164, + 268.15340655516866, 257.51972527825063, 252.37443581516317, + 240.2350200991827, 226.7934378461001, 215.6843154761616, 205.29642061839976, + 196.1045766458788, 187.8877512906125, 181.02280334620997, 174.3841985016997, + 168.89682869616146, 163.26922686486438, 156.9038092092109, + 151.8122021476867, 147.76763670544176, 142.76703402267583, + 139.42467111888197, 135.57264180680082, 131.54507632900757, + 128.13065761781925, 125.13790039111521, 121.58005562952474, + 119.6829421096738, 116.62172564231415, 111.62379899112378, + 105.66522363029574, 95.82072104393058, 83.26627635531125, 67.73612621765001, + 51.17955190866686, 35.035825752457136, 21.833954458292723, + 11.898546973097192, 5.500514947580921, 1.9787473615718654, + 0.4784359895496153, 0.07199035632820446 + ], + "im": [ + 0.05577830506769951, 0.20572517741235427, 0.8451069740944221, + 2.2870960825729827, 4.923698209988249, 8.847956319466984, + 14.316514713934886, 21.199115636740004, 27.240159261207786, + 33.40347776260558, 37.995575829125315, 41.91017071636159, + 44.484937009840976, 45.53951296131973, 46.088629859971554, + 47.15904816331761, 47.88743982043701, 48.99617131277847, 49.98956065936565, + 50.59158101755245, 51.418549938339844, 52.07238223229794, 54.17506521643794, + 55.68899515389479, 56.91443278357387, 57.08474250121182, 58.54025103033614, + 60.29071701736305, 62.03748667360032, 63.32624430460133, 66.22834685824618, + 68.63368048570865, 71.06431546994976, 73.68420000877663, 79.42314571898991, + 89.96359437152951, 82.26340919434783, 99.84340487324663, 108.28394478027619, + 73.78232418350795, 85.99765814674677, 86.88941118981113, 94.1318993100956, + 105.63838860255353, 107.81353298589374, 118.84630919244108, + 126.04700765903287, 141.6712279943328, 162.72591455278808, + 194.80914619207948, 258.1385822352295, 467.4254081343631, + -2248.519611327398, -164.8527518383367, 5.367694389146088, + 56.22774664882688, 71.97701615266445, 109.90474130960264, 147.4622283247196, + 202.15023660901238, 292.84540409305197, 482.5304866652167, + 1759.8898119567712, -927.905713802795, -327.4237784589567, + -172.50095701641962, -95.73746260539752, -43.058214468085566, + 1.1817844164079503, 52.05128773209332, 134.36399049447434, 695.837306013699, + 102.43510852469005, 1687.0251831151654, 106.04538803510988, + 818.9334405888669, -294.9548119664901, -347.802387897141, + -127.78970748729729, 630.1984753596513, -224.08006426284527, + -196.68627344754168, -159.1569826641409, -136.1021833978419, + -118.52944983996048, -107.77252401525045, -96.97939210160999, + -88.3682337412525, -82.11193651502377, -75.09558192072056, + -66.54787545048885, -52.59003623489521, -54.55548771303111, + -55.63449580856931, -56.1294284889874, -53.56584450985689, + -51.02625640783536, -49.226288737467996, -47.58991714811566, + -44.96580806422662, -43.19126556116002, -41.76954042658609, + -39.22736899024041, -39.145241562526486, -37.67127448423553, + -35.74783108536349, -34.57763304866576, -33.5536546646777, + -32.08829677682186, -31.40492643958875, -30.488145066129846, + -29.36253085287622, -29.11168926760167, -27.514334116911183, + -26.753879798617554, -26.45871993421753, -23.252262271144378, + -21.897954612465817, -18.488520918855624, -15.078902088214754, + -11.669544984061256, -7.671798216042485, -4.6707088166639465, + -2.5201267199147352, -1.139163564198848, -0.3851556459397587, + -0.0887628656250763, 0.01244764990624696 + ] +} diff --git a/src/reim/__tests__/reimAutoPhaseCorrection.test.ts b/src/reim/__tests__/reimAutoPhaseCorrection.test.ts index fd7c1e49..f4268743 100644 --- a/src/reim/__tests__/reimAutoPhaseCorrection.test.ts +++ b/src/reim/__tests__/reimAutoPhaseCorrection.test.ts @@ -10,6 +10,10 @@ const data = JSON.parse( readFileSync(join(__dirname, 'data/perfect.json')).toString(), ); +const onlyOneBaselineZone = JSON.parse( + readFileSync(join(__dirname, 'data/onlyOneBaselineZone.json')).toString(), +); + test('reverse true', () => { const ph1 = 15; const ph0 = 90; @@ -34,6 +38,29 @@ test('reverse true', () => { expect(ph1 + newPh1).toBeLessThan(1); }); +test('onlyOneBaselineZone data should not be NaN', () => { + const { ph0, ph1 } = reimAutoPhaseCorrection(onlyOneBaselineZone, { + reverse: true, + }); + expect(ph0).not.toBeNaN(); + expect(ph1).not.toBeNaN(); +}); + +test('onlyOneBaselineZone data should not be NaN', () => { + const re = new Float64Array(128); + const im = new Float64Array(128); + const { ph0, ph1 } = reimAutoPhaseCorrection( + { re, im }, + { + reverse: true, + }, + ); + expect(ph0).not.toBeNaN(); + expect(ph1).not.toBeNaN(); + expect(ph0).toBe(0); + expect(ph1).toBe(0); +}); + test('reverse true with outlier', () => { const ph1 = 10; const ph0 = 90; diff --git a/src/reim/reimAutoPhaseCorrection.ts b/src/reim/reimAutoPhaseCorrection.ts index a5ab8df6..9f0907bb 100644 --- a/src/reim/reimAutoPhaseCorrection.ts +++ b/src/reim/reimAutoPhaseCorrection.ts @@ -107,6 +107,11 @@ function determiningGlobalValues( ph0Values: number[], weights: number[], ): { ph0: number; ph1: number } { + if (x.length === 0) { + return { ph0: 0, ph1: 0 }; + } else if (x.length === 1) { + return { ph0: ph0Values[0], ph1: 0 }; + } const [ph1, ph0] = weightedLinearRegression(x, ph0Values, weights); let indexMax = -1; let maxDiff = Number.MIN_SAFE_INTEGER;