2 Commits

Author SHA1 Message Date
awe
d13f3140c1 fix 2026-07-13 19:04:13 +03:00
awe
445b15608a fix 2026-07-13 18:40:45 +03:00
2 changed files with 139 additions and 33 deletions

View File

@ -37,6 +37,15 @@ def build_parser() -> argparse.ArgumentParser:
"в водопаде спектров (0 — отключить)"
),
)
parser.add_argument(
"--bscan-db-range",
dest="bscan_db_range",
default="80,150",
help=(
"Фиксированный диапазон цветовой шкалы B-scan в дБ (min,max). "
"Напр. 80,150. Применяется к обычному виду (без вычитания фона)."
),
)
parser.add_argument("--title", default="ADC Sweeps", help="Заголовок окна")
parser.add_argument(
"--fancy",

View File

@ -31,7 +31,12 @@ from rfg_adc_plotter.processing.calibration import (
save_complex_calibration,
set_calibration_base_value,
)
from rfg_adc_plotter.processing.fft import compute_fft_complex_row, compute_fft_mag_row, fft_mag_to_db
from rfg_adc_plotter.processing.fft import (
compute_distance_axis,
compute_fft_complex_row,
compute_fft_mag_row,
fft_mag_to_db,
)
from rfg_adc_plotter.processing.formatting import compute_auto_ylim, format_status_kv, parse_spec_clip
from rfg_adc_plotter.processing.normalization import (
normalize_by_complex_calibration,
@ -49,6 +54,7 @@ from rfg_adc_plotter.types import SweepAuxCurves, SweepInfo, SweepPacket
RAW_PLOT_MAX_POINTS = 2048
RAW_WATERFALL_MAX_POINTS = 2048
BSCAN_MAX_POINTS = 256
BSCAN_DB_RANGE_DEFAULT: Tuple[float, float] = (80.0, 150.0)
UI_QUEUE_MAXSIZE = 128
UI_MAX_PACKETS_PER_TICK = 8
DEBUG_FRAME_LOG_EVERY = 10
@ -821,6 +827,19 @@ def _db_to_linear_amplitude(values: np.ndarray) -> np.ndarray:
return np.maximum(out, 0.0).astype(np.float32, copy=False)
def parse_bscan_db_range(spec: Optional[str]) -> Tuple[float, float]:
"""Parse a fixed B-scan dB color window 'min,max'. Falls back to the default."""
if spec:
try:
p0, p1 = str(spec).strip().replace(";", ",").split(",")
lo, hi = float(p0), float(p1)
if np.isfinite(lo) and np.isfinite(hi) and lo < hi:
return (lo, hi)
except Exception:
pass
return BSCAN_DB_RANGE_DEFAULT
def compute_background_subtracted_bscan_levels(
disp_fft_lin: np.ndarray,
disp_fft: np.ndarray,
@ -903,6 +922,7 @@ def run_pyqtgraph(args) -> None:
fft_bins = FFT_LEN // 2 + 1
spec_clip = parse_spec_clip(getattr(args, "spec_clip", None))
spec_mean_sec = float(getattr(args, "spec_mean_sec", 0.0))
bscan_db_vmin, bscan_db_vmax = parse_bscan_db_range(getattr(args, "bscan_db_range", None))
runtime = RuntimeState(
ring=RingBuffer(max_sweeps),
range_min_ghz=float(SWEEP_FREQ_MIN_GHZ),
@ -1156,6 +1176,24 @@ def run_pyqtgraph(args) -> None:
range_max_spin.setValue(runtime.range_max_ghz)
range_group_layout.addRow("f min", range_min_spin)
range_group_layout.addRow("f max", range_max_spin)
bscan_db_group = QtWidgets.QGroupBox("B-scan дБ шкала")
bscan_db_group_layout = QtWidgets.QFormLayout(bscan_db_group)
bscan_db_group_layout.setContentsMargins(6, 6, 6, 6)
bscan_db_group_layout.setSpacing(6)
bscan_db_min_spin = QtWidgets.QDoubleSpinBox()
bscan_db_max_spin = QtWidgets.QDoubleSpinBox()
for spin in (bscan_db_min_spin, bscan_db_max_spin):
spin.setDecimals(1)
spin.setRange(-100.0, 400.0)
spin.setSingleStep(1.0)
try:
spin.setSuffix(" dB")
except Exception:
pass
bscan_db_min_spin.setValue(bscan_db_vmin)
bscan_db_max_spin.setValue(bscan_db_vmax)
bscan_db_group_layout.addRow("дБ мин", bscan_db_min_spin)
bscan_db_group_layout.addRow("дБ макс", bscan_db_max_spin)
fft_mode_label = QtWidgets.QLabel("IFFT режим")
fft_mode_combo = QtWidgets.QComboBox()
fft_mode_combo.addItem("Обычный", "direct")
@ -1305,6 +1343,7 @@ def run_pyqtgraph(args) -> None:
except Exception:
pass
settings_layout.addWidget(range_group)
settings_layout.addWidget(bscan_db_group)
settings_layout.addWidget(calib_group)
settings_layout.addWidget(complex_calib_group)
settings_layout.addWidget(tty_range_group)
@ -1346,6 +1385,13 @@ def run_pyqtgraph(args) -> None:
last_packet_processed_at: Optional[float] = None
axis_range_cache: Dict[str, Tuple[float, ...]] = {}
image_rect_cache: Dict[str, Tuple[float, ...]] = {}
bscan_axis_cache: Dict[Tuple, np.ndarray] = {}
# Fixed manual dB color window for the normal (no-background) B-scan view.
# Never recomputed per frame; only changed by the user via the spin boxes.
bscan_manual_levels: Dict[str, float] = {"vmin": bscan_db_vmin, "vmax": bscan_db_vmax}
# Frozen levels for the background-subtracted residual view (different scale):
# computed once when background is active, held until a reset event clears it.
bscan_bg_levels_frozen: Dict[str, Optional[Tuple[float, float]]] = {"held": None}
curve_has_data_cache: Dict[str, bool] = {}
item_visibility_cache: Dict[str, bool] = {}
text_value_cache: Dict[str, str] = {}
@ -1441,6 +1487,43 @@ def run_pyqtgraph(args) -> None:
image_rect_cache[key] = values
return True
def stable_full_scale_distance_axis() -> Optional[np.ndarray]:
"""Full-scale B-scan distance axis, computed once per (range, mode).
The per-sweep ``runtime.ring.distance_axis`` jitters when the reported
``f_max`` varies between sweeps, which rescales the B-scan y-axis every
frame. Deriving the axis from the configured range keeps it stable and
only recomputes when the operator changes ``f min``/``f max`` or the mode.
"""
key = (
round(float(runtime.range_min_ghz), 9),
round(float(runtime.range_max_ghz), 9),
str(fft_mode),
)
cached = bscan_axis_cache.get(key)
if cached is not None:
return cached
if display_distance_transform_enabled(fft_mode):
# positive_only_exact derives its step from real sweep geometry, so a
# synthetic 2-point freq array would be wrong. Hold the first valid
# real axis instead (still keyed by mode, so it re-seeds on switch).
real_axis = runtime.ring.distance_axis
if real_axis is None:
return None
axis = np.asarray(real_axis, dtype=np.float64).reshape(-1)
if axis.size <= 0 or not np.all(np.isfinite(axis)):
return None
else:
synth_freqs = np.asarray(
[runtime.range_min_ghz, runtime.range_max_ghz], dtype=np.float64
)
axis = compute_distance_axis(synth_freqs, fft_bins, mode=fft_mode)
if axis is None or axis.size <= 0 or not np.all(np.isfinite(axis)):
return None
bscan_axis_cache.clear()
bscan_axis_cache[key] = axis
return axis
def set_item_visible_if_changed(key: str, item, visible: bool) -> bool:
visible_value = bool(visible)
if item_visibility_cache.get(key) == visible_value:
@ -1887,6 +1970,9 @@ def run_pyqtgraph(args) -> None:
runtime.background_buffer.reset()
if clear_profile:
runtime.background_profile = None
# Re-freeze the background-subtracted B-scan levels so contrast doesn't
# drag an old residual scale across the background on/off transition.
bscan_bg_levels_frozen["held"] = None
runtime.current_fft_complex = None
runtime.current_fft_mag = None
runtime.current_fft_db = None
@ -1905,6 +1991,8 @@ def run_pyqtgraph(args) -> None:
runtime.ring.reset()
axis_range_cache.clear()
image_rect_cache.clear()
bscan_axis_cache.clear()
bscan_bg_levels_frozen["held"] = None
runtime.current_distances = None
runtime.current_fft_complex = None
runtime.current_fft_mag = None
@ -2238,6 +2326,20 @@ def run_pyqtgraph(args) -> None:
finally:
range_change_in_progress = False
def set_bscan_db_range() -> None:
try:
new_min = float(bscan_db_min_spin.value())
new_max = float(bscan_db_max_spin.value())
except Exception:
return
if (not np.isfinite(new_min)) or (not np.isfinite(new_max)) or new_min >= new_max:
set_status_note("B-scan дБ: мин должен быть меньше макс")
return
bscan_manual_levels["vmin"] = new_min
bscan_manual_levels["vmax"] = new_max
set_status_note(f"B-scan дБ: {new_min:.1f}..{new_max:.1f}")
runtime.mark_dirty()
def set_working_range() -> None:
nonlocal range_change_in_progress
if range_change_in_progress:
@ -2506,6 +2608,8 @@ def run_pyqtgraph(args) -> None:
background_enabled = False
if background_enabled and runtime.background_profile is None:
set_status_note("фон: профиль не загружен")
# Re-freeze residual levels so toggling background recomputes them once.
bscan_bg_levels_frozen["held"] = None
runtime.mark_dirty()
def set_fft_low_cut_percent() -> None:
@ -2526,6 +2630,7 @@ def run_pyqtgraph(args) -> None:
except Exception:
fft_mode = "symmetric"
runtime.ring.set_fft_mode(fft_mode)
bscan_axis_cache.clear()
reset_background_state(clear_profile=True)
runtime.current_distances = runtime.ring.distance_axis
runtime.current_fft_mag = None
@ -2559,6 +2664,8 @@ def run_pyqtgraph(args) -> None:
try:
range_min_spin.valueChanged.connect(lambda _v: set_working_range())
range_max_spin.valueChanged.connect(lambda _v: set_working_range())
bscan_db_min_spin.valueChanged.connect(lambda _v: set_bscan_db_range())
bscan_db_max_spin.valueChanged.connect(lambda _v: set_bscan_db_range())
tty_range_spin.valueChanged.connect(lambda _v: set_tty_range())
calib_cb.stateChanged.connect(lambda _v: set_calib_enabled())
complex_calib_cb.stateChanged.connect(lambda _v: set_complex_calib_enabled())
@ -3729,42 +3836,32 @@ def run_pyqtgraph(args) -> None:
log_debug_event("invalid_fft_image", "ui invalid FFT waterfall suppressed")
return
levels = None
# Fixed color scale — never recomputed per frame, so the image no
# longer "changes"/flickers as new sweeps roll in.
if active_background is not None:
levels = compute_background_subtracted_bscan_levels(disp_fft_lin, disp_fft)
# Residuals are on a different scale than absolute dB; use the
# dedicated bg-subtracted levels, computed ONCE and frozen.
levels = bscan_bg_levels_frozen["held"]
if levels is None:
try:
finite_rows = np.any(np.isfinite(disp_fft), axis=1)
if np.any(finite_rows):
mean_spec = np.nanmean(disp_fft[finite_rows], axis=1)
if mean_spec.size > 0:
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 runtime.ring.y_min_fft is not None
and runtime.ring.y_max_fft is not None
and np.isfinite(runtime.ring.y_min_fft)
and np.isfinite(runtime.ring.y_max_fft)
and runtime.ring.y_min_fft != runtime.ring.y_max_fft
):
levels = (runtime.ring.y_min_fft, runtime.ring.y_max_fft)
levels = compute_background_subtracted_bscan_levels(disp_fft_lin, disp_fft)
if levels is not None:
bscan_bg_levels_frozen["held"] = levels
else:
# Normal view: constant manual dB window from the spin boxes.
levels = (bscan_manual_levels["vmin"], bscan_manual_levels["vmax"])
disp_fft_display_axis = display_distance_axis_for_mode(disp_fft_axis, fft_mode)
if display_distance_transform_enabled(fft_mode) and disp_fft_display_axis.size == disp_fft.shape[0]:
disp_fft = disp_fft[::-1, :]
disp_fft_display_axis = disp_fft_display_axis[::-1]
# Use a stable full-scale distance axis for the y-range/rect so the
# B-scan always spans ~0..12 m and never rescales per sweep. Fall
# back to the per-frame axis only until a stable axis is available.
stable_axis = stable_full_scale_distance_axis()
if stable_axis is not None:
distance_bounds = resolve_axis_bounds(
display_distance_axis_for_mode(stable_axis, fft_mode)
)
else:
distance_bounds = resolve_axis_bounds(disp_fft_display_axis)
if distance_bounds is not None:
d_min, d_max = distance_bounds