17 Commits

Author SHA1 Message Date
awe
f89dedf4ab add new data format support 2026-02-10 20:02:55 +03:00
awe
2f6e5d0dda fix median 2026-02-04 14:56:33 +03:00
awe
5978d58dc5 fix sub matplotlib 2026-02-04 14:38:55 +03:00
awe
e0f7678c3e backend upd 2026-02-04 14:13:01 +03:00
awe
60a35b690f fix debug 2026-02-03 19:13:33 +03:00
awe
6f5dfafbfb fix matplotlib 2026-02-03 19:06:26 +03:00
awe
3a072f0298 fix 3 2026-02-03 18:55:39 +03:00
awe
d8b71b2cc4 fix 2 2026-02-03 15:34:37 +03:00
awe
53ff80a522 fix 2026-02-03 15:33:09 +03:00
awe
a32bef2250 reference cli add 2026-02-03 15:24:07 +03:00
awe
61816cf894 reference 2026-02-03 15:10:22 +03:00
awe
3bc2382bd0 new fft 2026-02-03 14:40:39 +03:00
awe
2af6c8a486 new project structure 2026-02-03 14:17:06 +03:00
awe
0332ebdd98 global phase try 2 2026-01-30 15:50:40 +03:00
awe
e84c155e25 try to modern fft 2026-01-30 12:38:17 +03:00
awe
508c835368 add logging 2026-01-29 17:05:47 +03:00
awe
23cff76dd2 test fix for dropping points 2026-01-29 16:58:01 +03:00
39 changed files with 8603 additions and 4246 deletions

BIN
().npy

Binary file not shown.

12
.gitignore vendored
View File

@ -1,12 +0,0 @@
my_picocom_logfile.txt
*pyc
__pycache__/
*.log
*.tmp
*.bak
*.swp
*.swo
acm_9
build
.venv
sample_data

187
README.md Normal file
View File

@ -0,0 +1,187 @@
# RFG STM32 ADC Receiver GUI
Реалтайм-плоттер для визуализации данных FMCW радара, получаемых через виртуальный COM-порт от STM32 ADC.
## Описание
Приложение визуализирует данные в реальном времени, отображая 6 синхронизированных графиков:
1. **Сырые данные** - график последнего полученного свипа
2. **Водопад сырых данных** - временная серия последних N свипов
3. **FFT спектр** - спектр текущего свипа в частотной области
4. **B-scan** - спектрограмма (водопад FFT)
5. **Фаза спектра** - развернутая фаза для анализа расстояния
6. **Водопад фазы** - временная эволюция фазы
## Возможности
- ✅ Высокопроизводительная визуализация в реальном времени
- ✅ Два бэкенда визуализации: matplotlib (совместимость) и pyqtgraph (скорость)
- ✅ Автоматическая обработка фазы для FMCW радара
- ✅ Преобразование фазы в расстояние
- ✅ Поддержка pyserial или raw TTY доступа
- ✅ Заполнение пропущенных точек (режим --fancy)
- ✅ Инверсия сигнала при отрицательном уровне
- ✅ Диагностика потерь данных
## Установка
### Минимальные требования
```bash
pip install -r requirements.txt
```
### Зависимости
**Обязательные:**
- `numpy` - обработка массивов и FFT
- `matplotlib` - визуализация
**Опциональные (рекомендуется):**
- `pyserial` - доступ к serial порту (обязательно для Windows)
- `pyqtgraph` + `PyQt5` или `PySide6` - быстрый бэкенд визуализации
## Использование
### Базовый запуск
```bash
python -m rfg_adc_plotter.cli /dev/ttyACM0
```
### С параметрами
```bash
python -m rfg_adc_plotter.cli /dev/ttyACM0 \
--baud 115200 \
--max-sweeps 200 \
--max-fps 30 \
--backend pg \
--fancy
```
### Параметры командной строки
- `port` - путь к порту (например `/dev/ttyACM0`, `COM3`)
- `--baud` - скорость порта (по умолчанию 115200)
- `--max-sweeps` - количество свипов в водопаде (по умолчанию 200)
- `--max-fps` - ограничение частоты отрисовки (по умолчанию 30)
- `--cmap` - цветовая карта для водопадов (по умолчанию viridis)
- `--spec-clip` - процентильная обрезка контраста B-scan (по умолчанию 2,98)
- `--title` - заголовок окна (по умолчанию "ADC Sweeps")
- `--fancy` - заполнение пропущенных точек средними значениями
- `--ylim` - фиксированные пределы по Y (формат: min,max)
- `--backend` - бэкенд визуализации:
- `auto` - автоматический выбор (сначала pyqtgraph, fallback на matplotlib)
- `pg` - pyqtgraph (быстрее)
- `mpl` - matplotlib (совместимее)
## Формат данных
Приложение ожидает текстовые строки через serial порт:
```
Sweep_start
s 0 1234
s 1 1256
s 2 1278
...
Sweep_start
s 0 1235
...
```
- `Sweep_start` - начало нового свипа
- `s X Y` - точка данных (индекс X, значение Y), целые числа со знаком
## Архитектура проекта
```
rfg_adc_plotter/
├── __init__.py
├── config.py # Константы и типы
├── cli.py # Точка входа CLI
├── data_acquisition/
│ ├── __init__.py
│ ├── serial_io.py # Serial порт I/O
│ └── sweep_reader.py # Фоновый поток чтения данных
├── signal_processing/
│ ├── __init__.py
│ └── phase_analysis.py # Обработка фазы
├── visualization/
│ ├── __init__.py
│ ├── matplotlib_backend.py # Matplotlib визуализация
│ └── pyqtgraph_backend.py # PyQtGraph визуализация
└── utils/
├── __init__.py
└── formatting.py # Утилиты форматирования
```
## Технические особенности
### Оптимизации производительности
- Фоновый поток для чтения и парсинга данных
- Векторизованные numpy операции
- Кольцевые буферы для водопадов
- Неблокирующее чтение из serial порта
- Буферизация с увеличенным размером (256KB)
### Обработка сигналов
- **FFT анализ**: окно Хэннинга, длина 1024
- **Phase unwrapping**: адаптивный алгоритм с порогом 0.8π
- **Преобразование фазы в расстояние**: формула Δl = φ × c / (4π × ν)
- **Инверсия сигнала**: автоматическая при среднем уровне < порога
### Диагностика
Каждые 10 секунд в stderr выводится диагностическая информация:
- Номер свипа
- Среднее количество валидных точек
- Количество принятых строк
- Ошибки парсинга
- Ошибки чтения
- Размер буфера
- Потерянные свипы
## Примеры использования
### Linux с pyserial
```bash
python -m rfg_adc_plotter.cli /dev/ttyACM0 --backend pg
```
### Linux с raw TTY (без pyserial)
```bash
python -m rfg_adc_plotter.cli /dev/ttyACM0 --backend mpl
```
### Windows
```bash
python -m rfg_adc_plotter.cli COM3 --backend pg --baud 115200
```
### С высоким разрешением времени
```bash
python -m rfg_adc_plotter.cli /dev/ttyACM0 --max-sweeps 500 --max-fps 60
```
### С заполнением пропусков и фиксированным Y
```bash
python -m rfg_adc_plotter.cli /dev/ttyACM0 --fancy --ylim -2000,2000
```
## Лицензия
См. LICENSE файл в корне проекта.
## Авторы
Разработано для визуализации данных FMCW радара с STM32 ADC.

Binary file not shown.

Binary file not shown.

View File

@ -1,106 +0,0 @@
#!/usr/bin/env python3
"""
Эмулятор серийного порта: воспроизводит лог-файл в цикле через PTY.
Использование:
python3 replay_pty.py my_picocom_logfile.txt
python3 replay_pty.py my_picocom_logfile.txt --pty /tmp/ttyVIRT0
python3 replay_pty.py my_picocom_logfile.txt --speed 2.0 # в 2 раза быстрее реального
python3 replay_pty.py my_picocom_logfile.txt --speed 0 # максимально быстро
Затем в другом терминале:
python -m rfg_adc_plotter.main /tmp/ttyVIRT0
"""
import argparse
import os
import sys
import time
def main():
parser = argparse.ArgumentParser(
description="Воспроизводит лог-файл через PTY как виртуальный серийный порт."
)
parser.add_argument("file", help="Путь к лог-файлу (например my_picocom_logfile.txt)")
parser.add_argument(
"--pty",
default="/tmp/ttyVIRT0",
help="Путь симлинка PTY (по умолчанию /tmp/ttyVIRT0)",
)
parser.add_argument(
"--speed",
type=float,
default=1.0,
help=(
"Множитель скорости воспроизведения: "
"1.0 = реальное время при --baud, "
"2.0 = вдвое быстрее, "
"0 = максимально быстро"
),
)
parser.add_argument(
"--baud",
type=int,
default=115200,
help="Скорость (бод) для расчёта задержек (по умолчанию 115200)",
)
args = parser.parse_args()
if not os.path.isfile(args.file):
sys.stderr.write(f"[error] Файл не найден: {args.file}\n")
sys.exit(1)
# Открываем PTY-пару: master (мы пишем) / slave (GUI читает)
master_fd, slave_fd = os.openpty()
slave_path = os.ttyname(slave_fd)
os.close(slave_fd) # GUI откроет slave сам по симлинку
# Симлинк с удобным именем
try:
os.unlink(args.pty)
except FileNotFoundError:
pass
os.symlink(slave_path, args.pty)
print(f"PTY slave : {slave_path}")
print(f"Симлинк : {args.pty}{slave_path}")
print(f"Запустите : python -m rfg_adc_plotter.main {args.pty}")
print("Ctrl+C для остановки.\n")
# Задержка на байт: 10 бит (8N1) / скорость / множитель
if args.speed > 0:
bytes_per_sec = args.baud / 10.0 * args.speed
delay_per_byte = 1.0 / bytes_per_sec
else:
delay_per_byte = 0.0
_CHUNK = 4096
loop = 0
try:
while True:
loop += 1
print(f"[loop {loop}] {args.file}")
with open(args.file, "rb") as f:
while True:
chunk = f.read(_CHUNK)
if not chunk:
break
os.write(master_fd, chunk)
if delay_per_byte > 0:
time.sleep(delay_per_byte * len(chunk))
except KeyboardInterrupt:
print("\nОстановлено.")
finally:
try:
os.unlink(args.pty)
except Exception:
pass
try:
os.close(master_fd)
except Exception:
pass
if __name__ == "__main__":
main()

14
requirements.txt Normal file
View File

@ -0,0 +1,14 @@
# Основные зависимости
numpy>=1.20.0
# Визуализация (matplotlib - обязательна)
matplotlib>=3.3.0
# Serial порт (опционально, но рекомендуется)
pyserial>=3.5
# Быстрый бэкенд визуализации (опционально)
pyqtgraph>=0.12.0
PyQt5>=5.15.0
# Альтернатива PyQt5:
# PySide6>=6.0.0

110
rfg_adc_plotter/cli.py Normal file
View File

@ -0,0 +1,110 @@
#!/usr/bin/env python3
"""
Точка входа для RFG ADC Data Plotter.
Реалтайм-плоттер для свипов из виртуального COM-порта.
Формат строк:
- "Sweep_start" — начало нового свипа (предыдущий считается завершённым)
- "s X Y" — точка (индекс X, значение Y), все целые со знаком
Отрисовываются шесть графиков:
- Левый верхний: последний полученный свип (Y vs X)
- Правый верхний: водопад (последние N свипов во времени)
- Левый средний: FFT спектр текущего свипа
- Правый средний: B-scan (водопад FFT спектров)
- Левый нижний: Фаза спектра (развернутая)
- Правый нижний: Водопад фазы
Оптимизации для скорости:
- Парсинг и чтение в фоновой нити
- Анимация с обновлением только данных (без лишнего пересоздания фигур)
- Кольцевой буфер под водопад с фиксированным числом свипов
Зависимости: matplotlib, numpy. PySerial опционален — при его отсутствии
используется сырой доступ к TTY через termios.
"""
import argparse
import sys
def main():
"""Основная функция CLI."""
parser = argparse.ArgumentParser(
description=(
"Читает свипы из виртуального COM-порта и рисует: "
"последний свип и водопад (реалтайм)."
)
)
parser.add_argument(
"port",
help="Путь к порту, например /dev/ttyACM1 или COM3 (COM10+: \\\\.\\COM10)",
)
parser.add_argument("--baud", type=int, default=115200, help="Скорость (по умолчанию 115200)")
parser.add_argument("--max-sweeps", type=int, default=200, help="Количество видимых свипов в водопаде")
parser.add_argument("--max-fps", type=float, default=30.0, help="Лимит частоты отрисовки, кадров/с")
parser.add_argument("--cmap", default="viridis", help="Цветовая карта водопада")
parser.add_argument(
"--spec-clip",
default="2,98",
help=(
"Процентильная обрезка уровней водопада спектров, % (min,max). "
"Напр. 2,98. 'off' — отключить"
),
)
parser.add_argument("--title", default="ADC Sweeps", help="Заголовок окна")
parser.add_argument(
"--fancy",
action="store_true",
help="Заполнять выпавшие точки средними значениями между соседними",
)
parser.add_argument(
"--ylim",
type=str,
default=None,
help="Фиксированные Y-пределы для кривой формата min,max (например -1000,1000). По умолчанию авто",
)
parser.add_argument(
"--backend",
choices=["auto", "pg", "mpl"],
default="auto",
help="Графический бэкенд: pyqtgraph (pg) — быстрее; matplotlib (mpl) — совместимый. По умолчанию auto",
)
parser.add_argument(
"--ref-out",
type=str,
default=None,
help="Сохранить медиану последних 1000 свипов в указанный файл при накоплении данных",
)
parser.add_argument(
"--ref-in",
type=str,
default=None,
help="Загрузить медиану из файла и вычитать её из входящего сигнала",
)
args = parser.parse_args()
# Попробуем быстрый бэкенд (pyqtgraph) при auto/pg
if args.backend in ("auto", "pg"):
try:
from .visualization.pyqtgraph_backend import run_pyqtgraph
return run_pyqtgraph(args)
except Exception as e:
if args.backend == "pg":
sys.stderr.write(f"[error] PyQtGraph бэкенд недоступен: {e}\n")
sys.exit(1)
# При auto — тихо откатываемся на matplotlib
# Fallback на matplotlib
try:
from .visualization.matplotlib_backend import run_matplotlib
return run_matplotlib(args)
except Exception as e:
sys.stderr.write(f"[error] Matplotlib бэкенд недоступен: {e}\n")
sys.exit(1)
if __name__ == "__main__":
main()

28
rfg_adc_plotter/config.py Normal file
View File

@ -0,0 +1,28 @@
"""
Константы и типы для RFG ADC Data Plotter.
"""
from typing import Dict, Tuple, Union
import numpy as np
# Максимальное число точек в ряду водопада
WF_WIDTH = 1000
# Длина БПФ для спектра/водопада спектров
FFT_LEN = 2048
# Частотный диапазон для FFT (в ГГц)
FREQ_MIN_GHZ = -10.0 # Начало частотной оси
FREQ_MAX_GHZ = 10.0 # Конец частотной оси
DATA_FREQ_START_GHZ = 1.0 # Начало реальных данных
DATA_FREQ_END_GHZ = 10.0 # Конец реальных данных
# Порог для инверсии сырых данных: если среднее значение свипа ниже порога —
# считаем, что сигнал «меньше нуля» и домножаем свип на -1
DATA_INVERSION_THRASHOLD = 10.0
# Типы данных
Number = Union[int, float]
SweepInfo = Dict[str, Number]
SweepPacket = Tuple[np.ndarray, SweepInfo]

View File

@ -1,21 +0,0 @@
WF_WIDTH = 1000 # максимальное число точек в ряду водопада
FFT_LEN = 4096 # длина БПФ для спектра/водопада спектров
LOG_EXP = 2.0 # основание экспоненты для опции --logscale
# Порог для инверсии сырых данных: если среднее значение свипа ниже порога —
# считаем, что сигнал «меньше нуля» и домножаем свип на -1
DATA_INVERSION_THRESHOLD = 10.0
# Частотная сетка рабочего свипа (положительная часть), ГГц
FREQ_MIN_GHZ = 3.323
FREQ_MAX_GHZ = 14.323
# Скорость света для перевода времени пролёта в one-way depth
SPEED_OF_LIGHT_M_S = 299_792_458.0
# Параметры IFFT-спектра (временной профиль из спектра 3.2..14.3 ГГц)
# Двусторонний спектр формируется как: [нули -14.3..-3.2 | нули -3.2..+3.2 | данные +3.2..+14.3]
ZEROS_LOW = 758 # нули от -14.3 до -3.2 ГГц
ZEROS_MID = 437 # нули от -3.2 до +3.2 ГГц
SWEEP_LEN = 758 # ожидаемая длина свипа (3.2 → 14.3 ГГц)
FREQ_SPAN_GHZ = 28.6 # полная двусторонняя полоса (-14.3 .. +14.3 ГГц)
IFFT_LEN = ZEROS_LOW + ZEROS_MID + SWEEP_LEN # = 1953

View File

