6 Commits

9 changed files with 296 additions and 70 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ __pycache__/
*.bak *.bak
*.swp *.swp
*.swo *.swo
acm_9

BIN
background.npy Normal file

Binary file not shown.

Binary file not shown.

View File

@ -12,7 +12,7 @@ from rfg_adc_plotter.constants import FFT_LEN, FREQ_SPAN_GHZ, IFFT_LEN
_IFFT_T_MAX_NS = float((IFFT_LEN - 1) / (FREQ_SPAN_GHZ * 1e9) * 1e9) _IFFT_T_MAX_NS = float((IFFT_LEN - 1) / (FREQ_SPAN_GHZ * 1e9) * 1e9)
from rfg_adc_plotter.io.sweep_reader import SweepReader from rfg_adc_plotter.io.sweep_reader import SweepReader
from rfg_adc_plotter.processing.normalizer import build_calib_envelopes from rfg_adc_plotter.processing.normalizer import build_calib_envelopes
from rfg_adc_plotter.state.app_state import CALIB_ENVELOPE_PATH, AppState, format_status from rfg_adc_plotter.state.app_state import BACKGROUND_PATH, CALIB_ENVELOPE_PATH, AppState, format_status
from rfg_adc_plotter.state.ring_buffer import RingBuffer from rfg_adc_plotter.state.ring_buffer import RingBuffer
from rfg_adc_plotter.types import SweepPacket from rfg_adc_plotter.types import SweepPacket
@ -89,7 +89,14 @@ def run_matplotlib(args):
q: Queue[SweepPacket] = Queue(maxsize=1000) q: Queue[SweepPacket] = Queue(maxsize=1000)
stop_event = threading.Event() stop_event = threading.Event()
reader = SweepReader(args.port, args.baud, q, stop_event, fancy=bool(args.fancy)) reader = SweepReader(
args.port,
args.baud,
q,
stop_event,
fancy=bool(args.fancy),
bin_mode=bool(getattr(args, "bin_mode", False)),
)
reader.start() reader.start()
max_sweeps = int(max(10, args.max_sweeps)) max_sweeps = int(max(10, args.max_sweeps))
@ -204,6 +211,24 @@ def run_matplotlib(args):
state.set_calib_enabled(bool(calib_cb.get_status()[0])) state.set_calib_enabled(bool(calib_cb.get_status()[0]))
fig.canvas.draw_idle() fig.canvas.draw_idle()
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])
from matplotlib.widgets import Button as MplButton
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()
fig.canvas.draw_idle()
def _on_bg_clicked(_v):
state.set_background_enabled(bool(bg_cb.get_status()[0]))
save_bg_btn.on_clicked(_on_save_bg)
bg_cb.on_clicked(_on_bg_clicked)
ymin_slider.on_changed(_on_ylim_change) ymin_slider.on_changed(_on_ylim_change)
ymax_slider.on_changed(_on_ylim_change) ymax_slider.on_changed(_on_ylim_change)
contrast_slider.on_changed(lambda _v: fig.canvas.draw_idle()) contrast_slider.on_changed(lambda _v: fig.canvas.draw_idle())
@ -250,7 +275,15 @@ def run_matplotlib(args):
m = float(np.nanmax(np.abs(data))) m = float(np.nanmax(np.abs(data)))
return data / m if m > 0.0 else data return data / m if m > 0.0 else data
line_obj.set_data(xs, _norm_to_max(raw)) line_obj.set_data(xs, _norm_to_max(raw))
if state.last_calib_sweep is not None: if state.calib_mode == "file" and state.calib_file_envelope is not None:
upper = state.calib_file_envelope
lower = -upper
m_env = float(np.nanmax(np.abs(upper)))
if m_env <= 0.0:
m_env = 1.0
line_env_lo.set_data(xs[: upper.size], lower / m_env)
line_env_hi.set_data(xs[: upper.size], upper / m_env)
elif state.last_calib_sweep is not None:
calib = state.last_calib_sweep calib = state.last_calib_sweep
m_calib = float(np.nanmax(np.abs(calib))) m_calib = float(np.nanmax(np.abs(calib)))
if m_calib <= 0.0: if m_calib <= 0.0:

View File

