import sys import time from pyqtgraph.Qt import QtCore, QtGui import numpy as np import pyqtgraph as pg import serial nloop = 1000 nloopfourier = nloop/2+1 # the length of the FFT array naverage = 75 #75 path1 = 0*np.ones(nloop) path2 = 0*np.ones(nloopfourier) hiarr = np.zeros(naverage) port = "/dev/tty.usbserial-FT9P3WMR" def lefttoright(x): ''' this is just a helper function that transforms right adjusted binarys to left adjusted ones, with input and output as decimals ''' binin= bin(x)[2:] binout=(binin.zfill(8))[::-1] res = int(binout, 2) return res class App(QtGui.QMainWindow): def __init__(self, parent=None): super(App, self).__init__(parent) #### Create Gui Elements ########### self.mainbox = QtGui.QWidget() self.setCentralWidget(self.mainbox) self.mainbox.setLayout(QtGui.QVBoxLayout()) self.canvas = pg.GraphicsLayoutWidget() self.mainbox.layout().addWidget(self.canvas) self.label = QtGui.QLabel() self.mainbox.layout().addWidget(self.label) # line plots self.otherplot = self.canvas.addPlot() self.h1 = self.otherplot.plot(pen='y') self.canvas.nextRow() self.otherplot = self.canvas.addPlot() self.h2 = self.otherplot.plot(pen='y') self.counter = 0 self.fps = 0. self.lastupdate = time.time() #### Start ##################### self._update() def _update(self): global zerosig, path1, path2 #the period of the main loop is 24s as measured manually tmax=24.0 self.xdata1= np.linspace(0, tmax, nloop) self.ydata1 = path1 self.h1.setData(self.xdata1, self.ydata1) self.xdata2 = np.array([tmax/float(i+1) for i in range(nloopfourier)]) self.ydata2 = path2 self.h2.setData(self.xdata2,self.ydata2) now = time.time() dt = (now-self.lastupdate) if dt <= 0: dt = 0.000000000001 fps2 = 1.0 / dt self.lastupdate = now self.fps = self.fps * 0.9 + fps2 * 0.1 tx = 'Mean Frame Rate: {fps:.3f} FPS'.format(fps=self.fps ) self.label.setText(tx) QtCore.QTimer.singleShot(1, self._update) self.counter += 1 for i in range(naverage): hiarr[i]=lefttoright(ord(ser.read())) # hiarr[i]=ord(ser.read()) reading = np.average(hiarr) path1 = np.roll(path1, -1) #this gives a constant shift of a time graph # to normalize the signal, the log is used with respect #to an average value of hiarr(190 in my case) path1[-1] = reading # this removes the rolling of "0" and "-1" elements and adds new datum #20*np.log10(reading/190.0) #below is the fourier transform conversion to obtain the average period fftarr = np.fft.rfft(path1) path2 = np.imag(fftarr) if __name__ == '__main__': ser = serial.Serial(port,115200) # the maximum baud rate for the serial cable app = QtGui.QApplication(sys.argv) thisapp = App() thisapp.show() sys.exit(app.exec_())