@ -1,4 +1,6 @@
"""Источники последовательного ввода: обёртки над pyserial и raw TTY."""
"""
Модули для работы с serial портом: чтение данных через pyserial или raw TTY.
"""
import io
import os
@ -7,12 +9,21 @@ from typing import Optional
def try_open_pyserial(path: str, baud: int, timeout: float):
"""Попытка открыть порт через pyserial."""
try:
import serial # type: ignore
except Exception:
return None
try:
ser = serial.Serial(path, baudrate=baud, timeout=timeout)
# ВРЕМЕННО ОТКЛЮЧЕН: hardware flow control для проверки
# ser.rtscts = True
# Увеличиваем буфер приема ядра до 64KB
try:
ser.set_buffer_size(rx_size=65536, tx_size=4096)
except (AttributeError, NotImplementedError):
# Не все платформы/версии pyserial поддерживают set_buffer_size
pass
return ser
except Exception:
return None
@ -22,10 +33,12 @@ class FDReader:
"""Простой враппер чтения строк из файлового дескриптора TTY."""
def __init__(self, fd: int):
# Отдельно буферизуем для корректной readline()
self._fd = fd
raw = os.fdopen(fd, "rb", closefd=False)
self._file = raw
self._buf = io.BufferedReader(raw, buffer_size=65536)
# Увеличен размер буфера до 256KB для предотвращения потерь
self._buf = io.BufferedReader(raw, buffer_size=262144)
def fileno(self) -> int:
return self._fd
@ -58,8 +71,10 @@ def open_raw_tty(path: str, baud: int) -> Optional[FDReader]:
try:
attrs = termios.tcgetattr(fd)
# Установим «сырое» состояние
tty.setraw(fd)
# Скорость
baud_map = {
9600: termios.B9600,
19200: termios.B19200,
@ -131,11 +146,13 @@ class SerialLineSource:
class SerialChunkReader:
"""Быстрое неблокирующее чтение чанков из serial/raw TTY для максимального дренажа буфера."""
def __init__(self, src: SerialLineSource):
def __init__(self, src: SerialLineSource, error_counter: Optional[list] = None):
self._src = src
self._ser = src._pyserial
self._fd: Optional[int] = None
self._error_counter = error_counter # Список с 1 элементом для передачи по ссылке
if self._ser is not None:
# Неблокирующий режим для быстрой откачки
try:
self._ser.timeout = 0
except Exception:
@ -156,11 +173,15 @@ class SerialChunkReader:
try:
n = int(getattr(self._ser, "in_waiting", 0))
except Exception:
if self._error_counter:
self._error_counter[0] += 1
n = 0
if n > 0:
try:
return self._ser.read(n)
except Exception:
if self._error_counter:
self._error_counter[0] += 1
return b""
return b""
if self._fd is None:
@ -177,5 +198,7 @@ class SerialChunkReader:
except BlockingIOError:
break
except Exception:
if self._error_counter:
self._error_counter[0] += 1
break
return bytes(out)

View File

@ -0,0 +1,269 @@
"""
Фоновый поток для чтения и сборки свипов из serial порта.
"""
import sys
import threading
import time
from collections import deque
from queue import Queue, Full
import numpy as np
from ..config import DATA_INVERSION_THRASHOLD, SweepInfo, SweepPacket
from .serial_io import SerialChunkReader, SerialLineSource
class SweepReader(threading.Thread):
"""Фоновый поток: читает строки, формирует завершённые свипы и кладёт в очередь."""
def __init__(
self,
port_path: str,
baud: int,
out_queue: Queue[SweepPacket],
stop_event: threading.Event,
fancy: bool = False,
):
super().__init__(daemon=True)
self._port_path = port_path
self._baud = baud
self._q = out_queue
self._stop = stop_event
self._src: SerialLineSource | None = None
self._fancy = bool(fancy)
self._max_width: int = 0
self._sweep_idx: int = 0
self._last_sweep_ts: float | None = None
self._n_valid_hist = deque()
# Счетчик потерь данных (выброшенных свипов из-за переполнения очереди)
self._dropped_sweeps: int = 0
# Диагностика потери точек внутри свипа
self._total_lines_received: int = 0 # Всего принято строк с данными
self._total_parse_errors: int = 0 # Ошибок парсинга строк
self._total_empty_lines: int = 0 # Пустых строк
self._max_buf_size: int = 0 # Максимальный размер буфера парсинга
self._read_errors: int = 0 # Ошибок чтения из порта
self._last_diag_time: float = 0.0 # Время последнего вывода диагностики
self._cal_mode: int = -1 # Режим калибровки (07), -1 = неизвестен
def _finalize_current(self, xs, ys, cal_mode: int = -1):
if not xs:
return
max_x = max(xs)
width = max_x + 1
self._max_width = max(self._max_width, width)
target_width = self._max_width if self._fancy else width
# Быстрый векторизованный путь
sweep = np.full((target_width,), np.nan, dtype=np.float32)
try:
idx = np.asarray(xs, dtype=np.int64)
vals = np.asarray(ys, dtype=np.float32)
sweep[idx] = vals
except Exception:
# Запасной путь
for x, y in zip(xs, ys):
if 0 <= x < target_width:
sweep[x] = float(y)
# Метрики валидных точек до заполнения пропусков
finite_pre = np.isfinite(sweep)
n_valid_cur = int(np.count_nonzero(finite_pre))
# Дополнительная обработка пропусков: при --fancy заполняем внутренние разрывы, края и дотягиваем до максимальной длины
if self._fancy:
try:
known = ~np.isnan(sweep)
if np.any(known):
known_idx = np.nonzero(known)[0]
# Для каждой пары соседних известных индексов заполним промежуток средним значением
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:
# В случае ошибки просто оставляем как есть
pass
# Инверсия данных при «отрицательном» уровне (среднее ниже порога)
try:
m = float(np.nanmean(sweep))
if np.isfinite(m) and m < DATA_INVERSION_THRASHOLD:
sweep *= -1.0
except Exception:
pass
sweep -= float(np.nanmean(sweep))
# Метрики для статусной строки (вид словаря: переменная -> значение)
self._sweep_idx += 1
now = time.time()
if self._last_sweep_ts is None:
dt_ms = float("nan")
else:
dt_ms = (now - self._last_sweep_ts) * 1000.0
self._last_sweep_ts = now
self._n_valid_hist.append((now, n_valid_cur))
while self._n_valid_hist and (now - self._n_valid_hist[0][0]) > 1.0:
self._n_valid_hist.popleft()
if self._n_valid_hist:
n_valid = float(sum(v for _t, v in self._n_valid_hist) / len(self._n_valid_hist))
else:
n_valid = float(n_valid_cur)
if n_valid_cur > 0:
vmin = float(np.nanmin(sweep))
vmax = float(np.nanmax(sweep))
mean = float(np.nanmean(sweep))
std = float(np.nanstd(sweep))
else:
vmin = vmax = mean = std = float("nan")
info: SweepInfo = {
"sweep": self._sweep_idx,
"n_valid": n_valid,
"min": vmin,
"max": vmax,
"mean": mean,
"std": std,
"dt_ms": dt_ms,
"dropped": self._dropped_sweeps,
"lines": self._total_lines_received,
"parse_err": self._total_parse_errors,
"read_err": self._read_errors,
"max_buf": self._max_buf_size,
"cal_mode": cal_mode,
}
# Периодический вывод детальной диагностики в stderr (каждые 10 секунд)
now = time.time()
if now - self._last_diag_time > 10.0:
self._last_diag_time = now
sys.stderr.write(
f"[DIAG] sweep={self._sweep_idx} n_valid={n_valid:.1f} "
f"lines={self._total_lines_received} parse_err={self._total_parse_errors} "
f"read_err={self._read_errors} max_buf={self._max_buf_size} "
f"dropped={self._dropped_sweeps}\n"
)
sys.stderr.flush()
# Кладём готовый свип (если очередь полна — выбрасываем самый старый)
try:
self._q.put_nowait((sweep, info))
except Full:
# Счетчик потерь для диагностики
self._dropped_sweeps += 1
try:
_ = self._q.get_nowait()
except Exception:
pass
try:
self._q.put_nowait((sweep, info))
except Exception:
pass
def run(self):
# Состояние текущего свипа
xs: list[int] = []
ys: list[int] = []
current_cal_mode: int = -1 # Режим калибровки для текущего свипа
try:
self._src = SerialLineSource(self._port_path, self._baud, timeout=1.0)
sys.stderr.write(f"[info] Открыл порт {self._port_path} ({self._src._using})\n")
except Exception as e:
sys.stderr.write(f"[error] {e}\n")
return
try:
# Быстрый неблокирующий дренаж порта с разбором по байтам
# Передаем счетчик ошибок чтения как список для изменения по ссылке
error_counter = [0]
chunk_reader = SerialChunkReader(self._src, error_counter)
buf = bytearray()
while not self._stop.is_set():
data = chunk_reader.read_available()
# Обновляем счетчик ошибок чтения
self._read_errors = error_counter[0]
if data:
buf += data
# Отслеживаем максимальный размер буфера парсинга
if len(buf) > self._max_buf_size:
self._max_buf_size = len(buf)
else:
# Короткая уступка CPU, если нет новых данных (уменьшена до 0.1ms)
time.sleep(0.0001)
continue
# Обрабатываем все полные строки
while True:
nl = buf.find(b"\n")
if nl == -1:
break
line = bytes(buf[:nl])
del buf[: nl + 1]
if line.endswith(b"\r"):
line = line[:-1]
if not line:
self._total_empty_lines += 1
continue
if line.startswith(b"Sweep_start"):
self._finalize_current(xs, ys, current_cal_mode)
xs.clear()
ys.clear()
current_cal_mode = -1
continue
# Формат строки данных: "sN X Y" или "s X Y"
# где N — цифра режима калибровки 07 (слитно с 's')
# X — индекс точки, Y — значение (целое со знаком)
if len(line) >= 3:
parts = line.split()
if parts and len(parts[0]) >= 1 and parts[0][:1].lower() == b"s":
tag = parts[0].lower() # b"s" или b"s0"..b"s7"
if len(tag) == 2 and b"0" <= tag[1:2] <= b"7":
# Новый формат: режим калибровки встроен в тег
current_cal_mode = int(tag[1:2])
data_parts = parts[1:]
elif len(tag) == 1:
# Старый формат: "s X Y"
data_parts = parts[1:]
else:
self._total_parse_errors += 1
continue
if len(data_parts) >= 2:
try:
x = int(data_parts[0], 10)
y = int(data_parts[1], 10)
except Exception:
self._total_parse_errors += 1
continue
xs.append(x)
ys.append(y)
self._total_lines_received += 1
else:
self._total_parse_errors += 1
else:
# Строка не начинается с 's'
self._total_parse_errors += 1
else:
# Строка слишком короткая
self._total_parse_errors += 1
# Защита от переполнения буфера при отсутствии переводов строки (снижен порог)
if len(buf) > 262144:
del buf[:-131072]
finally:
try:
# Завершаем оставшийся свип
self._finalize_current(xs, ys, current_cal_mode)
except Exception:
pass
try:
if self._src is not None:
self._src.close()
except Exception:
pass

View File

@ -1,680 +0,0 @@
"""Matplotlib-бэкенд реалтайм-плоттера свипов."""
import sys
import threading
from queue import Queue
from typing import Optional, Tuple
import numpy as np
from rfg_adc_plotter.constants import FFT_LEN, FREQ_MAX_GHZ, FREQ_MIN_GHZ, IFFT_LEN
from rfg_adc_plotter.io.sweep_reader import SweepReader
from rfg_adc_plotter.processing.normalizer import build_calib_envelopes
from rfg_adc_plotter.state.app_state import AppState, format_status
from rfg_adc_plotter.state.ring_buffer import RingBuffer
from rfg_adc_plotter.types import SweepPacket
def _parse_ylim(ylim_str: Optional[str]) -> Optional[Tuple[float, float]]:
if not ylim_str:
return None
try:
y0, y1 = ylim_str.split(",")
return (float(y0), float(y1))
except Exception:
sys.stderr.write("[warn] Некорректный формат --ylim, игнорирую. Ожидалось min,max\n")
return None
def _parse_spec_clip(spec: Optional[str]) -> Optional[Tuple[float, float]]:
if not spec:
return None
s = str(spec).strip().lower()
if s in ("off", "none", "no"):
return None
try:
p0, p1 = s.replace(";", ",").split(",")
low, high = float(p0), float(p1)
if not (0.0 <= low < high <= 100.0):
return None
return (low, high)
except Exception:
return None
def _visible_levels(data: np.ndarray, axis) -> Optional[Tuple[float, float]]:
"""(vmin, vmax) по текущей видимой области imshow."""
if data.size == 0:
return None
ny, nx = data.shape[0], data.shape[1]
try:
x0, x1 = axis.get_xlim()
y0, y1 = axis.get_ylim()
except Exception:
x0, x1 = 0.0, float(nx - 1)
y0, y1 = 0.0, float(ny - 1)
xmin, xmax = sorted((float(x0), float(x1)))
ymin, ymax = sorted((float(y0), float(y1)))
ix0 = max(0, min(nx - 1, int(np.floor(xmin))))
ix1 = max(0, min(nx - 1, int(np.ceil(xmax))))
iy0 = max(0, min(ny - 1, int(np.floor(ymin))))
iy1 = max(0, min(ny - 1, int(np.ceil(ymax))))
if ix1 < ix0:
ix1 = ix0
if iy1 < iy0:
iy1 = iy0
sub = data[iy0 : iy1 + 1, ix0 : ix1 + 1]
finite = np.isfinite(sub)
if not finite.any():
return None
vals = sub[finite]
vmin = float(np.min(vals))
vmax = float(np.max(vals))
if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax:
return None
return (vmin, vmax)
def run_matplotlib(args):
try:
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Button as MplButton
from matplotlib.widgets import CheckButtons, RadioButtons, Slider, TextBox
except Exception as e:
sys.stderr.write(f"[error] Нужны matplotlib и её зависимости: {e}\n")
sys.exit(1)
q: Queue[SweepPacket] = Queue(maxsize=1000)
stop_event = threading.Event()
reader = SweepReader(
args.port,
args.baud,
q,
stop_event,
fancy=bool(args.fancy),
bin_mode=bool(getattr(args, "bin_mode", False)),
logscale=bool(getattr(args, "logscale", False)),
debug=bool(getattr(args, "debug", False)),
)
reader.start()
max_sweeps = int(max(10, args.max_sweeps))
max_fps = max(1.0, float(args.max_fps))
interval_ms = int(1000.0 / max_fps)
spec_clip = _parse_spec_clip(getattr(args, "spec_clip", None))
spec_mean_sec = float(getattr(args, "spec_mean_sec", 0.0))
fixed_ylim = _parse_ylim(getattr(args, "ylim", None))
norm_type = str(getattr(args, "norm_type", "projector")).strip().lower()
logscale_enabled = bool(getattr(args, "logscale", False))
state = AppState(norm_type=norm_type)
state.configure_capture_import(fancy=bool(args.fancy), logscale=bool(getattr(args, "logscale", False)))
ring = RingBuffer(max_sweeps)
try:
ring.set_fft_complex_mode(str(getattr(args, "ifft_complex_mode", "arccos")))
except Exception:
pass
# --- Создание фигуры ---
fig, axs = plt.subplots(2, 2, figsize=(12, 8))
(ax_line, ax_img), (ax_fft, ax_spec) = axs
if hasattr(fig.canvas.manager, "set_window_title"):
fig.canvas.manager.set_window_title(args.title)
fig.subplots_adjust(wspace=0.25, hspace=0.35, left=0.07, right=0.90, top=0.92, bottom=0.22)
# Статусная строка
status_text = fig.text(0.01, 0.01, "", ha="left", va="bottom", fontsize=8, family="monospace")
pipeline_text = fig.text(0.01, 0.03, "", ha="left", va="bottom", fontsize=8, family="monospace")
ref_text = fig.text(0.01, 0.05, "", ha="left", va="bottom", fontsize=8, family="monospace")
# График последнего свипа
line_obj, = ax_line.plot([], [], lw=1, color="tab:blue")
line_norm_obj, = ax_line.plot([], [], lw=1, color="tab:green")
line_pre_exp_obj, = ax_line.plot([], [], lw=1, color="tab:red")
line_post_exp_obj, = ax_line.plot([], [], lw=1, color="tab:green")
line_env_lo, = ax_line.plot([], [], lw=1, color="tab:orange", linestyle="--", alpha=0.7)
line_env_hi, = ax_line.plot([], [], lw=1, color="tab:orange", linestyle="--", alpha=0.7)
ax_line.set_title("Сырые данные", pad=1)
ax_line.set_xlabel("Частота, ГГц")
channel_text = ax_line.text(
0.98, 0.98, "", transform=ax_line.transAxes,
ha="right", va="top", fontsize=9, family="monospace",
)
if fixed_ylim is not None:
ax_line.set_ylim(fixed_ylim)
# График спектра
fft_line_t1, = ax_fft.plot([], [], lw=1, color="tab:blue", label="1/3 (low f)")
fft_line_t2, = ax_fft.plot([], [], lw=1, color="tab:orange", label="2/3 (mid f)")
fft_line_t3, = ax_fft.plot([], [], lw=1, color="tab:green", label="3/3 (high f)")
ax_fft.set_title("FFT", pad=1)
ax_fft.set_xlabel("Глубина, м")
ax_fft.set_ylabel("Амплитуда")
ax_fft.legend(loc="upper right", fontsize=8)
# Водопад сырых данных
img_obj = ax_img.imshow(
np.zeros((1, 1), dtype=np.float32),
aspect="auto", interpolation="nearest", origin="lower", cmap=args.cmap,
)
ax_img.set_title("Сырые данные", pad=12)
ax_img.set_ylabel("частота")
try:
ax_img.tick_params(axis="x", labelbottom=False)
except Exception:
pass
# Водопад спектров
img_fft_obj = ax_spec.imshow(
np.zeros((1, 1), dtype=np.float32),
aspect="auto", interpolation="nearest", origin="lower", cmap=args.cmap,
)
ax_spec.set_title("B-scan", pad=12)
ax_spec.set_ylabel("Глубина, м")
try:
ax_spec.tick_params(axis="x", labelbottom=False)
except Exception:
pass
# Слайдеры и чекбокс
contrast_slider = None
try:
fft_bins = ring.fft_bins if ring.fft_bins > 0 else IFFT_LEN
ax_smin = fig.add_axes([0.92, 0.55, 0.02, 0.35])
ax_smax = fig.add_axes([0.95, 0.55, 0.02, 0.35])
ax_sctr = fig.add_axes([0.98, 0.55, 0.02, 0.35])
ax_cb = fig.add_axes([0.92, 0.45, 0.08, 0.08])
ax_cb_file = fig.add_axes([0.92, 0.36, 0.08, 0.08])
ax_line_mode = fig.add_axes([0.92, 0.10, 0.08, 0.08])
ax_ifft_mode = fig.add_axes([0.92, 0.01, 0.08, 0.08])
ymin_slider = Slider(ax_smin, "Y min", 0, max(1, fft_bins - 1), valinit=0, valstep=1, orientation="vertical")
ymax_slider = Slider(ax_smax, "Y max", 0, max(1, fft_bins - 1), valinit=max(1, fft_bins - 1), valstep=1, orientation="vertical")
contrast_slider = Slider(ax_sctr, "Int max", 0, 100, valinit=100, valstep=1, orientation="vertical")
calib_cb = CheckButtons(ax_cb, ["калибровка"], [False])
calib_file_cb = CheckButtons(ax_cb_file, ["из файла"], [False])
line_mode_rb = RadioButtons(ax_line_mode, ("raw", "processed"), active=0)
ifft_mode_rb = RadioButtons(
ax_ifft_mode,
("arccos", "diff"),
active=(1 if ring.fft_complex_mode == "diff" else 0),
)
try:
ax_line_mode.set_title("Линия", fontsize=8, pad=2)
except Exception:
pass
try:
ax_ifft_mode.set_title("IFFT", fontsize=8, pad=2)
except Exception:
pass
line_mode_state = {"value": "raw"}
ifft_mode_state = {"value": str(ring.fft_complex_mode)}
import os as _os
try:
import tkinter as _tk
from tkinter import filedialog as _tk_filedialog
_tk_available = True
except Exception:
_tk = None
_tk_filedialog = None
_tk_available = False
# Нижняя панель путей и кнопок (работает без Qt; выбор файла через tkinter опционален).
ax_calib_path = fig.add_axes([0.07, 0.14, 0.40, 0.04])
ax_calib_load = fig.add_axes([0.48, 0.14, 0.07, 0.04])
ax_calib_pick = fig.add_axes([0.56, 0.14, 0.06, 0.04])
ax_calib_sample = fig.add_axes([0.63, 0.14, 0.09, 0.04])
ax_calib_save = fig.add_axes([0.73, 0.14, 0.10, 0.04])
ax_bg_path = fig.add_axes([0.07, 0.09, 0.40, 0.04])
ax_bg_load = fig.add_axes([0.48, 0.09, 0.07, 0.04])
ax_bg_pick = fig.add_axes([0.56, 0.09, 0.06, 0.04])
ax_bg_sample = fig.add_axes([0.63, 0.09, 0.09, 0.04])
ax_bg_save2 = fig.add_axes([0.73, 0.09, 0.10, 0.04])
calib_path_box = TextBox(ax_calib_path, "Калибр", initial=state.calib_envelope_path)
bg_path_box = TextBox(ax_bg_path, "Фон", initial=state.background_path)
calib_load_btn2 = MplButton(ax_calib_load, "Загруз.")
calib_pick_btn2 = MplButton(ax_calib_pick, "Файл")
calib_sample_btn2 = MplButton(ax_calib_sample, "sample")
calib_save_btn2 = MplButton(ax_calib_save, "Сохр env")
bg_load_btn2 = MplButton(ax_bg_load, "Загруз.")
bg_pick_btn2 = MplButton(ax_bg_pick, "Файл")
bg_sample_btn2 = MplButton(ax_bg_sample, "sample")
bg_save_btn2 = MplButton(ax_bg_save2, "Сохр фон")
if not _tk_available:
try:
calib_pick_btn2.label.set_text("Файл-")
bg_pick_btn2.label.set_text("Файл-")
except Exception:
pass
def _tb_text(tb):
try:
return str(tb.text).strip()
except Exception:
return ""
def _pick_file_dialog(initial_path: str) -> str:
if not _tk_available or _tk is None or _tk_filedialog is None:
return ""
root = None
try:
root = _tk.Tk()
root.withdraw()
root.attributes("-topmost", True)
except Exception:
root = None
try:
return str(
_tk_filedialog.askopenfilename(
initialdir=_os.path.dirname(initial_path) or ".",
initialfile=_os.path.basename(initial_path) or "",
title="Выбрать файл эталона (.npy или capture)",
)
)
finally:
try:
if root is not None:
root.destroy()
except Exception:
pass
def _sync_path_boxes():
try:
if _tb_text(calib_path_box) != state.calib_envelope_path:
calib_path_box.set_val(state.calib_envelope_path)
except Exception:
pass
try:
if _tb_text(bg_path_box) != state.background_path:
bg_path_box.set_val(state.background_path)
except Exception:
pass
def _refresh_status_texts():
pipeline_text.set_text(f"{state.format_pipeline_status()} | cplx:{ring.fft_complex_mode}")
ref_text.set_text(state.format_reference_status())
try:
fig.canvas.draw_idle()
except Exception:
pass
def _line_mode() -> str:
return str(line_mode_state.get("value", "raw"))
def _refresh_checkboxes():
try:
# file-mode чекбокс показываем всегда; он активен при наличии пути/данных.
ax_cb_file.set_visible(True)
except Exception:
pass
def _load_calib_from_ui():
p = _tb_text(calib_path_box)
if p:
state.set_calib_envelope_path(p)
ok = state.load_calib_reference()
if ok and bool(calib_file_cb.get_status()[0]):
state.set_calib_mode("file")
state.set_calib_enabled(bool(calib_cb.get_status()[0]))
_sync_path_boxes()
_refresh_checkboxes()
_refresh_status_texts()
return ok
def _load_bg_from_ui():
p = _tb_text(bg_path_box)
if p:
state.set_background_path(p)
ok = state.load_background_reference()
_sync_path_boxes()
_refresh_status_texts()
return ok
def _on_ylim_change(_val):
try:
y0 = int(min(ymin_slider.val, ymax_slider.val))
y1 = int(max(ymin_slider.val, ymax_slider.val))
ax_spec.set_ylim(y0, y1)
fig.canvas.draw_idle()
except Exception:
pass
def _on_calib_file_clicked(_v):
use_file = bool(calib_file_cb.get_status()[0])
if use_file:
ok = _load_calib_from_ui()
if ok:
state.set_calib_mode("file")
else:
calib_file_cb.set_active(0) # снять галочку
else:
state.set_calib_mode("live")
state.set_calib_enabled(bool(calib_cb.get_status()[0]))
_refresh_status_texts()
def _on_calib_clicked(_v):
state.set_calib_enabled(bool(calib_cb.get_status()[0]))
_refresh_checkboxes()
_refresh_status_texts()
ax_btn_bg = fig.add_axes([0.92, 0.27, 0.08, 0.05])
ax_cb_bg = fig.add_axes([0.92, 0.20, 0.08, 0.06])
save_bg_btn = MplButton(ax_btn_bg, "Сохр. фон")
bg_cb = CheckButtons(ax_cb_bg, ["вычет фона"], [False])
def _on_save_bg(_event):
ok = state.save_background()
if ok:
state.load_background()
_sync_path_boxes()
_refresh_status_texts()
def _on_bg_clicked(_v):
state.set_background_enabled(bool(bg_cb.get_status()[0]))
_refresh_status_texts()
def _on_calib_load_btn(_event):
_load_calib_from_ui()
def _on_calib_pick_btn(_event):
path = _pick_file_dialog(_tb_text(calib_path_box) or state.calib_envelope_path)
if not path:
return
state.set_calib_envelope_path(path)
_sync_path_boxes()
_refresh_status_texts()
def _on_calib_sample_btn(_event):
state.set_calib_envelope_path(_os.path.join("sample_data", "no_antennas_35dB_attenuators"))
_sync_path_boxes()
if _load_calib_from_ui() and not bool(calib_file_cb.get_status()[0]):
calib_file_cb.set_active(0)
def _on_calib_save_btn(_event):
state.save_calib_envelope()
_sync_path_boxes()
_refresh_status_texts()
def _on_bg_load_btn(_event):
_load_bg_from_ui()
def _on_bg_pick_btn(_event):
path = _pick_file_dialog(_tb_text(bg_path_box) or state.background_path)
if not path:
return
state.set_background_path(path)
_sync_path_boxes()
_refresh_status_texts()
def _on_bg_sample_btn(_event):
state.set_background_path(_os.path.join("sample_data", "empty"))
_sync_path_boxes()
_load_bg_from_ui()
def _on_bg_save_btn2(_event):
ok = state.save_background()
if ok:
state.load_background()
_sync_path_boxes()
_refresh_status_texts()
def _on_line_mode_clicked(label):
line_mode_state["value"] = str(label)
try:
fig.canvas.draw_idle()
except Exception:
pass
def _on_ifft_mode_clicked(label):
ifft_mode_state["value"] = str(label)
try:
ring.set_fft_complex_mode(str(label))
except Exception:
pass
fft_line_t1.set_data([], [])
fft_line_t2.set_data([], [])
fft_line_t3.set_data([], [])
_refresh_status_texts()
try:
fig.canvas.draw_idle()
except Exception:
pass
save_bg_btn.on_clicked(_on_save_bg)
bg_cb.on_clicked(_on_bg_clicked)
calib_load_btn2.on_clicked(_on_calib_load_btn)
calib_pick_btn2.on_clicked(_on_calib_pick_btn)
calib_sample_btn2.on_clicked(_on_calib_sample_btn)
calib_save_btn2.on_clicked(_on_calib_save_btn)
bg_load_btn2.on_clicked(_on_bg_load_btn)
bg_pick_btn2.on_clicked(_on_bg_pick_btn)
bg_sample_btn2.on_clicked(_on_bg_sample_btn)
bg_save_btn2.on_clicked(_on_bg_save_btn2)
line_mode_rb.on_clicked(_on_line_mode_clicked)
ifft_mode_rb.on_clicked(_on_ifft_mode_clicked)
ymin_slider.on_changed(_on_ylim_change)
ymax_slider.on_changed(_on_ylim_change)
contrast_slider.on_changed(lambda _v: fig.canvas.draw_idle())
calib_cb.on_clicked(_on_calib_clicked)
calib_file_cb.on_clicked(_on_calib_file_clicked)
_sync_path_boxes()
_refresh_checkboxes()
_refresh_status_texts()
except Exception:
calib_cb = None
line_mode_state = {"value": "raw"}
ifft_mode_state = {"value": str(getattr(ring, "fft_complex_mode", "arccos"))}
FREQ_MIN = float(FREQ_MIN_GHZ)
FREQ_MAX = float(FREQ_MAX_GHZ)
def _fft_depth_max() -> float:
axis = ring.fft_depth_axis_m
if axis is None or axis.size == 0:
return 1.0
try:
vmax = float(axis[-1])
except Exception:
vmax = float(np.nanmax(axis))
if not np.isfinite(vmax) or vmax <= 0.0:
return 1.0
return vmax
# --- Инициализация imshow при первом свипе ---
def _init_imshow_extents():
w = ring.width
ms = ring.max_sweeps
fb = max(1, int(ring.fft_bins))
depth_max = _fft_depth_max()
img_obj.set_data(np.zeros((w, ms), dtype=np.float32))
img_obj.set_extent((0, ms - 1, FREQ_MIN, FREQ_MAX))
ax_img.set_xlim(0, ms - 1)
ax_img.set_ylim(FREQ_MIN, FREQ_MAX)
img_fft_obj.set_data(np.zeros((fb, ms), dtype=np.float32))
img_fft_obj.set_extent((0, ms - 1, 0.0, depth_max))
ax_spec.set_xlim(0, ms - 1)
ax_spec.set_ylim(0.0, depth_max)
ax_fft.set_xlim(0.0, depth_max)
_imshow_initialized = [False]
def update(_frame):
changed = state.drain_queue(q, ring) > 0
if changed and not _imshow_initialized[0] and ring.is_ready:
_init_imshow_extents()
_imshow_initialized[0] = True
# Линейный график свипа
if state.current_sweep_raw is not None:
raw = state.current_sweep_raw
if ring.x_shared is not None and raw.size <= ring.x_shared.size:
xs = ring.x_shared[: raw.size]
else:
xs = np.arange(raw.size, dtype=np.int32)
line_mode = str(line_mode_state.get("value", "raw"))
main = state.current_sweep_processed if line_mode == "processed" else raw
if main is not None:
line_obj.set_data(xs[: main.size], main)
else:
line_obj.set_data([], [])
if line_mode == "raw":
if state.calib_mode == "file" and state.calib_file_envelope is not None:
upper = np.asarray(state.calib_file_envelope, dtype=np.float32)
n_env = min(xs.size, upper.size)
if n_env > 0:
x_env = xs[:n_env]
y_env = upper[:n_env]
line_env_lo.set_data(x_env, -y_env)
line_env_hi.set_data(x_env, y_env)
else:
line_env_lo.set_data([], [])
line_env_hi.set_data([], [])
elif state.last_calib_sweep is not None:
calib = np.asarray(state.last_calib_sweep, dtype=np.float32)
lower, upper = build_calib_envelopes(calib)
n_env = min(xs.size, lower.size, upper.size)
if n_env > 0:
line_env_lo.set_data(xs[:n_env], lower[:n_env])
line_env_hi.set_data(xs[:n_env], upper[:n_env])
else:
line_env_lo.set_data([], [])
line_env_hi.set_data([], [])
else:
line_env_lo.set_data([], [])
line_env_hi.set_data([], [])
else:
line_env_lo.set_data([], [])
line_env_hi.set_data([], [])
if logscale_enabled:
if state.current_sweep_pre_exp is not None:
pre = state.current_sweep_pre_exp
line_pre_exp_obj.set_data(xs[: pre.size], pre)
else:
line_pre_exp_obj.set_data([], [])
post = state.current_sweep_post_exp if state.current_sweep_post_exp is not None else raw
line_post_exp_obj.set_data(xs[: post.size], post)
if line_mode == "processed":
if state.current_sweep_processed is not None:
proc = state.current_sweep_processed
line_obj.set_data(xs[: proc.size], proc)
else:
line_obj.set_data([], [])
else:
line_obj.set_data(xs[: raw.size], raw)
line_norm_obj.set_data([], [])
else:
line_pre_exp_obj.set_data([], [])
line_post_exp_obj.set_data([], [])
if line_mode == "raw" and state.current_sweep_norm is not None:
line_norm_obj.set_data(
xs[: state.current_sweep_norm.size], state.current_sweep_norm
)
else:
line_norm_obj.set_data([], [])
ax_line.set_xlim(FREQ_MIN, FREQ_MAX)
if fixed_ylim is not None:
ax_line.set_ylim(fixed_ylim)
else:
ax_line.relim()
ax_line.autoscale_view(scalex=False, scaley=True)
ax_line.set_ylabel("Y")
# Профиль по глубине: три линии для 1/3, 2/3, 3/3 частотного диапазона.
third_axes = ring.last_fft_third_axes_m
third_vals = ring.last_fft_third_vals
lines = (fft_line_t1, fft_line_t2, fft_line_t3)
xs_max = []
ys_min = []
ys_max = []
for line_fft, xs_fft, fft_vals in zip(lines, third_axes, third_vals):
if xs_fft is None or fft_vals is None:
line_fft.set_data([], [])
continue
n = min(int(xs_fft.size), int(fft_vals.size))
if n <= 0:
line_fft.set_data([], [])
continue
x_seg = xs_fft[:n]
y_seg = fft_vals[:n]
line_fft.set_data(x_seg, y_seg)
xs_max.append(float(x_seg[n - 1]))
ys_min.append(float(np.nanmin(y_seg)))
ys_max.append(float(np.nanmax(y_seg)))
if xs_max and ys_min and ys_max:
ax_fft.set_xlim(0, float(max(xs_max)))
ax_fft.set_ylim(float(min(ys_min)), float(max(ys_max)))
# Водопад сырых данных
if changed and ring.is_ready:
disp = ring.get_display_ring()
if ring.x_shared is not None:
n = ring.x_shared.size
disp = disp[:n, :]
img_obj.set_data(disp)
img_obj.set_extent((0, ring.max_sweeps - 1, FREQ_MIN, FREQ_MAX))
ax_img.set_ylim(FREQ_MIN, FREQ_MAX)
levels = _visible_levels(disp, ax_img)
if levels is not None:
img_obj.set_clim(vmin=levels[0], vmax=levels[1])
# Водопад спектров
if changed and ring.is_ready:
disp_fft = ring.get_display_ring_fft()
disp_fft = ring.subtract_recent_mean_fft(disp_fft, spec_mean_sec)
img_fft_obj.set_data(disp_fft)
depth_max = _fft_depth_max()
img_fft_obj.set_extent((0, ring.max_sweeps - 1, 0.0, depth_max))
ax_spec.set_ylim(0.0, depth_max)
levels = ring.compute_fft_levels(disp_fft, spec_clip)
if levels is not None:
try:
c = float(contrast_slider.val) / 100.0 if contrast_slider is not None else 1.0
except Exception:
c = 1.0
vmax_eff = levels[0] + c * (levels[1] - levels[0])
img_fft_obj.set_clim(vmin=levels[0], vmax=vmax_eff)
# Статус и подпись канала
if changed and state.current_info:
status_text.set_text(format_status(state.current_info))
channel_text.set_text(state.format_channel_label())
pipeline_text.set_text(f"{state.format_pipeline_status()} | cplx:{ring.fft_complex_mode}")
ref_text.set_text(state.format_reference_status())
elif changed:
pipeline_text.set_text(f"{state.format_pipeline_status()} | cplx:{ring.fft_complex_mode}")
ref_text.set_text(state.format_reference_status())
return (
line_obj,
line_norm_obj,
line_pre_exp_obj,
line_post_exp_obj,
line_env_lo,
line_env_hi,
img_obj,
fft_line_t1,
fft_line_t2,
fft_line_t3,
img_fft_obj,
status_text,
pipeline_text,
ref_text,
channel_text,
)
ani = FuncAnimation(fig, update, interval=interval_ms, blit=False)
plt.show()
stop_event.set()
reader.join(timeout=1.0)

View File

@ -1,711 +0,0 @@
"""PyQtGraph-бэкенд реалтайм-плоттера свипов."""
import os
import sys
import threading
from queue import Queue
from typing import Optional, Tuple
import numpy as np
from rfg_adc_plotter.constants import FREQ_MAX_GHZ, FREQ_MIN_GHZ
from rfg_adc_plotter.io.sweep_reader import SweepReader
from rfg_adc_plotter.processing.normalizer import build_calib_envelopes
from rfg_adc_plotter.state.app_state import AppState, format_status
from rfg_adc_plotter.state.ring_buffer import RingBuffer
from rfg_adc_plotter.types import SweepPacket
def _parse_ylim(ylim_str: Optional[str]) -> Optional[Tuple[float, float]]:
if not ylim_str:
return None
try:
y0, y1 = ylim_str.split(",")
return (float(y0), float(y1))
except Exception:
return None
def _parse_spec_clip(spec: Optional[str]) -> Optional[Tuple[float, float]]:
if not spec:
return None
s = str(spec).strip().lower()
if s in ("off", "none", "no"):
return None
try:
p0, p1 = s.replace(";", ",").split(",")
low, high = float(p0), float(p1)
if not (0.0 <= low < high <= 100.0):
return None
return (low, high)
except Exception:
return None
def _visible_levels(
data: np.ndarray,
plot_item,
freq_min: Optional[float] = None,
freq_max: Optional[float] = None,
) -> Optional[Tuple[float, float]]:
"""(vmin, vmax) по текущей видимой области ImageItem.
Если freq_min/freq_max заданы, ось Y трактуется как частота [freq_min..freq_max]
и пересчитывается в индексы строк данных.
"""
if data.size == 0:
return None
ny, nx = data.shape[0], data.shape[1]
try:
(x0, x1), (y0, y1) = plot_item.viewRange()
except Exception:
x0, x1 = 0.0, float(nx - 1)
y0, y1 = 0.0, float(ny - 1)
xmin, xmax = sorted((float(x0), float(x1)))
ymin, ymax = sorted((float(y0), float(y1)))
ix0 = max(0, min(nx - 1, int(np.floor(xmin))))
ix1 = max(0, min(nx - 1, int(np.ceil(xmax))))
if freq_min is not None and freq_max is not None and freq_max > freq_min:
span = freq_max - freq_min
iy0 = max(0, min(ny - 1, int(np.floor((ymin - freq_min) / span * ny))))
iy1 = max(0, min(ny - 1, int(np.ceil((ymax - freq_min) / span * ny))))
else:
iy0 = max(0, min(ny - 1, int(np.floor(ymin))))
iy1 = max(0, min(ny - 1, int(np.ceil(ymax))))
if ix1 < ix0:
ix1 = ix0
if iy1 < iy0:
iy1 = iy0
sub = data[iy0 : iy1 + 1, ix0 : ix1 + 1]
finite = np.isfinite(sub)
if not finite.any():
return None
vals = sub[finite]
vmin = float(np.min(vals))
vmax = float(np.max(vals))
if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax:
return None
return (vmin, vmax)
def _short_path(path: str, max_len: int = 48) -> str:
p = str(path or "").strip()
if not p:
return "(не задан)"
if len(p) <= max_len:
return p
base = os.path.basename(p)
if len(base) <= max_len:
return f".../{base}"
return "..." + p[-(max_len - 3) :]
def run_pyqtgraph(args):
"""Быстрый GUI на PyQtGraph. Требует pyqtgraph и PyQt5/PySide6."""
try:
import pyqtgraph as pg
from PyQt5 import QtCore, QtWidgets # noqa: F401
except Exception:
try:
import pyqtgraph as pg
from PySide6 import QtCore, QtWidgets # noqa: F401
except Exception as e:
raise RuntimeError(
"pyqtgraph/PyQt5(PySide6) не найдены. Установите: pip install pyqtgraph PyQt5"
) from e
q: Queue[SweepPacket] = Queue(maxsize=1000)
stop_event = threading.Event()
reader = SweepReader(
args.port,
args.baud,
q,
stop_event,
fancy=bool(args.fancy),
bin_mode=bool(getattr(args, "bin_mode", False)),
logscale=bool(getattr(args, "logscale", False)),
debug=bool(getattr(args, "debug", False)),
)
reader.start()
max_sweeps = int(max(10, args.max_sweeps))
max_fps = max(1.0, float(args.max_fps))
interval_ms = int(1000.0 / max_fps)
spec_clip = _parse_spec_clip(getattr(args, "spec_clip", None))
spec_mean_sec = float(getattr(args, "spec_mean_sec", 0.0))
fixed_ylim = _parse_ylim(getattr(args, "ylim", None))
norm_type = str(getattr(args, "norm_type", "projector")).strip().lower()
logscale_enabled = bool(getattr(args, "logscale", False))
state = AppState(norm_type=norm_type)
state.configure_capture_import(fancy=bool(args.fancy), logscale=bool(getattr(args, "logscale", False)))
ring = RingBuffer(max_sweeps)
try:
ring.set_fft_complex_mode(str(getattr(args, "ifft_complex_mode", "arccos")))
except Exception:
pass
try:
_qt_text_selectable = QtCore.Qt.TextSelectableByMouse
except Exception:
try:
_qt_text_selectable = QtCore.Qt.TextInteractionFlag.TextSelectableByMouse
except Exception:
_qt_text_selectable = None
# --- Создание окна ---
pg.setConfigOptions(useOpenGL=True, antialias=False)
app = pg.mkQApp(args.title)
win = pg.GraphicsLayoutWidget(show=True, title=args.title)
win.resize(1280, 760)
# График последнего свипа (слева-сверху)
p_line = win.addPlot(row=0, col=0, title="Сырые данные")
p_line.showGrid(x=True, y=True, alpha=0.3)
curve = p_line.plot(pen=pg.mkPen((80, 120, 255), width=1))
curve_norm = p_line.plot(pen=pg.mkPen((60, 180, 90), width=1))
curve_pre_exp = p_line.plot(pen=pg.mkPen((220, 60, 60), width=1))
curve_post_exp = p_line.plot(pen=pg.mkPen((60, 180, 90), width=1))
curve_env_lo = p_line.plot(pen=pg.mkPen((255, 165, 0), width=1, style=QtCore.Qt.DashLine))
curve_env_hi = p_line.plot(pen=pg.mkPen((255, 165, 0), width=1, style=QtCore.Qt.DashLine))
p_line.setLabel("bottom", "Частота, ГГц")
p_line.setLabel("left", "Y")
p_line.setXRange(3.323, 14.323, padding=0)
p_line.enableAutoRange(axis="x", enable=False)
ch_text = pg.TextItem("", anchor=(1, 1))
ch_text.setZValue(10)
p_line.addItem(ch_text)
if fixed_ylim is not None:
p_line.setYRange(fixed_ylim[0], fixed_ylim[1], padding=0)
# Водопад (справа-сверху)
p_img = win.addPlot(row=0, col=1, title="Сырые данные водопад")
p_img.invertY(False)
p_img.showGrid(x=False, y=False)
p_img.setLabel("bottom", "Время (новое справа)")
try:
p_img.getAxis("bottom").setStyle(showValues=False)
except Exception:
pass
p_img.setLabel("left", "Частота, ГГц")
p_img.enableAutoRange(enable=False)
img = pg.ImageItem()
p_img.addItem(img)
# Применяем LUT из цветовой карты
try:
cm = pg.colormap.get(args.cmap)
img.setLookupTable(cm.getLookupTable(0.0, 1.0, 256))
except Exception:
pass
# FFT (слева-снизу)
p_fft = win.addPlot(row=1, col=0, title="FFT")
p_fft.showGrid(x=True, y=True, alpha=0.3)
curve_fft_t1 = p_fft.plot(pen=pg.mkPen((80, 120, 255), width=1))
curve_fft_t2 = p_fft.plot(pen=pg.mkPen((255, 140, 70), width=1))
curve_fft_t3 = p_fft.plot(pen=pg.mkPen((60, 180, 90), width=1))
p_fft.setLabel("bottom", "Глубина, м")
p_fft.setLabel("left", "Амплитуда")
# Водопад спектров (справа-снизу)
p_spec = win.addPlot(row=1, col=1, title="B-scan")
p_spec.invertY(True)
p_spec.showGrid(x=False, y=False)
p_spec.setLabel("bottom", "Время (новое справа)")
try:
p_spec.getAxis("bottom").setStyle(showValues=False)
except Exception:
pass
p_spec.setLabel("left", "Глубина, м")
img_fft = pg.ImageItem()
p_spec.addItem(img_fft)
# Блок управления калибровкой
calib_widget = QtWidgets.QWidget()
calib_layout = QtWidgets.QVBoxLayout(calib_widget)
calib_layout.setContentsMargins(2, 2, 2, 2)
calib_layout.setSpacing(4)
calib_row_1 = QtWidgets.QHBoxLayout()
calib_row_1.setSpacing(8)
calib_row_2 = QtWidgets.QHBoxLayout()
calib_row_2.setSpacing(6)
calib_cb = QtWidgets.QCheckBox("калибровка")
calib_file_cb = QtWidgets.QCheckBox("из файла")
calib_file_cb.setEnabled(False)
calib_path_label = QtWidgets.QLabel()
calib_path_label.setMinimumWidth(260)
if _qt_text_selectable is not None:
calib_path_label.setTextInteractionFlags(_qt_text_selectable)
calib_pick_btn = QtWidgets.QPushButton("Файл…")
calib_load_btn = QtWidgets.QPushButton("Загрузить")
calib_save_btn = QtWidgets.QPushButton("Сохранить env")
calib_sample_btn = QtWidgets.QPushButton("sample calib")
calib_row_1.addWidget(calib_cb)
calib_row_1.addWidget(calib_file_cb)
calib_row_1.addStretch(1)
calib_row_2.addWidget(QtWidgets.QLabel("Калибр:"))
calib_row_2.addWidget(calib_path_label, 1)
calib_row_2.addWidget(calib_pick_btn)
calib_row_2.addWidget(calib_load_btn)
calib_row_2.addWidget(calib_save_btn)
calib_row_2.addWidget(calib_sample_btn)
calib_layout.addLayout(calib_row_1)
calib_layout.addLayout(calib_row_2)
cb_container_proxy = QtWidgets.QGraphicsProxyWidget()
cb_container_proxy.setWidget(calib_widget)
win.addItem(cb_container_proxy, row=2, col=1)
def _refresh_calib_controls():
calib_path_label.setText(_short_path(state.calib_envelope_path))
calib_path_label.setToolTip(state.calib_envelope_path)
calib_load_btn.setEnabled(bool(state.calib_envelope_path) and os.path.isfile(state.calib_envelope_path))
calib_save_btn.setEnabled(state.last_calib_sweep is not None)
# Переключатель file-mode доступен, если файл существует или уже загружен в память.
calib_file_cb.setEnabled(state.has_calib_envelope_file() or state.calib_file_envelope is not None)
def _on_calib_file_toggled(checked):
if checked:
ok = state.load_calib_reference()
if ok:
state.set_calib_mode("file")
else:
calib_file_cb.setChecked(False)
else:
state.set_calib_mode("live")
state.set_calib_enabled(calib_cb.isChecked())
_refresh_calib_controls()
_refresh_pipeline_label()
def _on_calib_toggled(_v):
state.set_calib_enabled(calib_cb.isChecked())
_refresh_calib_controls()
_refresh_pipeline_label()
def _on_pick_calib_path():
path, _ = QtWidgets.QFileDialog.getOpenFileName(
win,
"Выбрать источник калибровки (.npy или capture)",
state.calib_envelope_path,
"Все файлы (*);;NumPy (*.npy)",
)
if not path:
return
state.set_calib_envelope_path(path)
if calib_file_cb.isChecked():
if state.load_calib_reference():
state.set_calib_mode("file")
state.set_calib_enabled(calib_cb.isChecked())
else:
calib_file_cb.setChecked(False)
_refresh_calib_controls()
_refresh_pipeline_label()
def _on_load_calib():
if state.load_calib_reference():
if calib_file_cb.isChecked():
state.set_calib_mode("file")
state.set_calib_enabled(calib_cb.isChecked())
_refresh_calib_controls()
_refresh_pipeline_label()
def _on_save_calib():
if state.save_calib_envelope():
if calib_file_cb.isChecked():
state.load_calib_envelope()
state.set_calib_mode("file")
state.set_calib_enabled(calib_cb.isChecked())
_refresh_calib_controls()
_refresh_pipeline_label()
def _on_sample_calib():
sample_path = os.path.join("sample_data", "no_antennas_35dB_attenuators")
state.set_calib_envelope_path(sample_path)
if state.load_calib_reference():
calib_file_cb.setChecked(True)
state.set_calib_mode("file")
state.set_calib_enabled(calib_cb.isChecked())
_refresh_calib_controls()
_refresh_pipeline_label()
calib_cb.stateChanged.connect(_on_calib_toggled)
calib_file_cb.stateChanged.connect(lambda _v: _on_calib_file_toggled(calib_file_cb.isChecked()))
calib_pick_btn.clicked.connect(_on_pick_calib_path)
calib_load_btn.clicked.connect(_on_load_calib)
calib_save_btn.clicked.connect(_on_save_calib)
calib_sample_btn.clicked.connect(_on_sample_calib)
# Блок управления фоном
bg_widget = QtWidgets.QWidget()
bg_layout = QtWidgets.QVBoxLayout(bg_widget)
bg_layout.setContentsMargins(2, 2, 2, 2)
bg_layout.setSpacing(4)
bg_row_1 = QtWidgets.QHBoxLayout()
bg_row_1.setSpacing(8)
bg_row_2 = QtWidgets.QHBoxLayout()
bg_row_2.setSpacing(6)
save_bg_btn = QtWidgets.QPushButton("Сохранить фон")
load_bg_btn = QtWidgets.QPushButton("Загрузить")
bg_pick_btn = QtWidgets.QPushButton("Файл…")
bg_sample_btn = QtWidgets.QPushButton("sample bg")
bg_cb = QtWidgets.QCheckBox("вычет фона")
bg_cb.setEnabled(False) # активируется при успешной загрузке/сохранении
bg_path_label = QtWidgets.QLabel()
bg_path_label.setMinimumWidth(260)
if _qt_text_selectable is not None:
bg_path_label.setTextInteractionFlags(_qt_text_selectable)
bg_row_1.addWidget(bg_cb)
bg_row_1.addStretch(1)
bg_row_2.addWidget(QtWidgets.QLabel("Фон:"))
bg_row_2.addWidget(bg_path_label, 1)
bg_row_2.addWidget(bg_pick_btn)
bg_row_2.addWidget(load_bg_btn)
bg_row_2.addWidget(save_bg_btn)
bg_row_2.addWidget(bg_sample_btn)
bg_layout.addLayout(bg_row_1)
bg_layout.addLayout(bg_row_2)
bg_container_proxy = QtWidgets.QGraphicsProxyWidget()
bg_container_proxy.setWidget(bg_widget)
win.addItem(bg_container_proxy, row=2, col=0)
def _refresh_bg_controls():
bg_path_label.setText(_short_path(state.background_path))
bg_path_label.setToolTip(state.background_path)
load_bg_btn.setEnabled(bool(state.background_path) and os.path.isfile(state.background_path))
bg_cb.setEnabled(state.background is not None or state.background_source_type == "capture_raw")
def _on_pick_bg_path():
path, _ = QtWidgets.QFileDialog.getOpenFileName(
win,
"Выбрать источник фона (.npy или capture)",
state.background_path,
"Все файлы (*);;NumPy (*.npy)",
)
if not path:
return
state.set_background_path(path)
if bg_cb.isChecked():
if not state.load_background_reference():
bg_cb.setChecked(False)
_refresh_bg_controls()
_refresh_pipeline_label()
def _on_load_bg():
state.load_background_reference()
_refresh_bg_controls()
_refresh_pipeline_label()
def _on_save_bg():
ok = state.save_background()
if ok:
state.load_background()
_refresh_bg_controls()
_refresh_pipeline_label()
def _on_bg_toggled(_v):
state.set_background_enabled(bg_cb.isChecked())
_refresh_pipeline_label()
def _on_sample_bg():
sample_path = os.path.join("sample_data", "empty")
state.set_background_path(sample_path)
if state.load_background_reference():
bg_cb.setEnabled(True)
_refresh_bg_controls()
_refresh_pipeline_label()
bg_pick_btn.clicked.connect(_on_pick_bg_path)
load_bg_btn.clicked.connect(_on_load_bg)
save_bg_btn.clicked.connect(_on_save_bg)
bg_cb.stateChanged.connect(_on_bg_toggled)
bg_sample_btn.clicked.connect(_on_sample_bg)
# Переключатель отображения верхнего линейного графика
line_mode_widget = QtWidgets.QWidget()
line_mode_layout = QtWidgets.QHBoxLayout(line_mode_widget)
line_mode_layout.setContentsMargins(2, 2, 2, 2)
line_mode_layout.setSpacing(8)
line_mode_layout.addWidget(QtWidgets.QLabel("Линия:"))
line_mode_raw_rb = QtWidgets.QRadioButton("raw")
line_mode_proc_rb = QtWidgets.QRadioButton("processed")
line_mode_raw_rb.setChecked(True)
line_mode_layout.addWidget(line_mode_raw_rb)
line_mode_layout.addWidget(line_mode_proc_rb)
line_mode_layout.addStretch(1)
line_mode_proxy = QtWidgets.QGraphicsProxyWidget()
line_mode_proxy.setWidget(line_mode_widget)
win.addItem(line_mode_proxy, row=6, col=0, colspan=2)
def _line_mode() -> str:
return "processed" if line_mode_proc_rb.isChecked() else "raw"
# Переключатель режима реконструкции комплексного спектра перед IFFT
ifft_mode_widget = QtWidgets.QWidget()
ifft_mode_layout = QtWidgets.QHBoxLayout(ifft_mode_widget)
ifft_mode_layout.setContentsMargins(2, 2, 2, 2)
ifft_mode_layout.setSpacing(8)
ifft_mode_layout.addWidget(QtWidgets.QLabel("IFFT mode:"))
ifft_mode_arccos_rb = QtWidgets.QRadioButton("arccos")
ifft_mode_diff_rb = QtWidgets.QRadioButton("diff")
if ring.fft_complex_mode == "diff":
ifft_mode_diff_rb.setChecked(True)
else:
ifft_mode_arccos_rb.setChecked(True)
ifft_mode_layout.addWidget(ifft_mode_arccos_rb)
ifft_mode_layout.addWidget(ifft_mode_diff_rb)
ifft_mode_layout.addStretch(1)
ifft_mode_proxy = QtWidgets.QGraphicsProxyWidget()
ifft_mode_proxy.setWidget(ifft_mode_widget)
win.addItem(ifft_mode_proxy, row=7, col=0, colspan=2)
# Статусная строка
status = pg.LabelItem(justify="left")
win.addItem(status, row=3, col=0, colspan=2)
pipeline_status = pg.LabelItem(justify="left")
win.addItem(pipeline_status, row=4, col=0, colspan=2)
ref_status = pg.LabelItem(justify="left")
win.addItem(ref_status, row=5, col=0, colspan=2)
def _refresh_pipeline_label():
txt = state.format_pipeline_status()
txt = f"{txt} | cplx:{ring.fft_complex_mode}"
trace = state.format_stage_trace()
if trace:
txt = f"{txt} | trace: {trace}"
pipeline_status.setText(txt)
ref_status.setText(state.format_reference_status())
def _apply_ifft_complex_mode(mode: str):
try:
changed = ring.set_fft_complex_mode(mode)
except Exception:
changed = False
if changed:
try:
curve_fft_t1.setData([], [])
curve_fft_t2.setData([], [])
curve_fft_t3.setData([], [])
except Exception:
pass
_refresh_pipeline_label()
ifft_mode_arccos_rb.toggled.connect(
lambda checked: _apply_ifft_complex_mode("arccos") if checked else None
)
ifft_mode_diff_rb.toggled.connect(
lambda checked: _apply_ifft_complex_mode("diff") if checked else None
)
_refresh_calib_controls()
_refresh_bg_controls()
_refresh_pipeline_label()
_imshow_initialized = [False]
FREQ_MIN = float(FREQ_MIN_GHZ)
FREQ_MAX = float(FREQ_MAX_GHZ)
def _fft_depth_max() -> float:
axis = ring.fft_depth_axis_m
if axis is None or axis.size == 0:
return 1.0
try:
vmax = float(axis[-1])
except Exception:
vmax = float(np.nanmax(axis))
if not np.isfinite(vmax) or vmax <= 0.0:
return 1.0
return vmax
def _init_imshow_extents():
ms = ring.max_sweeps
img.setImage(ring.ring.T, autoLevels=False)
img.setRect(pg.QtCore.QRectF(0.0, FREQ_MIN, float(ms), FREQ_MAX - FREQ_MIN))
p_img.setRange(xRange=(0, ms - 1), yRange=(FREQ_MIN, FREQ_MAX), padding=0)
p_line.setXRange(FREQ_MIN, FREQ_MAX, padding=0)
disp_fft = ring.get_display_ring_fft()
img_fft.setImage(disp_fft, autoLevels=False)
depth_max = _fft_depth_max()
img_fft.setRect(pg.QtCore.QRectF(0.0, 0.0, float(ms), depth_max))
p_spec.setRange(xRange=(0, ms - 1), yRange=(0.0, depth_max), padding=0)
p_fft.setXRange(0.0, depth_max, padding=0)
def _img_rect(ms: int) -> "pg.QtCore.QRectF":
return pg.QtCore.QRectF(0.0, FREQ_MIN, float(ms), FREQ_MAX - FREQ_MIN)
def update():
changed = state.drain_queue(q, ring) > 0
if changed and not _imshow_initialized[0] and ring.is_ready:
_init_imshow_extents()
_imshow_initialized[0] = True
if changed:
_refresh_calib_controls()
_refresh_bg_controls()
_refresh_pipeline_label()
# Линейный график свипа
if state.current_sweep_raw is not None and ring.x_shared is not None:
raw = state.current_sweep_raw
xs = ring.x_shared[: raw.size] if raw.size <= ring.x_shared.size else np.arange(raw.size)
line_mode = _line_mode()
main = state.current_sweep_processed if line_mode == "processed" else raw
if main is not None:
curve.setData(xs[: main.size], main, autoDownsample=True)
else:
curve.setData([], [])
if line_mode == "raw":
if state.calib_mode == "file" and state.calib_file_envelope is not None:
upper = np.asarray(state.calib_file_envelope, dtype=np.float32)
n_env = min(xs.size, upper.size)
if n_env > 0:
x_env = xs[:n_env]
y_env = upper[:n_env]
curve_env_lo.setData(x_env, -y_env, autoDownsample=True)
curve_env_hi.setData(x_env, y_env, autoDownsample=True)
else:
curve_env_lo.setData([], [])
curve_env_hi.setData([], [])
elif state.last_calib_sweep is not None:
calib = np.asarray(state.last_calib_sweep, dtype=np.float32)
lower, upper = build_calib_envelopes(calib)
n_env = min(xs.size, upper.size, lower.size)
if n_env > 0:
curve_env_lo.setData(xs[:n_env], lower[:n_env], autoDownsample=True)
curve_env_hi.setData(xs[:n_env], upper[:n_env], autoDownsample=True)
else:
curve_env_lo.setData([], [])
curve_env_hi.setData([], [])
else:
curve_env_lo.setData([], [])
curve_env_hi.setData([], [])
else:
curve_env_lo.setData([], [])
curve_env_hi.setData([], [])
if logscale_enabled:
if state.current_sweep_pre_exp is not None:
pre = state.current_sweep_pre_exp
curve_pre_exp.setData(xs[: pre.size], pre, autoDownsample=True)
else:
curve_pre_exp.setData([], [])
post = state.current_sweep_post_exp if state.current_sweep_post_exp is not None else raw
curve_post_exp.setData(xs[: post.size], post, autoDownsample=True)
if line_mode == "processed":
if state.current_sweep_processed is not None:
proc = state.current_sweep_processed
curve.setData(xs[: proc.size], proc, autoDownsample=True)
else:
curve.setData([], [])
else:
curve.setData(xs[: raw.size], raw, autoDownsample=True)
curve_norm.setData([], [])
else:
curve_pre_exp.setData([], [])
curve_post_exp.setData([], [])
if line_mode == "raw" and state.current_sweep_norm is not None:
curve_norm.setData(
xs[: state.current_sweep_norm.size],
state.current_sweep_norm,
autoDownsample=True,
)
else:
curve_norm.setData([], [])
if fixed_ylim is not None:
p_line.setYRange(fixed_ylim[0], fixed_ylim[1], padding=0)
else:
p_line.enableAutoRange(axis="y", enable=True)
p_line.setLabel("left", "Y")
# Профиль по глубине: три линии для 1/3, 2/3, 3/3 частотного диапазона.
third_axes = ring.last_fft_third_axes_m
third_vals = ring.last_fft_third_vals
curves = (curve_fft_t1, curve_fft_t2, curve_fft_t3)
xs_max = []
ys_min = []
ys_max = []
for curve_fft, xs_fft, fft_vals in zip(curves, third_axes, third_vals):
if xs_fft is None or fft_vals is None:
curve_fft.setData([], [])
continue
n = min(int(xs_fft.size), int(fft_vals.size))
if n <= 0:
curve_fft.setData([], [])
continue
x_seg = xs_fft[:n]
y_seg = fft_vals[:n]
curve_fft.setData(x_seg, y_seg)
xs_max.append(float(x_seg[n - 1]))
ys_min.append(float(np.nanmin(y_seg)))
ys_max.append(float(np.nanmax(y_seg)))
if xs_max and ys_min and ys_max:
p_fft.setXRange(0.0, float(max(xs_max)), padding=0)
p_fft.setYRange(float(min(ys_min)), float(max(ys_max)), padding=0)
# Позиция подписи канала
try:
(x0, x1), (y0, y1) = p_line.viewRange()
dx = 0.01 * max(1.0, float(x1 - x0))
dy = 0.01 * max(1.0, float(y1 - y0))
ch_text.setPos(float(x1 - dx), float(y1 - dy))
except Exception:
pass
# Водопад сырых данных — новые данные справа (без реверса)
if changed and ring.is_ready:
disp = ring.get_display_ring() # (width, time), новые справа
levels = _visible_levels(disp, p_img, FREQ_MIN, FREQ_MAX)
if levels is not None:
img.setImage(disp, autoLevels=False, levels=levels)
else:
img.setImage(disp, autoLevels=False)
img.setRect(_img_rect(ring.max_sweeps))
# Статус и подпись канала
if changed and state.current_info:
try:
status.setText(format_status(state.current_info))
except Exception:
pass
ch_text.setText(state.format_channel_label())
elif changed:
_refresh_pipeline_label()
# Водопад спектров — новые данные справа (без реверса)
if changed and ring.is_ready:
disp_fft = ring.get_display_ring_fft() # (bins, time), новые справа
disp_fft = ring.subtract_recent_mean_fft(disp_fft, spec_mean_sec)
levels = ring.compute_fft_levels(disp_fft, spec_clip)
if levels is not None:
img_fft.setImage(disp_fft, autoLevels=False, levels=levels)
else:
img_fft.setImage(disp_fft, autoLevels=False)
img_fft.setRect(pg.QtCore.QRectF(0.0, 0.0, float(ring.max_sweeps), _fft_depth_max()))
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(interval_ms)
def on_quit():
stop_event.set()
reader.join(timeout=1.0)
app.aboutToQuit.connect(on_quit)
win.show()
exec_fn = getattr(app, "exec_", None) or getattr(app, "exec", None)
exec_fn()
on_quit()

View File

@ -1,227 +0,0 @@
"""Загрузка эталонов (калибровка/фон) из .npy или бинарных capture-файлов."""
from __future__ import annotations
from collections import Counter
from dataclasses import dataclass
import os
from typing import Iterable, List, Optional, Tuple
import numpy as np
from rfg_adc_plotter.io.sweep_parser_core import BinaryRecordStreamParser, SweepAssembler
from rfg_adc_plotter.types import SweepPacket
@dataclass(frozen=True)
class CaptureParseSummary:
path: str
format: str # "npy" | "bin_capture"
sweeps_total: int
sweeps_valid: int
channels_seen: Tuple[int, ...]
dominant_width: Optional[int]
dominant_n_valid: Optional[int]
aggregation: str
warnings: Tuple[str, ...]
@dataclass(frozen=True)
class ReferenceLoadResult:
vector: np.ndarray
summary: CaptureParseSummary
kind: str # "calibration_envelope" | "background_raw" | "background_processed"
source_type: str # "npy" | "capture"
def detect_reference_file_format(path: str) -> Optional[str]:
"""Определить формат файла эталона: .npy или бинарный capture."""
p = str(path).strip()
if not p or not os.path.isfile(p):
return None
if p.lower().endswith(".npy"):
return "npy"
try:
size = os.path.getsize(p)
except Exception:
return None
if size <= 0 or (size % 8) != 0:
return None
try:
with open(p, "rb") as f:
sample = f.read(min(size, 8 * 2048))
except Exception:
return None
if len(sample) < 8:
return None
# Быстрый sniff aligned-записей: в валидных записях байт 6 == 0x0A.
recs = len(sample) // 8
if recs <= 0:
return None
marker_hits = 0
start_hits = 0
for i in range(0, recs * 8, 8):
b = sample[i : i + 8]
if b[6] == 0x0A:
marker_hits += 1
if b[:6] == b"\xff\xff\xff\xff\xff\xff":
start_hits += 1
if marker_hits >= max(4, int(recs * 0.8)) and start_hits >= 1:
return "bin_capture"
return None
def load_capture_sweeps(path: str, *, fancy: bool = False, logscale: bool = False) -> List[SweepPacket]:
"""Загрузить свипы из бинарного capture-файла в формате --bin."""
parser = BinaryRecordStreamParser()
assembler = SweepAssembler(fancy=fancy, logscale=logscale, debug=False)
sweeps: List[SweepPacket] = []
with open(path, "rb") as f:
while True:
chunk = f.read(65536)
if not chunk:
break
events = parser.feed(chunk)
for ev in events:
packets = assembler.consume_binary_event(ev)
if packets:
sweeps.extend(packets)
tail = assembler.finalize_current()
if tail is not None:
sweeps.append(tail)
return sweeps
def _mode_int(values: Iterable[int]) -> Optional[int]:
vals = [int(v) for v in values]
if not vals:
return None
ctr = Counter(vals)
return int(max(ctr.items(), key=lambda kv: (kv[1], kv[0]))[0])
def aggregate_capture_reference(
sweeps: List[SweepPacket],
*,
channel: int = 0,
method: str = "median",
path: str = "",
) -> Tuple[np.ndarray, CaptureParseSummary]:
"""Отфильтровать и агрегировать свипы из capture в один эталонный вектор."""
ch_target = int(channel)
meth = str(method).strip().lower() or "median"
warnings: list[str] = []
if meth != "median":
warnings.append(f"aggregation '{meth}' не поддерживается, использую median")
meth = "median"
channels_seen: set[int] = set()
candidate_rows: list[np.ndarray] = []
widths: list[int] = []
n_valids: list[int] = []
for sweep, info in sweeps:
chs = info.get("chs") if isinstance(info, dict) else None
ch_set: set[int] = set()
if isinstance(chs, (list, tuple, set)):
for v in chs:
try:
ch_set.add(int(v))
except Exception:
pass
else:
try:
ch_set.add(int(info.get("ch", 0))) # type: ignore[union-attr]
except Exception:
pass
channels_seen.update(ch_set)
if ch_target not in ch_set:
continue
row = np.asarray(sweep, dtype=np.float32).reshape(-1)
candidate_rows.append(row)
widths.append(int(row.size))
n_valids.append(int(np.count_nonzero(np.isfinite(row))))
sweeps_total = len(sweeps)
if not candidate_rows:
summary = CaptureParseSummary(
path=path,
format="bin_capture",
sweeps_total=sweeps_total,
sweeps_valid=0,
channels_seen=tuple(sorted(channels_seen)),
dominant_width=None,
dominant_n_valid=None,
aggregation=meth,
warnings=tuple(warnings + [f"канал ch{ch_target} не найден"]),
)
raise ValueError(summary.warnings[-1])
dominant_width = _mode_int(widths)
dominant_n_valid = _mode_int(n_valids)
if dominant_width is None or dominant_n_valid is None:
summary = CaptureParseSummary(
path=path,
format="bin_capture",
sweeps_total=sweeps_total,
sweeps_valid=0,
channels_seen=tuple(sorted(channels_seen)),
dominant_width=dominant_width,
dominant_n_valid=dominant_n_valid,
aggregation=meth,
warnings=tuple(warnings + ["не удалось определить доминирующие параметры свипа"]),
)
raise ValueError(summary.warnings[-1])
valid_rows: list[np.ndarray] = []
n_valid_threshold = max(1, int(np.floor(0.95 * dominant_n_valid)))
for row in candidate_rows:
if row.size != dominant_width:
continue
n_valid = int(np.count_nonzero(np.isfinite(row)))
if n_valid < n_valid_threshold:
continue
valid_rows.append(row)
if not valid_rows:
warnings.append("после фильтрации не осталось валидных свипов")
summary = CaptureParseSummary(
path=path,
format="bin_capture",
sweeps_total=sweeps_total,
sweeps_valid=0,
channels_seen=tuple(sorted(channels_seen)),
dominant_width=dominant_width,
dominant_n_valid=dominant_n_valid,
aggregation=meth,
warnings=tuple(warnings),
)
raise ValueError(summary.warnings[-1])
# Детерминированная агрегация: медиана по валидным свипам.
stack = np.stack(valid_rows, axis=0).astype(np.float32, copy=False)
vector = np.nanmedian(stack, axis=0).astype(np.float32, copy=False)
if len(valid_rows) < len(candidate_rows):
warnings.append(f"отфильтровано {len(candidate_rows) - len(valid_rows)} неполных/нестандартных свипов")
summary = CaptureParseSummary(
path=path,
format="bin_capture",
sweeps_total=sweeps_total,
sweeps_valid=len(valid_rows),
channels_seen=tuple(sorted(channels_seen)),
dominant_width=dominant_width,
dominant_n_valid=dominant_n_valid,
aggregation=meth,
warnings=tuple(warnings),
)
return vector, summary

View File

@ -1,247 +0,0 @@
"""Переиспользуемые компоненты парсинга бинарных свипов и сборки SweepPacket."""
from __future__ import annotations
from collections import deque
import time
from typing import Iterable, List, Optional, Sequence, Set, Tuple
import numpy as np
from rfg_adc_plotter.constants import DATA_INVERSION_THRESHOLD, LOG_EXP
from rfg_adc_plotter.types import SweepInfo, SweepPacket
# Binary parser events:
# ("start", ch)
# ("point", ch, x, y)
BinaryEvent = Tuple[str, int] | Tuple[str, int, int, int]
def u32_to_i32(v: int) -> int:
"""Преобразование 32-bit слова в знаковое значение."""
return v - 0x1_0000_0000 if (v & 0x8000_0000) else v
class BinaryRecordStreamParser:
"""Инкрементальный парсер бинарных записей протокола (по 8 байт)."""
def __init__(self):
self._buf = bytearray()
self.bytes_consumed: int = 0
self.start_count: int = 0
self.point_count: int = 0
self.desync_count: int = 0
def feed(self, data: bytes) -> List[BinaryEvent]:
if data:
self._buf += data
events: List[BinaryEvent] = []
buf = self._buf
while len(buf) >= 8:
w0 = int(buf[0]) | (int(buf[1]) << 8)
w1 = int(buf[2]) | (int(buf[3]) << 8)
w2 = int(buf[4]) | (int(buf[5]) << 8)
if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and buf[6] == 0x0A:
ch = int(buf[7])
events.append(("start", ch))
del buf[:8]
self.bytes_consumed += 8
self.start_count += 1
continue
if buf[6] == 0x0A:
ch = int(buf[7])
value_u32 = (w1 << 16) | w2
events.append(("point", ch, int(w0), u32_to_i32(value_u32)))
del buf[:8]
self.bytes_consumed += 8
self.point_count += 1
continue
del buf[:1]
self.bytes_consumed += 1
self.desync_count += 1
return events
def buffered_size(self) -> int:
return len(self._buf)
def clear_buffer_keep_tail(self, max_tail: int = 262_144):
if len(self._buf) > max_tail:
del self._buf[:-max_tail]
class SweepAssembler:
"""Собирает точки в свип и применяет ту же постобработку, что realtime parser."""
def __init__(self, fancy: bool = False, logscale: bool = False, debug: bool = False):
self._fancy = bool(fancy)
self._logscale = bool(logscale)
self._debug = bool(debug)
self._max_width: int = 0
self._sweep_idx: int = 0
self._last_sweep_ts: Optional[float] = None
self._n_valid_hist = deque()
self._xs: list[int] = []
self._ys: list[int] = []
self._cur_channel: Optional[int] = None
self._cur_channels: set[int] = set()
def reset_current(self):
self._xs.clear()
self._ys.clear()
self._cur_channel = None
self._cur_channels.clear()
def add_point(self, ch: int, x: int, y: int):
if self._cur_channel is None:
self._cur_channel = int(ch)
self._cur_channels.add(int(ch))
self._xs.append(int(x))
self._ys.append(int(y))
def start_new_sweep(self, ch: int, now_ts: Optional[float] = None) -> Optional[SweepPacket]:
packet = self.finalize_current(now_ts=now_ts)
self.reset_current()
self._cur_channel = int(ch)
self._cur_channels.add(int(ch))
return packet
def consume_binary_event(self, event: BinaryEvent, now_ts: Optional[float] = None) -> List[SweepPacket]:
out: List[SweepPacket] = []
tag = event[0]
if tag == "start":
packet = self.start_new_sweep(int(event[1]), now_ts=now_ts)
if packet is not None:
out.append(packet)
return out
# point
_tag, ch, x, y = event # type: ignore[misc]
self.add_point(int(ch), int(x), int(y))
return out
def finalize_arrays(
self,
xs: Sequence[int],
ys: Sequence[int],
channels: Optional[Set[int]],
now_ts: Optional[float] = None,
) -> Optional[SweepPacket]:
if self._debug:
if not xs:
import sys
sys.stderr.write("[debug] _finalize_current: xs пуст — свип пропущен\n")
else:
import sys
sys.stderr.write(
f"[debug] _finalize_current: {len(xs)} точек → свип #{self._sweep_idx + 1}\n"
)
if not xs:
return None
ch_list = sorted(channels) if channels else [0]
ch_primary = ch_list[0] if ch_list else 0
max_x = max(int(v) for v in xs)
width = max_x + 1
self._max_width = max(self._max_width, width)
target_width = self._max_width if self._fancy else width
sweep = np.full((target_width,), np.nan, dtype=np.float32)
try:
idx = np.asarray(xs, dtype=np.int64)
vals = np.asarray(ys, dtype=np.float32)
sweep[idx] = vals
except Exception:
for x, y in zip(xs, ys):
xi = int(x)
if 0 <= xi < target_width:
sweep[xi] = float(y)
n_valid_cur = int(np.count_nonzero(np.isfinite(sweep)))
if self._fancy:
try:
known = ~np.isnan(sweep)
if np.any(known):
known_idx = np.nonzero(known)[0]
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:
pass
try:
m = float(np.nanmean(sweep))
if np.isfinite(m) and m < DATA_INVERSION_THRESHOLD:
sweep *= -1.0
except Exception:
pass
pre_exp_sweep = None
if self._logscale:
try:
pre_exp_sweep = sweep.copy()
with np.errstate(over="ignore", invalid="ignore"):
sweep = np.power(LOG_EXP, np.asarray(sweep, dtype=np.float64)).astype(np.float32)
sweep[~np.isfinite(sweep)] = np.nan
except Exception:
pass
self._sweep_idx += 1
if len(ch_list) > 1:
import sys
sys.stderr.write(f"[warn] Sweep {self._sweep_idx}: изменялся номер канала: {ch_list}\n")
now = float(time.time() if now_ts is None else now_ts)
if self._last_sweep_ts is None:
dt_ms = float("nan")
else:
dt_ms = (now - self._last_sweep_ts) * 1000.0
self._last_sweep_ts = now
self._n_valid_hist.append((now, n_valid_cur))
while self._n_valid_hist and (now - self._n_valid_hist[0][0]) > 1.0:
self._n_valid_hist.popleft()
if self._n_valid_hist:
n_valid = float(sum(v for _t, v in self._n_valid_hist) / len(self._n_valid_hist))
else:
n_valid = float(n_valid_cur)
if n_valid_cur > 0:
vmin = float(np.nanmin(sweep))
vmax = float(np.nanmax(sweep))
mean = float(np.nanmean(sweep))
std = float(np.nanstd(sweep))
else:
vmin = vmax = mean = std = float("nan")
info: SweepInfo = {
"sweep": self._sweep_idx,
"ch": ch_primary,
"chs": ch_list,
"n_valid": n_valid,
"min": vmin,
"max": vmax,
"mean": mean,
"std": std,
"dt_ms": dt_ms,
}
if pre_exp_sweep is not None:
info["pre_exp_sweep"] = pre_exp_sweep
return (sweep, info)
def finalize_current(self, now_ts: Optional[float] = None) -> Optional[SweepPacket]:
return self.finalize_arrays(self._xs, self._ys, self._cur_channels, now_ts=now_ts)

View File

@ -1,245 +0,0 @@
"""Фоновый поток чтения и парсинга свипов из последовательного порта."""
import sys
import threading
import time
from queue import Full, Queue
from typing import Optional
from rfg_adc_plotter.io.sweep_parser_core import BinaryRecordStreamParser, SweepAssembler
from rfg_adc_plotter.io.serial_source import SerialChunkReader, SerialLineSource
from rfg_adc_plotter.types import SweepPacket
class SweepReader(threading.Thread):
"""Фоновый поток: читает строки, формирует завершённые свипы и кладёт в очередь."""
def __init__(
self,
port_path: str,
baud: int,
out_queue: "Queue[SweepPacket]",
stop_event: threading.Event,
fancy: bool = False,
bin_mode: bool = False,
logscale: bool = False,
debug: bool = False,
):
super().__init__(daemon=True)
self._port_path = port_path
self._baud = baud
self._q = out_queue
self._stop = stop_event
self._src: Optional[SerialLineSource] = None
self._fancy = bool(fancy)
self._bin_mode = bool(bin_mode)
self._logscale = bool(logscale)
self._debug = bool(debug)
self._assembler = SweepAssembler(fancy=self._fancy, logscale=self._logscale, debug=self._debug)
def _finalize_current(self, xs, ys, channels: Optional[set]):
packet = self._assembler.finalize_arrays(xs, ys, channels)
if packet is None:
return
sweep, info = packet
try:
self._q.put_nowait((sweep, info))
except Full:
try:
_ = self._q.get_nowait()
except Exception:
pass
try:
self._q.put_nowait((sweep, info))
except Exception:
pass
def _run_ascii_stream(self, chunk_reader: SerialChunkReader):
xs: list[int] = []
ys: list[int] = []
cur_channel: Optional[int] = None
cur_channels: set[int] = set()
buf = bytearray()
_dbg_line_count = 0
_dbg_match_count = 0
_dbg_sweep_count = 0
while not self._stop.is_set():
data = chunk_reader.read_available()
if data:
buf += data
else:
time.sleep(0.0005)
continue
while True:
nl = buf.find(b"\n")
if nl == -1:
break
line = bytes(buf[:nl])
del buf[: nl + 1]
if line.endswith(b"\r"):
line = line[:-1]
if not line:
continue
_dbg_line_count += 1
if line.startswith(b"Sweep_start"):
if self._debug:
sys.stderr.write(f"[debug] ASCII строка #{_dbg_line_count}: Sweep_start → финализация свипа\n")
_dbg_sweep_count += 1
self._finalize_current(xs, ys, cur_channels)
xs.clear()
ys.clear()
cur_channel = None
cur_channels.clear()
continue
if len(line) >= 3:
parts = line.split()
if len(parts) >= 3 and (parts[0].lower() == b"s" or parts[0].lower().startswith(b"s")):
try:
if parts[0].lower() == b"s":
if len(parts) >= 4:
ch = int(parts[1], 10)
x = int(parts[2], 10)
y = int(parts[3], 10)
else:
ch = 0
x = int(parts[1], 10)
y = int(parts[2], 10)
else:
ch = int(parts[0][1:], 10)
x = int(parts[1], 10)
y = int(parts[2], 10)
except Exception:
if self._debug and _dbg_line_count <= 5:
hex_repr = " ".join(f"{b:02x}" for b in line[:16])
sys.stderr.write(
f"[debug] ASCII строка #{_dbg_line_count} ({len(line)} байт): {hex_repr}"
f"{'...' if len(line) > 16 else ''} → похожа на 's', но не парсится\n"
)
continue
_dbg_match_count += 1
if self._debug and _dbg_match_count <= 3:
sys.stderr.write(f"[debug] ASCII точка: ch={ch} x={x} y={y}\n")
if cur_channel is None:
cur_channel = ch
cur_channels.add(ch)
xs.append(x)
ys.append(y)
continue
if self._debug and _dbg_line_count <= 5:
hex_repr = " ".join(f"{b:02x}" for b in line[:16])
sys.stderr.write(
f"[debug] ASCII строка #{_dbg_line_count} ({len(line)} байт): {hex_repr}"
f"{'...' if len(line) > 16 else ''} → нет совпадения\n"
)
if self._debug and _dbg_line_count % 100 == 0:
sys.stderr.write(
f"[debug] ASCII статистика: строк={_dbg_line_count}, "
f"совпадений={_dbg_match_count}, свипов={_dbg_sweep_count}\n"
)
if len(buf) > 1_000_000:
del buf[:-262144]
self._finalize_current(xs, ys, cur_channels)
def _run_binary_stream(self, chunk_reader: SerialChunkReader):
xs: list[int] = []
ys: list[int] = []
cur_channel: Optional[int] = None
cur_channels: set[int] = set()
parser = BinaryRecordStreamParser()
# Бинарный протокол (4 слова LE u16 = 8 байт на запись):
# старт свипа: 0xFFFF, 0xFFFF, 0xFFFF, (ch<<8)|0x0A
# Байты на проводе: ff ff ff ff ff ff 0a [ch]
# ch=0 → последнее слово=0x000A; ch=1 → 0x010A; и т.д.
# точка данных: step_u16, value_hi_u16, value_lo_u16, (ch<<8)|0x0A
# Байты на проводе: [step_lo step_hi] [hi_lo hi_hi] [lo_lo lo_hi] 0a [ch]
# value_i32 = sign_extend((value_hi<<16)|value_lo)
# Признак записи: байт 6 == 0x0A, байт 7 — номер канала.
# При десинхронизации сдвигаемся на 1 БАЙТ (не слово) для самосинхронизации.
_dbg_byte_count = 0
_dbg_desync_count = 0
_dbg_sweep_count = 0
_dbg_point_count = 0
while not self._stop.is_set():
data = chunk_reader.read_available()
if data:
events = parser.feed(data)
else:
time.sleep(0.0005)
continue
for ev in events:
tag = ev[0]
if tag == "start":
ch_new = int(ev[1])
if self._debug:
sys.stderr.write(f"[debug] BIN: старт свипа, ch={ch_new}\n")
_dbg_sweep_count += 1
self._finalize_current(xs, ys, cur_channels)
xs.clear()
ys.clear()
cur_channels.clear()
cur_channel = ch_new
cur_channels.add(cur_channel)
continue
_tag, ch_from_term, step, value_i32 = ev # type: ignore[misc]
if cur_channel is None:
cur_channel = int(ch_from_term)
cur_channels.add(int(cur_channel))
xs.append(int(step))
ys.append(int(value_i32))
_dbg_point_count += 1
if self._debug and _dbg_point_count <= 3:
sys.stderr.write(
f"[debug] BIN точка: step={int(step)} ch={int(ch_from_term)} → value={int(value_i32)}\n"
)
_dbg_byte_count = parser.bytes_consumed
_dbg_desync_count = parser.desync_count
if self._debug and _dbg_byte_count > 0 and _dbg_byte_count % 4000 < 8:
sys.stderr.write(
f"[debug] BIN статистика: байт={_dbg_byte_count}, "
f"десинхронизаций={_dbg_desync_count}, точек={_dbg_point_count}, свипов={_dbg_sweep_count}\n"
)
if parser.buffered_size() > 1_000_000:
parser.clear_buffer_keep_tail(262_144)
self._finalize_current(xs, ys, cur_channels)
def run(self):
try:
self._src = SerialLineSource(self._port_path, self._baud, timeout=1.0)
sys.stderr.write(f"[info] Открыл порт {self._port_path} ({self._src._using})\n")
except Exception as e:
sys.stderr.write(f"[error] {e}\n")
return
try:
chunk_reader = SerialChunkReader(self._src)
if self._debug:
mode_str = "бинарный (--bin)" if self._bin_mode else "ASCII (по умолчанию)"
sys.stderr.write(f"[debug] Режим парсера: {mode_str}\n")
if self._bin_mode:
self._run_binary_stream(chunk_reader)
else:
self._run_ascii_stream(chunk_reader)
finally:
try:
if self._src is not None:
self._src.close()
except Exception:
pass

View File

@ -1,138 +0,0 @@
#!/usr/bin/env python3
"""
Реалтайм-плоттер для свипов из виртуального COM-порта.
Формат строк:
- "Sweep_start" — начало нового свипа (предыдущий считается завершённым)
- "s CH X Y" — точка (номер канала, индекс X, значение Y), все целые со знаком
Отрисовываются четыре графика:
- Сырые данные: последний полученный свип (Y vs X)
- Водопад сырых данных: последние N свипов
- FFT текущего свипа
- B-scan: водопад FFT-строк
Зависимости: numpy. PySerial опционален — при его отсутствии
используется сырой доступ к TTY через termios.
GUI: matplotlib (совместимый) или pyqtgraph (быстрый).
"""
import argparse
import sys
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description=(
"Читает свипы из виртуального COM-порта и рисует: "
"последний свип и водопад (реалтайм)."
)
)
parser.add_argument(
"port",
help="Путь к порту, например /dev/ttyACM1 или COM3 (COM10+: \\\\.\\COM10)",
)
parser.add_argument("--baud", type=int, default=115200, help="Скорость (по умолчанию 115200)")
parser.add_argument("--max-sweeps", type=int, default=200, help="Количество видимых свипов в водопаде")
parser.add_argument("--max-fps", type=float, default=30.0, help="Лимит частоты отрисовки, кадров/с")
parser.add_argument("--cmap", default="viridis", help="Цветовая карта водопада")
parser.add_argument(
"--spec-clip",
default="2,98",
help=(
"Процентильная обрезка уровней водопада спектров, %% (min,max). "
"Напр. 2,98. 'off' — отключить"
),
)
parser.add_argument(
"--spec-mean-sec",
type=float,
default=0.0,
help=(
"Вычитание среднего по каждой частоте за последние N секунд "
"в водопаде спектров (0 — отключить)"
),
)
parser.add_argument("--title", default="ADC Sweeps", help="Заголовок окна")
parser.add_argument(
"--fancy",
action="store_true",
help="Заполнять выпавшие точки средними значениями между соседними",
)
parser.add_argument(
"--ylim",
type=str,
default=None,
help="Фиксированные Y-пределы для кривой формата min,max (например -1000,1000). По умолчанию авто",
)
parser.add_argument(
"--backend",
choices=["auto", "pg", "mpl"],
default="auto",
help="Графический бэкенд: pyqtgraph (pg) — быстрее; matplotlib (mpl) — совместимый. По умолчанию auto",
)
parser.add_argument(
"--norm-type",
choices=["projector", "simple"],
default="projector",
help="Тип нормировки: projector (по огибающим в [-1000,+1000]) или simple (raw/calib)",
)
parser.add_argument(
"--ifft-complex-mode",
choices=["arccos", "diff"],
default="arccos",
help=(
"Режим реконструкции комплексного спектра перед IFFT: "
"arccos (phi=arccos(x), unwrap) или diff (sin(phi) через численную производную)"
),
)
parser.add_argument(
"--bin",
dest="bin_mode",
action="store_true",
help=(
"Бинарный протокол (8 байт на запись, LE u16 слова): "
"старт свипа ff ff ff ff ff ff 0a [ch]; "
"точка step_u16 hi_u16 lo_u16 0a [ch]; "
"value=sign_ext((hi<<16)|lo); ch=0..N в старшем байте маркера"
),
)
parser.add_argument(
"--logscale",
action="store_true",
help="После поправки знака применять экспоненту LOG_EXP**x (LOG_EXP=2)",
)
parser.add_argument(
"--debug",
action="store_true",
help="Отладочный вывод парсера: показывает принятые строки/слова и причины отсутствия свипов",
)
return parser
def main():
args = build_parser().parse_args()
if args.backend == "pg":
from rfg_adc_plotter.gui.pyqtgraph_backend import run_pyqtgraph
try:
run_pyqtgraph(args)
except Exception as e:
sys.stderr.write(f"[error] PyQtGraph бэкенд недоступен: {e}\n")
sys.exit(1)
return
if args.backend == "auto":
try:
from rfg_adc_plotter.gui.pyqtgraph_backend import run_pyqtgraph
run_pyqtgraph(args)
return
except Exception:
pass # Откатываемся на matplotlib
from rfg_adc_plotter.gui.matplotlib_backend import run_matplotlib
run_matplotlib(args)
if __name__ == "__main__":
main()

