-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplotting_scan.py
More file actions
94 lines (70 loc) · 3.44 KB
/
Copy pathplotting_scan.py
File metadata and controls
94 lines (70 loc) · 3.44 KB
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
#! /usr/bin/python3
##-------------------------------------------------------------------------------\
# tinySA_python (tsapython)
# './examples/plotting_scan.py'
# A short example using matplotlib to plot requested SCAN data
#
# Last update: June 3, 2026
##-------------------------------------------------------------------------------\
# import tinySA_python (tsapython) package
from tsapython import tinySA
# imports FOR THE EXAMPLE
# This example needs the optional plotting dependencies.
# Install them with: pip install "tsapython[plotting]"
try:
import numpy as np
import matplotlib.pyplot as plt
except ImportError as exc:
raise SystemExit(
"This example requires the plotting extra (numpy and matplotlib). "
'Install it with: pip install "tsapython[plotting]"'
) from exc
def convert_data_to_arrays(start, stop, pts, data):
# using the start and stop frequencies, and the number of points,
freq_arr = np.linspace(start, stop, pts) # note that the decimals might go out to many places.
# you can truncate this because its only used
# for plotting in this example
# As of the Jan. 2024 build in some data returned with SWEEP or SCAN calls there is error data.
# https://groups.io/g/tinysa/topic/tinasa_ultra_sweep_command/104194367
# this shows up as "-:.000000e+01" (and the unsigned ":.000000e-01" / ":.000000e+01").
# The ':' is ASCII 0x3A, one past '9' (0x39): the firmware overflows a single digit
# slot, so what should read "10" renders as ":". Both signed AND unsigned forms occur.
# TEMP fix - replace the colon form with 10. This puts the 'filled in' points around the
# noise floor. More advanced filtering should be applied for actual analysis.
# NOTE: order matters -- handle the negative form first, then the bare/unsigned form.
data1 =bytearray(data.replace(b"-:.0", b"-10.0").replace(b":.0", b"10.0"))
# get both values in each row returned (for reference)
#data_arr = [list(map(float, line.split())) for line in data.decode('utf-8').split('\n') if line.strip()]
# get first value in each returned row
data_arr = [float(line.split()[0]) for line in data1.decode('utf-8').split('\n') if line.strip()]
return freq_arr, data_arr
# create a new tinySA object
tsa = tinySA()
# set the return message preferences
tsa.set_verbose(True) #detailed messages
tsa.set_error_byte_return(True) #get explicit b'ERROR' if error thrown
# attempt to autoconnect
found_bool, connected_bool = tsa.autoconnect()
# if port closed, then return error message
if connected_bool == False:
print("ERROR: could not connect to port")
else: # if port found and connected, then complete task(s) and disconnect
# set scan values
start = int(1e9) # 1 GHz
stop = int(3e9) # 3 GHz
pts = 450 # sample points
outmask = 2 # get measured data (y axis)
# scan
data_bytes = tsa.scan(start, stop, pts, outmask)
print(data_bytes)
tsa.resume() #resume so screen isn't still frozen
tsa.disconnect()
# processing after disconnect (just for this example)
# convert data to 2 arrays
freq_arr, data_arr = convert_data_to_arrays(start, stop, pts, data_bytes)
# plot
plt.plot(freq_arr, data_arr)
plt.xlabel("Frequency (Hz)")
plt.ylabel("Measured Data (dBm)")
plt.title("tinySA Scan Plot")
plt.show()