calib fix
This commit is contained in:
@ -2,11 +2,13 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any, Mapping
|
||||
|
||||
import numpy as np
|
||||
|
||||
from rfg_adc_plotter.constants import SWEEP_FREQ_MAX_GHZ, SWEEP_FREQ_MIN_GHZ
|
||||
from rfg_adc_plotter.processing.normalization import build_calib_envelopes
|
||||
from rfg_adc_plotter.types import SweepData
|
||||
|
||||
|
||||
@ -79,3 +81,44 @@ def calibrate_freqs(sweep: Mapping[str, Any]) -> SweepData:
|
||||
"F": freqs_cal,
|
||||
"I": values_cal,
|
||||
}
|
||||
|
||||
|
||||
def build_calib_envelope(sweep: np.ndarray) -> np.ndarray:
|
||||
"""Build the active calibration envelope from a raw sweep."""
|
||||
values = np.asarray(sweep, dtype=np.float32).reshape(-1)
|
||||
if values.size == 0:
|
||||
raise ValueError("Calibration sweep is empty")
|
||||
_, upper = build_calib_envelopes(values)
|
||||
return np.asarray(upper, dtype=np.float32)
|
||||
|
||||
|
||||
def validate_calib_envelope(envelope: np.ndarray) -> np.ndarray:
|
||||
"""Validate a saved calibration envelope payload."""
|
||||
values = np.asarray(envelope, dtype=np.float32).reshape(-1)
|
||||
if values.size == 0:
|
||||
raise ValueError("Calibration envelope is empty")
|
||||
if not np.issubdtype(values.dtype, np.number):
|
||||
raise ValueError("Calibration envelope must be numeric")
|
||||
return values
|
||||
|
||||
|
||||
def _normalize_calib_path(path: str | Path) -> Path:
|
||||
out = Path(path).expanduser()
|
||||
if out.suffix.lower() != ".npy":
|
||||
out = out.with_suffix(".npy")
|
||||
return out
|
||||
|
||||
|
||||
def save_calib_envelope(path: str | Path, envelope: np.ndarray) -> str:
|
||||
"""Persist a calibration envelope as a .npy file and return the final path."""
|
||||
normalized_path = _normalize_calib_path(path)
|
||||
values = validate_calib_envelope(envelope)
|
||||
np.save(normalized_path, values.astype(np.float32, copy=False))
|
||||
return str(normalized_path)
|
||||
|
||||
|
||||
def load_calib_envelope(path: str | Path) -> np.ndarray:
|
||||
"""Load and validate a calibration envelope from a .npy file."""
|
||||
normalized_path = _normalize_calib_path(path)
|
||||
loaded = np.load(normalized_path, allow_pickle=False)
|
||||
return validate_calib_envelope(loaded)
|
||||
|
||||
Reference in New Issue
Block a user