@ -10,7 +10,7 @@ import numpy as np
from rfg_adc_plotter.constants import FREQ_SPAN_GHZ, IFFT_LEN from rfg_adc_plotter.constants import FREQ_SPAN_GHZ, IFFT_LEN
from rfg_adc_plotter.io.sweep_reader import SweepReader from rfg_adc_plotter.io.sweep_reader import SweepReader
from rfg_adc_plotter.processing.normalizer import build_calib_envelopes from rfg_adc_plotter.processing.normalizer import build_calib_envelopes
from rfg_adc_plotter.state.app_state import CALIB_ENVELOPE_PATH, AppState, format_status from rfg_adc_plotter.state.app_state import BACKGROUND_PATH, CALIB_ENVELOPE_PATH, AppState, format_status
from rfg_adc_plotter.state.ring_buffer import RingBuffer from rfg_adc_plotter.state.ring_buffer import RingBuffer
from rfg_adc_plotter.types import SweepPacket from rfg_adc_plotter.types import SweepPacket
@ -106,7 +106,14 @@ def run_pyqtgraph(args):
q: Queue[SweepPacket] = Queue(maxsize=1000) q: Queue[SweepPacket] = Queue(maxsize=1000)
stop_event = threading.Event() stop_event = threading.Event()
reader = SweepReader(args.port, args.baud, q, stop_event, fancy=bool(args.fancy)) reader = SweepReader(
args.port,
args.baud,
q,
stop_event,
fancy=bool(args.fancy),
bin_mode=bool(getattr(args, "bin_mode", False)),
)
reader.start() reader.start()
max_sweeps = int(max(10, args.max_sweeps)) max_sweeps = int(max(10, args.max_sweeps))
@ -225,6 +232,32 @@ def run_pyqtgraph(args):
calib_cb.stateChanged.connect(_on_calib_toggled) calib_cb.stateChanged.connect(_on_calib_toggled)
calib_file_cb.stateChanged.connect(lambda _v: _on_calib_file_toggled(calib_file_cb.isChecked())) calib_file_cb.stateChanged.connect(lambda _v: _on_calib_file_toggled(calib_file_cb.isChecked()))
# Кнопка сохранения фона + чекбокс вычета фона
bg_widget = QtWidgets.QWidget()
bg_layout = QtWidgets.QHBoxLayout(bg_widget)
bg_layout.setContentsMargins(2, 2, 2, 2)
bg_layout.setSpacing(8)
save_bg_btn = QtWidgets.QPushButton("Сохр. фон")
bg_cb = QtWidgets.QCheckBox("вычет фона")
bg_cb.setEnabled(False)
bg_layout.addWidget(save_bg_btn)
bg_layout.addWidget(bg_cb)
bg_container_proxy = QtWidgets.QGraphicsProxyWidget()
bg_container_proxy.setWidget(bg_widget)
win.addItem(bg_container_proxy, row=2, col=0)
def _on_save_bg():
ok = state.save_background()
if ok:
state.load_background()
bg_cb.setEnabled(True)
save_bg_btn.clicked.connect(_on_save_bg)
bg_cb.stateChanged.connect(lambda _v: state.set_background_enabled(bg_cb.isChecked()))
# Статусная строка # Статусная строка
status = pg.LabelItem(justify="left") status = pg.LabelItem(justify="left")
win.addItem(status, row=3, col=0, colspan=2) win.addItem(status, row=3, col=0, colspan=2)
@ -264,7 +297,15 @@ def run_pyqtgraph(args):
m = float(np.nanmax(np.abs(data))) m = float(np.nanmax(np.abs(data)))
return data / m if m > 0.0 else data return data / m if m > 0.0 else data
curve.setData(xs, _norm_to_max(raw), autoDownsample=True) curve.setData(xs, _norm_to_max(raw), autoDownsample=True)
if state.last_calib_sweep is not None: if state.calib_mode == "file" and state.calib_file_envelope is not None:
upper = state.calib_file_envelope
lower = -upper
m_env = float(np.nanmax(np.abs(upper)))
if m_env <= 0.0:
m_env = 1.0
curve_env_lo.setData(xs[: upper.size], lower / m_env, autoDownsample=True)
curve_env_hi.setData(xs[: upper.size], upper / m_env, autoDownsample=True)
elif state.last_calib_sweep is not None:
calib = state.last_calib_sweep calib = state.last_calib_sweep
m_calib = float(np.nanmax(np.abs(calib))) m_calib = float(np.nanmax(np.abs(calib)))
if m_calib <= 0.0: if m_calib <= 0.0:

View File

