|
| 1 | +#! /usr/bin/python3 |
| 2 | + |
| 3 | +##-------------------------------------------------------------------------------\ |
| 4 | +# tinySA_python (tsapython) |
| 5 | +# './examples/plotting_scan.py' |
| 6 | +# A short example using matplotlib to plot requested SCAN data |
| 7 | +# |
| 8 | +# Last update: August 17, 2025 |
| 9 | +##-------------------------------------------------------------------------------\ |
| 10 | + |
| 11 | + |
| 12 | +# import tinySA_python (tsapython) package |
| 13 | +fromtsapythonimporttinySA |
| 14 | + |
| 15 | + |
| 16 | +# imports FOR THE EXAMPLE |
| 17 | +importnumpyasnp |
| 18 | +importmatplotlib.pyplotasplt |
| 19 | + |
| 20 | +defconvert_data_to_arrays(start,stop,pts,data): |
| 21 | +# using the start and stop frequencies, and the number of points, |
| 22 | + |
| 23 | +freq_arr=np.linspace(start,stop,pts)# note that the decimals might go out to many places. |
| 24 | +# you can truncate this because its only used |
| 25 | +# for plotting in this example |
| 26 | + |
| 27 | +# As of the Jan. 2024 build in some data returned with SWEEP or SCAN calls there is error data. |
| 28 | +# https://groups.io/g/tinysa/topic/tinasa_ultra_sweep_command/104194367 |
| 29 | +# this shows up as "-:.000000e+01". |
| 30 | +# TEMP fix - replace the colon character with a -10. This puts the 'filled in' points around the noise floor. |
| 31 | +# more advanced filtering should be applied for actual analysis. |
| 32 | + |
| 33 | +data1=bytearray(data.replace(b"-:.0",b"-10.0")) |
| 34 | + |
| 35 | +# get both values in each row returned (for reference) |
| 36 | +#data_arr = [list(map(float, line.split())) for line in data.decode('utf-8').split('\n') if line.strip()] |
| 37 | + |
| 38 | +# get first value in each returned row |
| 39 | +data_arr= [float(line.split()[0])forlineindata1.decode('utf-8').split('\n')ifline.strip()] |
| 40 | + |
| 41 | +returnfreq_arr,data_arr |
| 42 | + |
| 43 | + |
| 44 | +# create a new tinySA object |
| 45 | +tsa=tinySA() |
| 46 | + |
| 47 | +# set the return message preferences |
| 48 | +tsa.set_verbose(True)#detailed messages |
| 49 | +tsa.set_error_byte_return(True)#get explicit b'ERROR' if error thrown |
| 50 | + |
| 51 | +# attempt to autoconnect |
| 52 | +found_bool,connected_bool=tsa.autoconnect() |
| 53 | + |
| 54 | +# if port closed, then return error message |
| 55 | +ifconnected_bool==False: |
| 56 | +print("ERROR: could not connect to port") |
| 57 | +else:# if port found and connected, then complete task(s) and disconnect |
| 58 | +# set scan values |
| 59 | +start=int(1e9)# 1 GHz |
| 60 | +stop=int(3e9)# 3 GHz |
| 61 | +pts=450# sample points |
| 62 | +outmask=2# get measured data (y axis) |
| 63 | + |
| 64 | +# scan |
| 65 | +data_bytes=tsa.scan(start,stop,pts,outmask) |
| 66 | + |
| 67 | +print(data_bytes) |
| 68 | + |
| 69 | +tsa.resume()#resume so screen isn't still frozen |
| 70 | + |
| 71 | +tsa.disconnect() |
| 72 | + |
| 73 | +# processing after disconnect (just for this example) |
| 74 | + |
| 75 | +# convert data to 2 arrays |
| 76 | +freq_arr,data_arr=convert_data_to_arrays(start,stop,pts,data_bytes) |
| 77 | + |
| 78 | +# plot |
| 79 | +plt.plot(freq_arr,data_arr) |
| 80 | +plt.xlabel("Frequency (Hz)") |
| 81 | +plt.ylabel("Measured Data (dBm)") |
| 82 | +plt.title("tinySA Scan Plot") |
| 83 | +plt.show() |