Skip to content

Commit

Permalink
Add basic peak measurement to adiplot
Browse files Browse the repository at this point in the history
Signed-off-by: Travis F. Collins <[email protected]>
  • Loading branch information
tfcollins committed Mar 3, 2023
1 parent 698440d commit b0ad9eb
Showing 1 changed file with 91 additions and 21 deletions.
112 changes: 91 additions & 21 deletions examples/adiplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

import adi
import numpy as np
# import pyqtgraph as pg
# from PyQt5 import QtGui
# from pyqtgraph.Qt import QtCore, QtGui

from scipy import signal
from scipy.fftpack import fft

Expand All @@ -20,14 +18,13 @@
from random import randint

pg.setConfigOptions(antialias=True)
pg.setConfigOption("background", "k")

REAL_DEV_NAME = "cn05".lower()


class ADIPlotter(object):

def __init__(self, classname, uri):

self.classname = classname
self.q = Queue(maxsize=20)
self.stream = eval("adi." + classname + "(uri='" + uri + "')")
Expand All @@ -36,42 +33,54 @@ def __init__(self, classname, uri):
self.stream.rx_lo = 1000000000
self.stream.tx_lo = 1000000000
self.stream.dds_single_tone(3000000, 0.9)
self.stream.rx_buffer_size = 2 ** 12
self.stream.rx_buffer_size = 2**12
self.stream.rx_enabled_channels = [0]

self.app = QtWidgets.QApplication(sys.argv)

self.qmw = QtWidgets.QMainWindow()
self.qmw.graphWidget = pg.PlotWidget()
self.qmw.setCentralWidget(self.qmw.graphWidget)
self.qmw.graphWidget.setBackground('w')

self.win = self.qmw.graphWidget
self.qmw.central_widget = QtWidgets.QWidget()
self.qmw.vertical_layout = QtWidgets.QVBoxLayout()
self.qmw.setCentralWidget(self.qmw.central_widget)
self.qmw.central_widget.setLayout(self.qmw.vertical_layout)

#### Add Plot
self.qmw.graphWidget = pg.PlotWidget()
self.qmw.graphWidget.setBackground("black")

self.traces = {}

self.win = GraphicsLayoutWidget(self.qmw.centralWidget())
self.win = GraphicsLayoutWidget()
self.qmw.vertical_layout.addWidget(self.win, 1)
self.win.setWindowTitle("Spectrum Analyzer")
self.win.setGeometry(5, 115, 1910, 1070)
self.win.setBackground(background="black")

wf_xaxis = pg.AxisItem(orientation="bottom")
wf_xaxis.setLabel(units="Seconds")

if REAL_DEV_NAME in classname.lower():
wf_ylabels = [(0, "0"), (2 ** 11, "2047")]
wf_ylabels = [(0, "0"), (2**11, "2047")]
else:
wf_ylabels = [(-2 * 11, "-2047"), (0, "0"), (2 ** 11, "2047")]
wf_ylabels = [(-2 * 11, "-2047"), (0, "0"), (2**11, "2047")]
wf_yaxis = pg.AxisItem(orientation="left")
wf_yaxis.setTicks([wf_ylabels])

sp_xaxis = pg.AxisItem(orientation="bottom")
sp_xaxis.setLabel(units="Hz")

self.waveform = self.win.addPlot(
title="WAVEFORM", row=1, col=1, axisItems={"bottom": wf_xaxis},
title="WAVEFORM",
row=1,
col=1,
axisItems={"bottom": wf_xaxis},
)
self.spectrum = self.win.addPlot(
title="SPECTRUM", row=2, col=1, axisItems={"bottom": sp_xaxis},
title="SPECTRUM",
row=2,
col=1,
axisItems={"bottom": sp_xaxis},
)
self.waveform.showGrid(x=True, y=True)
self.spectrum.showGrid(x=True, y=True)
Expand All @@ -88,18 +97,25 @@ def __init__(self, classname, uri):
self.min = -100
self.window = signal.kaiser(self.stream.rx_buffer_size, beta=38)