@ -24,6 +24,7 @@ class SweepReader(threading.Thread):
out_queue: "Queue[SweepPacket]", out_queue: "Queue[SweepPacket]",
stop_event: threading.Event, stop_event: threading.Event,
fancy: bool = False, fancy: bool = False,
bin_mode: bool = False,
): ):
super().__init__(daemon=True) super().__init__(daemon=True)
self._port_path = port_path self._port_path = port_path
@ -32,11 +33,17 @@ class SweepReader(threading.Thread):
self._stop = stop_event self._stop = stop_event
self._src: Optional[SerialLineSource] = None self._src: Optional[SerialLineSource] = None
self._fancy = bool(fancy) self._fancy = bool(fancy)
self._bin_mode = bool(bin_mode)
self._max_width: int = 0 self._max_width: int = 0
self._sweep_idx: int = 0 self._sweep_idx: int = 0
self._last_sweep_ts: Optional[float] = None self._last_sweep_ts: Optional[float] = None
self._n_valid_hist = deque() self._n_valid_hist = deque()
@staticmethod
def _u32_to_i32(v: int) -> int:
"""Преобразование 32-bit слова в знаковое значение."""
return v - 0x1_0000_0000 if (v & 0x8000_0000) else v
def _finalize_current(self, xs, ys, channels: Optional[set]): def _finalize_current(self, xs, ys, channels: Optional[set]):
if not xs: if not xs:
return return
@ -135,11 +142,148 @@ class SweepReader(threading.Thread):
except Exception: except Exception:
pass pass
def run(self): def _run_ascii_stream(self, chunk_reader: SerialChunkReader):
xs: list = [] xs: list[int] = []
ys: list = [] ys: list[int] = []
cur_channel: Optional[int] = None cur_channel: Optional[int] = None
cur_channels: set = set() cur_channels: set[int] = set()
buf = bytearray()
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
if line.startswith(b"Sweep_start"):
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:
continue
if cur_channel is None:
cur_channel = ch
cur_channels.add(ch)
xs.append(x)
ys.append(y)
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()
words = deque()
buf = bytearray()
while not self._stop.is_set():
data = chunk_reader.read_available()
if data:
buf += data
else:
time.sleep(0.0005)
continue
usable = len(buf) & ~1
if usable == 0:
continue
i = 0
while i < usable:
w = int(buf[i]) | (int(buf[i + 1]) << 8)
words.append(w)
i += 2
# Бинарный протокол:
# старт свипа (актуальный): 0xFFFF, 0xFFFF, 0xFFFF, (ch<<8)|0x0A
# старт свипа (legacy): 0xFFFF, 0xFFFF, channel, 0x0A0A
# точка: step, value_hi, value_lo, 0x000A
while len(words) >= 4:
w0 = int(words[0])
w1 = int(words[1])
w2 = int(words[2])
w3 = int(words[3])
if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and (w3 & 0x00FF) == 0x000A:
self._finalize_current(xs, ys, cur_channels)
xs.clear()
ys.clear()
cur_channels.clear()
cur_channel = (w3 >> 8) & 0x00FF
cur_channels.add(cur_channel)
for _ in range(4):
words.popleft()
continue
if w0 == 0xFFFF and w1 == 0xFFFF and w3 == 0x0A0A:
self._finalize_current(xs, ys, cur_channels)
xs.clear()
ys.clear()
cur_channels.clear()
cur_channel = w2
cur_channels.add(cur_channel)
for _ in range(4):
words.popleft()
continue
if w3 == 0x000A:
if cur_channel is not None:
cur_channels.add(cur_channel)
xs.append(w0)
value_u32 = (w1 << 16) | w2
ys.append(self._u32_to_i32(value_u32))
for _ in range(4):
words.popleft()
continue
# Поток может начаться с середины пакета; сдвигаемся по слову до ресинхронизации.
words.popleft()
del buf[:usable]
if len(buf) > 1_000_000:
del buf[:-262144]
self._finalize_current(xs, ys, cur_channels)
def run(self):
try: try:
self._src = SerialLineSource(self._port_path, self._baud, timeout=1.0) self._src = SerialLineSource(self._port_path, self._baud, timeout=1.0)
@ -150,66 +294,11 @@ class SweepReader(threading.Thread):
try: try:
chunk_reader = SerialChunkReader(self._src) chunk_reader = SerialChunkReader(self._src)
buf = bytearray() if self._bin_mode:
while not self._stop.is_set(): self._run_binary_stream(chunk_reader)
data = chunk_reader.read_available() else:
if data: self._run_ascii_stream(chunk_reader)
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
if line.startswith(b"Sweep_start"):
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:
continue
if cur_channel is None:
cur_channel = ch
cur_channels.add(ch)
xs.append(x)
ys.append(y)
if len(buf) > 1_000_000:
del buf[:-262144]
finally: finally:
try:
self._finalize_current(xs, ys, cur_channels)
except Exception:
pass
try: try:
if self._src is not None: if self._src is not None:
self._src.close() self._src.close()

9
rfg_adc_plotter/main.py Normal file → Executable file
View File

