examples/a121/algo/phase_tracking/phase_tracking.py#
1# Copyright (c) Acconeer AB, 2022-2026
2# All rights reserved
3
4from __future__ import annotations
5
6import numpy as np
7
8# Added here to force pyqtgraph to choose PySide
9import PySide6 # noqa: F401
10
11import pyqtgraph as pg
12
13import acconeer.exptool as et
14from acconeer.exptool import a121
15from acconeer.exptool.a121.algo._utils import get_distances_m
16from acconeer.exptool.a121.algo.phase_tracking import (
17 Processor,
18 ProcessorConfig,
19 ProcessorContext,
20 get_sensor_config,
21)
22
23
24def main():
25 args = a121.ExampleArgumentParser().parse_args()
26 et.utils.config_logging(args)
27
28 sensor_config = get_sensor_config()
29
30 client = a121.Client.open(**a121.get_client_args(args))
31 metadata = client.setup_session(sensor_config)
32
33 processor = Processor(
34 sensor_config=sensor_config,
35 metadata=metadata,
36 processor_config=ProcessorConfig(),
37 context=ProcessorContext(),
38 )
39 pg_updater = PGUpdater(sensor_config, metadata)
40 pg_process = et.PGProcess(pg_updater)
41 pg_process.start()
42
43 client.start_session()
44 interrupt_handler = et.utils.ExampleInterruptHandler()
45
46 while not interrupt_handler.got_signal:
47 result = client.get_next()
48 plot_data = processor.process(result)
49 try:
50 pg_process.put_data(plot_data)
51 except et.PGProccessDiedException:
52 break
53
54 print("Disconnecting...")
55 client.close()
56
57
58class PGUpdater:
59 def __init__(self, sensor_config: a121.SensorConfig, metadata: a121.Metadata):
60 self.distances_m = get_distances_m(sensor_config, metadata)
61
62 def setup(self, win):
63 pens = [et.utils.pg_pen_cycler(i) for i in range(3)]
64 brush = et.utils.pg_brush_cycler(0)
65 symbol_kw = dict(symbol="o", symbolSize=1, symbolBrush=brush, symbolPen="k")
66 feat_kws = [dict(pen=pen, **symbol_kw) for pen in pens]
67
68 # sweep and threshold plot
69 self.sweep_plot = win.addPlot(row=0, col=0)
70 self.sweep_plot.setMenuEnabled(False)
71 self.sweep_plot.showGrid(x=True, y=True)
72 self.sweep_plot.addLegend()
73 self.sweep_plot.setLabel("left", "Amplitude")
74 self.sweep_plot.setLabel("bottom", "Distance (m)")
75 self.sweep_plot.addItem(pg.PlotDataItem())
76 self.sweeps_curve = [self.sweep_plot.plot(**feat_kw) for feat_kw in feat_kws]
77 self.sweep_vertical_line = pg.InfiniteLine(pen=pens[2])
78 self.sweep_plot.addItem(self.sweep_vertical_line)
79 self.sweep_smooth_max = et.utils.SmoothMax()
80 sweep_plot_legend = pg.LegendItem(offset=(0.0, 0.5))
81 sweep_plot_legend.setParentItem(self.sweep_plot)
82 sweep_plot_legend.addItem(self.sweeps_curve[0], "Sweep")
83 sweep_plot_legend.addItem(self.sweeps_curve[1], "Threshold")
84
85 # argument plot
86 argument_plot = win.addPlot(row=1, col=0)
87 argument_plot.setMenuEnabled(False)
88 argument_plot.showGrid(x=True, y=True)
89 argument_plot.addLegend()
90 argument_plot.setLabel("bottom", "Distance (m)")
91 argument_plot.setLabel("left", "Phase")
92 argument_plot.setYRange(-np.pi, np.pi)
93 argument_plot.getAxis("left").setTicks(et.utils.pg_phase_ticks)
94 argument_plot.setYRange(-np.pi, np.pi)
95 argument_plot.addItem(pg.ScatterPlotItem())
96 self.argument_curve = argument_plot.plot(
97 **dict(pen=None, symbol="o", symbolSize=5, symbolPen="k")
98 )
99 self.argument_vertical_line = pg.InfiniteLine(pen=pens[2])
100 argument_plot.addItem(self.argument_vertical_line)
101
102 # history plot
103 self.history_plot = win.addPlot(row=2, col=0)
104 self.history_plot.setMenuEnabled(False)
105 self.history_plot.showGrid(x=True, y=True)
106 self.history_plot.addLegend()
107 self.history_plot.setLabel("left", "Distance (mm)")
108 self.history_plot.setLabel("bottom", "Time (s)")
109 self.history_plot.addItem(pg.PlotDataItem())
110 self.history_curve = self.history_plot.plot(**feat_kws[0])
111
112 self.sweep_smooth_max = et.utils.SmoothMax()
113 self.distance_hist_smooth_lim = et.utils.SmoothLimits(tau_decay=0.5, tau_grow=0.1)
114
115 def update(self, processor_result):
116 assert processor_result is not None
117 assert processor_result.threshold is not None
118
119 sweep = processor_result.lp_abs_sweep
120 threshold = processor_result.threshold * np.ones(sweep.size)
121 angle_sweep = processor_result.angle_sweep
122 peak_loc = processor_result.peak_loc_m
123 history = processor_result.distance_history
124 rel_time_stamps = processor_result.rel_time_stamps
125
126 # update sweep plot
127 self.sweeps_curve[0].setData(self.distances_m, sweep)
128 self.sweeps_curve[1].setData(self.distances_m, threshold)
129 max_val_in_sweep_plot = max(np.max(sweep), np.max(threshold))
130 self.sweep_plot.setYRange(0, self.sweep_smooth_max.update(max_val_in_sweep_plot))
131
132 # update argument plot
133 self.argument_curve.setData(self.distances_m, angle_sweep)
134
135 if peak_loc is not None:
136 # update vertical lines
137 self.sweep_vertical_line.setValue(peak_loc)
138 self.argument_vertical_line.setValue(peak_loc)
139 self.sweep_vertical_line.show()
140 self.argument_vertical_line.show()
141 else:
142 self.sweep_vertical_line.hide()
143 self.argument_vertical_line.hide()
144
145 if history.shape[0] != 0:
146 # update history plot
147 self.history_curve.setData(rel_time_stamps, history)
148 lims = self.distance_hist_smooth_lim.update(history)
149 self.history_plot.setYRange(lims[0], lims[1])
150 self.history_plot.setXRange(-Processor.TIME_HORIZON_S, 0)
151
152
153if __name__ == "__main__":
154 main()
View this example on GitHub: acconeer/acconeer-python-exploration