View File

@ -1,300 +0,0 @@
"""Преобразование свипа в IFFT-профиль по глубине (м).
Поддерживает несколько режимов восстановления комплексного спектра перед IFFT:
- ``arccos``: phi = arccos(x), continuous unwrap, z = exp(1j*phi)
- ``diff``: x ~= cos(phi), diff(x) -> sin(phi), z = cos + 1j*sin (с проекцией на единичную окружность)
"""
from __future__ import annotations
import logging
from typing import Optional
import numpy as np
from rfg_adc_plotter.constants import (
FREQ_MAX_GHZ,
FREQ_MIN_GHZ,
FREQ_SPAN_GHZ,
IFFT_LEN,
SPEED_OF_LIGHT_M_S,
)
logger = logging.getLogger(__name__)
_EPS = 1e-12
_TWO_PI = float(2.0 * np.pi)
_VALID_COMPLEX_MODES = {"arccos", "diff"}
def _fallback_depth_response(
size: int,
values: Optional[np.ndarray] = None,
) -> tuple[np.ndarray, np.ndarray]:
"""Безопасный fallback для GUI/ring: всегда возвращает ненулевую длину."""
n = max(1, int(size))
depth = np.linspace(0.0, 1.0, n, dtype=np.float32)
if values is None:
return depth, np.zeros((n,), dtype=np.float32)
arr = np.asarray(values)
if arr.size == 0:
return depth, np.zeros((n,), dtype=np.float32)
if np.iscomplexobj(arr):
src = np.abs(arr)
else:
src = np.abs(np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0))
src = np.asarray(src, dtype=np.float32).ravel()
out = np.zeros((n,), dtype=np.float32)
take = min(n, src.size)
if take > 0:
out[:take] = src[:take]
return depth, out
def _normalize_complex_mode(mode: str) -> str:
m = str(mode).strip().lower()
if m not in _VALID_COMPLEX_MODES:
raise ValueError(f"Invalid complex reconstruction mode: {mode!r}")
return m
def build_ifft_time_axis_ns() -> np.ndarray:
"""Legacy helper: старая временная ось IFFT в наносекундах (фиксированная длина)."""
return (
np.arange(IFFT_LEN, dtype=np.float64) / (FREQ_SPAN_GHZ * 1e9) * 1e9
).astype(np.float32)
def build_frequency_axis_hz(sweep_width: int) -> np.ndarray:
"""Построить частотную сетку (Гц) для текущей длины свипа."""
n = int(sweep_width)
if n <= 0:
return np.zeros((0,), dtype=np.float64)
if n == 1:
return np.array([FREQ_MIN_GHZ * 1e9], dtype=np.float64)
return np.linspace(FREQ_MIN_GHZ * 1e9, FREQ_MAX_GHZ * 1e9, n, dtype=np.float64)
def normalize_trace_unit_range(x: np.ndarray) -> np.ndarray:
"""Signed-нормировка массива по max(abs(.)) в диапазон около [-1, 1]."""
arr = np.asarray(x, dtype=np.float64).ravel()
if arr.size == 0:
return arr
arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
amax = float(np.max(np.abs(arr)))
if (not np.isfinite(amax)) or amax <= _EPS:
return np.zeros_like(arr, dtype=np.float64)
return arr / amax
def normalize_sweep_for_phase(sweep: np.ndarray) -> np.ndarray:
"""Совместимый alias: нормировка свипа перед восстановлением фазы."""
return normalize_trace_unit_range(sweep)
def unwrap_arccos_phase_continuous(x_norm: np.ndarray) -> np.ndarray:
"""Непрерывно развернуть фазу, восстановленную через arccos.
Для каждой точки рассматриваются ветви ±phi + 2πk и выбирается кандидат,
ближайший к предыдущей фазе (nearest continuous).
"""
x = np.asarray(x_norm, dtype=np.float64).ravel()
if x.size == 0:
return np.zeros((0,), dtype=np.float64)
x = np.nan_to_num(x, nan=0.0, posinf=1.0, neginf=-1.0)
x = np.clip(x, -1.0, 1.0)
phi0 = np.arccos(x)
out = np.empty_like(phi0, dtype=np.float64)
out[0] = float(phi0[0])
for i in range(1, phi0.size):
base_phi = float(phi0[i])
prev = float(out[i - 1])
best_cand: Optional[float] = None
best_key: Optional[tuple[float, float]] = None
for sign in (1.0, -1.0):
base = sign * base_phi
k_center = int(np.round((prev - base) / _TWO_PI))
for k in (k_center - 1, k_center, k_center + 1):
cand = base + _TWO_PI * float(k)
step = abs(cand - prev)
# Tie-break: при равенстве шага предпочесть больший кандидат.
key = (step, -cand)
if best_key is None or key < best_key:
best_key = key
best_cand = cand
out[i] = prev if best_cand is None else float(best_cand)
return out
def reconstruct_complex_spectrum_arccos(sweep: np.ndarray) -> np.ndarray:
"""Режим arccos: cos(phi) -> phi -> exp(i*phi)."""
x_norm = normalize_trace_unit_range(sweep)
if x_norm.size == 0:
return np.zeros((0,), dtype=np.complex128)
phi = unwrap_arccos_phase_continuous(np.clip(x_norm, -1.0, 1.0))
return np.exp(1j * phi).astype(np.complex128, copy=False)
def reconstruct_complex_spectrum_diff(sweep: np.ndarray) -> np.ndarray:
"""Режим diff: x~=cos(phi), diff(x)->sin(phi), z=cos+i*sin с проекцией на единичную окружность."""
cos_phi = normalize_trace_unit_range(sweep)
if cos_phi.size == 0:
return np.zeros((0,), dtype=np.complex128)
cos_phi = np.clip(cos_phi, -1.0, 1.0)
if cos_phi.size < 2:
sin_est = np.zeros_like(cos_phi, dtype=np.float64)
else:
d = np.gradient(cos_phi)
sin_est = normalize_trace_unit_range(d)
sin_est = np.clip(sin_est, -1.0, 1.0)
sin_est = normalize_trace_unit_range(d)
# mag = np.abs(sin_est)
# mask = mag > _EPS
# if np.any(mask):
# sin_est[mask] = sin_est[mask] / mag[mask]
z = cos_phi.astype(np.complex128, copy=False) + 1j * sin_est.astype(np.complex128, copy=False)
mag = np.abs(z)
z_unit = np.ones_like(z, dtype=np.complex128)
mask = mag > _EPS
if np.any(mask):
z_unit[mask] = z[mask] / mag[mask]
return z_unit
def reconstruct_complex_spectrum_from_real_trace(
sweep: np.ndarray,
*,
complex_mode: str = "arccos",
) -> np.ndarray:
"""Восстановить комплексный спектр из вещественного свипа в выбранном режиме."""
mode = _normalize_complex_mode(complex_mode)
if mode == "arccos":
return reconstruct_complex_spectrum_arccos(sweep)
if mode == "diff":
return reconstruct_complex_spectrum_diff(sweep)
raise ValueError(f"Unsupported complex reconstruction mode: {complex_mode!r}")
def perform_ifft_depth_response(
s_array: np.ndarray,
frequencies_hz: np.ndarray,
*,
axis: str = "abs",
start_hz: float | None = None,
stop_hz: float | None = None,
) -> tuple[np.ndarray, np.ndarray]:
"""Frequency-to-depth conversion with zero-padding and frequency offset handling."""
try:
s_in = np.asarray(s_array, dtype=np.complex128).ravel()
f_in = np.asarray(frequencies_hz, dtype=np.float64).ravel()
m = min(s_in.size, f_in.size)
if m < 2:
raise ValueError("Not enough points")
s = s_in[:m]
f = f_in[:m]
lo = float(FREQ_MIN_GHZ * 1e9 if start_hz is None else start_hz)
hi = float(FREQ_MAX_GHZ * 1e9 if stop_hz is None else stop_hz)
if hi < lo:
lo, hi = hi, lo
mask = (
np.isfinite(f)
& np.isfinite(np.real(s))
& np.isfinite(np.imag(s))
& (f >= lo)
& (f <= hi)
)
f = f[mask]
s = s[mask]
n = int(f.size)
if n < 2:
raise ValueError("Not enough frequency points after filtering")
if np.any(np.diff(f) <= 0.0):
raise ValueError("Non-increasing frequency grid")
df = float((f[-1] - f[0]) / (n - 1))
if not np.isfinite(df) or df <= 0.0:
raise ValueError("Invalid frequency step")
k0 = int(np.round(float(f[0]) / df))
if k0 < 0:
raise ValueError("Negative frequency offset index")
min_len = int(2 * (k0 + n - 1))
if min_len <= 0:
raise ValueError("Invalid FFT length")
n_fft = 1 << int(np.ceil(np.log2(float(min_len))))
dt = 1.0 / (n_fft * df)
t_sec = np.arange(n_fft, dtype=np.float64) * dt
h = np.zeros((n_fft,), dtype=np.complex128)
end = k0 + n
if end > n_fft:
raise ValueError("Spectrum placement exceeds FFT buffer")
h[k0:end] = s
y = np.fft.ifft(h)
depth_m = t_sec * SPEED_OF_LIGHT_M_S
axis_name = str(axis).strip().lower()
if axis_name == "abs":
y_fin = np.abs(y)
elif axis_name == "real":
y_fin = np.real(y)
elif axis_name == "imag":
y_fin = np.imag(y)
elif axis_name == "phase":
y_fin = np.angle(y)
else:
raise ValueError(f"Invalid axis parameter: {axis!r}")
return depth_m.astype(np.float32, copy=False), np.asarray(y_fin, dtype=np.float32)
except Exception as exc: # noqa: BLE001
logger.error("IFFT depth response failed: %r", exc)
return _fallback_depth_response(np.asarray(s_array).size, np.asarray(s_array))
def compute_ifft_profile_from_sweep(
sweep: Optional[np.ndarray],
*,
complex_mode: str = "arccos",
) -> tuple[np.ndarray, np.ndarray]:
"""Высокоуровневый pipeline: sweep -> complex spectrum -> IFFT(abs) depth profile."""
if sweep is None:
return _fallback_depth_response(1, None)
try:
s = np.asarray(sweep, dtype=np.float64).ravel()
if s.size == 0:
return _fallback_depth_response(1, None)
freqs_hz = build_frequency_axis_hz(s.size)
s_complex = reconstruct_complex_spectrum_from_real_trace(s, complex_mode=complex_mode)
depth_m, y = perform_ifft_depth_response(s_complex, freqs_hz, axis="abs")
n = min(depth_m.size, y.size)
if n <= 0:
return _fallback_depth_response(s.size, s)
return depth_m[:n].astype(np.float32, copy=False), np.maximum(y[:n], 1e-12).astype(np.float32, copy=False) # log10 для лучшей визуализации в водопаде
except Exception as exc: # noqa: BLE001
logger.error("compute_ifft_profile_from_sweep failed: %r", exc)
return _fallback_depth_response(np.asarray(sweep).size if sweep is not None else 1, sweep)
def compute_ifft_db_profile(sweep: Optional[np.ndarray]) -> np.ndarray:
"""Legacy wrapper (deprecated name): возвращает линейный |IFFT| профиль."""
_depth_m, y = compute_ifft_profile_from_sweep(sweep, complex_mode="arccos")
return y

