2 Commits

View File

@ -33,18 +33,18 @@ import numpy as np
WF_WIDTH = 1000 # максимальное число точек в ряду водопада WF_WIDTH = 1000 # максимальное число точек в ряду водопада
FFT_LEN = 1024 # длина БПФ для спектра/водопада спектров FFT_LEN = 1024 # длина БПФ для спектра/водопада спектров
LOG_BASE = 10.0
LOG_SCALER = 0.001 # int32 значения приходят в fixed-point лог-шкале с шагом 1e-3
LOG_POSTSCALER = 1000
LOG_EXP_LIMIT = 300.0 # запас до переполнения float64 при возведении LOG_BASE в степень
# Порог для инверсии сырых данных: если среднее значение свипа ниже порога — # Порог для инверсии сырых данных: если среднее значение свипа ниже порога —
# считаем, что сигнал «меньше нуля» и домножаем свип на -1 # считаем, что сигнал «меньше нуля» и домножаем свип на -1
DATA_INVERSION_THRASHOLD = 10.0 DATA_INVERSION_THRASHOLD = 10.0
LOG_DETECTOR_OFFSET = 0.0
LOG_DETECTOR_SCALER = -0.001
LOG_DETECTOR_BASE = 2.0
LOG_DETECTOR_EXP_MIN = -149.0
LOG_DETECTOR_EXP_MAX = 128.0
Number = Union[int, float] Number = Union[int, float]
SweepInfo = Dict[str, Any] SweepInfo = Dict[str, Any]
SweepAuxCurves = Optional[Tuple[np.ndarray, np.ndarray]] SweepPacket = Tuple[np.ndarray, SweepInfo]
SweepPacket = Tuple[np.ndarray, SweepInfo, SweepAuxCurves]
def _format_status_kv(data: Mapping[str, Any]) -> str: def _format_status_kv(data: Mapping[str, Any]) -> str:
@ -64,7 +64,8 @@ def _format_status_kv(data: Mapping[str, Any]) -> str:
return f"{fv:.3g}" return f"{fv:.3g}"
return f"{fv:.3f}".rstrip("0").rstrip(".") return f"{fv:.3f}".rstrip("0").rstrip(".")
parts = [f"{k}:{_fmt(v)}" for k, v in data.items()] hidden_keys = {"pre_exp_sweep", "sweep_1", "sweep_2"}
parts = [f"{k}:{_fmt(v)}" for k, v in data.items() if k not in hidden_keys]
return " ".join(parts) return " ".join(parts)
@ -90,44 +91,6 @@ def _parse_spec_clip(spec: Optional[str]) -> Optional[Tuple[float, float]]:
return None return None
def _log_value_to_linear(value: int) -> float:
"""Преобразовать fixed-point логарифмическое значение в линейную шкалу."""
exponent = max(-LOG_EXP_LIMIT, min(LOG_EXP_LIMIT, float(value) * LOG_SCALER))
return float(LOG_BASE ** exponent)
def _log_pair_to_sweep(avg_1: int, avg_2: int) -> float:
"""Разность двух логарифмических усреднений в линейной шкале."""
return (_log_value_to_linear(avg_1) - _log_value_to_linear(avg_2))*LOG_POSTSCALER
def _compute_auto_ylim(*series_list: Optional[np.ndarray]) -> Optional[Tuple[float, float]]:
"""Общий Y-диапазон по всем переданным кривым с небольшим запасом."""
y_min: Optional[float] = None
y_max: Optional[float] = None
for series in series_list:
if series is None:
continue
arr = np.asarray(series)
if arr.size == 0:
continue
finite = arr[np.isfinite(arr)]
if finite.size == 0:
continue
cur_min = float(np.min(finite))
cur_max = float(np.max(finite))
y_min = cur_min if y_min is None else min(y_min, cur_min)
y_max = cur_max if y_max is None else max(y_max, cur_max)
if y_min is None or y_max is None:
return None
if y_min == y_max:
pad = max(1.0, abs(y_min) * 0.05)
else:
pad = 0.05 * (y_max - y_min)
return (y_min - pad, y_max + pad)
def _normalize_sweep_simple(raw: np.ndarray, calib: np.ndarray) -> np.ndarray: def _normalize_sweep_simple(raw: np.ndarray, calib: np.ndarray) -> np.ndarray:
"""Простая нормировка: поэлементное деление raw/calib.""" """Простая нормировка: поэлементное деление raw/calib."""
w = min(raw.size, calib.size) w = min(raw.size, calib.size)
@ -428,7 +391,7 @@ class SweepReader(threading.Thread):
stop_event: threading.Event, stop_event: threading.Event,
fancy: bool = False, fancy: bool = False,
bin_mode: bool = False, bin_mode: bool = False,
logscale: bool = False, logdetector: bool = False,
): ):
super().__init__(daemon=True) super().__init__(daemon=True)
self._port_path = port_path self._port_path = port_path
@ -438,7 +401,7 @@ class SweepReader(threading.Thread):
self._src: Optional[SerialLineSource] = None self._src: Optional[SerialLineSource] = None
self._fancy = bool(fancy) self._fancy = bool(fancy)
self._bin_mode = bool(bin_mode) self._bin_mode = bool(bin_mode)
self._logscale = bool(logscale) self._logdetector = bool(logdetector)
self._max_width: int = 0 self._max_width: int = 0
self._sweep_idx: int = 0 self._sweep_idx: int = 0
self._last_sweep_ts: Optional[float] = None self._last_sweep_ts: Optional[float] = None
@ -454,8 +417,8 @@ class SweepReader(threading.Thread):
xs, xs,
ys, ys,
channels: Optional[set[int]], channels: Optional[set[int]],
raw_curves: Optional[Tuple[list[int], list[int]]] = None, ys1: Optional[list[int]] = None,
apply_inversion: bool = True, ys2: Optional[list[int]] = None,
): ):
if not xs: if not xs:
return return
@ -465,43 +428,26 @@ class SweepReader(threading.Thread):
width = max_x + 1 width = max_x + 1
self._max_width = max(self._max_width, width) self._max_width = max(self._max_width, width)
target_width = self._max_width if self._fancy else width target_width = self._max_width if self._fancy else width
def _build_sweep(values) -> np.ndarray:
def _scatter(values, dtype) -> np.ndarray: arr = np.full((target_width,), np.nan, dtype=np.float32)
series = np.full((target_width,), np.nan, dtype=dtype)
try: try:
idx = np.asarray(xs, dtype=np.int64) idx = np.asarray(xs, dtype=np.int64)
vals = np.asarray(values, dtype=dtype) vals = np.asarray(values, dtype=np.float32)
series[idx] = vals arr[idx] = vals
except Exception: except Exception:
for x, y in zip(xs, values): for x, y in zip(xs, values):
if 0 <= x < target_width: if 0 <= x < target_width:
series[x] = y arr[x] = float(y)
return series return arr
def _fill_missing(series: np.ndarray): sweep_1: Optional[np.ndarray] = None
known = ~np.isnan(series) sweep_2: Optional[np.ndarray] = None
if not np.any(known): if ys1 is not None and ys2 is not None and len(ys1) == len(xs) and len(ys2) == len(xs):
return sweep_1 = _build_sweep(ys1)
known_idx = np.nonzero(known)[0] sweep_2 = _build_sweep(ys2)
for i0, i1 in zip(known_idx[:-1], known_idx[1:]): sweep = sweep_1 - sweep_2
if i1 - i0 > 1: else:
avg = (series[i0] + series[i1]) * 0.5 sweep = _build_sweep(ys)
series[i0 + 1 : i1] = avg
first_idx = int(known_idx[0])
last_idx = int(known_idx[-1])
if first_idx > 0:
series[:first_idx] = series[first_idx]
if last_idx < series.size - 1:
series[last_idx + 1 :] = series[last_idx]
# Быстрый векторизованный путь
sweep = _scatter(ys, np.float32)
aux_curves: SweepAuxCurves = None
if raw_curves is not None:
aux_curves = (
_scatter(raw_curves[0], np.float32),
_scatter(raw_curves[1], np.float32),
)
# Метрики валидных точек до заполнения пропусков # Метрики валидных точек до заполнения пропусков
finite_pre = np.isfinite(sweep) finite_pre = np.isfinite(sweep)
n_valid_cur = int(np.count_nonzero(finite_pre)) n_valid_cur = int(np.count_nonzero(finite_pre))
@ -509,22 +455,62 @@ class SweepReader(threading.Thread):
# Дополнительная обработка пропусков: при --fancy заполняем внутренние разрывы, края и дотягиваем до максимальной длины # Дополнительная обработка пропусков: при --fancy заполняем внутренние разрывы, края и дотягиваем до максимальной длины
if self._fancy: if self._fancy:
try: try:
_fill_missing(sweep) known = ~np.isnan(sweep)
if aux_curves is not None: if np.any(known):
_fill_missing(aux_curves[0]) known_idx = np.nonzero(known)[0]
_fill_missing(aux_curves[1]) # Для каждой пары соседних известных индексов заполним промежуток средним значением
for i0, i1 in zip(known_idx[:-1], known_idx[1:]):
if i1 - i0 > 1:
avg = (sweep[i0] + sweep[i1]) * 0.5
sweep[i0 + 1 : i1] = avg
first_idx = int(known_idx[0])
last_idx = int(known_idx[-1])
if first_idx > 0:
sweep[:first_idx] = sweep[first_idx]
if last_idx < sweep.size - 1:
sweep[last_idx + 1 :] = sweep[last_idx]
except Exception: except Exception:
# В случае ошибки просто оставляем как есть # В случае ошибки просто оставляем как есть
pass pass
'''
# Инверсия данных при «отрицательном» уровне (среднее ниже порога) # Инверсия данных при «отрицательном» уровне (среднее ниже порога)
if apply_inversion:
try:
m = float(np.nanmean(sweep))
if np.isfinite(m) and m < DATA_INVERSION_THRASHOLD:
sweep *= -1.0
except Exception:
pass
'''
pre_exp_sweep: Optional[np.ndarray] = None
if self._logdetector:
try: try:
m = float(np.nanmean(sweep)) if sweep_1 is not None and sweep_2 is not None:
if np.isfinite(m) and m < DATA_INVERSION_THRASHOLD: s1_pre = (sweep_1 - LOG_DETECTOR_OFFSET) * LOG_DETECTOR_SCALER
sweep *= -1.0 s2_pre = (sweep_2 - LOG_DETECTOR_OFFSET) * LOG_DETECTOR_SCALER
s1_pre = np.clip(s1_pre, LOG_DETECTOR_EXP_MIN, LOG_DETECTOR_EXP_MAX)
s2_pre = np.clip(s2_pre, LOG_DETECTOR_EXP_MIN, LOG_DETECTOR_EXP_MAX)
# with np.errstate(over="ignore", invalid="ignore"):
# sweep_1 = np.power(LOG_DETECTOR_BASE, np.asarray(s1_pre, dtype=np.float64)).astype(np.float32)
# sweep_2 = np.power(LOG_DETECTOR_BASE, np.asarray(s2_pre, dtype=np.float64)).astype(np.float32)
sweep_1 = np.power(LOG_DETECTOR_BASE, np.asarray(s1_pre, dtype=np.float64)).astype(np.float32)
sweep_2 = np.power(LOG_DETECTOR_BASE, np.asarray(s2_pre, dtype=np.float64)).astype(np.float32)
sweep_1[~np.isfinite(sweep_1)] = np.nan
sweep_2[~np.isfinite(sweep_2)] = np.nan
sweep = sweep_1 - sweep_2
else:
sweep = (sweep - LOG_DETECTOR_OFFSET) * LOG_DETECTOR_SCALER
sweep = np.clip(sweep, LOG_DETECTOR_EXP_MIN, LOG_DETECTOR_EXP_MAX)
pre_exp_sweep = sweep.copy()
with np.errstate(over="ignore", invalid="ignore"):
sweep = np.power(LOG_DETECTOR_BASE, np.asarray(sweep, dtype=np.float64)).astype(np.float32)
sweep[~np.isfinite(sweep)] = np.nan
except Exception: except Exception:
pass pass
#sweep = np.abs(sweep)
#print(sweep)
#sweep -= float(np.nanmean(sweep)) #sweep -= float(np.nanmean(sweep))
# Метрики для статусной строки (вид словаря: переменная -> значение) # Метрики для статусной строки (вид словаря: переменная -> значение)
@ -565,17 +551,22 @@ class SweepReader(threading.Thread):
"std": std, "std": std,
"dt_ms": dt_ms, "dt_ms": dt_ms,
} }
if pre_exp_sweep is not None:
info["pre_exp_sweep"] = pre_exp_sweep
if sweep_1 is not None and sweep_2 is not None:
info["sweep_1"] = sweep_1
info["sweep_2"] = sweep_2
# Кладём готовый свип (если очередь полна — выбрасываем самый старый) # Кладём готовый свип (если очередь полна — выбрасываем самый старый)
try: try:
self._q.put_nowait((sweep, info, aux_curves)) self._q.put_nowait((sweep, info))
except Full: except Full:
try: try:
_ = self._q.get_nowait() _ = self._q.get_nowait()
except Exception: except Exception:
pass pass
try: try:
self._q.put_nowait((sweep, info, aux_curves)) self._q.put_nowait((sweep, info))
except Exception: except Exception:
pass pass
@ -584,6 +575,7 @@ class SweepReader(threading.Thread):
ys: list[int] = [] ys: list[int] = []
cur_channel: Optional[int] = None cur_channel: Optional[int] = None
cur_channels: set[int] = set() cur_channels: set[int] = set()
buf = bytearray() buf = bytearray()
while not self._stop.is_set(): while not self._stop.is_set():
data = chunk_reader.read_available() data = chunk_reader.read_available()
@ -612,7 +604,6 @@ class SweepReader(threading.Thread):
cur_channels.clear() cur_channels.clear()
continue continue
# sCH X Y или s CH X Y (все целые со знаком). Разделяем по любым пробелам/табам.
if len(line) >= 3: if len(line) >= 3:
parts = line.split() parts = line.split()
if len(parts) >= 3 and (parts[0].lower() == b"s" or parts[0].lower().startswith(b"s")): if len(parts) >= 3 and (parts[0].lower() == b"s" or parts[0].lower().startswith(b"s")):
@ -621,16 +612,15 @@ class SweepReader(threading.Thread):
if len(parts) >= 4: if len(parts) >= 4:
ch = int(parts[1], 10) ch = int(parts[1], 10)
x = int(parts[2], 10) x = int(parts[2], 10)
y = int(parts[3], 10) # поддержка знака: "+…" и "-…" y = int(parts[3], 10)
else: else:
ch = 0 ch = 0
x = int(parts[1], 10) x = int(parts[1], 10)
y = int(parts[2], 10) # поддержка знака: "+…" и "-…" y = int(parts[2], 10)
else: else:
# формат вида "s0"
ch = int(parts[0][1:], 10) ch = int(parts[0][1:], 10)
x = int(parts[1], 10) x = int(parts[1], 10)
y = int(parts[2], 10) # поддержка знака: "+…" и "-…" y = int(parts[2], 10)
except Exception: except Exception:
continue continue
if cur_channel is None: if cur_channel is None:
@ -647,6 +637,8 @@ class SweepReader(threading.Thread):
def _run_binary_stream(self, chunk_reader: SerialChunkReader): def _run_binary_stream(self, chunk_reader: SerialChunkReader):
xs: list[int] = [] xs: list[int] = []
ys: list[int] = [] ys: list[int] = []
ys1: list[int] = []
ys2: list[int] = []
cur_channel: Optional[int] = None cur_channel: Optional[int] = None
cur_channels: set[int] = set() cur_channels: set[int] = set()
words = deque() words = deque()
@ -670,88 +662,9 @@ class SweepReader(threading.Thread):
words.append(w) words.append(w)
i += 2 i += 2
# Бинарный протокол: # Новый бинарный формат:
# старт свипа (актуальный): 0xFFFF, 0xFFFF, 0xFFFF, (ch<<8)|0x0A # - старт: FFFF,FFFF,FFFF,FFFF,FFFF,(CH<<8)|0x0A
# старт свипа (legacy): 0xFFFF, 0xFFFF, channel, 0x0A0A # - точка: X,avg1_hi,avg1_lo,avg2_hi,avg2_lo,0x000A
# точка: step, value_hi, value_lo, 0x000A
while len(words) >= 4:
w0 = int(words[0])
w1 = int(words[1])
w2 = int(words[2])
w3 = int(words[3])
if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and (w3 & 0x00FF) == 0x000A:
self._finalize_current(xs, ys, cur_channels)
xs.clear()
ys.clear()
cur_channels.clear()
cur_channel = (w3 >> 8) & 0x00FF
cur_channels.add(cur_channel)
for _ in range(4):
words.popleft()
continue
if w0 == 0xFFFF and w1 == 0xFFFF and w3 == 0x0A0A:
self._finalize_current(xs, ys, cur_channels)
xs.clear()
ys.clear()
cur_channels.clear()
cur_channel = w2
cur_channels.add(cur_channel)
for _ in range(4):
words.popleft()
continue
if w3 == 0x000A:
if cur_channel is not None:
cur_channels.add(cur_channel)
xs.append(w0)
value_u32 = (w1 << 16) | w2
ys.append(self._u32_to_i32(value_u32))
for _ in range(4):
words.popleft()
continue
# Поток может начаться с середины пакета; сдвигаемся по слову до ресинхронизации.
words.popleft()
del buf[:usable]
if len(buf) > 1_000_000:
del buf[:-262144]
self._finalize_current(xs, ys, cur_channels)
def _run_logscale_binary_stream(self, chunk_reader: SerialChunkReader):
xs: list[int] = []
ys: list[float] = []
avg_1_vals: list[int] = []
avg_2_vals: list[int] = []
cur_channel: Optional[int] = None
cur_channels: set[int] = set()
words = deque()
buf = bytearray()
while not self._stop.is_set():
data = chunk_reader.read_available()
if data:
buf += data
else:
time.sleep(0.0005)
continue
usable = len(buf) & ~1
if usable == 0:
continue
i = 0
while i < usable:
w = int(buf[i]) | (int(buf[i + 1]) << 8)
words.append(w)
i += 2
# Бинарный logscale-протокол:
# старт свипа: 0xFFFF x5, затем (ch<<8)|0x0A
# точка: step, avg1_hi, avg1_lo, avg2_hi, avg2_lo, 0x000A
while len(words) >= 6: while len(words) >= 6:
w0 = int(words[0]) w0 = int(words[0])
w1 = int(words[1]) w1 = int(words[1])
@ -761,24 +674,14 @@ class SweepReader(threading.Thread):
w5 = int(words[5]) w5 = int(words[5])
if ( if (
w0 == 0xFFFF w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF
and w1 == 0xFFFF and w3 == 0xFFFF and w4 == 0xFFFF and (w5 & 0x00FF) == 0x000A
and w2 == 0xFFFF
and w3 == 0xFFFF
and w4 == 0xFFFF
and (w5 & 0x00FF) == 0x000A
): ):
self._finalize_current( self._finalize_current(xs, ys, cur_channels, ys1=ys1, ys2=ys2)
xs,
ys,
cur_channels,
raw_curves=(avg_1_vals, avg_2_vals),
apply_inversion=False,
)
xs.clear() xs.clear()
ys.clear() ys.clear()
avg_1_vals.clear() ys1.clear()
avg_2_vals.clear() ys2.clear()
cur_channels.clear() cur_channels.clear()
cur_channel = (w5 >> 8) & 0x00FF cur_channel = (w5 >> 8) & 0x00FF
cur_channels.add(cur_channel) cur_channels.add(cur_channel)
@ -789,13 +692,14 @@ class SweepReader(threading.Thread):
if w5 == 0x000A: if w5 == 0x000A:
if cur_channel is not None: if cur_channel is not None:
cur_channels.add(cur_channel) cur_channels.add(cur_channel)
avg_1 = self._u32_to_i32((w1 << 16) | w2)
avg_2 = self._u32_to_i32((w3 << 16) | w4)
xs.append(w0) xs.append(w0)
avg_1_vals.append(avg_1) avg1_u32 = (w1 << 16) | w2
avg_2_vals.append(avg_2) avg2_u32 = (w3 << 16) | w4
ys.append(_log_pair_to_sweep(avg_1, avg_2)) avg1 = self._u32_to_i32(avg1_u32)
#ys.append(LOG_BASE**(avg_1/LOG_SCALER) - LOG_BASE**(avg_2/LOG_SCALER)) avg2 = self._u32_to_i32(avg2_u32)
ys1.append(avg1)
ys2.append(avg2)
ys.append(avg1 - avg2)
for _ in range(6): for _ in range(6):
words.popleft() words.popleft()
continue continue
@ -806,13 +710,7 @@ class SweepReader(threading.Thread):
if len(buf) > 1_000_000: if len(buf) > 1_000_000:
del buf[:-262144] del buf[:-262144]
self._finalize_current( self._finalize_current(xs, ys, cur_channels, ys1=ys1, ys2=ys2)
xs,
ys,
cur_channels,
raw_curves=(avg_1_vals, avg_2_vals),
apply_inversion=False,
)
def run(self): def run(self):
try: try:
@ -824,9 +722,7 @@ class SweepReader(threading.Thread):
try: try:
chunk_reader = SerialChunkReader(self._src) chunk_reader = SerialChunkReader(self._src)
if self._logscale: if self._bin_mode:
self._run_logscale_binary_stream(chunk_reader)
elif self._bin_mode:
self._run_binary_stream(chunk_reader) self._run_binary_stream(chunk_reader)
else: else:
self._run_ascii_stream(chunk_reader) self._run_ascii_stream(chunk_reader)
@ -857,7 +753,7 @@ def main():
"--spec-clip", "--spec-clip",
default="2,98", default="2,98",
help=( help=(
"Процентильная обрезка уровней водопада спектров, % (min,max). " "Процентильная обрезка уровней водопада спектров, %% (min,max). "
"Напр. 2,98. 'off' — отключить" "Напр. 2,98. 'off' — отключить"
), ),
) )
@ -898,19 +794,16 @@ def main():
"--bin", "--bin",
dest="bin_mode", dest="bin_mode",
action="store_true", action="store_true",
default=True,
help=( help=(
"Бинарный протокол: старт свипа 0xFFFF,0xFFFF,0xFFFF,(CH<<8)|0x0A; " "Бинарный протокол: старт FFFFx5,(CH<<8)|0x0A; "
"точки step,uint32(hi16,lo16),0x000A" "точки X,avg1_hi,avg1_lo,avg2_hi,avg2_lo,0x000A (sweep=avg1-avg2)"
), ),
) )
parser.add_argument( parser.add_argument(
"--logscale", "--logdetector",
action="store_true", action="store_true",
default=True, help="Лог-детектор: после инверсии ((sweep-OFFSET)*SCALER) и затем BASE**sweep",
help=(
"Новый бинарный протокол: точка несёт пару int32 (avg_1, avg_2), "
"а свип считается как 10**(avg_1*0.001) - 10**(avg_2*0.001)"
),
) )
args = parser.parse_args() args = parser.parse_args()
@ -943,8 +836,8 @@ def main():
q, q,
stop_event, stop_event,
fancy=bool(args.fancy), fancy=bool(args.fancy),
bin_mode=bool(args.bin_mode), bin_mode=bool(getattr(args, "bin_mode", False)),
logscale=bool(args.logscale), logdetector=bool(getattr(args, "logdetector", False)),
) )
reader.start() reader.start()
@ -957,7 +850,9 @@ def main():
# Состояние для отображения # Состояние для отображения
current_sweep_raw: Optional[np.ndarray] = None current_sweep_raw: Optional[np.ndarray] = None
current_aux_curves: SweepAuxCurves = None current_sweep_1: Optional[np.ndarray] = None
current_sweep_2: Optional[np.ndarray] = None
current_sweep_pre_exp: Optional[np.ndarray] = None
current_sweep_norm: Optional[np.ndarray] = None current_sweep_norm: Optional[np.ndarray] = None
last_calib_sweep: Optional[np.ndarray] = None last_calib_sweep: Optional[np.ndarray] = None
current_info: Optional[SweepInfo] = None current_info: Optional[SweepInfo] = None
@ -982,6 +877,7 @@ def main():
contrast_slider = None contrast_slider = None
calib_enabled = False calib_enabled = False
norm_type = str(getattr(args, "norm_type", "projector")).strip().lower() norm_type = str(getattr(args, "norm_type", "projector")).strip().lower()
logdetector_enabled = bool(getattr(args, "logdetector", False))
cb = None cb = None
# Статусная строка (внизу окна) # Статусная строка (внизу окна)
@ -996,10 +892,8 @@ def main():
) )
# Линейный график последнего свипа # Линейный график последнего свипа
line_avg1_obj, = ax_line.plot([], [], lw=1, color="0.65")
line_avg2_obj, = ax_line.plot([], [], lw=1, color="0.45")
line_obj, = ax_line.plot([], [], lw=1, color="tab:blue") line_obj, = ax_line.plot([], [], lw=1, color="tab:blue")
line_calib_obj, = ax_line.plot([], [], lw=1, color="tab:red") line_calib_obj, = ax_line.plot([], [], lw=1, color="gold")
line_norm_obj, = ax_line.plot([], [], lw=1, color="tab:green") line_norm_obj, = ax_line.plot([], [], lw=1, color="tab:green")
ax_line.set_title("Сырые данные", pad=1) ax_line.set_title("Сырые данные", pad=1)
ax_line.set_xlabel("ГГц") ax_line.set_xlabel("ГГц")
@ -1138,7 +1032,7 @@ def main():
freq_shared = np.arange(fft_bins, dtype=np.int32) freq_shared = np.arange(fft_bins, dtype=np.int32)
def _visible_levels_matplotlib(data: np.ndarray, axis) -> Optional[Tuple[float, float]]: def _visible_levels_matplotlib(data: np.ndarray, axis) -> Optional[Tuple[float, float]]:
"""(vmin, vmax) по центральным 90% значений в видимой области imshow.""" """(vmin, vmax) по текущей видимой области imshow (без накопления по времени)."""
if data.size == 0: if data.size == 0:
return None return None
ny, nx = data.shape[0], data.shape[1] ny, nx = data.shape[0], data.shape[1]
@ -1163,8 +1057,8 @@ def main():
if not finite.any(): if not finite.any():
return None return None
vals = sub[finite] vals = sub[finite]
vmin = float(np.nanpercentile(vals, 5)) vmin = float(np.min(vals))
vmax = float(np.nanpercentile(vals, 95)) vmax = float(np.max(vals))
if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax: if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax:
return None return None
return (vmin, vmax) return (vmin, vmax)
@ -1217,17 +1111,22 @@ def main():
y_max_fft = float(fr_max) y_max_fft = float(fr_max)
def drain_queue(): def drain_queue():
nonlocal current_sweep_raw, current_aux_curves, current_sweep_norm, current_info, last_calib_sweep nonlocal current_sweep_raw, current_sweep_1, current_sweep_2, current_sweep_pre_exp, current_sweep_norm, current_info, last_calib_sweep
drained = 0 drained = 0
while True: while True:
try: try:
s, info, aux_curves = q.get_nowait() s, info = q.get_nowait()
except Empty: except Empty:
break break
drained += 1 drained += 1
current_sweep_raw = s current_sweep_raw = s
current_aux_curves = aux_curves
current_info = info current_info = info
s1 = info.get("sweep_1") if isinstance(info, dict) else None
s2 = info.get("sweep_2") if isinstance(info, dict) else None
current_sweep_1 = s1 if isinstance(s1, np.ndarray) else None
current_sweep_2 = s2 if isinstance(s2, np.ndarray) else None
pre = info.get("pre_exp_sweep") if isinstance(info, dict) else None
current_sweep_pre_exp = pre if isinstance(pre, np.ndarray) else None
ch = 0 ch = 0
try: try:
ch = int(info.get("ch", 0)) if isinstance(info, dict) else 0 ch = int(info.get("ch", 0)) if isinstance(info, dict) else 0
@ -1296,31 +1195,46 @@ def main():
else: else:
xs = np.arange(current_sweep_raw.size, dtype=np.int32) xs = np.arange(current_sweep_raw.size, dtype=np.int32)
line_obj.set_data(xs, current_sweep_raw) line_obj.set_data(xs, current_sweep_raw)
if current_aux_curves is not None: if current_sweep_1 is not None and current_sweep_2 is not None:
avg_1_curve, avg_2_curve = current_aux_curves line_calib_obj.set_data(xs[: current_sweep_1.size], current_sweep_1)
line_avg1_obj.set_data(xs[: avg_1_curve.size], avg_1_curve) line_norm_obj.set_data(xs[: current_sweep_2.size], current_sweep_2)
line_avg2_obj.set_data(xs[: avg_2_curve.size], avg_2_curve) elif logdetector_enabled:
else:
line_avg1_obj.set_data([], [])
line_avg2_obj.set_data([], [])
if last_calib_sweep is not None:
line_calib_obj.set_data(xs[: last_calib_sweep.size], last_calib_sweep)
else:
line_calib_obj.set_data([], []) line_calib_obj.set_data([], [])
if current_sweep_norm is not None: if current_sweep_pre_exp is not None:
line_norm_obj.set_data(xs[: current_sweep_norm.size], current_sweep_norm) line_norm_obj.set_data(xs[: current_sweep_pre_exp.size], current_sweep_pre_exp)
else:
line_norm_obj.set_data([], [])
else: else:
line_norm_obj.set_data([], []) if last_calib_sweep is not None:
line_calib_obj.set_data(xs[: last_calib_sweep.size], last_calib_sweep)
else:
line_calib_obj.set_data([], [])
if current_sweep_norm is not None:
line_norm_obj.set_data(xs[: current_sweep_norm.size], current_sweep_norm)
else:
line_norm_obj.set_data([], [])
# Лимиты по X: 3.3 ГГц .. 14.3 ГГц # Лимиты по X: 3.3 ГГц .. 14.3 ГГц
ax_line.set_xlim(3.3, 14.3) ax_line.set_xlim(3.3, 14.3)
# Адаптивные Y-лимиты (если не задан --ylim) # Адаптивные Y-лимиты (если не задан --ylim)
if fixed_ylim is None: if fixed_ylim is None:
y_series = [current_sweep_raw, last_calib_sweep, current_sweep_norm] y_candidates = [current_sweep_raw]
if current_aux_curves is not None: if current_sweep_1 is not None and current_sweep_2 is not None:
y_series.extend(current_aux_curves) y_candidates.extend([current_sweep_1, current_sweep_2])
y_limits = _compute_auto_ylim(*y_series) elif logdetector_enabled and current_sweep_pre_exp is not None:
if y_limits is not None: y_candidates.append(current_sweep_pre_exp)
ax_line.set_ylim(y_limits[0], y_limits[1]) y_concat = np.concatenate([np.asarray(v, dtype=np.float32) for v in y_candidates])
y0 = float(np.nanmin(y_concat))
y1 = float(np.nanmax(y_concat))
if np.isfinite(y0) and np.isfinite(y1):
if y0 == y1:
pad = max(1.0, abs(y0) * 0.05)
y0 -= pad
y1 += pad
else:
pad = 0.05 * (y1 - y0)
y0 -= pad
y1 += pad
ax_line.set_ylim(y0, y1)
# Обновление спектра текущего свипа # Обновление спектра текущего свипа
sweep_for_fft = current_sweep_norm if current_sweep_norm is not None else current_sweep_raw sweep_for_fft = current_sweep_norm if current_sweep_norm is not None else current_sweep_raw
@ -1410,8 +1324,6 @@ def main():
# Возвращаем обновлённые артисты # Возвращаем обновлённые артисты
return ( return (
line_obj, line_obj,
line_avg1_obj,
line_avg2_obj,
line_calib_obj, line_calib_obj,
line_norm_obj, line_norm_obj,
img_obj, img_obj,
@ -1453,8 +1365,8 @@ def run_pyqtgraph(args):
q, q,
stop_event, stop_event,
fancy=bool(args.fancy), fancy=bool(args.fancy),
bin_mode=bool(args.bin_mode), bin_mode=bool(getattr(args, "bin_mode", False)),
logscale=bool(args.logscale), logdetector=bool(getattr(args, "logdetector", False)),
) )
reader.start() reader.start()
@ -1472,10 +1384,8 @@ def run_pyqtgraph(args):
# Плот последнего свипа (слева-сверху) # Плот последнего свипа (слева-сверху)
p_line = win.addPlot(row=0, col=0, title="Сырые данные") p_line = win.addPlot(row=0, col=0, title="Сырые данные")
p_line.showGrid(x=True, y=True, alpha=0.3) p_line.showGrid(x=True, y=True, alpha=0.3)
curve_avg1 = p_line.plot(pen=pg.mkPen((170, 170, 170), width=1))
curve_avg2 = p_line.plot(pen=pg.mkPen((110, 110, 110), width=1))
curve = p_line.plot(pen=pg.mkPen((80, 120, 255), width=1)) curve = p_line.plot(pen=pg.mkPen((80, 120, 255), width=1))
curve_calib = p_line.plot(pen=pg.mkPen((220, 60, 60), width=1)) curve_calib = p_line.plot(pen=pg.mkPen((220, 200, 60), width=1))
curve_norm = p_line.plot(pen=pg.mkPen((60, 180, 90), width=1)) curve_norm = p_line.plot(pen=pg.mkPen((60, 180, 90), width=1))
p_line.setLabel("bottom", "ГГц") p_line.setLabel("bottom", "ГГц")
p_line.setLabel("left", "Y") p_line.setLabel("left", "Y")
@ -1533,7 +1443,9 @@ def run_pyqtgraph(args):
width: Optional[int] = None width: Optional[int] = None
x_shared: Optional[np.ndarray] = None x_shared: Optional[np.ndarray] = None
current_sweep_raw: Optional[np.ndarray] = None current_sweep_raw: Optional[np.ndarray] = None
current_aux_curves: SweepAuxCurves = None current_sweep_1: Optional[np.ndarray] = None
current_sweep_2: Optional[np.ndarray] = None
current_sweep_pre_exp: Optional[np.ndarray] = None
current_sweep_norm: Optional[np.ndarray] = None current_sweep_norm: Optional[np.ndarray] = None
last_calib_sweep: Optional[np.ndarray] = None last_calib_sweep: Optional[np.ndarray] = None
current_info: Optional[SweepInfo] = None current_info: Optional[SweepInfo] = None
@ -1548,6 +1460,7 @@ def run_pyqtgraph(args):
spec_mean_sec = float(getattr(args, "spec_mean_sec", 0.0)) spec_mean_sec = float(getattr(args, "spec_mean_sec", 0.0))
calib_enabled = False calib_enabled = False
norm_type = str(getattr(args, "norm_type", "projector")).strip().lower() norm_type = str(getattr(args, "norm_type", "projector")).strip().lower()
logdetector_enabled = bool(getattr(args, "logdetector", False))
# Диапазон по Y: авто по умолчанию (поддерживает отрицательные значения) # Диапазон по Y: авто по умолчанию (поддерживает отрицательные значения)
fixed_ylim: Optional[Tuple[float, float]] = None fixed_ylim: Optional[Tuple[float, float]] = None
if args.ylim: if args.ylim:
@ -1600,7 +1513,7 @@ def run_pyqtgraph(args):
freq_shared = np.arange(fft_bins, dtype=np.int32) freq_shared = np.arange(fft_bins, dtype=np.int32)
def _visible_levels_pyqtgraph(data: np.ndarray) -> Optional[Tuple[float, float]]: def _visible_levels_pyqtgraph(data: np.ndarray) -> Optional[Tuple[float, float]]:
"""(vmin, vmax) по центральным 90% значений в видимой области ImageItem.""" """(vmin, vmax) по текущей видимой области ImageItem (без накопления по времени)."""
if data.size == 0: if data.size == 0:
return None return None
ny, nx = data.shape[0], data.shape[1] ny, nx = data.shape[0], data.shape[1]
@ -1624,8 +1537,8 @@ def run_pyqtgraph(args):
if not finite.any(): if not finite.any():
return None return None
vals = sub[finite] vals = sub[finite]
vmin = float(np.nanpercentile(vals, 5)) vmin = float(np.min(vals))
vmax = float(np.nanpercentile(vals, 95)) vmax = float(np.max(vals))
if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax: if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax:
return None return None
return (vmin, vmax) return (vmin, vmax)
@ -1667,17 +1580,22 @@ def run_pyqtgraph(args):
y_max_fft = float(fr_max) y_max_fft = float(fr_max)
def drain_queue(): def drain_queue():
nonlocal current_sweep_raw, current_aux_curves, current_sweep_norm, current_info, last_calib_sweep nonlocal current_sweep_raw, current_sweep_1, current_sweep_2, current_sweep_pre_exp, current_sweep_norm, current_info, last_calib_sweep
drained = 0 drained = 0
while True: while True:
try: try:
s, info, aux_curves = q.get_nowait() s, info = q.get_nowait()
except Empty: except Empty:
break break
drained += 1 drained += 1
current_sweep_raw = s current_sweep_raw = s
current_aux_curves = aux_curves
current_info = info current_info = info
s1 = info.get("sweep_1") if isinstance(info, dict) else None
s2 = info.get("sweep_2") if isinstance(info, dict) else None
current_sweep_1 = s1 if isinstance(s1, np.ndarray) else None
current_sweep_2 = s2 if isinstance(s2, np.ndarray) else None
pre = info.get("pre_exp_sweep") if isinstance(info, dict) else None
current_sweep_pre_exp = pre if isinstance(pre, np.ndarray) else None
ch = 0 ch = 0
try: try:
ch = int(info.get("ch", 0)) if isinstance(info, dict) else 0 ch = int(info.get("ch", 0)) if isinstance(info, dict) else 0
@ -1715,28 +1633,36 @@ def run_pyqtgraph(args):
else: else:
xs = np.arange(current_sweep_raw.size) xs = np.arange(current_sweep_raw.size)
curve.setData(xs, current_sweep_raw, autoDownsample=True) curve.setData(xs, current_sweep_raw, autoDownsample=True)
if current_aux_curves is not None: if current_sweep_1 is not None and current_sweep_2 is not None:
avg_1_curve, avg_2_curve = current_aux_curves curve_calib.setData(xs[: current_sweep_1.size], current_sweep_1, autoDownsample=True)
curve_avg1.setData(xs[: avg_1_curve.size], avg_1_curve, autoDownsample=True) curve_norm.setData(xs[: current_sweep_2.size], current_sweep_2, autoDownsample=True)
curve_avg2.setData(xs[: avg_2_curve.size], avg_2_curve, autoDownsample=True) elif logdetector_enabled:
else:
curve_avg1.setData([], [])
curve_avg2.setData([], [])
if last_calib_sweep is not None:
curve_calib.setData(xs[: last_calib_sweep.size], last_calib_sweep, autoDownsample=True)
else:
curve_calib.setData([], []) curve_calib.setData([], [])
if current_sweep_norm is not None: if current_sweep_pre_exp is not None:
curve_norm.setData(xs[: current_sweep_norm.size], current_sweep_norm, autoDownsample=True) curve_norm.setData(xs[: current_sweep_pre_exp.size], current_sweep_pre_exp, autoDownsample=True)
else:
curve_norm.setData([], [])
else: else:
curve_norm.setData([], []) if last_calib_sweep is not None:
curve_calib.setData(xs[: last_calib_sweep.size], last_calib_sweep, autoDownsample=True)
else:
curve_calib.setData([], [])
if current_sweep_norm is not None:
curve_norm.setData(xs[: current_sweep_norm.size], current_sweep_norm, autoDownsample=True)
else:
curve_norm.setData([], [])
if fixed_ylim is None: if fixed_ylim is None:
y_series = [current_sweep_raw, last_calib_sweep, current_sweep_norm] y_candidates = [current_sweep_raw]
if current_aux_curves is not None: if current_sweep_1 is not None and current_sweep_2 is not None:
y_series.extend(current_aux_curves) y_candidates.extend([current_sweep_1, current_sweep_2])
y_limits = _compute_auto_ylim(*y_series) elif logdetector_enabled and current_sweep_pre_exp is not None:
if y_limits is not None: y_candidates.append(current_sweep_pre_exp)
p_line.setYRange(y_limits[0], y_limits[1], padding=0) y_concat = np.concatenate([np.asarray(v, dtype=np.float32) for v in y_candidates])
y0 = float(np.nanmin(y_concat))
y1 = float(np.nanmax(y_concat))
if np.isfinite(y0) and np.isfinite(y1):
margin = 0.05 * max(1.0, (y1 - y0))
p_line.setYRange(y0 - margin, y1 + margin, padding=0)
# Обновим спектр # Обновим спектр
sweep_for_fft = current_sweep_norm if current_sweep_norm is not None else current_sweep_raw sweep_for_fft = current_sweep_norm if current_sweep_norm is not None else current_sweep_raw