From 5644dbd5ad165b9a71950bd5024f339c9ae1d9f0 Mon Sep 17 00:00:00 2001 From: serversdown Date: Sat, 31 Oct 2026 01:28:23 -0400 Subject: [PATCH 1/3] fix(decode): one MicL count is 0.25 Pa -- the mic psi was ~95x low SFM converted mic counts to psi with a '~0.5 psi full-scale' guess (0.0125 / 32768 psi per count). Measured against 1,729,253 DL2 histogram intervals' 'MicL Peak' columns: one count is exactly 0.25 Pa. count x 0.25 Pa matches Blastware's psi(L) on every interval, and 20 log10(count x 0.25 Pa / 20 uPa) its dB(L) on all but 17 exact half-way cases. - event_file_io: _MIC_FS_PSI -> _MIC_PSI_PER_COUNT = 0.25 / 6894.757 (3.626e-5 psi per count, was 3.81e-7). The event's mic peak in psi, and with it the .h5 mic trace (event_hdf5 back-solves its scale from that peak), come right on events with no report. - waveform_codec.mic_count_to_db: the offset is 20 log10(0.25 / 20e-6) = 81.938, not 81.94 -- the old value missed 49 of those intervals at a rounding edge (count 119 printed 123.4, 81.94 gave 123.451). The decoded count was always right: count 16 on K441LTON.900 is Blastware's 106.0 dB. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9 --- minimateplus/decode_check.py | 6 +++--- minimateplus/event_file_io.py | 14 ++++++++------ minimateplus/waveform_codec.py | 23 ++++++++++++++--------- tests/test_bw_file_fields.py | 26 ++++++++++++++++++++++++++ tests/test_decode_check.py | 5 +++++ 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/minimateplus/decode_check.py b/minimateplus/decode_check.py index bcfc2c0..871b48e 100644 --- a/minimateplus/decode_check.py +++ b/minimateplus/decode_check.py @@ -18,7 +18,7 @@ from dataclasses import dataclass from typing import Callable, Optional, Sequence from .bw_xml_report import XmlReport -from .event_file_io import _GEO_INT16_FS, _GEO_NORMAL_FS_INS, _MIC_FS_PSI, decode_blastware_bytes +from .event_file_io import _GEO_INT16_FS, _GEO_NORMAL_FS_INS, _MIC_PSI_PER_COUNT, decode_blastware_bytes from .models import Event from .waveform_codec import mic_count_to_db @@ -133,7 +133,7 @@ def _mic_converter(ref: XmlReport) -> Optional[Callable[[int], float]]: if "dB(L)" in units: return mic_count_to_db if "psi(L)" in units: - return lambda c: c * _MIC_FS_PSI + return lambda c: c * _MIC_PSI_PER_COUNT return None @@ -200,7 +200,7 @@ def _interval_converter(title: str) -> Optional[Callable[[int], float]]: if ch == "MicL" and rest == "Peak dB(L)": return mic_count_to_db if ch == "MicL" and rest == "Peak psi(L)": - return lambda c: c * _MIC_FS_PSI + return lambda c: c * _MIC_PSI_PER_COUNT return None diff --git a/minimateplus/event_file_io.py b/minimateplus/event_file_io.py index 983078f..6bb4a3f 100644 --- a/minimateplus/event_file_io.py +++ b/minimateplus/event_file_io.py @@ -708,10 +708,12 @@ _GEO_INT16_FS = 32000.0 # the Normal range (the same assumption as the samples above). _HIST_GEO_UNIT_INS = 16 * _GEO_NORMAL_FS_INS / _GEO_INT16_FS -# Microphone scale factor, psi per ADC count. Approximate — exact factor -# depends on the geophone-vs-mic ADC scaling and the firmware reference. -# We mark mic_psi as "computed approximate" in the sidecar. -_MIC_FS_PSI = 0.0125 / _INT16_FS # ~0.5 psi full-scale assumption +# Microphone scale factor, psi per MicL count: one count is 0.25 Pa +# (waveform_codec.MIC_PA_PER_COUNT, measured against Blastware's psi(L) on +# 1,729,253 DL2 intervals). It replaces a "~0.5 psi full-scale" guess, +# 0.0125 / 32768, that read every mic peak ~95x low (2026-10-31). +_PA_PER_PSI = 6894.757 +_MIC_PSI_PER_COUNT = _wc.MIC_PA_PER_COUNT / _PA_PER_PSI def _decode_strt(strt: bytes) -> dict: @@ -763,9 +765,9 @@ def _peaks_from_samples(samples: dict[str, list[int]]) -> PeakValues: vert = _peak_ins(samples.get("Vert", [])) long_ = _peak_ins(samples.get("Long", [])) - # Mic in psi (approximate) + # Mic in psi: one count = 0.25 Pa mic_ch = samples.get("MicL", []) or [] - mic = max((abs(int(v)) for v in mic_ch), default=0) * _MIC_FS_PSI + mic = max((abs(int(v)) for v in mic_ch), default=0) * _MIC_PSI_PER_COUNT # Peak vector sum: max over time of sqrt(T^2 + V^2 + L^2) pvs = 0.0 diff --git a/minimateplus/waveform_codec.py b/minimateplus/waveform_codec.py index e8d04dc..25b3dc0 100644 --- a/minimateplus/waveform_codec.py +++ b/minimateplus/waveform_codec.py @@ -628,23 +628,28 @@ def decoded_to_adc_counts(decoded: dict) -> dict: } +# One MicL count is 0.25 Pa (measured 2026-10-31): count x 0.25 Pa matches +# Blastware's "MicL Peak psi(L)" on all 1,729,253 DL2 histogram intervals +# checked, and 20 log10(count x 0.25 Pa / 20 uPa) its dB(L) on all but 17 +# exact half-way cases. The dB offset below is that, 81.938 -- not the 81.94 +# first fitted to a handful of values, which missed 49 of those intervals. +MIC_PA_PER_COUNT = 0.25 +_MIC_DB_REF = 20.0 * math.log10(MIC_PA_PER_COUNT / 20e-6) + + def mic_count_to_db(count: int) -> float: """Convert a MicL ADC count to dB(L) for BW-display-compatible output. - Empirical formula (confirmed 2026-05-11 against V70 fixture: count=813 - → 140.1 dB; count=±1 → ±81.94 dB; count=±24 → ±109.5 dB): + dB = sign(count) × (20 log10(0.25 Pa / 20 µPa) + 20 × log10(|count|)) + = sign(count) × (81.938 + 20 × log10(|count|)) for |count| ≥ 1 + dB = 0.0 for count == 0 - dB = sign(count) × (81.94 + 20 × log10(|count|)) for |count| ≥ 1 - dB = 0.0 for count == 0 - - The constant 81.94 corresponds to 10^(81.94/20) ≈ 12490 mic ADC counts - being the dB(L) reference level — almost certainly a calibration - constant from the device's mic. + count=±1 → ±81.94 dB; count=813 → 140.14 dB (the V70 fixture). """ if count == 0: return 0.0 sign = 1.0 if count > 0 else -1.0 - return sign * (81.94 + 20.0 * math.log10(abs(count))) + return sign * (_MIC_DB_REF + 20.0 * math.log10(abs(count))) # ── A5-frame entry point ──────────────────────────────────────────────────── diff --git a/tests/test_bw_file_fields.py b/tests/test_bw_file_fields.py index a5bb906..a07da2f 100644 --- a/tests/test_bw_file_fields.py +++ b/tests/test_bw_file_fields.py @@ -179,3 +179,29 @@ def test_interval_pvs_per_interval(): # Blastware's interval table, first four "Geo PVS" values for got, want in zip(pvs[:4], (0.065, 0.034, 0.015, 0.461)): assert abs(got * 0.005 - want) <= 0.0005 + 1e-9, (got, want) + + +# ── The mic: one count = 0.25 Pa ──────────────────────────────────────────── +# Measured 2026-10-31 against 1,729,253 DL2 histogram intervals' "MicL Peak" +# columns: count x 0.25 Pa matches Blastware's psi(L) on every one, and +# 20 log10(count x 0.25 Pa / 20 uPa) its dB(L) on all but 17 exact half-way +# cases. The old psi factor (a "~0.5 psi full-scale" guess) was ~95x small. + +from minimateplus.waveform_codec import mic_count_to_db # noqa: E402 + + +@pytest.mark.parametrize("count,printed", [ + (1, "81.94"), # XML sample cell + (119, "123.4"), # Blastware interval tables, where 81.94 + 20 log10 gave 123.451 + (132, "124.3"), # ... and 124.351 + (813, "140.14"), # V70 fixture, Blastware display +]) +def test_mic_db_matches_blastware(count, printed): + d = len(printed.split(".")[1]) + assert abs(mic_count_to_db(count) - float(printed)) <= 0.5 * 10 ** -d + 1e-9 + + +def test_mic_peak_in_psi(): + # Blastware: MicL PSPL 106.0 dB(L) = 20 uPa x 10^(106/20) = 3.990 Pa = 5.787e-4 psi. + ev = read_blastware_file(_FIX / "decode-checker-10-29-26/M529L4XQ.R30H") + assert ev.peak_values.micl == pytest.approx(5.787e-4, rel=0.01) diff --git a/tests/test_decode_check.py b/tests/test_decode_check.py index efdf68c..6669772 100644 --- a/tests/test_decode_check.py +++ b/tests/test_decode_check.py @@ -142,3 +142,8 @@ def test_pvs_carveout_needs_the_clipped_channel_at_the_ceiling(): side.values["TranPPV"] = 1.256 res = {r.field: r for r in compare(side, ref, CARVEOUTS)} assert res["PeakVectorSum"].verdict == "mismatch" + + +def test_interval_mic_psi_matches(): + # psi(L) report: every interval's "MicL Peak psi(L)" now converts right + assert _results("T004JTRF.ME0H")["intervals.MicL Peak psi(L)"].verdict == "match" From 18951505f0bb1ac61cf0016bc22783459469dd06 Mon Sep 17 00:00:00 2001 From: serversdown Date: Sat, 31 Oct 2026 01:55:45 -0400 Subject: [PATCH 2/3] fix(decode): Blastware's mic dB offset is 81.9384, a hair above exact 0.25 Pa The checker caught 280 regressions in the first mic fix: every one a dB value at count 129, where 20 log10(129 x 0.25 Pa / 20 uPa) = 124.149997 rounds to 124.1 and Blastware prints 124.2. Blastware's dB offset sits in [81.9383, 81.9385] -- any value there matches its dB(L) on 1,729,252 / 1,729,253 DL2 intervals (the last printed '0.000'); exact 81.93820 rounds count 129 down, and the old 81.94 misses counts 119 and 132. Single precision does not explain it. psi keeps 0.25 Pa per count, which matches every interval. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9 --- minimateplus/waveform_codec.py | 20 +++++++++++++------- tests/test_bw_file_fields.py | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/minimateplus/waveform_codec.py b/minimateplus/waveform_codec.py index 25b3dc0..68f1428 100644 --- a/minimateplus/waveform_codec.py +++ b/minimateplus/waveform_codec.py @@ -630,20 +630,26 @@ def decoded_to_adc_counts(decoded: dict) -> dict: # One MicL count is 0.25 Pa (measured 2026-10-31): count x 0.25 Pa matches # Blastware's "MicL Peak psi(L)" on all 1,729,253 DL2 histogram intervals -# checked, and 20 log10(count x 0.25 Pa / 20 uPa) its dB(L) on all but 17 -# exact half-way cases. The dB offset below is that, 81.938 -- not the 81.94 -# first fitted to a handful of values, which missed 49 of those intervals. +# checked. MIC_PA_PER_COUNT = 0.25 -_MIC_DB_REF = 20.0 * math.log10(MIC_PA_PER_COUNT / 20e-6) + +# Blastware's dB(L) offset sits a hair above the exact 20 log10(0.25 Pa / +# 20 uPa) = 81.93820: any value in [81.9383, 81.9385] matches its dB on +# 1,729,252 / 1,729,253 intervals (the one left printed "0.000"), while +# 81.93820 rounds count 129 (162 intervals) to 124.1 against Blastware's +# 124.2, and the 81.94 first fitted to a handful of values misses counts 119 +# and 132. Its display chain presumably carries rounded constants; the +# window, not the physics, is what is measured. +_MIC_DB_REF = 81.9384 def mic_count_to_db(count: int) -> float: """Convert a MicL ADC count to dB(L) for BW-display-compatible output. - dB = sign(count) × (20 log10(0.25 Pa / 20 µPa) + 20 × log10(|count|)) - = sign(count) × (81.938 + 20 × log10(|count|)) for |count| ≥ 1 - dB = 0.0 for count == 0 + dB = sign(count) × (81.9384 + 20 × log10(|count|)) for |count| ≥ 1 + dB = 0.0 for count == 0 + 81.9384 ≈ 20 log10(0.25 Pa / 20 µPa); see _MIC_DB_REF. count=±1 → ±81.94 dB; count=813 → 140.14 dB (the V70 fixture). """ if count == 0: diff --git a/tests/test_bw_file_fields.py b/tests/test_bw_file_fields.py index a07da2f..4027f7f 100644 --- a/tests/test_bw_file_fields.py +++ b/tests/test_bw_file_fields.py @@ -194,6 +194,7 @@ from minimateplus.waveform_codec import mic_count_to_db # noqa: E402 (1, "81.94"), # XML sample cell (119, "123.4"), # Blastware interval tables, where 81.94 + 20 log10 gave 123.451 (132, "124.3"), # ... and 124.351 + (129, "124.2"), # 162 intervals; exactly 0.25 Pa gives 124.149997, a hair under half-way (813, "140.14"), # V70 fixture, Blastware display ]) def test_mic_db_matches_blastware(count, printed): From 287ef2a6530de64c989222e9dfa887ea2183f6f6 Mon Sep 17 00:00:00 2001 From: serversdown Date: Sat, 31 Oct 2026 02:19:49 -0400 Subject: [PATCH 3/3] docs(changelog): Unreleased -- one MicL count is 0.25 Pa Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9 --- CHANGELOG.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 390cf08..7bb5bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,37 @@ All notable changes to seismo-relay are documented here. ## Unreleased ### Fixed +- **The mic in psi was ~95× low: one MicL count is 0.25 Pa.** + - SFM converted mic counts with a "~0.5 psi full-scale" guess (0.0125 / + 32768 psi per count). The decoded count was always right: count 16 on + `K441LTON.900` is Blastware's 106.0 dB. + - Measured against 1,729,253 DL2 histogram intervals' "MicL Peak" + columns: count × 0.25 Pa matches Blastware's psi(L) on every one. + - `event_file_io._MIC_FS_PSI` is now `_MIC_PSI_PER_COUNT` = 0.25 / 6894.757 + (3.626e-5 psi per count). + - The event's mic peak comes right on events with no report, and so does + the `.h5` mic trace (`event_hdf5` derives its scale from that peak). + - **dB offset:** `waveform_codec.mic_count_to_db` now uses 81.9384, not + 81.94. Blastware's dB sits in a measured window, [81.9383, 81.9385], a + hair above the exact 81.93820. Any value in the window matches on + 1,729,252 / 1,729,253 intervals. + - The checker's first run of this fix flagged 280 regressions, every one + count 129, which exact 0.25 Pa rounds to 124.1 against Blastware's + 124.2. + - The old 81.94 misses counts 119 and 132. + - Decode checker, against the pre-fix baseline: 45,035 fixed, 0 + regressions. + + | Field | Mismatches before | After | + |---|---|---| + | interval psi | 42,783 | 26 | + | interval dB | 974 | 20 | + | mic peak | 345 | 40 | + | waveform mic samples | 1,056 | 37 | + + - ⚠ **Codec change, covered by the same release bump.** The backfill + rewrites stored mic values on report-less events; report-paired events + keep the report's. No schema change. - **A histogram's peak vector sum is now the one the unit stores.** SFM computed it from each channel's interval peak, and those can come from different moments. So on 86% of DL2's histograms with no report it read