View File

@ -1,149 +0,0 @@
"""Алгоритмы нормировки свипов по калибровочной кривой."""
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]:
"""Оценить огибающую по модулю сигнала.
Возвращает (lower, upper) = (-envelope, +envelope), где envelope —
интерполяция через локальные максимумы |calib|.
"""
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)
a = np.abs(y)
if n < 3:
env = a.copy()
return -env, env
da = np.diff(a)
s = np.sign(da).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
x = np.arange(n, dtype=np.float32)
if max_idx.size == 0:
idx = np.array([0, n - 1], dtype=np.int64)
else:
idx = np.unique(np.concatenate(([0], max_idx, [n - 1]))).astype(np.int64)
env = np.interp(x, idx.astype(np.float32), a[idx]).astype(np.float32)
return -env, env
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)
def normalize_by_envelope(raw: np.ndarray, envelope: np.ndarray) -> np.ndarray:
"""Нормировка свипа через проекцию на огибающую из файла.
Воспроизводит логику normalize_projector: проецирует raw в [-1000, +1000]
используя готовую верхнюю огибающую (upper = envelope, lower = -envelope).
"""
w = min(raw.size, envelope.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)
upper = np.asarray(envelope[:w], dtype=np.float32)
lower = -upper
span = upper - lower # = 2 * upper
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

