calib fix

This commit is contained in:
awe
2026-03-12 16:48:26 +03:00
parent c2a892f397
commit 157447a237
6 changed files with 327 additions and 59 deletions

View File

@ -1,11 +1,24 @@
from __future__ import annotations
import os
import tempfile
import numpy as np
import unittest
from rfg_adc_plotter.processing.calibration import calibrate_freqs, recalculate_calibration_c
from rfg_adc_plotter.processing.calibration import (
build_calib_envelope,
calibrate_freqs,
load_calib_envelope,
recalculate_calibration_c,
save_calib_envelope,
)
from rfg_adc_plotter.processing.fft import compute_distance_axis, compute_fft_mag_row, compute_fft_row
from rfg_adc_plotter.processing.normalization import build_calib_envelopes, normalize_by_calib
from rfg_adc_plotter.processing.normalization import (
build_calib_envelopes,
normalize_by_calib,
normalize_by_envelope,
resample_envelope,
)
from rfg_adc_plotter.processing.peaks import find_peak_width_markers, find_top_peaks_over_ref, rolling_median_ref
@ -39,6 +52,32 @@ class ProcessingTests(unittest.TestCase):
self.assertTrue(np.any(np.isfinite(simple)))
self.assertTrue(np.any(np.isfinite(projector)))
def test_file_calibration_envelope_roundtrip_and_division(self):
raw = (np.sin(np.linspace(0.0, 8.0 * np.pi, 128)) * 50.0 + 100.0).astype(np.float32)
envelope = build_calib_envelope(raw)
normalized = normalize_by_envelope(raw, envelope)
resampled = resample_envelope(envelope, 96)
self.assertEqual(envelope.shape, raw.shape)
self.assertEqual(normalized.shape, raw.shape)
self.assertEqual(resampled.shape, (96,))
self.assertTrue(np.any(np.isfinite(normalized)))
self.assertTrue(np.all(np.isfinite(envelope)))
with tempfile.TemporaryDirectory() as tmp_dir:
path = os.path.join(tmp_dir, "calibration_envelope")
saved_path = save_calib_envelope(path, envelope)
loaded = load_calib_envelope(saved_path)
self.assertTrue(saved_path.endswith(".npy"))
self.assertTrue(np.allclose(loaded, envelope))
def test_load_calib_envelope_rejects_empty_payload(self):
with tempfile.TemporaryDirectory() as tmp_dir:
path = os.path.join(tmp_dir, "empty.npy")
np.save(path, np.zeros((0,), dtype=np.float32))
with self.assertRaises(ValueError):
load_calib_envelope(path)
def test_fft_helpers_return_expected_shapes(self):
sweep = np.sin(np.linspace(0.0, 4.0 * np.pi, 128)).astype(np.float32)
freqs = np.linspace(3.3, 14.3, 128, dtype=np.float64)