#### Add a plot to contain our measurement data
# This is faster than using a table
self.measurements = self.win.addPlot(title="Measurements", row=3, col=1)
self.measurements.hideAxis("left")
self.measurements.hideAxis("bottom")

self.qmw.show()

self.run_source = True
self.thread = threading.Thread(target=self.source)
self.thread.start()

self.markers_added = False

def source(self):
print("Thread running")
self.counter = 0
while self.run_source:
data = self.stream.rx()
# print("counter: ", self.counter)
self.counter += 1
try:
self.q.put(data, block=False, timeout=4)
Expand All @@ -109,6 +125,43 @@ def source(self):
def start(self):
self.app.exec_()

def add_markers(self, plot):
#### Add peak marker for spectrum plot
data = plot.getData()
if data[0] is None:
return
self.curve_point = pg.CurvePoint(plot)
self.spectrum.addItem(self.curve_point)
self.text_peak = pg.TextItem("TEST", anchor=(0.5, 1.0))
self.text_peak.setParentItem(parent=self.curve_point)

self.build_custom_table_from_textitems()

self.markers_added = True

def build_custom_table_from_textitems(self):
text_items = ["Peak", "Frequency", "Amplitude"]
self.custom_table = {}
self.table_x = 180
self.table_y = 50
scaler = 30
for i, text in enumerate(text_items):
self.custom_table[text] = pg.TextItem(text=text)
# set parent plot
self.custom_table[text].setParentItem(parent=self.measurements)
# set position
self.custom_table[text].setPos(self.table_x, self.table_y + scaler * i)

def update_custom_table(self):
if not self.markers_added:
return
self.custom_table["Frequency"].setText(
"Frequency: {:.2f} Hz".format(self.curve_point.pos().x())
)
self.custom_table["Amplitude"].setText(
"Amplitude: {:.2f} dB".format(self.curve_point.pos().y())
)

def set_plotdata(self, name, data_x, data_y):
if name in self.traces:
self.traces[name].setData(data_x, data_y)
Expand All @@ -122,11 +175,13 @@ def set_plotdata(self, name, data_x, data_y):
else -1 * self.stream.sample_rate / 2
)
self.spectrum.setXRange(
start, self.stream.sample_rate / 2, padding=0.005,
start,
self.stream.sample_rate / 2,
padding=0.005,
)
elif name == "waveform":
self.traces[name] = self.waveform.plot(pen="c", width=3)
self.waveform.setYRange(-(2 ** 11) - 200, 2 ** 11 + 200, padding=0)
self.waveform.setYRange(-(2**11) - 200, 2**11 + 200, padding=0)
self.waveform.setXRange(
0,
self.stream.rx_buffer_size / self.stream.sample_rate,
Expand All @@ -137,13 +192,29 @@ def update(self):
while not self.q.empty():
wf_data = self.q.get()
self.set_plotdata(
name="waveform", data_x=self.x, data_y=np.real(wf_data),
name="waveform",
data_x=self.x,
data_y=np.real(wf_data),
)
sp_data = np.fft.fft(wf_data)
sp_data = np.abs(np.fft.fftshift(sp_data)) / self.stream.rx_buffer_size
sp_data = 20 * np.log10(sp_data / (2 ** 11))
sp_data = 20 * np.log10(sp_data / (2**11))
self.set_plotdata(name="spectrum", data_x=self.f, data_y=sp_data)

if not self.markers_added:
self.add_markers(self.traces["spectrum"])

# Find peak of spectrum
index = np.argmax(sp_data)

# Add label to plot at the peak of the spectrum
if self.markers_added:
self.curve_point.setPos(float(index) / (len(self.f) - 1))
self.text_peak.setText(
"[%0.1f, %0.1f]" % (self.f[index], sp_data[index])
)
self.update_custom_table()

def animation(self):
timer = QtCore.QTimer()
timer.timeout.connect(self.update)
Expand All @@ -153,7 +224,6 @@ def animation(self):


if __name__ == "__main__":

parser = argparse.ArgumentParser(description="ADI fast plotting app")
parser.add_argument(
"class", help="pyadi class name to use as plot source", action="store"
Expand Down

0 comments on commit b0ad9eb

Please sign in to comment.