View File

@ -1,415 +0,0 @@
"""Явный pipeline предобработки свипов перед помещением в RingBuffer."""
from __future__ import annotations
from dataclasses import dataclass
import os
from typing import Optional, Tuple
import numpy as np
from rfg_adc_plotter.io.capture_reference_loader import (
CaptureParseSummary,
aggregate_capture_reference,
detect_reference_file_format,
load_capture_sweeps,
)
from rfg_adc_plotter.processing.normalizer import (
build_calib_envelopes,
normalize_by_calib,
normalize_by_envelope,
)
DEFAULT_CALIB_ENVELOPE_PATH = "calib_envelope.npy"
DEFAULT_BACKGROUND_PATH = "background.npy"
def _normalize_path(path: str) -> str:
return str(path).strip()
def _normalize_save_npy_path(path: str) -> str:
p = _normalize_path(path)
if not p:
return p
_root, ext = os.path.splitext(p)
if ext:
return p
return f"{p}.npy"
def _summary_for_npy(path: str) -> CaptureParseSummary:
return CaptureParseSummary(
path=path,
format="npy",
sweeps_total=0,
sweeps_valid=0,
channels_seen=tuple(),
dominant_width=None,
dominant_n_valid=None,
aggregation="median",
warnings=tuple(),
)
@dataclass(frozen=True)
class SweepProcessingResult:
"""Результат предобработки одного свипа."""
processed_sweep: np.ndarray
normalized_sweep: Optional[np.ndarray]
calibration_applied: bool
background_applied: bool
calibration_source: str # off|live|npy|capture
background_source: str # off|npy|capture(raw)|capture(raw->calib)
is_calibration_reference: bool
stage_trace: Tuple[str, ...]
class SweepPreprocessor:
"""Управляет калибровкой/фоном и применяет их к входному свипу."""
def __init__(
self,
norm_type: str = "projector",
calib_envelope_path: str = DEFAULT_CALIB_ENVELOPE_PATH,
background_path: str = DEFAULT_BACKGROUND_PATH,
auto_save_live_calib_envelope: bool = True,
):
self.norm_type = str(norm_type).strip().lower() or "projector"
self.calib_enabled = False
self.calib_mode = "live" # live | file
self.background_enabled = False
self.auto_save_live_calib_envelope = bool(auto_save_live_calib_envelope)
self.calib_envelope_path = _normalize_path(calib_envelope_path)
self.background_path = _normalize_path(background_path)
self.last_calib_sweep: Optional[np.ndarray] = None
self.calib_file_envelope: Optional[np.ndarray] = None
# background — в текущем домене вычитания (raw или normalized), UI использует для preview/state
self.background: Optional[np.ndarray] = None
# raw background loaded from capture file; преобразуется на лету при активной калибровке
self.background_raw_capture: Optional[np.ndarray] = None
# Источники и метаданные загрузки
self.calib_external_source_type: str = "none" # none|npy|capture
self.background_source_type: str = "none" # none|npy_processed|capture_raw
self.calib_reference_summary: Optional[CaptureParseSummary] = None
self.background_reference_summary: Optional[CaptureParseSummary] = None
self.last_reference_error: str = ""
# Параметры офлайн-парсинга capture (должны совпадать с live parser по настройке UI)
self.capture_fancy: bool = False
self.capture_logscale: bool = False
self.reference_aggregation_method: str = "median"
# ---- Конфигурация ----
def set_calib_mode(self, mode: str):
m = str(mode).strip().lower()
self.calib_mode = "file" if m == "file" else "live"
def set_calib_enabled(self, enabled: bool):
self.calib_enabled = bool(enabled)
def set_background_enabled(self, enabled: bool):
self.background_enabled = bool(enabled)
def set_capture_parse_options(self, *, fancy: Optional[bool] = None, logscale: Optional[bool] = None):
if fancy is not None:
self.capture_fancy = bool(fancy)
if logscale is not None:
self.capture_logscale = bool(logscale)
def set_calib_envelope_path(self, path: str):
p = _normalize_path(path)
if p:
if p != self.calib_envelope_path:
self.calib_file_envelope = None
if self.calib_external_source_type in ("npy", "capture"):
self.calib_external_source_type = "none"
self.calib_reference_summary = None
self.calib_envelope_path = p
def set_background_path(self, path: str):
p = _normalize_path(path)
if p:
if p != self.background_path:
self.background = None
self.background_raw_capture = None
self.background_source_type = "none"
self.background_reference_summary = None
self.background_path = p
def has_calib_envelope_file(self) -> bool:
return bool(self.calib_envelope_path) and os.path.isfile(self.calib_envelope_path)
def has_background_file(self) -> bool:
return bool(self.background_path) and os.path.isfile(self.background_path)
# ---- Загрузка/сохранение .npy ----
def _save_array(self, arr: np.ndarray, current_path: str, path: Optional[str]) -> str:
target = _normalize_save_npy_path(path if path is not None else current_path)
if not target:
raise ValueError("Пустой путь сохранения")
np.save(target, arr)
return target
def save_calib_envelope(self, path: Optional[str] = None) -> bool:
"""Сохранить огибающую из последнего live-калибровочного свипа (экспорт .npy)."""
if self.last_calib_sweep is None:
return False
try:
_lower, upper = build_calib_envelopes(self.last_calib_sweep)
self.calib_envelope_path = self._save_array(upper, self.calib_envelope_path, path)
self.last_reference_error = ""
return True
except Exception as exc:
self.last_reference_error = f"save calib envelope failed: {exc}"
return False
def save_background(self, sweep_for_ring: Optional[np.ndarray], path: Optional[str] = None) -> bool:
"""Сохранить текущий свип (в текущем домене обработки) как .npy-фон."""
if sweep_for_ring is None:
return False
try:
bg = np.asarray(sweep_for_ring, dtype=np.float32).copy()
self.background_path = self._save_array(bg, self.background_path, path)
self.background = bg
self.background_raw_capture = None
self.background_source_type = "npy_processed"
self.background_reference_summary = _summary_for_npy(self.background_path)
self.last_reference_error = ""
return True
except Exception as exc:
self.last_reference_error = f"save background failed: {exc}"
return False
# ---- Загрузка эталонов (.npy или capture) ----
def _detect_source_kind(self, path: str, source_kind: str) -> Optional[str]:
sk = str(source_kind).strip().lower() or "auto"
if sk == "auto":
return detect_reference_file_format(path)
if sk in ("npy", "bin_capture", "capture"):
return "bin_capture" if sk == "capture" else sk
return None
def _load_npy_vector(self, path: str) -> np.ndarray:
arr = np.load(path)
return np.asarray(arr, dtype=np.float32).reshape(-1)
def load_calib_reference(
self,
path: Optional[str] = None,
*,
source_kind: str = "auto",
method: str = "median",
) -> bool:
"""Загрузить калибровку из .npy (огибающая) или raw capture файла."""
if path is not None:
self.set_calib_envelope_path(path)
p = self.calib_envelope_path
if not p or not os.path.isfile(p):
self.last_reference_error = f"Файл калибровки не найден: {p}"
return False
fmt = self._detect_source_kind(p, source_kind)
if fmt is None:
self.last_reference_error = f"Неизвестный формат файла калибровки: {p}"
return False
try:
if fmt == "npy":
env = self._load_npy_vector(p)
self.calib_file_envelope = env
self.calib_external_source_type = "npy"
self.calib_reference_summary = _summary_for_npy(p)
self.last_reference_error = ""
return True
sweeps = load_capture_sweeps(p, fancy=self.capture_fancy, logscale=self.capture_logscale)
vec, summary = aggregate_capture_reference(
sweeps,
channel=0,
method=method or self.reference_aggregation_method,
path=p,
)
_lower, upper = build_calib_envelopes(vec)
self.calib_file_envelope = np.asarray(upper, dtype=np.float32)
self.calib_external_source_type = "capture"
self.calib_reference_summary = summary
self.last_reference_error = ""
return True
except Exception as exc:
self.last_reference_error = f"Ошибка загрузки калибровки: {exc}"
return False
def load_background_reference(
self,
path: Optional[str] = None,
*,
source_kind: str = "auto",
method: str = "median",
) -> bool:
"""Загрузить фон из .npy (готовый домен) или raw capture файла."""
if path is not None:
self.set_background_path(path)
p = self.background_path
if not p or not os.path.isfile(p):
self.last_reference_error = f"Файл фона не найден: {p}"
return False
fmt = self._detect_source_kind(p, source_kind)
if fmt is None:
self.last_reference_error = f"Неизвестный формат файла фона: {p}"
return False
try:
if fmt == "npy":
bg = self._load_npy_vector(p)
self.background = bg
self.background_raw_capture = None
self.background_source_type = "npy_processed"
self.background_reference_summary = _summary_for_npy(p)
self.last_reference_error = ""
return True
sweeps = load_capture_sweeps(p, fancy=self.capture_fancy, logscale=self.capture_logscale)
vec, summary = aggregate_capture_reference(
sweeps,
channel=0,
method=method or self.reference_aggregation_method,
path=p,
)
self.background_raw_capture = np.asarray(vec, dtype=np.float32)
# Для UI/preview текущий background отражает текущий домен (пока raw по умолчанию).
self.background = self.background_raw_capture
self.background_source_type = "capture_raw"
self.background_reference_summary = summary
self.last_reference_error = ""
return True
except Exception as exc:
self.last_reference_error = f"Ошибка загрузки фона: {exc}"
return False
# Совместимые обертки для старого API (строго .npy)
def load_calib_envelope(self, path: Optional[str] = None) -> bool:
target = path if path is not None else self.calib_envelope_path
return self.load_calib_reference(target, source_kind="npy")
def load_background(self, path: Optional[str] = None) -> bool:
target = path if path is not None else self.background_path
return self.load_background_reference(target, source_kind="npy")
# ---- Нормировка / фон ----
def _normalize_against_active_reference(self, raw: np.ndarray) -> Tuple[Optional[np.ndarray], bool, str]:
if not self.calib_enabled:
return None, False, "off"
if self.calib_mode == "file":
if self.calib_file_envelope is None:
return None, False, "off"
src = "capture" if self.calib_external_source_type == "capture" else "npy"
return normalize_by_envelope(raw, self.calib_file_envelope), True, src
if self.last_calib_sweep is None:
return None, False, "off"
return normalize_by_calib(raw, self.last_calib_sweep, self.norm_type), True, "live"
def _transform_raw_background_for_current_domain(self, calib_applied: bool) -> Optional[np.ndarray]:
if self.background_raw_capture is None:
return None
if not calib_applied:
return self.background_raw_capture
# Порядок pipeline фиксирован: raw -> calibration -> background -> IFFT.
# Поэтому raw-фон из capture нужно привести в тот же домен, что и текущий sweep_for_ring.
if self.calib_mode == "file" and self.calib_file_envelope is not None:
return normalize_by_envelope(self.background_raw_capture, self.calib_file_envelope)
if self.calib_mode == "live" and self.last_calib_sweep is not None:
return normalize_by_calib(self.background_raw_capture, self.last_calib_sweep, self.norm_type)
return None
def _effective_background(self, calib_applied: bool) -> Tuple[Optional[np.ndarray], str]:
if self.background_source_type == "capture_raw":
bg = self._transform_raw_background_for_current_domain(calib_applied)
if bg is None:
return None, "capture(raw->calib:missing-calib)"
self.background = np.asarray(bg, dtype=np.float32)
return self.background, ("capture(raw->calib)" if calib_applied else "capture(raw)")
if self.background_source_type == "npy_processed" and self.background is not None:
return self.background, "npy"
if self.background is not None:
return self.background, "unknown"
return None, "off"
def _subtract_background(self, sweep: np.ndarray, calib_applied: bool) -> Tuple[np.ndarray, bool, str]:
if not self.background_enabled:
return sweep, False, "off"
bg, bg_src = self._effective_background(calib_applied)
if bg is None:
return sweep, False, f"{bg_src}:missing"
out = np.asarray(sweep, dtype=np.float32).copy()
w = min(out.size, bg.size)
if w > 0:
out[:w] -= bg[:w]
return out, True, bg_src
def process(self, sweep: np.ndarray, channel: int, update_references: bool = True) -> SweepProcessingResult:
"""Применить к свипу калибровку/фон и вернуть явные этапы обработки."""
raw = np.asarray(sweep, dtype=np.float32)
ch = int(channel)
if ch == 0:
if update_references:
self.last_calib_sweep = raw
if self.auto_save_live_calib_envelope:
self.save_calib_envelope()
# ch0 всегда остаётся live-калибровочной ссылкой (raw), но при file-калибровке
# можем применять её и к ch0 для отображения/обработки независимо от канала.
calib_applied = False
calib_source = "off"
normalized: Optional[np.ndarray] = None
if self.calib_enabled and self.calib_mode == "file":
normalized, calib_applied, calib_source = self._normalize_against_active_reference(raw)
base = normalized if normalized is not None else raw
processed, bg_applied, bg_source = self._subtract_background(base, calib_applied=calib_applied)
stages = ["parsed_sweep", "raw_sweep", "ch0_live_calibration_reference"]
stages.append(f"calibration_{calib_source}" if calib_applied else "calibration_off")
stages.append(f"background_{bg_source}" if bg_applied else "background_off")
stages.extend(["ring_buffer", "ifft_db"])
return SweepProcessingResult(
processed_sweep=processed,
normalized_sweep=normalized,
calibration_applied=calib_applied,
background_applied=bg_applied,
calibration_source=calib_source if calib_applied else "off",
background_source=bg_source if bg_applied else "off",
is_calibration_reference=True,
stage_trace=tuple(stages),
)
normalized, calib_applied, calib_source = self._normalize_against_active_reference(raw)
base = normalized if normalized is not None else raw
processed, bg_applied, bg_source = self._subtract_background(base, calib_applied)
stages = ["parsed_sweep", "raw_sweep"]
stages.append(f"calibration_{calib_source}" if calib_applied else "calibration_off")
stages.append(f"background_{bg_source}" if bg_applied else "background_off")
stages.extend(["ring_buffer", "ifft_db"])
return SweepProcessingResult(
processed_sweep=processed,
normalized_sweep=normalized,
calibration_applied=calib_applied,
background_applied=bg_applied,
calibration_source=calib_source if calib_applied else "off",
background_source=bg_source if bg_applied else "off",
is_calibration_reference=False,
stage_trace=tuple(stages),
)

View File

