new project structure
This commit is contained in:
0
rfg_adc_plotter/processing/__init__.py
Normal file
0
rfg_adc_plotter/processing/__init__.py
Normal file
115
rfg_adc_plotter/processing/normalizer.py
Normal file
115
rfg_adc_plotter/processing/normalizer.py
Normal file
@ -0,0 +1,115 @@
|
||||
"""Алгоритмы нормировки свипов по калибровочной кривой."""
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def normalize_simple(raw: np.ndarray, calib: np.ndarray) -> np.ndarray:
|
||||
"""Простая нормировка: поэлементное деление raw/calib."""
|
||||
w = min(raw.size, calib.size)
|
||||
if w <= 0:
|
||||
return raw
|
||||
out = np.full_like(raw, np.nan, dtype=np.float32)
|
||||
with np.errstate(divide="ignore", invalid="ignore"):
|
||||
out[:w] = raw[:w] / calib[:w]
|
||||
out = np.nan_to_num(out, nan=np.nan, posinf=np.nan, neginf=np.nan)
|
||||
return out
|
||||
|
||||
|
||||
def build_calib_envelopes(calib: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
||||
"""Оценить нижнюю/верхнюю огибающие калибровочной кривой."""
|
||||
n = int(calib.size)
|
||||
if n <= 0:
|
||||
empty = np.zeros((0,), dtype=np.float32)
|
||||
return empty, empty
|
||||
|
||||
y = np.asarray(calib, dtype=np.float32)
|
||||
finite = np.isfinite(y)
|
||||
if not np.any(finite):
|
||||
zeros = np.zeros_like(y, dtype=np.float32)
|
||||
return zeros, zeros
|
||||
|
||||
if not np.all(finite):
|
||||
x = np.arange(n, dtype=np.float32)
|
||||
y = y.copy()
|
||||
y[~finite] = np.interp(x[~finite], x[finite], y[finite]).astype(np.float32)
|
||||
|
||||
if n < 3:
|
||||
return y.copy(), y.copy()
|
||||
|
||||
dy = np.diff(y)
|
||||
s = np.sign(dy).astype(np.int8, copy=False)
|
||||
|
||||
if np.any(s == 0):
|
||||
for i in range(1, s.size):
|
||||
if s[i] == 0:
|
||||
s[i] = s[i - 1]
|
||||
for i in range(s.size - 2, -1, -1):
|
||||
if s[i] == 0:
|
||||
s[i] = s[i + 1]
|
||||
s[s == 0] = 1
|
||||
|
||||
max_idx = np.where((s[:-1] > 0) & (s[1:] < 0))[0] + 1
|
||||
min_idx = np.where((s[:-1] < 0) & (s[1:] > 0))[0] + 1
|
||||
|
||||
x = np.arange(n, dtype=np.float32)
|
||||
|
||||
def _interp_nodes(nodes: np.ndarray) -> np.ndarray:
|
||||
if nodes.size == 0:
|
||||
idx = np.array([0, n - 1], dtype=np.int64)
|
||||
else:
|
||||
idx = np.unique(np.concatenate(([0], nodes, [n - 1]))).astype(np.int64)
|
||||
return np.interp(x, idx.astype(np.float32), y[idx]).astype(np.float32)
|
||||
|
||||
upper = _interp_nodes(max_idx)
|
||||
lower = _interp_nodes(min_idx)
|
||||
|
||||
swap = lower > upper
|
||||
if np.any(swap):
|
||||
tmp = upper[swap].copy()
|
||||
upper[swap] = lower[swap]
|
||||
lower[swap] = tmp
|
||||
|
||||
return lower, upper
|
||||
|
||||
|
||||
def normalize_projector(raw: np.ndarray, calib: np.ndarray) -> np.ndarray:
|
||||
"""Нормировка через проекцию между огибающими калибровки в диапазон [-1000, +1000]."""
|
||||
w = min(raw.size, calib.size)
|
||||
if w <= 0:
|
||||
return raw
|
||||
|
||||
out = np.full_like(raw, np.nan, dtype=np.float32)
|
||||
raw_seg = np.asarray(raw[:w], dtype=np.float32)
|
||||
lower, upper = build_calib_envelopes(np.asarray(calib[:w], dtype=np.float32))
|
||||
span = upper - lower
|
||||
|
||||
finite_span = span[np.isfinite(span) & (span > 0)]
|
||||
if finite_span.size > 0:
|
||||
eps = max(float(np.median(finite_span)) * 1e-6, 1e-9)
|
||||
else:
|
||||
eps = 1e-9
|
||||
|
||||
valid = (
|
||||
np.isfinite(raw_seg)
|
||||
& np.isfinite(lower)
|
||||
& np.isfinite(upper)
|
||||
& (span > eps)
|
||||
)
|
||||
if np.any(valid):
|
||||
proj = np.empty_like(raw_seg, dtype=np.float32)
|
||||
proj[valid] = ((2.0 * (raw_seg[valid] - lower[valid]) / span[valid]) - 1.0) * 1000.0
|
||||
proj[valid] = np.clip(proj[valid], -1000.0, 1000.0)
|
||||
proj[~valid] = np.nan
|
||||
out[:w] = proj
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def normalize_by_calib(raw: np.ndarray, calib: np.ndarray, norm_type: str) -> np.ndarray:
|
||||
"""Нормировка свипа по выбранному алгоритму."""
|
||||
nt = str(norm_type).strip().lower()
|
||||
if nt == "simple":
|
||||
return normalize_simple(raw, calib)
|
||||
return normalize_projector(raw, calib)
|
||||
Reference in New Issue
Block a user