-
Notifications
You must be signed in to change notification settings - Fork 13
/
waterfall.py
1244 lines (1113 loc) · 37.1 KB
/
waterfall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import csv
import datetime
import json
import logging
import multiprocessing
import os
import shutil
import signal
import tempfile
import time
import warnings
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import zmq
from flask import Flask, current_app, send_file
from matplotlib.collections import LineCollection
from matplotlib.ticker import MultipleLocator, AutoMinorLocator
from matplotlib import style
from scipy.ndimage import gaussian_filter
from gamutrflib.peak_finder import get_peak_finder
from gamutrflib.zmqbucket import ZmqReceiver, parse_scanners, frame_resample
warnings.filterwarnings(action="ignore", message="Mean of empty slice")
warnings.filterwarnings(action="ignore", message="All-NaN slice encountered")
warnings.filterwarnings(action="ignore", message="Degrees of freedom <= 0 for slice.")
SCAN_FRES = 1e4
def safe_savefig(path):
basename = os.path.basename(path)
dirname = os.path.dirname(path)
tmp_path = os.path.join(dirname, "." + basename)
plt.savefig(tmp_path)
os.rename(tmp_path, path)
logging.debug("wrote %s", path)
return path
def draw_title(
ax,
title,
scan_duration,
tune_step_hz,
tune_step_fft,
tune_rate_hz,
tune_dwell_ms,
sample_rate,
freq_resolution,
):
title_text = {
"Time": str(datetime.datetime.now().isoformat()),
"Scan time": "%.2fs" % scan_duration,
"Step FFTs": "%u" % tune_step_fft,
"Step size": "%.2fMHz" % (tune_step_hz / 1e6),
"Sample rate": "%.2fMsps" % (sample_rate / 1e6),
"Resolution": "%.2fMHz" % freq_resolution,
"Tune rate": "%.2fHz" % tune_rate_hz,
"Tune dwell time": "%.2fms" % tune_dwell_ms,
}
title.set_fontsize(8)
title.set_text(str(title_text))
ax.draw_artist(title)
def filter_peaks(peaks, properties):
for i in range(len(peaks) - 1, -1, -1): # start from end of list
for j in range(len(peaks)):
if i == j:
continue
if (properties["left_ips"][i] > properties["left_ips"][j]) and (
properties["right_ips"][i] < properties["right_ips"][j]
):
peaks = np.delete(peaks, i)
for k in properties:
properties[k] = np.delete(properties[k], i)
break
# properties["left_ips"] = np.delete(properties["left_ips"], i)
# properties["right_ips"] = np.delete(properties["right_ips"], i)
# properties["width_heights"] = np.delete(properties["width_heights"], i)
return peaks, properties
def save_detections(
config,
state,
scan_time,
scan_configs,
peaks,
properties,
):
detection_save_dir = Path(state.save_path, "detections")
detection_config_save_path = str(
Path(
detection_save_dir,
f"detections_scan_config_{scan_time}.json",
)
)
detection_save_path = str(
Path(
detection_save_dir,
f"detections_{scan_time}.csv",
)
)
if not os.path.exists(detection_save_dir):
Path(detection_save_dir).mkdir(parents=True, exist_ok=True)
if state.previous_scan_config is None or state.previous_scan_config != scan_configs:
state.previous_scan_config = scan_configs
with open(detection_config_save_path, "w", encoding="utf8") as f:
json.dump(
{
"timestamp": scan_time,
"min_freq": config.min_freq,
"max_freq": config.max_freq,
"scan_configs": scan_configs,
},
f,
indent=4,
)
if not os.path.exists(detection_save_path):
with open(detection_save_path, "w", encoding="utf8") as detection_csv:
writer = csv.writer(detection_csv)
writer.writerow(
[
"timestamp",
"start_freq",
"end_freq",
"dB",
"type",
]
)
with open(detection_save_path, "a", encoding="utf8") as detection_csv:
writer = csv.writer(detection_csv)
for i in range(len(peaks)):
writer.writerow(
[
scan_time, # timestamp
state.psd_x_edges[
properties["left_ips"][i].astype(int)
], # start_freq
state.psd_x_edges[
properties["right_ips"][i].astype(int)
], # end_freq
properties["peak_heights"][i], # dB
state.peak_finder.name, # type
]
)
def save_waterfall(
state,
save_time,
scan_time,
fig_path=None,
):
now = datetime.datetime.now()
if state.last_save_time is None:
state.last_save_time = now
if now - state.last_save_time > datetime.timedelta(minutes=save_time):
waterfall_save_dir = Path(state.save_path, "waterfall")
if not os.path.exists(waterfall_save_dir):
Path(waterfall_save_dir).mkdir(parents=True, exist_ok=True)
waterfall_save_path = str(
Path(waterfall_save_dir, f"waterfall_{scan_time}.png")
)
if fig_path:
shutil.copyfile(fig_path, waterfall_save_path)
else:
safe_savefig(waterfall_save_path)
save_scan_configs = {
"start_scan_timestamp": state.scan_times[0],
"start_scan_config": state.scan_config_history[state.scan_times[0]],
"end_scan_timestamp": state.scan_times[-1],
"end_scan_config": state.scan_config_history[state.scan_times[-1]],
}
config_save_path = str(Path(waterfall_save_dir, f"config_{scan_time}.json"))
with open(config_save_path, "w", encoding="utf8") as f:
json.dump(save_scan_configs, f, indent=4)
state.last_save_time = now
logging.info(f"Saving {waterfall_save_path}")
def argument_parser():
parser = argparse.ArgumentParser(description="waterfall plotter from scan data")
parser.add_argument(
"--min_freq",
default=0,
type=float,
help="Minimum frequency for plot (or 0 for automatic).",
)
parser.add_argument(
"--max_freq",
default=0,
type=float,
help="Maximum frequency for plot (or 0 for automatic).",
)
parser.add_argument(
"--n_detect", default=0, type=int, help="Number of detected signals to plot."
)
parser.add_argument(
"--plot_snr", action="store_true", help="Plot SNR rather than power."
)
parser.add_argument(
"--detection_type",
default="",
type=str,
help="Detection type to plot (wideband, narrowband).",
)
parser.add_argument(
"--save_path", default="", type=str, help="Path to save screenshots."
)
parser.add_argument(
"--save_time",
default=1,
type=int,
help="Save screenshot every save_time minutes. Only used if save_path also defined.",
)
parser.add_argument(
"--scanners",
default="127.0.0.1:8001",
type=str,
help="Scanner endpoints to use.",
)
parser.add_argument(
"--port",
default=0,
type=int,
help="If set, serve waterfall on this port.",
)
parser.add_argument(
"--rotate_secs",
default=900,
type=int,
help="If > 0, rotate save directories every N seconds",
)
parser.add_argument(
"--width",
default=28,
type=float,
help="Waterfall image width",
)
parser.add_argument(
"--height",
default=10,
type=float,
help="Waterfall image height",
)
parser.add_argument(
"--waterfall_height",
default=100,
type=int,
help="Waterfall height",
)
parser.add_argument(
"--waterfall_width",
default=5000,
type=int,
help="Waterfall width (maximum)",
)
parser.add_argument(
"--refresh",
default=5,
type=int,
help="Waterfall refresh time",
)
parser.add_argument(
"--predictions",
default=3,
type=int,
help="If set, render N recent predictions",
)
parser.add_argument(
"--inference_server",
default="",
type=str,
help="Address of scanner for inference feed",
)
parser.add_argument(
"--inference_port",
default=10002,
type=int,
help="Port on scanner to connect to inference feed",
)
return parser
def reset_mesh_psd(config, state, data=None):
if state.mesh_psd:
state.mesh_psd.remove()
X, Y = meshgrid(config, state.db_min, state.db_max, config.psd_db_resolution)
state.psd_x_edges = X[0]
state.psd_y_edges = Y[:, 0]
if data is None:
data = np.zeros(X[:-1, :-1].shape)
state.mesh_psd = state.ax_psd.pcolormesh(X, Y, data, shading="flat")
def reset_mesh(state, data):
if state.mesh:
state.mesh.remove()
state.mesh = state.ax.pcolormesh(state.X, state.Y, data, shading="nearest")
def reset_fig(
config,
state,
):
logging.info("resetting figure")
state.fig.clf()
plt.tight_layout()
plt.subplots_adjust(hspace=0.15)
state.ax_psd = state.fig.add_subplot(3, 1, 1)
state.ax = state.fig.add_subplot(3, 1, (2, 3))
state.psd_title = state.ax_psd.text(
0.5,
1.05,
"",
transform=state.ax_psd.transAxes,
va="center",
ha="center",
)
default_data = state.db_min * np.ones(state.freq_data.shape[1])
reset_mesh_psd(config, state)
def ax_psd_plot(linestyle=":", **kwargs):
return state.ax_psd.plot(
state.X[0],
default_data,
markevery=config.marker_distance,
linestyle=linestyle,
**kwargs,
)
(state.peak_lns,) = ax_psd_plot(
color="white",
marker="^",
markersize=12,
linestyle="none",
fillstyle="full",
)
(state.max_psd_ln,) = ax_psd_plot(
color="red",
marker=",",
label="max",
)
(state.min_psd_ln,) = ax_psd_plot(
color="pink",
marker=",",
label="min",
)
(state.mean_psd_ln,) = ax_psd_plot(
color="cyan",
marker="^",
markersize=8,
fillstyle="none",
label="mean",
)
(state.current_psd_ln,) = ax_psd_plot(
color="red",
marker="o",
markersize=8,
fillstyle="none",
label="current",
)
state.ax_psd.legend(loc="center left", bbox_to_anchor=(1, 0.5))
state.ax_psd.set_ylabel("dB")
# SPECTROGRAM
reset_mesh(state, state.db_data)
state.top_n_lns = []
for _ in range(config.top_n):
(ln,) = state.ax.plot(
[state.X[0][0]] * len(state.Y[:, 0]),
state.Y[:, 0],
color="brown",
linestyle=":",
alpha=0,
)
ln.set_alpha(0.75)
state.top_n_lns.append(ln)
state.ax.set_xlabel("MHz")
state.ax.set_ylabel("Time")
# COLORBAR
state.sm = plt.cm.ScalarMappable(cmap=state.cmap)
state.sm.set_clim(vmin=state.db_min, vmax=state.db_max)
if config.plot_snr:
state.sm.set_clim(vmin=config.snr_min, vmax=config.snr_max)
state.cbar_ax = state.fig.add_axes([0.92, 0.10, 0.03, 0.5])
state.cbar = state.fig.colorbar(state.sm, cax=state.cbar_ax)
state.cbar.set_label("dB", rotation=0)
# SPECTROGRAM TITLE
_title = state.ax.text(
0.5, 1.05, "", transform=state.ax.transAxes, va="center", ha="center"
)
for ax in (state.ax.xaxis, state.ax_psd.xaxis):
ax.set_major_locator(state.major_tick_separator)
if config.freq_resolution < 0.01:
ax.set_major_formatter("{x:.1f}")
else:
ax.set_major_formatter("{x:.0f}")
ax.set_minor_locator(state.minor_tick_separator)
for ax in (state.ax_psd.yaxis, state.cbar_ax.yaxis, state.ax.yaxis):
ax.set_animated(True)
state.ax.draw_artist(state.mesh)
state.fig.canvas.blit(state.ax.bbox)
if not config.batch:
plt.show(block=False)
state.fig.canvas.flush_events()
state.background = state.fig.canvas.copy_from_bbox(state.fig.bbox)
if config.savefig_path:
safe_savefig(config.savefig_path)
def meshgrid(config, start, stop, num):
return np.meshgrid(
np.linspace(
config.min_freq,
config.max_freq,
config.waterfall_width,
),
np.linspace(start, stop, num),
)
def init_fig(
config,
state,
onresize,
):
logging.info("initializing figure")
plt.close("all")
state.cmap = plt.get_cmap("viridis")
state.cmap_psd = plt.get_cmap("turbo")
state.minor_tick_separator = AutoMinorLocator()
state.major_tick_separator = MultipleLocator(config.freq_range / config.n_ticks)
plt.rcParams["savefig.facecolor"] = "#2A3459"
plt.rcParams["figure.facecolor"] = "#2A3459"
for param in (
"text.color",
"axes.labelcolor",
"xtick.color",
"ytick.color",
"axes.facecolor",
):
plt.rcParams[param] = "#d2d5dd"
state.fig = plt.figure(figsize=(config.width, config.height), dpi=100)
if not config.batch:
state.fig.canvas.mpl_connect("resize_event", onresize)
def draw_peaks(
config,
state,
scan_time,
scan_configs,
):
peaks, properties = state.peak_finder.find_peaks(state.db_data[-1])
peaks, properties = filter_peaks(peaks, properties)
left_ips = properties["left_ips"].astype(int)
right_ips = properties["right_ips"].astype(int)
if state.save_path:
save_detections(
config,
state,
scan_time,
scan_configs,
peaks,
properties,
)
state.peak_lns.set_xdata(state.psd_x_edges[peaks])
state.peak_lns.set_ydata(properties["width_heights"])
for child in state.ax_psd.get_children():
if isinstance(child, LineCollection):
child.remove()
for i in range(len(state.detection_text) - 1, -1, -1):
state.detection_text[i].set_visible(False)
state.detection_text.pop(i)
if len(peaks) > 0:
vl_center = state.ax_psd.vlines(
x=state.psd_x_edges[peaks],
ymin=state.db_data[-1][peaks] - properties["prominences"],
ymax=state.db_data[-1][peaks],
color="white",
)
state.ax_psd.draw_artist(vl_center)
vl_edges = state.ax_psd.vlines(
x=np.concatenate(
(
state.psd_x_edges[left_ips],
state.psd_x_edges[right_ips],
)
),
ymin=state.db_min,
ymax=np.tile(state.db_data[-1][peaks], 2),
color="white",
)
state.ax_psd.draw_artist(vl_edges)
for l_ips, r_ips, p in zip(
state.psd_x_edges[left_ips],
state.psd_x_edges[right_ips],
state.db_data[-1][peaks],
):
shaded = state.ax_psd.fill_between(
[l_ips, r_ips], state.db_min, p, alpha=0.7
)
state.ax_psd.draw_artist(shaded)
hl = state.ax_psd.hlines(
y=properties["width_heights"],
xmin=state.psd_x_edges[left_ips],
xmax=state.psd_x_edges[right_ips],
color="white",
)
state.ax_psd.draw_artist(hl)
for l_ips, r_ips, p in zip(
state.psd_x_edges[left_ips],
state.psd_x_edges[right_ips],
peaks,
):
for txt in (
state.ax_psd.text(
l_ips + ((r_ips - l_ips) / 2),
(0.15 * (state.db_max - state.db_min)) + state.db_min,
f"f={l_ips + ((r_ips - l_ips)/2):.0f}MHz",
size=10,
ha="center",
color="white",
rotation=40,
),
state.ax_psd.text(
l_ips + ((r_ips - l_ips) / 2),
(0.05 * (state.db_max - state.db_min)) + state.db_min,
f"BW={r_ips - l_ips:.0f}MHz",
size=10,
ha="center",
color="white",
rotation=40,
),
):
state.detection_text.append(txt)
state.ax_psd.draw_artist(txt)
def update_fig(config, state, results):
if not state.fig or not state.ax:
raise NotImplementedError
if config.base_save_path and config.rotate_secs:
state.save_path = os.path.join(
config.base_save_path,
str(int(time.time() / config.rotate_secs) * config.rotate_secs),
)
if not os.path.exists(state.save_path):
Path(state.save_path).mkdir(parents=True, exist_ok=True)
if len(results) > 1:
logging.info("processing backlog of %u results", len(results))
scan_duration = 0
for scan_configs, scan_df in results:
tune_step_hz = min(scan_config["tune_step_hz"] for scan_config in scan_configs)
tune_step_fft = min(
scan_config["tune_step_fft"] for scan_config in scan_configs
)
scan_duration = scan_df.ts.max() - scan_df.ts.min()
tune_count = scan_df.tune_count.max()
if scan_duration:
tune_rate_hz = tune_count / scan_duration
tune_dwell_ms = (scan_duration * 1e3) / tune_count
else:
tune_rate_hz = 0
tune_dwell_ms = 0
idx = (
((scan_df.freq - config.min_freq) / config.freq_resolution)
.round()
.clip(lower=0, upper=(state.db_data.shape[1] - 1))
.values.flatten()
.astype(int)
)
state.freq_data = np.roll(state.freq_data, -1, axis=0)
state.freq_data[-1][idx] = scan_df.freq.values.flatten()
state.db_data = np.roll(state.db_data, -1, axis=0)
state.db_data[-1][idx] = scan_df.db.values.flatten()
scan_time = scan_df.ts.iloc[-1]
row_time = datetime.datetime.fromtimestamp(scan_time)
if scan_time not in state.scan_config_history:
state.scan_times.append(scan_time)
state.scan_config_history[scan_time] = scan_configs
while len(state.scan_times) > config.waterfall_height:
remove_time = state.scan_times.pop(0)
state.scan_config_history.pop(remove_time)
if state.counter % config.y_label_skip == 0:
state.y_labels.append(row_time.strftime("%Y-%m-%d %H:%M:%S"))
else:
state.y_labels.append("")
state.y_ticks.append(config.waterfall_height)
for j in range(len(state.y_ticks) - 2, -1, -1):
state.y_ticks[j] -= 1
if state.y_ticks[j] < 1:
state.y_ticks.pop(j)
state.y_labels.pop(j)
state.counter += 1
if state.counter % config.draw_rate == 0:
now = time.time()
since_last_plot = 0
if state.last_plot:
since_last_plot = now - state.last_plot
state.last_plot = now
logging.info(f"Plotting {row_time} (seconds since last plot {since_last_plot})")
state.db_min = np.nanmin(state.db_data)
state.db_max = np.nanmax(state.db_data)
data, _xedge, _yedge = np.histogram2d(
state.freq_data[~np.isnan(state.freq_data)].flatten(),
state.db_data[~np.isnan(state.db_data)].flatten(),
density=False,
bins=[state.psd_x_edges, state.psd_y_edges],
)
heatmap = gaussian_filter(data, sigma=2)
data = heatmap / np.max(heatmap)
db_norm = (state.db_data - state.db_min) / (state.db_max - state.db_min)
if config.plot_snr:
db_norm = (
(state.db_data - np.nanmin(state.db_data, axis=0)) - config.snr_min
) / (config.snr_max - config.snr_min)
top_n_bins = state.freq_bins[
np.argsort(
np.nanvar(state.db_data - np.nanmin(state.db_data, axis=0), axis=0)
)[::-1][: config.top_n]
]
state.ax.set_yticks(state.y_ticks, labels=state.y_labels)
if state.background:
state.fig.canvas.restore_region(state.background)
for top_n_bin, ln in zip(top_n_bins, state.top_n_lns):
ln.set_xdata([top_n_bin] * len(state.Y[:, 0]))
state.fig.canvas.blit(state.ax.yaxis.axes.figure.bbox)
reset_mesh_psd(config, state, data=state.cmap_psd(data.T))
state.ax_psd.set_ylim(state.db_min, state.db_max)
state.current_psd_ln.set_ydata(state.db_data[-1])
for ln, ln_func in (
(state.min_psd_ln, np.nanmin),
(state.max_psd_ln, np.nanmax),
(state.mean_psd_ln, np.nanmean),
):
ln.set_ydata(ln_func(state.db_data, axis=0))
state.ax_psd.draw_artist(state.mesh_psd)
if state.peak_finder:
draw_peaks(
config,
state,
scan_time,
scan_configs,
)
for ln in (
state.peak_lns,
state.min_psd_ln,
state.max_psd_ln,
state.mean_psd_ln,
state.current_psd_ln,
):
state.ax_psd.draw_artist(ln)
reset_mesh(state, state.cmap(db_norm))
state.ax.draw_artist(state.mesh)
draw_title(
state.ax_psd,
state.psd_title,
scan_duration,
tune_step_hz,
tune_step_fft,
tune_rate_hz,
tune_dwell_ms,
config.sampling_rate,
config.freq_resolution,
)
state.sm.set_clim(vmin=state.db_min, vmax=state.db_max)
state.cbar.update_normal(state.sm)
for ax in (state.cbar_ax.yaxis, state.ax_psd.yaxis):
state.cbar_ax.draw_artist(ax)
state.fig.canvas.blit(ax.axes.figure.bbox)
for ln in state.top_n_lns:
state.ax.draw_artist(ln)
state.ax.draw_artist(state.ax.yaxis)
for bmap in (
state.ax_psd.bbox,
state.ax.yaxis.axes.figure.bbox,
state.ax.bbox,
state.cbar_ax.bbox,
state.fig.bbox,
):
state.fig.canvas.blit(bmap)
state.fig.canvas.flush_events()
fig_path = None
if config.savefig_path:
fig_path = safe_savefig(config.savefig_path)
if state.save_path:
save_waterfall(
state,
config.save_time,
scan_time,
fig_path=fig_path,
)
class WaterfallConfig:
def __init__(
self,
engine,
plot_snr,
savefig_path,
sampling_rate,
fft_len,
min_freq,
max_freq,
top_n,
base_save_path,
width,
height,
waterfall_height,
waterfall_width,
batch,
rotate_secs,
save_time,
):
self.engine = engine
self.plot_snr = plot_snr
self.savefig_path = savefig_path
self.snr_min = 0
self.snr_max = 50
self.waterfall_height = waterfall_height # number of waterfall rows
self.marker_distance = 0.1
self.scale = 1e6
self.fft_len = fft_len
self.sampling_rate = sampling_rate
self.psd_db_resolution = 90
self.y_label_skip = 3
self.top_n = top_n
self.draw_rate = 1
self.base_save_path = base_save_path
self.width = width
self.height = height
self.batch = batch
self.reclose_interval = 25
self.min_freq = min_freq / self.scale
self.max_freq = max_freq / self.scale
self.freq_range = self.max_freq - self.min_freq
self.freq_resolution = self.sampling_rate / self.scale / self.fft_len * 2
self.waterfall_width = int(
min(self.freq_range / self.freq_resolution, waterfall_width)
)
self.freq_resolution = self.freq_range / self.waterfall_width
self.n_ticks = 20
self.rotate_secs = rotate_secs
self.save_time = save_time
def __eq__(self, other):
for attr in ("fft_len", "sampling_rate", "min_freq", "max_freq"):
if getattr(self, attr) != getattr(other, attr):
return False
return True
class WaterfallState:
def __init__(self, config, save_path, peak_finder):
self.db_min = -220
self.db_max = -150
self.detection_text = []
self.scan_times = []
self.scan_config_history = {}
self.y_ticks = []
self.y_labels = []
self.previous_scan_config = None
self.last_save_time = None
self.counter = 0
self.minor_tick_separator = None
self.major_tick_separator = None
self.cmap_psd = None
self.cmap = None
self.fig = None
self.top_n_lns = None
self.background = None
self.mesh = None
self.psd_title = None
self.cbar_ax = None
self.cbar = None
self.psd_x_edges = None
self.psd_y_edges = None
self.min_psd_ln = None
self.max_psd_ln = None
self.mean_psd_ln = None
self.current_psd_ln = None
self.ax_psd = None
self.ax = None
self.save_path = save_path
self.mesh_psd = None
self.peak_finder = peak_finder
self.last_plot = 0
self.X, self.Y = meshgrid(
config, 1, config.waterfall_height, config.waterfall_height
)
self.freq_bins = self.X[0]
self.db_data = np.empty(self.X.shape)
self.db_data.fill(np.nan)
self.freq_data = np.empty(self.X.shape)
self.freq_data.fill(np.nan)
def make_config(
scan_configs,
min_freq,
max_freq,
engine,
plot_snr,
savefig_path,
top_n,
base_save_path,
width,
height,
waterfall_height,
waterfall_width,
batch,
rotate_secs,
save_time,
):
sampling_rate = max([scan_config["sample_rate"] for scan_config in scan_configs])
fft_len = max([scan_config["nfft"] for scan_config in scan_configs])
if min_freq == 0:
min_freq = min([scan_config["freq_start"] for scan_config in scan_configs])
if max_freq == 0:
max_freq = max([scan_config["freq_end"] for scan_config in scan_configs])
config = WaterfallConfig(
engine,
plot_snr,
savefig_path,
sampling_rate,
fft_len,
min_freq,
max_freq,
top_n,
base_save_path,
width,
height,
waterfall_height,
waterfall_width,
batch,
rotate_secs,
save_time,
)
return config
def waterfall(
min_freq,
max_freq,
plot_snr,
top_n,
base_save_path,
save_time,
peak_finder,
engine,
savefig_path,
rotate_secs,
width,
height,
waterfall_height,
waterfall_width,
batch,
refresh,
zmqr,
):
style.use("fast")
global need_reset_fig
need_reset_fig = True
global running
running = True
need_init = True
need_reconfig = True
def onresize(_event): # nosemgrep
global need_reset_fig
need_reset_fig = True
def sig_handler(_sig=None, _frame=None):
global running
running = False
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
logging.info("awaiting scanner startup")
while not zmqr.healthy():
time.sleep(0.1)
logging.info("awaiting initial config from scanner(s)")
scan_configs = None
scan_df = None
while zmqr.healthy() and running:
scan_configs, scan_df = zmqr.read_buff(scan_fres=SCAN_FRES)
if scan_df is not None:
break
time.sleep(0.1)
if not scan_configs:
return
while zmqr.healthy() and running:
if need_reconfig:
config = make_config(
scan_configs,
min_freq,
max_freq,
engine,
plot_snr,
savefig_path,
top_n,
base_save_path,
width,
height,
waterfall_height,
waterfall_width,
batch,
rotate_secs,
save_time,
)
logging.info(
"scanning %fMHz to %fMHz at %fMsps with %u FFT points at %fMHz resolution",
config.min_freq,
config.max_freq,
config.sampling_rate / 1e6,
config.fft_len,
config.freq_resolution,
)
state = WaterfallState(config, base_save_path, peak_finder)
matplotlib.use(config.engine)
results = [
(scan_configs, frame_resample(scan_df, config.freq_resolution * 1e6))
]
need_reconfig = False