@ -0,0 +1,107 @@
"""
Обработка фазы для FMCW радара: развертка фазы и преобразование в расстояние.
"""
from typing import Optional, Tuple
import numpy as np
def apply_temporal_unwrap(
current_phase: np.ndarray,
prev_phase: Optional[np.ndarray],
phase_offset: Optional[np.ndarray],
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Применяет улучшенный phase unwrapping для FMCW радара с адаптивным порогом.
Алгоритм учитывает особенности косинусоидального сигнала и заранее корректирует
фазу при приближении к границам ±π для получения монотонно растущей абсолютной фазы.
Args:
current_phase: Текущая фаза (развернутая по частоте) для всех бинов
prev_phase: Предыдущая фаза, может быть None при первом вызове
phase_offset: Накопленные смещения для каждого бина, может быть None
Returns:
(unwrapped_phase, new_prev_phase, new_phase_offset)
unwrapped_phase - абсолютная развёрнутая фаза (может быть > 2π)
new_prev_phase - обновлённая предыдущая фаза (для следующего вызова)
new_phase_offset - обновлённые смещения (для следующего вызова)
"""
n_bins = current_phase.size
# Инициализация при первом вызове
if prev_phase is None:
prev_phase = current_phase.copy()
phase_offset = np.zeros(n_bins, dtype=np.float32)
# При первом вызове просто возвращаем текущую фазу
return current_phase.copy(), prev_phase, phase_offset
if phase_offset is None:
phase_offset = np.zeros(n_bins, dtype=np.float32)
# Адаптивный порог для обнаружения приближения к границам
THRESHOLD = 0.8 * np.pi
# Вычисляем разницу между текущей и предыдущей фазой
delta = current_phase - prev_phase
# Обнаруживаем скачки и корректируем offset
# Используем улучшенный алгоритм с адаптивным порогом
# Метод 1: Стандартная коррекция для больших скачков (> π)
# Это ловит случаи, когда фаза уже перескочила границу
phase_offset = phase_offset - 2.0 * np.pi * np.round(delta / (2.0 * np.pi))
# Метод 2: Адаптивная коррекция при приближении к границам
# Проверяем текущую развернутую фазу
unwrapped_phase = current_phase + phase_offset
# Если фаза близка к нечетным π (π, 3π, 5π...), проверяем направление
# и корректируем для обеспечения монотонности
phase_mod = np.mod(unwrapped_phase + np.pi, 2.0 * np.pi) - np.pi # Приводим к [-π, π]
# Обнаруживаем точки, близкие к границам
near_upper = phase_mod > THRESHOLD # Приближение к +π
near_lower = phase_mod < -THRESHOLD # Приближение к -π
# Для точек, приближающихся к границам, анализируем тренд
if np.any(near_upper) or np.any(near_lower):
# Если delta положительна и мы около +π, готовимся к переходу
should_add = near_upper & (delta > 0)
# Если delta отрицательна и мы около -π, готовимся к переходу
should_sub = near_lower & (delta < 0)
# Применяем дополнительную коррекцию только там, где нужно
# (этот код срабатывает редко, только при быстром движении объекта)
pass # Основная коррекция уже сделана выше
# Финальная развернутая фаза
unwrapped_phase = current_phase + phase_offset
# Сохраняем текущую фазу как предыдущую для следующего свипа
new_prev_phase = current_phase.copy()
new_phase_offset = phase_offset.copy()
return unwrapped_phase, new_prev_phase, new_phase_offset
def phase_to_distance(phase: np.ndarray, center_freq_hz: float = 6e9) -> np.ndarray:
"""Преобразует развернутую фазу в расстояние для FMCW радара.
Формула: Δl = φ * c / (4π * ν)
где:
φ - фаза (радианы)
c - скорость света (м/с)
ν - центральная частота свипа (Гц)
Args:
phase: Развернутая фаза в радианах
center_freq_hz: Центральная частота диапазона в Гц (по умолчанию 6 ГГц для 2-10 ГГц)
Returns:
Расстояние в метрах
"""
c = 299792458.0 # Скорость света в м/с
distance = phase * c / (4.0 * np.pi * center_freq_hz)
return distance.astype(np.float32)

View File

@ -1,355 +0,0 @@
"""Состояние приложения: текущие свипы и настройки калибровки/нормировки."""
from queue import Empty, Queue
from typing import Any, Mapping, Optional
import numpy as np
from rfg_adc_plotter.processing.pipeline import (
DEFAULT_BACKGROUND_PATH,
DEFAULT_CALIB_ENVELOPE_PATH,
SweepPreprocessor,
)
from rfg_adc_plotter.state.ring_buffer import RingBuffer
from rfg_adc_plotter.types import SweepInfo, SweepPacket
CALIB_ENVELOPE_PATH = DEFAULT_CALIB_ENVELOPE_PATH
BACKGROUND_PATH = DEFAULT_BACKGROUND_PATH
def format_status(data: Mapping[str, Any]) -> str:
"""Преобразовать словарь метрик в одну строку 'k:v'."""
def _fmt(v: Any) -> str:
if v is None:
return "NA"
try:
fv = float(v)
except Exception:
return str(v)
if not np.isfinite(fv):
return "nan"
if abs(fv) >= 1000 or (0 < abs(fv) < 0.01):
return f"{fv:.3g}"
return f"{fv:.3f}".rstrip("0").rstrip(".")
parts = [f"{k}:{_fmt(v)}" for k, v in data.items() if k != "pre_exp_sweep"]
return " ".join(parts)
class AppState:
"""Весь изменяемый GUI-state: текущие данные + pipeline предобработки."""
def __init__(self, norm_type: str = "projector"):
self.current_sweep_pre_exp: Optional[np.ndarray] = None
self.current_sweep_post_exp: Optional[np.ndarray] = None
self.current_sweep_processed: Optional[np.ndarray] = None
self.current_sweep_raw: Optional[np.ndarray] = None
self.current_sweep_norm: Optional[np.ndarray] = None
self.current_info: Optional[SweepInfo] = None
self.norm_type: str = str(norm_type).strip().lower()
self.preprocessor = SweepPreprocessor(norm_type=self.norm_type)
self._last_sweep_for_ring: Optional[np.ndarray] = None
self._last_stage_trace: tuple[str, ...] = tuple()
def configure_capture_import(self, *, fancy: Optional[bool] = None, logscale: Optional[bool] = None):
self.preprocessor.set_capture_parse_options(fancy=fancy, logscale=logscale)
# ---- Свойства pipeline (для совместимости с GUI) ----
@property
def calib_enabled(self) -> bool:
return self.preprocessor.calib_enabled
@property
def calib_mode(self) -> str:
return self.preprocessor.calib_mode
@property
def calib_file_envelope(self) -> Optional[np.ndarray]:
return self.preprocessor.calib_file_envelope
@property
def last_calib_sweep(self) -> Optional[np.ndarray]:
return self.preprocessor.last_calib_sweep
@property
def background(self) -> Optional[np.ndarray]:
return self.preprocessor.background
@property
def background_enabled(self) -> bool:
return self.preprocessor.background_enabled
@property
def calib_source_type(self) -> str:
return self.preprocessor.calib_external_source_type
@property
def background_source_type(self) -> str:
return self.preprocessor.background_source_type
@property
def calib_reference_summary(self):
return self.preprocessor.calib_reference_summary
@property
def background_reference_summary(self):
return self.preprocessor.background_reference_summary
@property
def last_reference_error(self) -> str:
return self.preprocessor.last_reference_error
@property
def calib_envelope_path(self) -> str:
return self.preprocessor.calib_envelope_path
@property
def background_path(self) -> str:
return self.preprocessor.background_path
# ---- Управление файлами калибровки/фона ----
def set_calib_envelope_path(self, path: str):
self.preprocessor.set_calib_envelope_path(path)
self._refresh_current_processed()
def set_background_path(self, path: str):
self.preprocessor.set_background_path(path)
self._refresh_current_processed()
def has_calib_envelope_file(self) -> bool:
return self.preprocessor.has_calib_envelope_file()
def has_background_file(self) -> bool:
return self.preprocessor.has_background_file()
def save_calib_envelope(self, path: Optional[str] = None) -> bool:
return self.preprocessor.save_calib_envelope(path)
def load_calib_reference(self, path: Optional[str] = None) -> bool:
ok = self.preprocessor.load_calib_reference(path)
if ok:
self._refresh_current_processed()
return ok
def load_calib_envelope(self, path: Optional[str] = None) -> bool:
return self.load_calib_reference(path)
def set_calib_mode(self, mode: str):
self.preprocessor.set_calib_mode(mode)
self._refresh_current_processed()
def save_background(self, path: Optional[str] = None) -> bool:
return self.preprocessor.save_background(self._last_sweep_for_ring, path)
def load_background_reference(self, path: Optional[str] = None) -> bool:
ok = self.preprocessor.load_background_reference(path)
if ok:
self._refresh_current_processed()
return ok
def load_background(self, path: Optional[str] = None) -> bool:
return self.load_background_reference(path)
def set_background_enabled(self, enabled: bool):
self.preprocessor.set_background_enabled(enabled)
self._refresh_current_processed()
def set_calib_enabled(self, enabled: bool):
self.preprocessor.set_calib_enabled(enabled)
self._refresh_current_processed()
# ---- Вспомогательные методы для UI ----
def _current_channel(self) -> Optional[int]:
if not isinstance(self.current_info, dict):
return None
try:
return int(self.current_info.get("ch", 0))
except Exception:
return 0
def _apply_result_to_current(self, result) -> None:
self._last_stage_trace = tuple(result.stage_trace)
if result.is_calibration_reference:
self.current_sweep_norm = None
elif result.calibration_applied or result.background_applied:
self.current_sweep_norm = result.processed_sweep
else:
self.current_sweep_norm = None
self.current_sweep_processed = result.processed_sweep
self._last_sweep_for_ring = result.processed_sweep
def _refresh_current_processed(self):
if self.current_sweep_raw is None:
self.current_sweep_norm = None
self.current_sweep_processed = None
self._last_stage_trace = tuple()
return
ch = self._current_channel() or 0
result = self.preprocessor.process(self.current_sweep_raw, ch, update_references=False)
self._apply_result_to_current(result)
def format_pipeline_status(self) -> str:
"""Краткое описание pipeline для UI: от распарсенного свипа до IFFT."""
ch = self._current_channel()
if ch is None:
ch_txt = "?"
else:
ch_txt = str(ch)
reader_stage = "log-exp" if self.current_sweep_pre_exp is not None else "linear"
if ch == 0:
file_calib_applies = (
self.calib_enabled
and self.calib_mode == "file"
and self.calib_file_envelope is not None
)
if self.calib_enabled and self.calib_mode == "file":
calib_stage = self.format_calib_source_status()
else:
calib_stage = "calib[off]"
if not self.background_enabled:
bg_stage = "bg[off]"
elif self.background_source_type == "capture_raw":
if self.background is None:
bg_stage = (
"bg[capture(raw->calib):missing]"
if file_calib_applies
else "bg[capture(raw):missing]"
)
else:
bg_stage = "bg[capture(raw->calib)]" if file_calib_applies else "bg[capture(raw)]"
elif self.background_source_type == "npy_processed":
bg_stage = "bg[npy]" if self.background is not None else "bg[npy:missing]"
else:
bg_stage = "bg[sub]" if self.background is not None else "bg[missing]"
return (
f"pipeline ch{ch_txt}: parsed -> {reader_stage} -> raw -> "
f"live-calib-ref -> {calib_stage} -> {bg_stage} -> ring -> IFFT(abs, depth_m)"
)
calib_stage = self.format_calib_source_status()
bg_stage = self.format_background_source_status()
return (
f"pipeline ch{ch_txt}: parsed -> {reader_stage} -> raw -> "
f"{calib_stage} -> {bg_stage} -> ring -> IFFT(abs, depth_m)"
)
def _format_summary(self, summary) -> str:
if summary is None:
return ""
parts: list[str] = []
if getattr(summary, "sweeps_valid", 0) or getattr(summary, "sweeps_total", 0):
parts.append(f"valid:{summary.sweeps_valid}/{summary.sweeps_total}")
if getattr(summary, "dominant_width", None) is not None:
parts.append(f"w:{summary.dominant_width}")
chs = getattr(summary, "channels_seen", tuple())
if chs:
parts.append("chs:" + ",".join(str(v) for v in chs))
warns = getattr(summary, "warnings", tuple())
if warns:
parts.append(f"warn:{warns[0]}")
return " ".join(parts)
def format_calib_source_status(self) -> str:
if not self.calib_enabled:
return "calib[off]"
if self.calib_mode == "live":
return "calib[live]" if self.last_calib_sweep is not None else "calib[live:no-ref]"
if self.calib_file_envelope is None:
return "calib[file:missing]"
if self.calib_source_type == "capture":
return "calib[capture]"
if self.calib_source_type == "npy":
return "calib[npy]"
return "calib[file]"
def format_background_source_status(self) -> str:
if not self.background_enabled:
return "bg[off]"
src = self.background_source_type
if src == "capture_raw":
if self.calib_enabled:
can_map = (
(self.calib_mode == "file" and self.calib_file_envelope is not None)
or (self.calib_mode == "live" and self.last_calib_sweep is not None)
)
if not can_map:
return "bg[capture(raw->calib):missing]"
if self.background is None:
return "bg[capture(raw->calib):missing]"
return "bg[capture(raw->calib)]" if self.calib_enabled else "bg[capture(raw)]"
if src == "npy_processed":
return "bg[npy]" if self.background is not None else "bg[npy:missing]"
if self.background is not None:
return "bg[sub]"
return "bg[missing]"
def format_reference_status(self) -> str:
parts: list[str] = []
calib_s = self._format_summary(self.calib_reference_summary)
if calib_s:
parts.append(f"calib[{calib_s}]")
bg_s = self._format_summary(self.background_reference_summary)
if bg_s:
parts.append(f"bg[{bg_s}]")
if self.last_reference_error:
parts.append(f"err:{self.last_reference_error}")
return " | ".join(parts)
def format_stage_trace(self) -> str:
if not self._last_stage_trace:
return ""
return " -> ".join(self._last_stage_trace)
def drain_queue(self, q: "Queue[SweepPacket]", ring: RingBuffer) -> int:
"""Вытащить все ожидающие свипы из очереди, обновить state и ring.
Возвращает количество обработанных свипов.
"""
drained = 0
while True:
try:
s, info = q.get_nowait()
except Empty:
break
drained += 1
self.current_sweep_raw = s
self.current_sweep_post_exp = s
self.current_info = info
pre_exp = info.get("pre_exp_sweep") if isinstance(info, dict) else None
self.current_sweep_pre_exp = pre_exp if isinstance(pre_exp, np.ndarray) else None
try:
ch = int(info.get("ch", 0)) if isinstance(info, dict) else 0
except Exception:
ch = 0
result = self.preprocessor.process(s, ch, update_references=True)
self._apply_result_to_current(result)
ring.ensure_init(s.size)
ring.push(result.processed_sweep)
return drained
def format_channel_label(self) -> str:
"""Строка с номерами каналов для подписи на графике."""
if self.current_info is None:
return ""
info = self.current_info
chs = info.get("chs") if isinstance(info, dict) else None
if chs is None:
chs = info.get("ch") if isinstance(info, dict) else None
if chs is None:
return ""
try:
if isinstance(chs, (list, tuple, set)):
ch_list = sorted(int(v) for v in chs)
return "chs " + ", ".join(str(v) for v in ch_list)
return f"chs {int(chs)}"
except Exception:
return f"chs {chs}"

View File

@ -1,314 +0,0 @@
"""Кольцевой буфер свипов и FFT-строк для водопадного отображения."""
import time
from typing import Optional, Tuple
import numpy as np
from rfg_adc_plotter.constants import (
FREQ_MAX_GHZ,
FREQ_MIN_GHZ,
WF_WIDTH,
)
from rfg_adc_plotter.processing.fourier import (
build_frequency_axis_hz,
compute_ifft_profile_from_sweep,
perform_ifft_depth_response,
reconstruct_complex_spectrum_from_real_trace,
)
class RingBuffer:
"""Хранит последние N свипов и соответствующие FFT-строки.
Все мутабельные поля водопада инкапсулированы здесь,
что устраняет необходимость nonlocal в GUI-коде.
"""
def __init__(self, max_sweeps: int):
self.max_sweeps = max_sweeps
# Размер IFFT-профиля теперь динамический и определяется по первому успешному свипу.
self.fft_bins = 0
self.fft_complex_mode: str = "arccos"
# Инициализируются при первом свипе (ensure_init)
self.ring: Optional[np.ndarray] = None # (max_sweeps, WF_WIDTH)
self.ring_fft: Optional[np.ndarray] = None # (max_sweeps, fft_bins)
self.ring_time: Optional[np.ndarray] = None # (max_sweeps,)
self.head: int = 0
self.width: Optional[int] = None
self.x_shared: Optional[np.ndarray] = None
self.fft_depth_axis_m: Optional[np.ndarray] = None # ось глубины IFFT в метрах
self.y_min_fft: Optional[float] = None
self.y_max_fft: Optional[float] = None
# FFT последнего свипа (для отображения без повторного вычисления)
self.last_fft_vals: Optional[np.ndarray] = None
# FFT-профили по третям входного частотного диапазона (для line-plot).
self.last_fft_third_axes_m: tuple[Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]] = (
None,
None,
None,
)
self.last_fft_third_vals: tuple[Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]] = (
None,
None,
None,
)
@property
def is_ready(self) -> bool:
return self.ring is not None
@property
def fft_time_axis(self) -> Optional[np.ndarray]:
"""Legacy alias: старое имя поля (раньше было время в нс, теперь глубина в м)."""
return self.fft_depth_axis_m
def set_fft_complex_mode(self, mode: str) -> bool:
"""Выбрать режим реконструкции комплексного спектра для IFFT.
Возвращает True, если режим изменился (и FFT-буфер был сброшен).
"""
m = str(mode).strip().lower()
if m not in ("arccos", "diff"):
raise ValueError(f"Unsupported IFFT complex mode: {mode!r}")
if m == self.fft_complex_mode:
return False
self.fft_complex_mode = m
# Сбрасываем только FFT-зависимые структуры. Сырые ряды сохраняем.
self.ring_fft = None
self.fft_depth_axis_m = None
self.fft_bins = 0
self.last_fft_vals = None
self.last_fft_third_axes_m = (None, None, None)
self.last_fft_third_vals = (None, None, None)
self.y_min_fft = None
self.y_max_fft = None
return True
def ensure_init(self, sweep_width: int):
"""Инициализировать буферы при первом свипе. Повторные вызовы — no-op (кроме x_shared)."""
if self.ring is None:
self.width = WF_WIDTH
self.ring = np.full((self.max_sweeps, self.width), np.nan, dtype=np.float32)
self.ring_time = np.full((self.max_sweeps,), np.nan, dtype=np.float64)
self.head = 0
# Обновляем x_shared если пришёл свип большего размера
if self.x_shared is None or sweep_width > self.x_shared.size:
self.x_shared = np.linspace(FREQ_MIN_GHZ, FREQ_MAX_GHZ, sweep_width, dtype=np.float32)
def push(self, s: np.ndarray):
"""Добавить строку свипа в кольцевой буфер, вычислить FFT-строку."""
if s is None or s.size == 0 or self.ring is None:
return
w = self.ring.shape[1]
row = np.full((w,), np.nan, dtype=np.float32)
take = min(w, s.size)
row[:take] = s[:take]
self.ring[self.head, :] = row
self.ring_time[self.head] = time.time()
self.head = (self.head + 1) % self.ring.shape[0]
self._push_fft(s)
def _push_fft(self, s: np.ndarray):
empty_thirds = (
np.zeros((0,), dtype=np.float32),
np.zeros((0,), dtype=np.float32),
np.zeros((0,), dtype=np.float32),
)
depth_axis_m, fft_row = compute_ifft_profile_from_sweep(
s,
complex_mode=self.fft_complex_mode,
)
fft_row = np.asarray(fft_row, dtype=np.float32).ravel()
depth_axis_m = np.asarray(depth_axis_m, dtype=np.float32).ravel()
n = min(int(fft_row.size), int(depth_axis_m.size))
if n <= 0:
self.last_fft_third_axes_m = empty_thirds
self.last_fft_third_vals = empty_thirds
return
if n != fft_row.size:
fft_row = fft_row[:n]
if n != depth_axis_m.size:
depth_axis_m = depth_axis_m[:n]
# Для отображения храним только первую половину IFFT-профиля:
# вторая половина для текущей схемы симметрична и визуально избыточна.
n_keep = max(1, (n + 1) // 2)
fft_row = fft_row[:n_keep]
depth_axis_m = depth_axis_m[:n_keep]
n = n_keep
needs_reset = (
self.ring_fft is None
or self.fft_depth_axis_m is None
or self.fft_bins != n
or self.ring_fft.shape != (self.max_sweeps, n)
or self.fft_depth_axis_m.size != n
)
if (not needs_reset) and n > 0:
prev_axis = self.fft_depth_axis_m
assert prev_axis is not None
if prev_axis.size != n:
needs_reset = True
else:
# Если ось изменилась (например, изменилась длина/частотная сетка), сбрасываем FFT-водопад.
if not np.allclose(prev_axis[[0, -1]], depth_axis_m[[0, -1]], rtol=1e-6, atol=1e-9):
needs_reset = True
if needs_reset:
self.fft_bins = n
self.ring_fft = np.full((self.max_sweeps, n), np.nan, dtype=np.float32)
self.fft_depth_axis_m = depth_axis_m.astype(np.float32, copy=True)
self.y_min_fft = None
self.y_max_fft = None
assert self.ring_fft is not None
prev_head = (self.head - 1) % self.ring_fft.shape[0]
self.ring_fft[prev_head, :] = fft_row
self.last_fft_vals = fft_row
self.last_fft_third_axes_m, self.last_fft_third_vals = self._compute_fft_thirds(s)
fr_min = np.nanmin(fft_row)
fr_max = float(np.nanpercentile(fft_row, 90))
if self.y_min_fft is None or (not np.isnan(fr_min) and fr_min < self.y_min_fft):
self.y_min_fft = float(fr_min)
if self.y_max_fft is None or (not np.isnan(fr_max) and fr_max > self.y_max_fft):
self.y_max_fft = float(fr_max)
def _compute_fft_thirds(
self, s: np.ndarray
) -> tuple[tuple[np.ndarray, np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray, np.ndarray]]:
sweep = np.asarray(s, dtype=np.float64).ravel()
total = int(sweep.size)
def _empty() -> np.ndarray:
return np.zeros((0,), dtype=np.float32)
if total <= 0:
return (_empty(), _empty(), _empty()), (_empty(), _empty(), _empty())
freq_hz = build_frequency_axis_hz(total)
edges = np.linspace(0, total, 4, dtype=np.int64)
axes: list[np.ndarray] = []
vals: list[np.ndarray] = []
for idx in range(3):
i0 = int(edges[idx])
i1 = int(edges[idx + 1])
if i1 - i0 < 2:
axes.append(_empty())
vals.append(_empty())
continue
seg = sweep[i0:i1]
seg_freq = freq_hz[i0:i1]
seg_complex = reconstruct_complex_spectrum_from_real_trace(
seg,
complex_mode=self.fft_complex_mode,
)
depth_m, seg_fft = perform_ifft_depth_response(seg_complex, seg_freq, axis="abs")
depth_m = np.asarray(depth_m, dtype=np.float32).ravel()
seg_fft = np.asarray(seg_fft, dtype=np.float32).ravel()
n = min(int(depth_m.size), int(seg_fft.size))
if n <= 0:
axes.append(_empty())
vals.append(_empty())
continue
depth_m = depth_m[:n]
seg_fft = seg_fft[:n]
n_keep = max(1, (n + 1) // 2)
axes.append(depth_m[:n_keep])
vals.append(seg_fft[:n_keep])
return (
axes[0],
axes[1],
axes[2],
), (
vals[0],
vals[1],
vals[2],
)
def get_display_ring(self) -> np.ndarray:
"""Кольцо в порядке от старого к новому, ось времени по X. Форма: (width, time)."""
if self.ring is None:
return np.zeros((1, 1), dtype=np.float32)
base = self.ring if self.head == 0 else np.roll(self.ring, -self.head, axis=0)
return base.T # (width, time)
def get_display_ring_fft(self) -> np.ndarray:
"""FFT-кольцо в порядке от старого к новому. Форма: (bins, time)."""
if self.ring_fft is None:
return np.zeros((1, 1), dtype=np.float32)
base = self.ring_fft if self.head == 0 else np.roll(self.ring_fft, -self.head, axis=0)
return base.T # (bins, time)
def get_display_times(self) -> Optional[np.ndarray]:
"""Временные метки строк в порядке от старого к новому."""
if self.ring_time is None:
return None
return self.ring_time if self.head == 0 else np.roll(self.ring_time, -self.head)
def subtract_recent_mean_fft(
self, disp_fft: np.ndarray, spec_mean_sec: float
) -> np.ndarray:
"""Вычесть среднее по каждой частоте за последние spec_mean_sec секунд."""
if spec_mean_sec <= 0.0:
return disp_fft
disp_times = self.get_display_times()
if disp_times is None:
return disp_fft
now_t = time.time()
mask = np.isfinite(disp_times) & (disp_times >= (now_t - spec_mean_sec))
if not np.any(mask):
return disp_fft
try:
mean_spec = np.nanmean(disp_fft[:, mask], axis=1)
except Exception:
return disp_fft
mean_spec = np.nan_to_num(mean_spec, nan=0.0)
return disp_fft - mean_spec[:, None]
def compute_fft_levels(
self, disp_fft: np.ndarray, spec_clip: Optional[Tuple[float, float]]
) -> Optional[Tuple[float, float]]:
"""Вычислить (vmin, vmax) для отображения водопада спектров."""
# 1. По среднему спектру за видимое время
try:
mean_spec = np.nanmean(disp_fft, axis=1)
vmin_v = float(np.nanmin(mean_spec))
vmax_v = float(np.nanmax(mean_spec))
if np.isfinite(vmin_v) and np.isfinite(vmax_v) and vmin_v != vmax_v:
return (vmin_v, vmax_v)
except Exception:
pass
# 2. Процентильная обрезка
if spec_clip is not None:
try:
vmin_v = float(np.nanpercentile(disp_fft, spec_clip[0]))
vmax_v = float(np.nanpercentile(disp_fft, spec_clip[1]))
if np.isfinite(vmin_v) and np.isfinite(vmax_v) and vmin_v != vmax_v:
return (vmin_v, vmax_v)
except Exception:
pass
# 3. Глобальные накопленные мин/макс
if (
self.y_min_fft is not None
and self.y_max_fft is not None
and np.isfinite(self.y_min_fft)
and np.isfinite(self.y_max_fft)
and self.y_min_fft != self.y_max_fft
):
return (self.y_min_fft, self.y_max_fft)
return None

View File

@ -1,7 +0,0 @@
from typing import Any, Dict, Tuple, Union
import numpy as np
Number = Union[int, float]
SweepInfo = Dict[str, Any]
SweepPacket = Tuple[np.ndarray, SweepInfo]

View File

@ -0,0 +1,50 @@
"""
Утилиты для форматирования данных и парсинга параметров.
"""
from typing import Any, Mapping, Optional, Tuple
import numpy as np
def format_status_kv(data: Mapping[str, Any]) -> str:
"""Преобразовать словарь метрик в одну строку 'k:v'."""
def _fmt(v: Any) -> str:
if v is None:
return "NA"
try:
fv = float(v)
except Exception:
return str(v)
if not np.isfinite(fv):
return "nan"
# Достаточно компактно для статус-строки.
if abs(fv) >= 1000 or (0 < abs(fv) < 0.01):
return f"{fv:.3g}"
return f"{fv:.3f}".rstrip("0").rstrip(".")
parts = [f"{k}:{_fmt(v)}" for k, v in data.items()]
return " ".join(parts)
def parse_spec_clip(spec: Optional[str]) -> Optional[Tuple[float, float]]:
"""Разобрать строку вида "low,high" процентов для контрастного отображения водопада спектров.
Возвращает пару (low, high) или None для отключения. Допустимы значения 0..100, low < high.
Ключевые слова отключения: "off", "none", "no".
"""
if not spec:
return None
s = str(spec).strip().lower()
if s in ("off", "none", "no"):
return None
try:
p0, p1 = s.replace(";", ",").split(",")
low = float(p0)
high = float(p1)
if not (0.0 <= low < high <= 100.0):
return None
return (low, high)
except Exception:
return None

View File

@ -0,0 +1,651 @@
"""
Визуализация данных с использованием matplotlib.
"""
import csv
import sys
import threading
import time
from datetime import datetime
from queue import Empty, Queue
from typing import Optional, Tuple
import numpy as np
try:
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Slider
except Exception as e:
raise RuntimeError(f"Нужны matplotlib и ее зависимости: {e}")
from ..config import (
FFT_LEN,
WF_WIDTH,
SweepInfo,
SweepPacket,
FREQ_MIN_GHZ,
FREQ_MAX_GHZ,
DATA_FREQ_START_GHZ,
DATA_FREQ_END_GHZ,
)
from ..data_acquisition.sweep_reader import SweepReader
from ..signal_processing.phase_analysis import apply_temporal_unwrap, phase_to_distance
from ..utils.formatting import format_status_kv, parse_spec_clip
def run_matplotlib(args):
"""Запуск визуализации с использованием matplotlib."""
# Очередь завершённых свипов и поток чтения
q: Queue[SweepPacket] = Queue(maxsize=1000)
stop_event = threading.Event()
reader = SweepReader(args.port, args.baud, q, stop_event, fancy=bool(args.fancy))
reader.start()
# Графика (3 ряда x 2 колонки = 6 графиков)
fig, axs = plt.subplots(3, 2, figsize=(12, 12))
(ax_line, ax_img), (ax_fft, ax_spec), (ax_phase, ax_phase_wf) = axs
fig.canvas.manager.set_window_title(args.title) if hasattr(fig.canvas.manager, "set_window_title") else None
# Увеличим расстояния и оставим место справа под ползунки оси Y B-scan
fig.subplots_adjust(wspace=0.25, hspace=0.35, left=0.07, right=0.90, top=0.95, bottom=0.05)
# Состояние для отображения
current_sweep: Optional[np.ndarray] = None
current_info: Optional[SweepInfo] = None
x_shared: Optional[np.ndarray] = None
width: Optional[int] = None
max_sweeps = int(max(10, args.max_sweeps))
ring = None # type: Optional[np.ndarray]
ring_time = None # type: Optional[np.ndarray]
head = 0
# Медианные данные для вычитания
median_data: Optional[np.ndarray] = None
median_subtract_enabled = False
# CLI параметры для автоматического сохранения/загрузки
ref_out_file = getattr(args, 'ref_out', None)
ref_in_file = getattr(args, 'ref_in', None)
ref_out_saved = False # Флаг, что медиана уже сохранена
# Отдельный буфер для накопления 1000 сырых свипов (не зависит от max_sweeps)
ref_ring: Optional[np.ndarray] = None
ref_ring_head = 0
ref_ring_count = 0
if ref_out_file:
print(f"[ref-out] Автосохранение включено, файл: {ref_out_file}")
# Автоматическая загрузка медианы при старте
if ref_in_file:
try:
pairs = []
with open(ref_in_file, 'r') as f:
reader = csv.reader(f)
next(reader) # Пропускаем заголовок
for row in reader:
if len(row) >= 2:
try:
pairs.append((int(row[0]), float(row[1])))
except ValueError:
continue
if pairs:
max_idx = max(idx for idx, _ in pairs)
median_data = np.full(max_idx + 1, np.nan, dtype=np.float32)
for idx, val in pairs:
median_data[idx] = val
median_subtract_enabled = True
print(f"[ref-in] Загружена медиана из {ref_in_file} ({len(median_data)} точек), вычитание включено")
else:
print(f"[ref-in] Предупреждение: файл {ref_in_file} пустой или неверный формат")
except Exception as e:
print(f"[ref-in] Ошибка загрузки {ref_in_file}: {e}")
# Авто-уровни цветовой шкалы водопада сырых данных пересчитываются по видимой области.
# FFT состояние (полное FFT для отрицательных частот)
fft_bins = FFT_LEN
ring_fft = None # type: Optional[np.ndarray]
y_min_fft, y_max_fft = None, None
freq_shared: Optional[np.ndarray] = None
# Phase состояние
ring_phase = None # type: Optional[np.ndarray]
prev_phase_per_bin: Optional[np.ndarray] = None
phase_offset_per_bin: Optional[np.ndarray] = None
y_min_phase, y_max_phase = None, None
# Параметры контраста водопада спектров
spec_clip = parse_spec_clip(getattr(args, "spec_clip", None))
# Ползунки управления Y для B-scan и контрастом
ymin_slider = None
ymax_slider = None
contrast_slider = None
# Статусная строка (внизу окна)
status_text = fig.text(
0.01,
0.01,
"",
ha="left",
va="bottom",
fontsize=8,
family="monospace",
)
# Линейный график последнего свипа
line_obj, = ax_line.plot([], [], lw=1)
ax_line.set_title("Сырые данные", pad=1)
ax_line.set_xlabel("F")
ax_line.set_ylabel("")
# Линейный график спектра текущего свипа
fft_line_obj, = ax_fft.plot([], [], lw=1)
ax_fft.set_title("FFT", pad=1)
ax_fft.set_xlabel("Частота, ГГц")
ax_fft.set_ylabel("Амплитуда, дБ")
# Диапазон по Y для последнего свипа: авто по умолчанию (поддерживает отрицательные значения)
fixed_ylim: Optional[Tuple[float, float]] = None
# CLI переопределение при необходимости
if args.ylim:
try:
y0, y1 = args.ylim.split(",")
fixed_ylim = (float(y0), float(y1))
except Exception:
sys.stderr.write("[warn] Некорректный формат --ylim, игнорирую. Ожидалось min,max\n")
if fixed_ylim is not None:
ax_line.set_ylim(fixed_ylim)
# Водопад (будет инициализирован при первом свипе)
img_obj = ax_img.imshow(
np.zeros((1, 1), dtype=np.float32),
aspect="auto",
interpolation="nearest",
origin="lower",
cmap=args.cmap,
)
ax_img.set_title("Сырые данные", pad=12)
ax_img.set_xlabel("")
ax_img.set_ylabel("частота")
# Не показываем численные значения по времени на водопаде сырых данных
try:
ax_img.tick_params(axis="x", labelbottom=False)
except Exception:
pass
# Водопад спектров
img_fft_obj = ax_spec.imshow(
np.zeros((1, 1), dtype=np.float32),
aspect="auto",
interpolation="nearest",
origin="lower",
cmap=args.cmap,
)
ax_spec.set_title("B-scan (дБ)", pad=12)
ax_spec.set_xlabel("")
ax_spec.set_ylabel("Частота, ГГц")
# Не показываем численные значения по времени на B-scan
try:
ax_spec.tick_params(axis="x", labelbottom=False)
except Exception:
pass
# График фазы текущего свипа
phase_line_obj, = ax_phase.plot([], [], lw=1)
ax_phase.set_title("Фаза спектра (развернутая)", pad=1)
ax_phase.set_xlabel("Частота, ГГц")
ax_phase.set_ylabel("Фаза, радианы")
# Добавим второй Y axis для расстояния
ax_phase_dist = ax_phase.twinx()
ax_phase_dist.set_ylabel("Расстояние, м", color='green')
# Водопад фазы
img_phase_obj = ax_phase_wf.imshow(
np.zeros((1, 1), dtype=np.float32),
aspect="auto",
interpolation="nearest",
origin="lower",
cmap=args.cmap,
)
ax_phase_wf.set_title("Водопад фазы", pad=12)
ax_phase_wf.set_xlabel("")
ax_phase_wf.set_ylabel("Частота, ГГц")
# Не показываем численные значения по времени
try:
ax_phase_wf.tick_params(axis="x", labelbottom=False)
except Exception:
pass
# Слайдеры для управления осью Y B-scan (мин/макс) и контрастом
try:
ax_smin = fig.add_axes([0.92, 0.55, 0.02, 0.35])
ax_smax = fig.add_axes([0.95, 0.55, 0.02, 0.35])
ax_sctr = fig.add_axes([0.98, 0.55, 0.02, 0.35])
ymin_slider = Slider(ax_smin, "Y min", FREQ_MIN_GHZ, FREQ_MAX_GHZ, valinit=FREQ_MIN_GHZ, valstep=0.1, orientation="vertical")
ymax_slider = Slider(ax_smax, "Y max", FREQ_MIN_GHZ, FREQ_MAX_GHZ, valinit=FREQ_MAX_GHZ, valstep=0.1, orientation="vertical")
contrast_slider = Slider(ax_sctr, "Int max", 0, 100, valinit=100, valstep=1, orientation="vertical")
def _on_ylim_change(_val):
try:
y0 = float(min(ymin_slider.val, ymax_slider.val))
y1 = float(max(ymin_slider.val, ymax_slider.val))
ax_spec.set_ylim(y0, y1)
fig.canvas.draw_idle()
except Exception:
pass
ymin_slider.on_changed(_on_ylim_change)
ymax_slider.on_changed(_on_ylim_change)
# Контраст влияет на верхнюю границу цветовой шкалы (процент от авто-диапазона)
contrast_slider.on_changed(lambda _v: fig.canvas.draw_idle())
except Exception:
pass
# Для контроля частоты обновления
max_fps = max(1.0, float(args.max_fps))
interval_ms = int(1000.0 / max_fps)
frames_since_ylim_update = 0
def ensure_buffer(_w: int):
nonlocal ring, width, head, x_shared, ring_fft, freq_shared, ring_time
nonlocal ring_phase, prev_phase_per_bin, phase_offset_per_bin
nonlocal ref_ring
if ring is not None:
return
width = WF_WIDTH
x_shared = np.arange(width, dtype=np.int32)
ring = np.full((max_sweeps, width), np.nan, dtype=np.float32)
ring_time = np.full((max_sweeps,), np.nan, dtype=np.float64)
head = 0
# Обновляем изображение под новые размеры: время по X (горизонталь), X по Y
img_obj.set_data(np.zeros((width, max_sweeps), dtype=np.float32))
img_obj.set_extent((0, max_sweeps - 1, 0, width - 1 if width > 0 else 1))
ax_img.set_xlim(0, max_sweeps - 1)
ax_img.set_ylim(0, max(1, width - 1))
# FFT буферы: время по X, бин по Y
ring_fft = np.full((max_sweeps, fft_bins), np.nan, dtype=np.float32)
img_fft_obj.set_data(np.zeros((fft_bins, max_sweeps), dtype=np.float32))
img_fft_obj.set_extent((0, max_sweeps - 1, FREQ_MIN_GHZ, FREQ_MAX_GHZ))
ax_spec.set_xlim(0, max_sweeps - 1)
ax_spec.set_ylim(FREQ_MIN_GHZ, FREQ_MAX_GHZ)
freq_shared = np.linspace(FREQ_MIN_GHZ, FREQ_MAX_GHZ, fft_bins, dtype=np.float32)
# Phase буферы: время по X, бин по Y
ring_phase = np.full((max_sweeps, fft_bins), np.nan, dtype=np.float32)
prev_phase_per_bin = np.zeros(fft_bins, dtype=np.float32)
phase_offset_per_bin = np.zeros(fft_bins, dtype=np.float32)
img_phase_obj.set_data(np.zeros((fft_bins, max_sweeps), dtype=np.float32))
img_phase_obj.set_extent((0, max_sweeps - 1, FREQ_MIN_GHZ, FREQ_MAX_GHZ))
ax_phase_wf.set_xlim(0, max_sweeps - 1)
ax_phase_wf.set_ylim(FREQ_MIN_GHZ, FREQ_MAX_GHZ)
# Буфер для медианы (отдельный от ring, размер всегда 1000)
if ref_out_file and ref_ring is None:
ref_ring = np.full((1000, width), np.nan, dtype=np.float32)
def _visible_levels_matplotlib(data: np.ndarray, axis) -> Optional[Tuple[float, float]]:
"""(vmin, vmax) по текущей видимой области imshow (без накопления по времени)."""
if data.size == 0:
return None
ny, nx = data.shape[0], data.shape[1]
try:
x0, x1 = axis.get_xlim()
y0, y1 = axis.get_ylim()
except Exception:
x0, x1 = 0.0, float(nx - 1)
y0, y1 = 0.0, float(ny - 1)
xmin, xmax = sorted((float(x0), float(x1)))
ymin, ymax = sorted((float(y0), float(y1)))
ix0 = max(0, min(nx - 1, int(np.floor(xmin))))
ix1 = max(0, min(nx - 1, int(np.ceil(xmax))))
iy0 = max(0, min(ny - 1, int(np.floor(ymin))))
iy1 = max(0, min(ny - 1, int(np.ceil(ymax))))
if ix1 < ix0:
ix1 = ix0
if iy1 < iy0:
iy1 = iy0
sub = data[iy0 : iy1 + 1, ix0 : ix1 + 1]
finite = np.isfinite(sub)
if not finite.any():
return None
vals = sub[finite]
vmin = float(np.min(vals))
vmax = float(np.max(vals))
if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax:
return None
return (vmin, vmax)
def push_sweep(s: np.ndarray):
nonlocal ring, head, ring_fft, y_min_fft, y_max_fft, ring_time
nonlocal ring_phase, prev_phase_per_bin, phase_offset_per_bin, y_min_phase, y_max_phase
nonlocal ref_ring_head, ref_ring_count
if s is None or s.size == 0 or ring is None:
return
# Сохраняем сырой свип в буфер медианы (до вычитания)
if ref_out_file and not ref_out_saved and ref_ring is not None:
w_ref = ref_ring.shape[1]
take_ref = min(w_ref, s.size)
ref_ring[ref_ring_head, :take_ref] = s[:take_ref]
ref_ring_head = (ref_ring_head + 1) % 1000
ref_ring_count = min(ref_ring_count + 1, 1000)
# Применяем вычитание медианы если включено
if median_subtract_enabled and median_data is not None:
take_median = min(s.size, median_data.size)
s_corrected = s.copy()
s_corrected[:take_median] = s[:take_median] - median_data[:take_median]
s = s_corrected
# Нормализуем длину до фиксированной ширины
w = ring.shape[1]
row = np.full((w,), np.nan, dtype=np.float32)
take = min(w, s.size)
row[:take] = s[:take]
ring[head, :] = row
if ring_time is not None:
ring_time[head] = time.time()
head = (head + 1) % ring.shape[0]
# FFT строка (дБ) и фаза
if ring_fft is not None:
bins = ring_fft.shape[1]
# Подготовка входа FFT_LEN, замена NaN на 0
take_fft = min(int(s.size), FFT_LEN)
if take_fft <= 0:
fft_row = np.full((bins,), np.nan, dtype=np.float32)
phase_row = np.full((bins,), np.nan, dtype=np.float32)
else:
# Создаем буфер для полного FFT (с отрицательными частотами)
fft_in = np.zeros((FFT_LEN,), dtype=np.float32)
# Вычисляем индексы для размещения данных (1-10 ГГц в диапазоне -10 до +10 ГГц)
freq_range_total = FREQ_MAX_GHZ - FREQ_MIN_GHZ # 20 ГГц
freq_range_data = DATA_FREQ_END_GHZ - DATA_FREQ_START_GHZ # 9 ГГц
# Начальный индекс для данных в FFT буфере
start_idx = int((DATA_FREQ_START_GHZ - FREQ_MIN_GHZ) / freq_range_total * FFT_LEN)
# Количество точек для данных
data_points = int(freq_range_data / freq_range_total * FFT_LEN)
data_points = min(data_points, take_fft, FFT_LEN - start_idx)
# Подготовка данных
seg = s[:data_points]
if isinstance(seg, np.ndarray):
seg = np.nan_to_num(seg, nan=0.0).astype(np.float32, copy=False)
else:
seg = np.asarray(seg, dtype=np.float32)
seg = np.nan_to_num(seg, nan=0.0)
# Окно Хэннинга
win = np.hanning(data_points).astype(np.float32)
# Размещаем данные в правильной позиции
fft_in[start_idx:start_idx + data_points] = seg * win
# Полное FFT (включая отрицательные частоты)
spec = np.fft.fft(fft_in)
# Сдвигаем для центрирования нулевой частоты
spec = np.fft.fftshift(spec)
mag = np.abs(spec).astype(np.float32)
fft_row = 20.0 * np.log10(mag + 1e-9)
if fft_row.shape[0] != bins:
fft_row = fft_row[:bins]
# Расчет фазы
phase = np.angle(spec).astype(np.float32)
if phase.shape[0] > bins:
phase = phase[:bins]
# Unwrapping по частоте (внутри свипа)
phase_unwrapped_freq = np.unwrap(phase)
# Unwrapping по времени (между свипами)
phase_unwrapped_time, prev_phase_per_bin, phase_offset_per_bin = apply_temporal_unwrap(
phase_unwrapped_freq, prev_phase_per_bin, phase_offset_per_bin
)
phase_row = phase_unwrapped_time
ring_fft[(head - 1) % ring_fft.shape[0], :] = fft_row
# Экстремумы для цветовой шкалы
fr_min = np.nanmin(fft_row)
fr_max = np.nanmax(fft_row)
fr_max = np.nanpercentile(fft_row, 90)
if y_min_fft is None or (not np.isnan(fr_min) and fr_min < y_min_fft):
y_min_fft = float(fr_min)
if y_max_fft is None or (not np.isnan(fr_max) and fr_max > y_max_fft):
y_max_fft = float(fr_max)
# Сохраняем фазу в буфер
if ring_phase is not None:
ring_phase[(head - 1) % ring_phase.shape[0], :] = phase_row
# Экстремумы для цветовой шкалы фазы
ph_min = np.nanmin(phase_row)
ph_max = np.nanmax(phase_row)
if y_min_phase is None or (not np.isnan(ph_min) and ph_min < y_min_phase):
y_min_phase = float(ph_min)
if y_max_phase is None or (not np.isnan(ph_max) and ph_max > y_max_phase):
y_max_phase = float(ph_max)
def drain_queue():
nonlocal current_sweep, current_info
drained = 0
while True:
try:
s, info = q.get_nowait()
except Empty:
break
drained += 1
current_sweep = s
current_info = info
ensure_buffer(s.size)
push_sweep(s)
return drained
def make_display_ring():
# Возвращаем буфер с правильным порядком по времени (старые→новые) и осью времени по X
if ring is None:
return np.zeros((1, 1), dtype=np.float32)
base = ring if head == 0 else np.roll(ring, -head, axis=0)
return base.T # (width, time)
def make_display_times():
if ring_time is None:
return None
base_t = ring_time if head == 0 else np.roll(ring_time, -head)
return base_t
def make_display_ring_fft():
if ring_fft is None:
return np.zeros((1, 1), dtype=np.float32)
base = ring_fft if head == 0 else np.roll(ring_fft, -head, axis=0)
return base.T # (bins, time)
def make_display_ring_phase():
if ring_phase is None:
return np.zeros((1, 1), dtype=np.float32)
base = ring_phase if head == 0 else np.roll(ring_phase, -head, axis=0)
return base.T # (bins, time)
def update(_frame):
nonlocal frames_since_ylim_update, ref_out_saved
changed = drain_queue() > 0
# Обновление линии последнего свипа
if current_sweep is not None:
# Применяем вычитание медианы для отображения
display_sweep = current_sweep
if median_subtract_enabled and median_data is not None:
take_median = min(current_sweep.size, median_data.size)
display_sweep = current_sweep.copy()
display_sweep[:take_median] = current_sweep[:take_median] - median_data[:take_median]
if x_shared is not None and display_sweep.size <= x_shared.size:
xs = x_shared[: display_sweep.size]
else:
xs = np.arange(display_sweep.size, dtype=np.int32)
line_obj.set_data(xs, display_sweep)
# Лимиты по X постоянные под текущую ширину
ax_line.set_xlim(0, max(1, display_sweep.size - 1))
# Адаптивные Y-лимиты (если не задан --ylim)
if fixed_ylim is None:
y0 = float(np.nanmin(display_sweep))
y1 = float(np.nanmax(display_sweep))
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)
# Обновление спектра и фазы текущего свипа
take_fft = min(int(display_sweep.size), FFT_LEN)
if take_fft > 0 and freq_shared is not None:
# Создаем буфер для полного FFT (с отрицательными частотами)
fft_in = np.zeros((FFT_LEN,), dtype=np.float32)
# Вычисляем индексы для размещения данных (1-10 ГГц в диапазоне -10 до +10 ГГц)
freq_range_total = FREQ_MAX_GHZ - FREQ_MIN_GHZ # 20 ГГц
freq_range_data = DATA_FREQ_END_GHZ - DATA_FREQ_START_GHZ # 9 ГГц
# Начальный индекс для данных в FFT буфере
start_idx = int((DATA_FREQ_START_GHZ - FREQ_MIN_GHZ) / freq_range_total * FFT_LEN)
# Количество точек для данных
data_points = int(freq_range_data / freq_range_total * FFT_LEN)
data_points = min(data_points, take_fft, FFT_LEN - start_idx)
# Подготовка данных с окном Хэннинга
seg = np.nan_to_num(display_sweep[:data_points], nan=0.0).astype(np.float32, copy=False)
win = np.hanning(data_points).astype(np.float32)
# Размещаем данные в правильной позиции
fft_in[start_idx:start_idx + data_points] = seg * win
# Полное FFT (включая отрицательные частоты)
spec = np.fft.fft(fft_in)
# Сдвигаем для центрирования нулевой частоты
spec = np.fft.fftshift(spec)
mag = np.abs(spec).astype(np.float32)
fft_vals = 20.0 * np.log10(mag + 1e-9)
xs_fft = freq_shared
if fft_vals.size > xs_fft.size:
fft_vals = fft_vals[: xs_fft.size]
fft_line_obj.set_data(xs_fft[: fft_vals.size], fft_vals)
# Авто-диапазон по Y для спектра
if np.isfinite(np.nanmin(fft_vals)) and np.isfinite(np.nanmax(fft_vals)):
ax_fft.set_xlim(FREQ_MIN_GHZ, FREQ_MAX_GHZ)
ax_fft.set_ylim(float(np.nanmin(fft_vals)), float(np.nanmax(fft_vals)))
# Расчет и отображение фазы текущего свипа
phase = np.angle(spec).astype(np.float32)
if phase.size > xs_fft.size:
phase = phase[: xs_fft.size]
# Unwrapping по частоте
phase_unwrapped = np.unwrap(phase)
phase_line_obj.set_data(xs_fft[: phase_unwrapped.size], phase_unwrapped)
# Авто-диапазон по Y для фазы
if np.isfinite(np.nanmin(phase_unwrapped)) and np.isfinite(np.nanmax(phase_unwrapped)):
ax_phase.set_xlim(FREQ_MIN_GHZ, FREQ_MAX_GHZ)
phase_min = float(np.nanmin(phase_unwrapped))
phase_max = float(np.nanmax(phase_unwrapped))
ax_phase.set_ylim(phase_min, phase_max)
# Обновляем вторую ось Y с расстоянием
try:
dist_min = phase_to_distance(np.array([phase_min]))[0]
dist_max = phase_to_distance(np.array([phase_max]))[0]
ax_phase_dist.set_ylim(dist_min, dist_max)
except Exception:
pass
# Обновление водопада
if changed and ring is not None:
disp = make_display_ring()
# Новые данные справа: без реверса
img_obj.set_data(disp)
# Подписи времени не обновляем динамически (оставляем авто-тики)
# Авто-уровни: по видимой области (не накапливаем за всё время)
levels = _visible_levels_matplotlib(disp, ax_img)
if levels is not None:
img_obj.set_clim(vmin=levels[0], vmax=levels[1])
# Обновление водопада спектров
if changed and ring_fft is not None:
disp_fft = make_display_ring_fft()
# Новые данные справа: без реверса
img_fft_obj.set_data(disp_fft)
# Подписи времени не обновляем динамически (оставляем авто-тики)
# Автодиапазон по среднему спектру за видимый интервал (как в хорошей версии)
try:
# disp_fft имеет форму (bins, time); берём среднее по времени
mean_spec = np.nanmean(disp_fft, axis=1)
vmin_v = float(np.nanmin(mean_spec))
vmax_v = float(np.nanmax(mean_spec))
except Exception:
vmin_v = vmax_v = None
# Если средние не дают валидный диапазон — используем процентильную обрезку (если задана)
if (vmin_v is None or not np.isfinite(vmin_v)) or (vmax_v is None or not np.isfinite(vmax_v)) or vmin_v == vmax_v:
if spec_clip is not None:
try:
vmin_v = float(np.nanpercentile(disp_fft, spec_clip[0]))
vmax_v = float(np.nanpercentile(disp_fft, spec_clip[1]))
except Exception:
vmin_v = vmax_v = None
# Фолбэк к отслеживаемым минимум/максимумам
if (vmin_v is None or not np.isfinite(vmin_v)) or (vmax_v is None or not np.isfinite(vmax_v)) or vmin_v == vmax_v:
if y_min_fft is not None and y_max_fft is not None and np.isfinite(y_min_fft) and np.isfinite(y_max_fft) and y_min_fft != y_max_fft:
vmin_v, vmax_v = y_min_fft, y_max_fft
if vmin_v is not None and vmax_v is not None and vmin_v != vmax_v:
# Применим скалирование контрастом (верхняя граница)
try:
c = float(contrast_slider.val) / 100.0 if contrast_slider is not None else 1.0
except Exception:
c = 1.0
vmax_eff = vmin_v + c * (vmax_v - vmin_v)
img_fft_obj.set_clim(vmin=vmin_v, vmax=vmax_eff)
# Обновление водопада фазы
if changed and ring_phase is not None:
disp_phase = make_display_ring_phase()
img_phase_obj.set_data(disp_phase)
# Автодиапазон для фазы
try:
mean_phase = np.nanmean(disp_phase, axis=1)
vmin_p = float(np.nanmin(mean_phase))
vmax_p = float(np.nanmax(mean_phase))
except Exception:
vmin_p = vmax_p = None
# Фолбэк к отслеживаемым минимум/максимумам
if (vmin_p is None or not np.isfinite(vmin_p)) or (vmax_p is None or not np.isfinite(vmax_p)) or vmin_p == vmax_p:
if y_min_phase is not None and y_max_phase is not None and np.isfinite(y_min_phase) and np.isfinite(y_max_phase) and y_min_phase != y_max_phase:
vmin_p, vmax_p = y_min_phase, y_max_phase
if vmin_p is not None and vmax_p is not None and vmin_p != vmax_p:
img_phase_obj.set_clim(vmin=vmin_p, vmax=vmax_p)
if changed and current_info:
status_text.set_text(format_status_kv(current_info))
# Автоматическое сохранение медианы при накоплении 1000 сырых свипов
if ref_out_file and not ref_out_saved and ref_ring is not None:
if ref_ring_count >= 1000:
try:
ordered = ref_ring if ref_ring_head == 0 else np.roll(ref_ring, -ref_ring_head, axis=0)
median_sweep = np.nanmedian(ordered, axis=0)
with open(ref_out_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Index', 'Median_Value'])
for i, value in enumerate(median_sweep):
if np.isfinite(value):
writer.writerow([i, float(value)])
ref_out_saved = True
print(f"[ref-out] Сохранена медиана 1000 свипов в {ref_out_file}")
status_text.set_text(f"[ref-out] Сохранено в {ref_out_file}")
except Exception as e:
print(f"[ref-out] Ошибка сохранения: {e}")
# Возвращаем обновлённые артисты
return (line_obj, img_obj, fft_line_obj, img_fft_obj, phase_line_obj, img_phase_obj, status_text)
ani = FuncAnimation(fig, update, interval=interval_ms, blit=False)
plt.show()
# Нормальное завершение при закрытии окна
stop_event.set()
reader.join(timeout=1.0)

View File

@ -0,0 +1,705 @@
"""
Визуализация данных с использованием pyqtgraph (быстрый бэкенд).
"""
import csv
import sys
import threading
import time
from datetime import datetime
from queue import Empty, Queue
from typing import Optional, Tuple
import numpy as np
try:
import pyqtgraph as pg
from PyQt5 import QtCore, QtWidgets # noqa: F401
from PyQt5.QtWidgets import QPushButton, QWidget, QHBoxLayout, QCheckBox, QFileDialog
except Exception:
# Возможно установлена PySide6
try:
import pyqtgraph as pg
from PySide6 import QtCore, QtWidgets # noqa: F401
from PySide6.QtWidgets import QPushButton, QWidget, QHBoxLayout, QCheckBox, QFileDialog
except Exception as e:
raise RuntimeError(
"pyqtgraph/PyQt5(Pyside6) не найдены. Установите: pip install pyqtgraph PyQt5"
) from e
from ..config import (
FFT_LEN,
WF_WIDTH,
SweepInfo,
SweepPacket,
FREQ_MIN_GHZ,
FREQ_MAX_GHZ,
DATA_FREQ_START_GHZ,
DATA_FREQ_END_GHZ,
)
from ..data_acquisition.sweep_reader import SweepReader
from ..signal_processing.phase_analysis import apply_temporal_unwrap, phase_to_distance
from ..utils.formatting import format_status_kv, parse_spec_clip
def run_pyqtgraph(args):
"""Быстрый GUI на PyQtGraph. Требует pyqtgraph и PyQt5/PySide6."""
# Очередь завершённых свипов и поток чтения
q: Queue[SweepPacket] = Queue(maxsize=1000)
stop_event = threading.Event()
reader = SweepReader(args.port, args.baud, q, stop_event, fancy=bool(args.fancy))
reader.start()
# Настройки скорости
max_sweeps = int(max(10, args.max_sweeps))
max_fps = max(1.0, float(args.max_fps))
interval_ms = int(1000.0 / max_fps)
# PyQtGraph настройки
pg.setConfigOptions(useOpenGL=True, antialias=False)
app = pg.mkQApp(args.title)
win = pg.GraphicsLayoutWidget(show=True, title=args.title)
win.resize(1200, 900)
# Плот последнего свипа (слева-сверху)
p_line = win.addPlot(row=0, col=0, title="Сырые данные")
p_line.showGrid(x=True, y=True, alpha=0.3)
curve = p_line.plot(pen=pg.mkPen((80, 120, 255), width=1))
p_line.setLabel("bottom", "X")
p_line.setLabel("left", "Y")
# Водопад (справа-сверху)
p_img = win.addPlot(row=0, col=1, title="Сырые данные водопад")
p_img.invertY(False)
p_img.showGrid(x=False, y=False)
p_img.setLabel("bottom", "Время, с (новое справа)")
try:
p_img.getAxis("bottom").setStyle(showValues=False)
except Exception:
pass
p_img.setLabel("left", "X (0 снизу)")
img = pg.ImageItem()
p_img.addItem(img)
# FFT (слева-средний ряд)
p_fft = win.addPlot(row=1, col=0, title="FFT")
p_fft.showGrid(x=True, y=True, alpha=0.3)
curve_fft = p_fft.plot(pen=pg.mkPen((255, 120, 80), width=1))
p_fft.setLabel("bottom", "Частота, ГГц")
p_fft.setLabel("left", "Амплитуда, дБ")
# Водопад спектров (справа-средний ряд)
p_spec = win.addPlot(row=1, col=1, title="B-scan (дБ)")
p_spec.invertY(True)
p_spec.showGrid(x=False, y=False)
p_spec.setLabel("bottom", "Время, с (новое справа)")
try:
p_spec.getAxis("bottom").setStyle(showValues=False)
except Exception:
pass
p_spec.setLabel("left", "Частота, ГГц (0 снизу)")
img_fft = pg.ImageItem()
p_spec.addItem(img_fft)
# График фазы (слева-снизу)
p_phase = win.addPlot(row=2, col=0, title="Фаза спектра (развернутая)")
p_phase.showGrid(x=True, y=True, alpha=0.3)
curve_phase = p_phase.plot(pen=pg.mkPen((120, 255, 80), width=1))
p_phase.setLabel("bottom", "Частота, ГГц")
p_phase.setLabel("left", "Фаза, радианы")
# Добавим вторую ось Y для расстояния
p_phase_dist_axis = pg.ViewBox()
p_phase.showAxis("right")
p_phase.scene().addItem(p_phase_dist_axis)
p_phase.getAxis("right").linkToView(p_phase_dist_axis)
p_phase_dist_axis.setXLink(p_phase)
p_phase.setLabel("right", "Расстояние, м")
def updateViews():
try:
p_phase_dist_axis.setGeometry(p_phase.vb.sceneBoundingRect())
p_phase_dist_axis.linkedViewChanged(p_phase.vb, p_phase_dist_axis.XAxis)
except Exception:
pass
updateViews()
p_phase.vb.sigResized.connect(updateViews)
# Водопад фазы (справа-снизу)
p_phase_wf = win.addPlot(row=2, col=1, title="Водопад фазы")
p_phase_wf.invertY(True)
p_phase_wf.showGrid(x=False, y=False)
p_phase_wf.setLabel("bottom", "Время, с (новое справа)")
try:
p_phase_wf.getAxis("bottom").setStyle(showValues=False)
except Exception:
pass
p_phase_wf.setLabel("left", "Частота, ГГц (0 снизу)")
img_phase = pg.ImageItem()
p_phase_wf.addItem(img_phase)
# Статусная строка (внизу окна)
status = pg.LabelItem(justify="left")
win.addItem(status, row=3, col=0, colspan=2)
# Функция сохранения медианы последних 1000 свипов
def save_median_data():
"""Сохранить медиану последних 1000 свипов в CSV файл"""
if ring is None:
status.setText("Нет данных для сохранения")
return
# Определяем сколько свипов доступно
n_sweeps = 1000
available = min(n_sweeps, max_sweeps)
# Проверяем сколько свипов реально заполнено
filled_count = np.count_nonzero(~np.isnan(ring[:, 0]))
if filled_count == 0:
status.setText("Нет данных для сохранения")
return
available = min(available, filled_count)
# Получаем хронологически упорядоченные данные
ordered = ring if head == 0 else np.roll(ring, -head, axis=0)
# Берем последние n свипов
recent_sweeps = ordered[-available:, :]
# Вычисляем медиану по свипам (ось 0)
median_sweep = np.nanmedian(recent_sweeps, axis=0)
# Сохраняем в CSV
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"median_sweep_{timestamp}.csv"
try:
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Index', 'Median_Value'])
for i, value in enumerate(median_sweep):
if np.isfinite(value):
writer.writerow([i, float(value)])
status.setText(f"Сохранено {available} свипов (медиана) в {filename}")
except Exception as e:
status.setText(f"Ошибка сохранения: {e}")
# Функция загрузки медианного файла
def load_median_file():
"""Загрузить медианный файл из CSV"""
nonlocal median_data
filename, _ = QFileDialog.getOpenFileName(
None,
"Выберите файл с медианой",
"",
"CSV Files (*.csv);;All Files (*)"
)
if not filename:
return
try:
# Загружаем CSV файл
pairs = []
with open(filename, 'r') as f:
reader = csv.reader(f)
next(reader) # Пропускаем заголовок
for row in reader:
if len(row) >= 2:
try:
pairs.append((int(row[0]), float(row[1])))
except ValueError:
continue
if not pairs:
status.setText("Ошибка: файл пустой или неверный формат")
return
max_idx = max(idx for idx, _ in pairs)
median_data = np.full(max_idx + 1, np.nan, dtype=np.float32)
for idx, val in pairs:
median_data[idx] = val
status.setText(f"Загружена медиана из {filename} ({len(median_data)} точек)")
# Автоматически включаем чекбокс
subtract_checkbox.setChecked(True)
except Exception as e:
status.setText(f"Ошибка загрузки: {e}")
median_data = None
# Функция переключения вычитания медианы
def toggle_median_subtraction(state):
nonlocal median_subtract_enabled
median_subtract_enabled = bool(state)
if median_subtract_enabled and median_data is None:
status.setText("Сначала загрузите файл с медианой")
subtract_checkbox.setChecked(False)
elif median_subtract_enabled:
status.setText("Вычитание медианы включено")
else:
status.setText("Вычитание медианы выключено")
# Создаем контейнер для кнопок управления
button_container = QWidget()
button_layout = QHBoxLayout()
# Кнопка сохранения медианы
save_btn = QPushButton("Сохранить медиану (1000 свипов)")
save_btn.clicked.connect(save_median_data)
button_layout.addWidget(save_btn)
# Кнопка загрузки медианы
load_btn = QPushButton("Загрузить медиану")
load_btn.clicked.connect(load_median_file)
button_layout.addWidget(load_btn)
# Чекбокс для включения вычитания
subtract_checkbox = QCheckBox("Вычитать медиану")
subtract_checkbox.stateChanged.connect(toggle_median_subtraction)
button_layout.addWidget(subtract_checkbox)
button_layout.setContentsMargins(5, 5, 5, 5)
button_container.setLayout(button_layout)
# Добавляем кнопки в окно
proxy_widget = QtWidgets.QGraphicsProxyWidget()
proxy_widget.setWidget(button_container)
win.addItem(proxy_widget, row=4, col=0, colspan=2)
# Состояние
ring: Optional[np.ndarray] = None
head = 0
width: Optional[int] = None
x_shared: Optional[np.ndarray] = None
current_sweep: Optional[np.ndarray] = None
current_info: Optional[SweepInfo] = None
# Медианные данные для вычитания
median_data: Optional[np.ndarray] = None
median_subtract_enabled = False
# CLI параметры для автоматического сохранения/загрузки
ref_out_file = getattr(args, 'ref_out', None)
ref_in_file = getattr(args, 'ref_in', None)
ref_out_saved = False # Флаг, что медиана уже сохранена
# Отдельный буфер для накопления 1000 сырых свипов (не зависит от max_sweeps)
ref_ring: Optional[np.ndarray] = None
ref_ring_head = 0
ref_ring_count = 0
# Автоматическая загрузка медианы при старте
if ref_in_file:
try:
pairs = []
with open(ref_in_file, 'r') as f:
reader = csv.reader(f)
next(reader) # Пропускаем заголовок
for row in reader:
if len(row) >= 2:
try:
pairs.append((int(row[0]), float(row[1])))
except ValueError:
continue
if pairs:
max_idx = max(idx for idx, _ in pairs)
median_data = np.full(max_idx + 1, np.nan, dtype=np.float32)
for idx, val in pairs:
median_data[idx] = val
median_subtract_enabled = True
print(f"[ref-in] Загружена медиана из {ref_in_file} ({len(median_data)} точек), вычитание включено")
else:
print(f"[ref-in] Предупреждение: файл {ref_in_file} пустой или неверный формат")
except Exception as e:
print(f"[ref-in] Ошибка загрузки {ref_in_file}: {e}")
# Авто-уровни цветовой шкалы водопада сырых данных пересчитываются по видимой области.
# Для спектров (полное FFT для отрицательных частот)
fft_bins = FFT_LEN
ring_fft: Optional[np.ndarray] = None
freq_shared: Optional[np.ndarray] = None
y_min_fft, y_max_fft = None, None
# Phase состояние
ring_phase: Optional[np.ndarray] = None
prev_phase_per_bin: Optional[np.ndarray] = None
phase_offset_per_bin: Optional[np.ndarray] = None
y_min_phase, y_max_phase = None, None
# Параметры контраста водопада спектров (процентильная обрезка)
spec_clip = parse_spec_clip(getattr(args, "spec_clip", None))
# Диапазон по Y: авто по умолчанию (поддерживает отрицательные значения)
fixed_ylim: Optional[Tuple[float, float]] = None
if args.ylim:
try:
y0, y1 = args.ylim.split(",")
fixed_ylim = (float(y0), float(y1))
except Exception:
pass
if fixed_ylim is not None:
p_line.setYRange(fixed_ylim[0], fixed_ylim[1], padding=0)
def ensure_buffer(_w: int):
nonlocal ring, head, width, x_shared, ring_fft, freq_shared
nonlocal ring_phase, prev_phase_per_bin, phase_offset_per_bin
nonlocal ref_ring
if ring is not None:
return
width = WF_WIDTH
x_shared = np.arange(width, dtype=np.int32)
ring = np.full((max_sweeps, width), np.nan, dtype=np.float32)
head = 0
# Водопад: время по оси X, X по оси Y
img.setImage(ring.T, autoLevels=False)
p_img.setRange(xRange=(0, max_sweeps - 1), yRange=(0, max(1, width - 1)), padding=0)
p_line.setXRange(0, max(1, width - 1), padding=0)
# FFT: время по оси X, бин по оси Y
ring_fft = np.full((max_sweeps, fft_bins), np.nan, dtype=np.float32)
img_fft.setImage(ring_fft.T, autoLevels=False)
p_spec.setRange(xRange=(0, max_sweeps - 1), yRange=(0, max(1, fft_bins - 1)), padding=0)
p_fft.setXRange(FREQ_MIN_GHZ, FREQ_MAX_GHZ, padding=0)
freq_shared = np.linspace(FREQ_MIN_GHZ, FREQ_MAX_GHZ, fft_bins, dtype=np.float32)
# Phase: время по оси X, бин по оси Y
ring_phase = np.full((max_sweeps, fft_bins), np.nan, dtype=np.float32)
prev_phase_per_bin = np.zeros(fft_bins, dtype=np.float32)
phase_offset_per_bin = np.zeros(fft_bins, dtype=np.float32)
img_phase.setImage(ring_phase.T, autoLevels=False)
p_phase_wf.setRange(xRange=(0, max_sweeps - 1), yRange=(0, max(1, fft_bins - 1)), padding=0)
p_phase.setXRange(0, max(1, fft_bins - 1), padding=0)
# Буфер для медианы (отдельный от ring, размер всегда 1000)
if ref_out_file and ref_ring is None:
ref_ring = np.full((1000, width), np.nan, dtype=np.float32)
def _visible_levels_pyqtgraph(data: np.ndarray) -> Optional[Tuple[float, float]]:
"""(vmin, vmax) по текущей видимой области ImageItem (без накопления по времени)."""
if data.size == 0:
return None
ny, nx = data.shape[0], data.shape[1]
try:
(x0, x1), (y0, y1) = p_img.viewRange()
except Exception:
x0, x1 = 0.0, float(nx - 1)
y0, y1 = 0.0, float(ny - 1)
xmin, xmax = sorted((float(x0), float(x1)))
ymin, ymax = sorted((float(y0), float(y1)))
ix0 = max(0, min(nx - 1, int(np.floor(xmin))))
ix1 = max(0, min(nx - 1, int(np.ceil(xmax))))
iy0 = max(0, min(ny - 1, int(np.floor(ymin))))
iy1 = max(0, min(ny - 1, int(np.ceil(ymax))))
if ix1 < ix0:
ix1 = ix0
if iy1 < iy0:
iy1 = iy0
sub = data[iy0 : iy1 + 1, ix0 : ix1 + 1]
finite = np.isfinite(sub)
if not finite.any():
return None
vals = sub[finite]
vmin = float(np.min(vals))
vmax = float(np.max(vals))
if not (np.isfinite(vmin) and np.isfinite(vmax)) or vmin == vmax:
return None
return (vmin, vmax)
def push_sweep(s: np.ndarray):
nonlocal ring, head, ring_fft, y_min_fft, y_max_fft
nonlocal ring_phase, prev_phase_per_bin, phase_offset_per_bin, y_min_phase, y_max_phase
nonlocal ref_ring_head, ref_ring_count
if s is None or s.size == 0 or ring is None:
return
# Сохраняем сырой свип в буфер медианы (до вычитания)
if ref_out_file and not ref_out_saved and ref_ring is not None:
w_ref = ref_ring.shape[1]
take_ref = min(w_ref, s.size)
ref_ring[ref_ring_head, :take_ref] = s[:take_ref]
ref_ring_head = (ref_ring_head + 1) % 1000
ref_ring_count = min(ref_ring_count + 1, 1000)
# Применяем вычитание медианы если включено
if median_subtract_enabled and median_data is not None:
# Вычитаем медиану из сигнала
take_median = min(s.size, median_data.size)
s_corrected = s.copy()
s_corrected[:take_median] = s[:take_median] - median_data[:take_median]
s = s_corrected
w = ring.shape[1]
row = np.full((w,), np.nan, dtype=np.float32)
take = min(w, s.size)
row[:take] = s[:take]
ring[head, :] = row
head = (head + 1) % ring.shape[0]
# FFT строка (дБ) и фаза
if ring_fft is not None:
bins = ring_fft.shape[1]
take_fft = min(int(s.size), FFT_LEN)
if take_fft > 0:
# Создаем буфер для полного FFT (с отрицательными частотами)
fft_in = np.zeros((FFT_LEN,), dtype=np.float32)
# Вычисляем индексы для размещения данных (1-10 ГГц в диапазоне -10 до +10 ГГц)
# Диапазон данных: от DATA_FREQ_START_GHZ (1) до DATA_FREQ_END_GHZ (10)
# Полный диапазон: от FREQ_MIN_GHZ (-10) до FREQ_MAX_GHZ (10)
freq_range_total = FREQ_MAX_GHZ - FREQ_MIN_GHZ # 20 ГГц
freq_range_data = DATA_FREQ_END_GHZ - DATA_FREQ_START_GHZ # 9 ГГц
# Начальный индекс для данных в FFT буфере
start_idx = int((DATA_FREQ_START_GHZ - FREQ_MIN_GHZ) / freq_range_total * FFT_LEN)
# Количество точек для данных
data_points = int(freq_range_data / freq_range_total * FFT_LEN)
data_points = min(data_points, take_fft, FFT_LEN - start_idx)
# Подготовка данных с окном Хэннинга
seg = np.nan_to_num(s[:data_points], nan=0.0).astype(np.float32, copy=False)
win = np.hanning(data_points).astype(np.float32)
# Размещаем данные в правильной позиции (от -10 до 1 ГГц - нули, от 1 до 10 ГГц - данные)
fft_in[start_idx:start_idx + data_points] = seg * win
# Полное FFT (включая отрицательные частоты)
spec = np.fft.fft(fft_in)
# Сдвигаем для центрирования нулевой частоты
spec = np.fft.fftshift(spec)
mag = np.abs(spec).astype(np.float32)
fft_row = 20.0 * np.log10(mag + 1e-9)
if fft_row.shape[0] != bins:
fft_row = fft_row[:bins]
# Расчет фазы
phase = np.angle(spec).astype(np.float32)
if phase.shape[0] > bins:
phase = phase[:bins]
# Unwrapping по частоте (внутри свипа)
phase_unwrapped_freq = np.unwrap(phase)
# Unwrapping по времени (между свипами)
phase_unwrapped_time, prev_phase_per_bin, phase_offset_per_bin = apply_temporal_unwrap(
phase_unwrapped_freq, prev_phase_per_bin, phase_offset_per_bin
)
phase_row = phase_unwrapped_time
else:
fft_row = np.full((bins,), np.nan, dtype=np.float32)
phase_row = np.full((bins,), np.nan, dtype=np.float32)
ring_fft[(head - 1) % ring_fft.shape[0], :] = fft_row
fr_min = np.nanmin(fft_row)
fr_max = np.nanmax(fft_row)
if y_min_fft is None or (not np.isnan(fr_min) and fr_min < y_min_fft):
y_min_fft = float(fr_min)
if y_max_fft is None or (not np.isnan(fr_max) and fr_max > y_max_fft):
y_max_fft = float(fr_max)
# Сохраняем фазу в буфер
if ring_phase is not None:
ring_phase[(head - 1) % ring_phase.shape[0], :] = phase_row
# Экстремумы для цветовой шкалы фазы
ph_min = np.nanmin(phase_row)
ph_max = np.nanmax(phase_row)
if y_min_phase is None or (not np.isnan(ph_min) and ph_min < y_min_phase):
y_min_phase = float(ph_min)
if y_max_phase is None or (not np.isnan(ph_max) and ph_max > y_max_phase):
y_max_phase = float(ph_max)
def drain_queue():
nonlocal current_sweep, current_info
drained = 0
while True:
try:
s, info = q.get_nowait()
except Empty:
break
drained += 1
current_sweep = s
current_info = info
ensure_buffer(s.size)
push_sweep(s)
return drained
# Попытка применить LUT из колормэпа (если доступен)
try:
cm_mod = getattr(pg, "colormap", None)
if cm_mod is not None:
cm = cm_mod.get(args.cmap)
img.setLookupTable(cm.getLookupTable(0.0, 1.0, 256))
except Exception:
pass
def update():
nonlocal ref_out_saved
changed = drain_queue() > 0
if current_sweep is not None and x_shared is not None:
# Применяем вычитание медианы для отображения
display_sweep = current_sweep
if median_subtract_enabled and median_data is not None:
take_median = min(current_sweep.size, median_data.size)
display_sweep = current_sweep.copy()
display_sweep[:take_median] = current_sweep[:take_median] - median_data[:take_median]
if display_sweep.size <= x_shared.size:
xs = x_shared[: display_sweep.size]
else:
xs = np.arange(display_sweep.size)
curve.setData(xs, display_sweep, autoDownsample=True)
if fixed_ylim is None:
y0 = float(np.nanmin(display_sweep))
y1 = float(np.nanmax(display_sweep))
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)
# Обновим спектр и фазу
take_fft = min(int(display_sweep.size), FFT_LEN)
if take_fft > 0 and freq_shared is not None:
# Создаем буфер для полного FFT (с отрицательными частотами)
fft_in = np.zeros((FFT_LEN,), dtype=np.float32)
# Вычисляем индексы для размещения данных (1-10 ГГц в диапазоне -10 до +10 ГГц)
freq_range_total = FREQ_MAX_GHZ - FREQ_MIN_GHZ # 20 ГГц
freq_range_data = DATA_FREQ_END_GHZ - DATA_FREQ_START_GHZ # 9 ГГц
# Начальный индекс для данных в FFT буфере
start_idx = int((DATA_FREQ_START_GHZ - FREQ_MIN_GHZ) / freq_range_total * FFT_LEN)
# Количество точек для данных
data_points = int(freq_range_data / freq_range_total * FFT_LEN)
data_points = min(data_points, take_fft, FFT_LEN - start_idx)
# Подготовка данных с окном Хэннинга
seg = np.nan_to_num(display_sweep[:data_points], nan=0.0).astype(np.float32, copy=False)
win = np.hanning(data_points).astype(np.float32)
# Размещаем данные в правильной позиции
fft_in[start_idx:start_idx + data_points] = seg * win
# Полное FFT (включая отрицательные частоты)
spec = np.fft.fft(fft_in)
# Сдвигаем для центрирования нулевой частоты
spec = np.fft.fftshift(spec)
mag = np.abs(spec).astype(np.float32)
fft_vals = 20.0 * np.log10(mag + 1e-9)
xs_fft = freq_shared
if fft_vals.size > xs_fft.size:
fft_vals = fft_vals[: xs_fft.size]
curve_fft.setData(xs_fft[: fft_vals.size], fft_vals)
p_fft.setYRange(float(np.nanmin(fft_vals)), float(np.nanmax(fft_vals)), padding=0)
# Расчет и отображение фазы текущего свипа
phase = np.angle(spec).astype(np.float32)
if phase.size > xs_fft.size:
phase = phase[: xs_fft.size]
# Unwrapping по частоте
phase_unwrapped = np.unwrap(phase)
curve_phase.setData(xs_fft[: phase_unwrapped.size], phase_unwrapped)
phase_min = float(np.nanmin(phase_unwrapped))
phase_max = float(np.nanmax(phase_unwrapped))
p_phase.setYRange(phase_min, phase_max, padding=0)
# Обновляем вторую ось Y с расстоянием
try:
dist_min = phase_to_distance(np.array([phase_min]))[0]
dist_max = phase_to_distance(np.array([phase_max]))[0]
p_phase_dist_axis.setYRange(dist_min, dist_max, padding=0)
except Exception:
pass
if changed and ring is not None:
disp = ring if head == 0 else np.roll(ring, -head, axis=0)
disp = disp.T[:, ::-1] # (width, time with newest at left)
levels = _visible_levels_pyqtgraph(disp)
if levels is not None:
img.setImage(disp, autoLevels=False, levels=levels)
else:
img.setImage(disp, autoLevels=False)
if changed and current_info:
try:
status.setText(format_status_kv(current_info))
except Exception:
pass
# Автоматическое сохранение медианы при накоплении 1000 сырых свипов
if ref_out_file and not ref_out_saved and ref_ring is not None:
if ref_ring_count >= 1000:
try:
ordered = ref_ring if ref_ring_head == 0 else np.roll(ref_ring, -ref_ring_head, axis=0)
median_sweep = np.nanmedian(ordered, axis=0)
with open(ref_out_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Index', 'Median_Value'])
for i, value in enumerate(median_sweep):
if np.isfinite(value):
writer.writerow([i, float(value)])
ref_out_saved = True
print(f"[ref-out] Сохранена медиана 1000 свипов в {ref_out_file}")
if status:
status.setText(f"[ref-out] Сохранено в {ref_out_file}")
except Exception as e:
print(f"[ref-out] Ошибка сохранения: {e}")
if changed and ring_fft is not None:
disp_fft = ring_fft if head == 0 else np.roll(ring_fft, -head, axis=0)
disp_fft = disp_fft.T[:, ::-1]
# Автодиапазон по среднему спектру за видимый интервал (как в хорошей версии)
levels = None
try:
mean_spec = np.nanmean(disp_fft, axis=1)
vmin_v = float(np.nanmin(mean_spec))
vmax_v = float(np.nanmax(mean_spec))
if np.isfinite(vmin_v) and np.isfinite(vmax_v) and vmin_v != vmax_v:
levels = (vmin_v, vmax_v)
except Exception:
levels = None
# Процентильная обрезка как запасной вариант
if levels is None and spec_clip is not None:
try:
vmin_v = float(np.nanpercentile(disp_fft, spec_clip[0]))
vmax_v = float(np.nanpercentile(disp_fft, spec_clip[1]))
if np.isfinite(vmin_v) and np.isfinite(vmax_v) and vmin_v != vmax_v:
levels = (vmin_v, vmax_v)
except Exception:
levels = None
# Ещё один фолбэк — глобальные накопленные мин/макс
if levels is None and y_min_fft is not None and y_max_fft is not None and np.isfinite(y_min_fft) and np.isfinite(y_max_fft) and y_min_fft != y_max_fft:
levels = (y_min_fft, y_max_fft)
if levels is not None:
img_fft.setImage(disp_fft, autoLevels=False, levels=levels)
else:
img_fft.setImage(disp_fft, autoLevels=False)
# Обновление водопада фазы
if changed and ring_phase is not None:
disp_phase = ring_phase if head == 0 else np.roll(ring_phase, -head, axis=0)
disp_phase = disp_phase.T[:, ::-1]
# Автодиапазон для фазы
levels_phase = None
try:
mean_phase = np.nanmean(disp_phase, axis=1)
vmin_p = float(np.nanmin(mean_phase))
vmax_p = float(np.nanmax(mean_phase))
if np.isfinite(vmin_p) and np.isfinite(vmax_p) and vmin_p != vmax_p:
levels_phase = (vmin_p, vmax_p)
except Exception:
levels_phase = None
# Фолбэк к отслеживаемым минимум/максимумам
if levels_phase is None and y_min_phase is not None and y_max_phase is not None and np.isfinite(y_min_phase) and np.isfinite(y_max_phase) and y_min_phase != y_max_phase:
levels_phase = (y_min_phase, y_max_phase)
if levels_phase is not None:
img_phase.setImage(disp_phase, autoLevels=False, levels=levels_phase)
else:
img_phase.setImage(disp_phase, autoLevels=False)
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(interval_ms)
def on_quit():
stop_event.set()
reader.join(timeout=1.0)
app.aboutToQuit.connect(on_quit)
win.show()
exec_fn = getattr(app, "exec_", None) or getattr(app, "exec", None)
exec_fn()
# На случай если aboutToQuit не сработал
on_quit()

9
run.py Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
"""
Скрипт запуска RFG ADC Data Plotter.
"""
from rfg_adc_plotter.cli import main
if __name__ == "__main__":
main()

View File

@ -1,2 +0,0 @@
#!/usr/bin/bash
python3 -m rfg_adc_plotter.main --bin --backend mpl $@

6447
test2.ipynb Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,84 +0,0 @@
from pathlib import Path
import numpy as np
from rfg_adc_plotter.io.capture_reference_loader import (
aggregate_capture_reference,
detect_reference_file_format,
load_capture_sweeps,
)
from rfg_adc_plotter.processing.pipeline import SweepPreprocessor
ROOT = Path(__file__).resolve().parents[1]
SAMPLE_BG = ROOT / "sample_data" / "empty"
SAMPLE_CALIB = ROOT / "sample_data" / "no_antennas_35dB_attenuators"
def test_detect_reference_file_format_for_sample_capture():
assert detect_reference_file_format(str(SAMPLE_BG)) == "bin_capture"
assert detect_reference_file_format(str(SAMPLE_CALIB)) == "bin_capture"
def test_load_capture_sweeps_parses_binary_capture():
sweeps = load_capture_sweeps(str(SAMPLE_BG), fancy=False, logscale=False)
assert len(sweeps) > 100
sweep0, info0 = sweeps[0]
assert isinstance(sweep0, np.ndarray)
assert "ch" in info0
channels = set()
for _s, info in sweeps:
chs = info.get("chs", [info.get("ch", 0)])
channels.update(int(v) for v in chs)
assert channels == {0}
def test_aggregate_capture_reference_filters_incomplete_sweeps():
sweeps = load_capture_sweeps(str(SAMPLE_BG), fancy=False, logscale=False)
vector, summary = aggregate_capture_reference(sweeps, channel=0, method="median", path=str(SAMPLE_BG))
assert isinstance(vector, np.ndarray)
assert vector.dtype == np.float32
assert summary.sweeps_total == len(sweeps)
assert summary.sweeps_valid > 0
assert summary.sweeps_valid < summary.sweeps_total
assert summary.dominant_width in (759, 758) # sample_data starts at x=1..758 => width=759
def test_preprocessor_can_load_capture_calib_and_background_and_apply():
p = SweepPreprocessor(norm_type="projector", auto_save_live_calib_envelope=False)
p.set_capture_parse_options(fancy=False, logscale=False)
assert p.load_calib_reference(str(SAMPLE_CALIB))
p.set_calib_mode("file")
p.set_calib_enabled(True)
assert p.calib_file_envelope is not None
assert p.calib_external_source_type == "capture"
assert p.load_background_reference(str(SAMPLE_BG))
p.set_background_enabled(True)
assert p.background_source_type == "capture_raw"
n = min(758, int(p.calib_file_envelope.size))
sweep = np.linspace(-100.0, 100.0, n, dtype=np.float32)
res = p.process(sweep, channel=1, update_references=False)
assert res.calibration_applied is True
assert res.background_applied is True
assert res.calibration_source == "capture"
assert "background_capture(raw->calib)" in res.stage_trace
def test_preprocessor_applies_background_for_ch0_reference_too():
p = SweepPreprocessor(norm_type="projector", auto_save_live_calib_envelope=False)
p.set_capture_parse_options(fancy=False, logscale=False)
assert p.load_background_reference(str(SAMPLE_BG))
p.set_background_enabled(True)
n = min(758, int(p.background.size)) if p.background is not None else 758
raw = np.linspace(-10.0, 10.0, n, dtype=np.float32)
res = p.process(raw, channel=0, update_references=True)
assert res.is_calibration_reference is True
assert res.background_applied is True
assert np.any(np.abs(res.processed_sweep - raw) > 0)
assert p.last_calib_sweep is not None
assert np.allclose(p.last_calib_sweep[:n], raw[:n], equal_nan=True)

View File

@ -1,54 +0,0 @@
import numpy as np
from rfg_adc_plotter.processing.fourier import (
compute_ifft_profile_from_sweep,
reconstruct_complex_spectrum_from_real_trace,
)
def test_reconstruct_complex_spectrum_arccos_mode_returns_complex128():
sweep = np.linspace(-3.0, 7.0, 128, dtype=np.float32)
z = reconstruct_complex_spectrum_from_real_trace(sweep, complex_mode="arccos")
assert z.dtype == np.complex128
assert z.shape == sweep.shape
assert np.all(np.isfinite(np.real(z)))
assert np.all(np.isfinite(np.imag(z)))
def test_reconstruct_complex_spectrum_diff_mode_returns_complex128():
sweep = np.linspace(-1.0, 1.0, 128, dtype=np.float32)
z = reconstruct_complex_spectrum_from_real_trace(sweep, complex_mode="diff")
assert z.dtype == np.complex128
assert z.shape == sweep.shape
assert np.all(np.isfinite(np.real(z)))
assert np.all(np.isfinite(np.imag(z)))
def test_reconstruct_complex_spectrum_diff_mode_projects_to_unit_circle():
sweep = np.sin(np.linspace(0.0, 6.0 * np.pi, 256)).astype(np.float32)
z = reconstruct_complex_spectrum_from_real_trace(sweep, complex_mode="diff")
mag = np.abs(z)
assert np.all(np.isfinite(mag))
assert np.allclose(mag, np.ones_like(mag), atol=1e-5, rtol=0.0)
def test_compute_ifft_profile_from_sweep_accepts_both_modes():
sweep = np.linspace(-5.0, 5.0, 257, dtype=np.float32)
d1, y1 = compute_ifft_profile_from_sweep(sweep, complex_mode="arccos")
d2, y2 = compute_ifft_profile_from_sweep(sweep, complex_mode="diff")
assert d1.dtype == np.float32 and y1.dtype == np.float32
assert d2.dtype == np.float32 and y2.dtype == np.float32
assert d1.size == y1.size and d2.size == y2.size
assert d1.size > 0 and d2.size > 0
assert np.all(np.diff(d1) >= 0.0)
assert np.all(np.diff(d2) >= 0.0)
def test_invalid_complex_mode_falls_back_deterministically_in_outer_wrapper():
sweep = np.linspace(-1.0, 1.0, 64, dtype=np.float32)
depth, y = compute_ifft_profile_from_sweep(sweep, complex_mode="unknown")
assert depth.dtype == np.float32
assert y.dtype == np.float32
assert depth.size == y.size
assert depth.size > 0

View File

@ -1,75 +0,0 @@
import numpy as np
from rfg_adc_plotter.processing.fourier import (
build_frequency_axis_hz,
compute_ifft_profile_from_sweep,
normalize_sweep_for_phase,
perform_ifft_depth_response,
reconstruct_complex_spectrum_from_real_trace,
unwrap_arccos_phase_continuous,
)
def test_normalize_sweep_for_phase_max_abs_and_finite():
sweep = np.array([np.nan, -10.0, 5.0, 20.0, -40.0, np.inf, -np.inf], dtype=np.float32)
x = normalize_sweep_for_phase(sweep)
assert x.dtype == np.float64
assert np.all(np.isfinite(x))
assert np.max(np.abs(x)) <= 1.0 + 1e-12
def test_arccos_unwrap_continuous_recovers_complex_phase_without_large_jumps():
phi_true = np.linspace(0.0, 4.0 * np.pi, 1000, dtype=np.float64)
x = np.cos(phi_true)
phi_rec = unwrap_arccos_phase_continuous(x)
assert phi_rec.shape == phi_true.shape
assert np.max(np.abs(np.diff(phi_rec))) < 0.2
z_true = np.exp(1j * phi_true)
z_rec = np.exp(1j * phi_rec)
assert np.allclose(z_rec, z_true, atol=2e-2, rtol=0.0)
def test_reconstruct_complex_spectrum_from_real_trace_output_complex128():
sweep = np.linspace(-1.0, 1.0, 64, dtype=np.float32)
z = reconstruct_complex_spectrum_from_real_trace(sweep)
assert z.dtype == np.complex128
assert z.shape == sweep.shape
assert np.all(np.isfinite(np.real(z)))
assert np.all(np.isfinite(np.imag(z)))
def test_perform_ifft_depth_response_basic_abs():
n = 128
freqs = build_frequency_axis_hz(n)
s = np.exp(1j * np.linspace(0.0, 2.0 * np.pi, n, dtype=np.float64))
depth_m, y = perform_ifft_depth_response(s, freqs, axis="abs")
assert depth_m.dtype == np.float32
assert y.dtype == np.float32
assert depth_m.ndim == 1 and y.ndim == 1
assert depth_m.size == y.size
assert depth_m.size >= n
assert np.all(np.diff(depth_m) >= 0.0)
assert np.all(y >= 0.0)
def test_perform_ifft_depth_response_bad_grid_returns_fallback_not_exception():
s = np.ones(16, dtype=np.complex128)
freqs_desc = np.linspace(10.0, 1.0, 16, dtype=np.float64)
depth_m, y = perform_ifft_depth_response(s, freqs_desc, axis="abs")
assert depth_m.size == y.size
assert depth_m.size == s.size
assert np.all(np.isfinite(depth_m))
def test_compute_ifft_profile_from_sweep_returns_depth_and_linear_abs():
sweep = np.linspace(-5.0, 7.0, 257, dtype=np.float32)
depth_m, y = compute_ifft_profile_from_sweep(sweep)
assert depth_m.dtype == np.float32
assert y.dtype == np.float32
assert depth_m.size == y.size
assert depth_m.size > 0
assert np.all(np.diff(depth_m) >= 0.0)

View File

@ -1,101 +0,0 @@
import numpy as np
from rfg_adc_plotter.state.ring_buffer import RingBuffer
def test_ring_buffer_allocates_fft_buffers_from_first_push():
ring = RingBuffer(max_sweeps=4)
ring.ensure_init(64)
sweep = np.linspace(-1.0, 1.0, 64, dtype=np.float32)
ring.push(sweep)
assert ring.ring_fft is not None
assert ring.fft_depth_axis_m is not None
assert ring.last_fft_vals is not None
assert ring.fft_bins == ring.ring_fft.shape[1]
assert ring.fft_bins == ring.fft_depth_axis_m.size
assert ring.fft_bins == ring.last_fft_vals.size
assert ring.last_fft_third_axes_m != (None, None, None)
assert ring.last_fft_third_vals != (None, None, None)
for axis, vals in zip(ring.last_fft_third_axes_m, ring.last_fft_third_vals):
assert axis is not None
assert vals is not None
assert axis.dtype == np.float32
assert vals.dtype == np.float32
assert axis.size == vals.size
# Legacy alias kept for compatibility with existing GUI code paths.
assert ring.fft_time_axis is ring.fft_depth_axis_m
def test_ring_buffer_reallocates_fft_buffers_when_ifft_length_changes():
ring = RingBuffer(max_sweeps=4)
ring.ensure_init(512)
ring.push(np.linspace(-1.0, 1.0, 64, dtype=np.float32))
first_bins = ring.fft_bins
first_shape = None if ring.ring_fft is None else ring.ring_fft.shape
ring.push(np.linspace(-1.0, 1.0, 512, dtype=np.float32))
second_bins = ring.fft_bins
second_shape = None if ring.ring_fft is None else ring.ring_fft.shape
assert ring.ring is not None # raw ring сохраняется
assert first_shape is not None and second_shape is not None
assert first_bins != second_bins
assert second_shape == (ring.max_sweeps, second_bins)
assert ring.fft_depth_axis_m is not None
assert ring.fft_depth_axis_m.size == second_bins
def test_ring_buffer_mode_switch_resets_fft_buffers_only():
ring = RingBuffer(max_sweeps=4)
ring.ensure_init(128)
ring.push(np.linspace(-1.0, 1.0, 128, dtype=np.float32))
assert ring.ring is not None
assert ring.ring_fft is not None
raw_before = ring.ring.copy()
assert ring.last_fft_third_axes_m != (None, None, None)
assert ring.last_fft_third_vals != (None, None, None)
changed = ring.set_fft_complex_mode("diff")
assert changed is True
assert ring.fft_complex_mode == "diff"
assert ring.ring is not None
assert np.array_equal(ring.ring, raw_before, equal_nan=True)
assert ring.ring_fft is None
assert ring.fft_depth_axis_m is None
assert ring.last_fft_vals is None
assert ring.last_fft_third_axes_m == (None, None, None)
assert ring.last_fft_third_vals == (None, None, None)
assert ring.fft_bins == 0
ring.push(np.linspace(-1.0, 1.0, 128, dtype=np.float32))
assert ring.ring_fft is not None
assert ring.fft_depth_axis_m is not None
assert ring.last_fft_vals is not None
assert ring.last_fft_third_axes_m != (None, None, None)
assert ring.last_fft_third_vals != (None, None, None)
for axis, vals in zip(ring.last_fft_third_axes_m, ring.last_fft_third_vals):
assert axis is not None
assert vals is not None
assert axis.dtype == np.float32
assert vals.dtype == np.float32
assert axis.size == vals.size
def test_ring_buffer_short_sweeps_keep_third_profiles_well_formed():
for n in (1, 2, 3):
ring = RingBuffer(max_sweeps=4)
ring.ensure_init(n)
ring.push(np.linspace(-1.0, 1.0, n, dtype=np.float32))
assert ring.last_fft_third_axes_m != (None, None, None)
assert ring.last_fft_third_vals != (None, None, None)
for axis, vals in zip(ring.last_fft_third_axes_m, ring.last_fft_third_vals):
assert axis is not None
assert vals is not None
assert axis.dtype == np.float32
assert vals.dtype == np.float32
assert axis.size == vals.size