@ -77,6 +77,15 @@ def build_parser() -> argparse.ArgumentParser:
default="projector", default="projector",
help="Тип нормировки: projector (по огибающим в [-1000,+1000]) или simple (raw/calib)", help="Тип нормировки: projector (по огибающим в [-1000,+1000]) или simple (raw/calib)",
) )
parser.add_argument(
"--bin",
dest="bin_mode",
action="store_true",
help=(
"Бинарный протокол: старт свипа 0xFFFF,0xFFFF,0xFFFF,(CH<<8)|0x0A; "
"точки step,uint32(hi16,lo16),0x000A"
),
)
return parser return parser

View File

@ -15,6 +15,7 @@ from rfg_adc_plotter.state.ring_buffer import RingBuffer
from rfg_adc_plotter.types import SweepInfo, SweepPacket from rfg_adc_plotter.types import SweepInfo, SweepPacket
CALIB_ENVELOPE_PATH = "calib_envelope.npy" CALIB_ENVELOPE_PATH = "calib_envelope.npy"
BACKGROUND_PATH = "background.npy"
def format_status(data: Mapping[str, Any]) -> str: def format_status(data: Mapping[str, Any]) -> str:
@ -54,6 +55,10 @@ class AppState:
# "live" — нормировка по текущему ch0-свипу; "file" — по огибающей из файла # "live" — нормировка по текущему ch0-свипу; "file" — по огибающей из файла
self.calib_mode: str = "live" self.calib_mode: str = "live"
self.calib_file_envelope: Optional[np.ndarray] = None self.calib_file_envelope: Optional[np.ndarray] = None
# Вычет фона
self.background: Optional[np.ndarray] = None
self.background_enabled: bool = False
self._last_sweep_for_ring: Optional[np.ndarray] = None
def _normalize(self, raw: np.ndarray, calib: np.ndarray) -> np.ndarray: def _normalize(self, raw: np.ndarray, calib: np.ndarray) -> np.ndarray:
if self.calib_mode == "file" and self.calib_file_envelope is not None: if self.calib_mode == "file" and self.calib_file_envelope is not None:
@ -96,6 +101,43 @@ class AppState:
"""Переключить режим калибровки: 'live' или 'file'.""" """Переключить режим калибровки: 'live' или 'file'."""
self.calib_mode = mode self.calib_mode = mode
def save_background(self, path: str = BACKGROUND_PATH) -> bool:
"""Сохранить текущий sweep_for_ring как фоновый спектр.
Сохраняет последний свип, который был записан в ринг-буфер
(нормированный, если калибровка включена, иначе сырой).
Возвращает True при успехе.
"""
if self._last_sweep_for_ring is None:
return False
try:
np.save(path, self._last_sweep_for_ring)
return True
except Exception as exc:
import sys
sys.stderr.write(f"[warn] Не удалось сохранить фон: {exc}\n")
return False
def load_background(self, path: str = BACKGROUND_PATH) -> bool:
"""Загрузить фоновый спектр из файла.
Возвращает True при успехе.
"""
if not os.path.isfile(path):
return False
try:
bg = np.load(path)
self.background = np.asarray(bg, dtype=np.float32)
return True
except Exception as exc:
import sys
sys.stderr.write(f"[warn] Не удалось загрузить фон: {exc}\n")
return False
def set_background_enabled(self, enabled: bool):
"""Включить/выключить вычет фона."""
self.background_enabled = enabled
def set_calib_enabled(self, enabled: bool): def set_calib_enabled(self, enabled: bool):
"""Включить/выключить режим калибровки, пересчитать norm-свип.""" """Включить/выключить режим калибровки, пересчитать norm-свип."""
self.calib_enabled = enabled self.calib_enabled = enabled
@ -140,6 +182,7 @@ class AppState:
self.save_calib_envelope() self.save_calib_envelope()
self.current_sweep_norm = None self.current_sweep_norm = None
sweep_for_ring = s sweep_for_ring = s
self._last_sweep_for_ring = sweep_for_ring
else: else:
can_normalize = self.calib_enabled and ( can_normalize = self.calib_enabled and (
(self.calib_mode == "file" and self.calib_file_envelope is not None) (self.calib_mode == "file" and self.calib_file_envelope is not None)
@ -153,6 +196,14 @@ class AppState:
self.current_sweep_norm = None self.current_sweep_norm = None
sweep_for_ring = s sweep_for_ring = s
# Вычет фона (в том же домене что и sweep_for_ring)
if self.background_enabled and self.background is not None and ch != 0:
w = min(sweep_for_ring.size, self.background.size)
sweep_for_ring = sweep_for_ring.copy()
sweep_for_ring[:w] -= self.background[:w]
self.current_sweep_norm = sweep_for_ring
self._last_sweep_for_ring = sweep_for_ring
ring.ensure_init(s.size) ring.ensure_init(s.size)
ring.push(sweep_for_ring) ring.push(sweep_for_ring)
return drained return drained

2
run_dataplotter Executable file
View File

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