From 5644dbd5ad165b9a71950bd5024f339c9ae1d9f0 Mon Sep 17 00:00:00 2001 From: serversdown Date: Sat, 31 Oct 2026 01:28:23 -0400 Subject: [PATCH] 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"