class PlotSignalWindow(): def __init__(self, width=1000, height=600): # signal self.signal = dict() # setup window self.app = QtGui.QApplication([]) self.win = GraphicsWindow(title="Plot Signal") self.win.resize(width, height) self.win.setWindowTitle('Plot Signal') # enable anti-aliasing setConfigOptions(antialias=True) self.canvas = self.win.addPlot(title="Fast Fourier Transform") # limit plot to 20Hz to 20kHz and clamp magnitude # self.canvas.setXRange(0, 20000) self.canvas.setYRange(0, 100000) def start(self): if (flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): QtGui.QApplication.instance().exec_() def draw(self, name: str, _x, _y, pen='y'): if name in self.signal: self.signal[name].setData(_x, _y) else: self.signal[name] = self.canvas.plot(pen=pen)
class MyWaveformViewer(object): def __init__(self): super(MyWaveformViewer, self).__init__() self.win = GraphicsWindow() self.p1 = self.win.addPlot() self.ptr = 0 self.init_ui() self.init_timer() def init_ui(self): self.win.setWindowTitle('Plot Demo') self.win.resize(600, 400) self.win.setBackground(background='k') self.p1.setLabel(axis='left', text='Y') self.p1.setLabel(axis='bottom', text='X') self.p1.addLegend() self.curve1 = self.p1.plot(pen='y', name='yaw') self.curve2 = self.p1.plot(pen='g', name='pitch') def init_timer(self): self.timer = pg.QtCore.QTimer() self.timer.timeout.connect(self.plot_data) self.timer.start(100) def plot_data(self): self.curve1.setData(list(range(11)), [x * x for x in range(11)]) if self.ptr == 0: # self.p1.disableAutoRange('xy') self.p1.enableAutoRange(axis='xy', enable=False, x=1, y=0.5) self.p1.disableAutoRange('xy') self.ptr += 1
class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(856, 406) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.widget = QtGui.QWidget(self.centralwidget) self.widget.setGeometry(QtCore.QRect(60, 100, 750, 166)) self.widget.setObjectName(_fromUtf8("widget")) self.verticalLayout = QtGui.QVBoxLayout(self.widget) self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) self.horizontalLayout_2 = QtGui.QHBoxLayout() self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2")) self.label = QtGui.QLabel(self.widget) self.label.setObjectName(_fromUtf8("label")) self.horizontalLayout_2.addWidget(self.label) self.cpuUsage = QtGui.QProgressBar(self.widget) self.cpuUsage.setProperty("value", 30) self.cpuUsage.setObjectName(_fromUtf8("cpuUsage")) self.horizontalLayout_2.addWidget(self.cpuUsage) self.verticalLayout.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtGui.QHBoxLayout() self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3")) self.label_3 = QtGui.QLabel(self.widget) self.label_3.setObjectName(_fromUtf8("label_3")) self.horizontalLayout_3.addWidget(self.label_3) self.memUsage = QtGui.QProgressBar(self.widget) self.memUsage.setProperty("value", 24) self.memUsage.setObjectName(_fromUtf8("memUsage")) self.horizontalLayout_3.addWidget(self.memUsage) self.verticalLayout.addLayout(self.horizontalLayout_3) self.horizontalLayout_4 = QtGui.QHBoxLayout() self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4")) self.label_2 = QtGui.QLabel(self.widget) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_2.setFixedWidth(166) self.horizontalLayout_4.addWidget(self.label_2) self.diskUsage = QtGui.QLabel(self.widget) self.diskUsage.setObjectName(_fromUtf8("diskUsage")) self.horizontalLayout_4.addWidget(self.diskUsage) self.verticalLayout.addLayout(self.horizontalLayout_4) self.horizontalLayout = QtGui.QHBoxLayout() self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.label_4 = QtGui.QLabel(self.widget) self.label_4.setObjectName(_fromUtf8("label_4")) self.label_4.setFixedWidth(166) self.horizontalLayout.addWidget(self.label_4) self.netUsage = QtGui.QLabel(self.widget) self.netUsage.setObjectName(_fromUtf8("netUsage")) self.horizontalLayout.addWidget(self.netUsage) self.netDetailButton = QtGui.QPushButton(self.centralwidget) self.netDetailButton.setGeometry(QtCore.QRect(725, 222.5, 60, 27)) self.netDetailButton.setObjectName(_fromUtf8("netDetailButton")) self.verticalLayout.addLayout(self.horizontalLayout) self.detailButton = QtGui.QPushButton(self.centralwidget) self.detailButton.setGeometry(QtCore.QRect(635, 300, 150, 46)) self.detailButton.setObjectName(_fromUtf8("detailButton")) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 796, 38)) self.menubar.setObjectName(_fromUtf8("menubar")) MainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) # update usage self.ctimer = QtCore.QTimer() QtCore.QObject.connect(self.ctimer, QtCore.SIGNAL("timeout()"), self.updateUsage) self.ntimer = None # detail window extend self.w = None self.detailButton.clicked.connect(self.popExtend) # network detail window extend self.nw = None self.netDetailButton.clicked.connect(self.netPopExtend) global popMsg popMsg = Windows10_notification() self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) # set up initial value of every usage self.memUsage.setValue(memUsage()) self.cpuUsage.setValue(cpuUsage()) self.diskUsage.setText("Read: 0.0 bytes/s | Write: 0.0 bytes/s") self.netUsage.setText("Sent: 0.0 bytes/s | Recv: 0.0 bytes/s") def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "SMS", None)) MainWindow.setWindowIcon(QtGui.QIcon(icon_path)) self.label.setText(_translate("MainWindow", "CPU usage: ", None)) self.label_3.setText(_translate("MainWindow", "Memory usage: ", None)) self.label_2.setText(_translate("MainWindow", "Disk usage: ", None)) self.label_4.setText(_translate("MainWindow", "Network usage:", None)) self.netUsage.setText(_translate("MainWindow", "TextLabel", None)) self.netDetailButton.setText(_translate("MainWindow",">>>",None)) self.detailButton.setText(_translate("MainWindow", "Detail >>>", None)) # start ctimer self.ctimer.start(500) def updateUsage(self): global networkUsageList global diskUsageList global warnCountList global popMsg ## check overusing processes for pid in pu.pids(): try: p = getProcess(pid) if warnCountList[pid][0] == 0 and getCPUusage(p) > 50 and pid != 0: warnCountList[pid][0] = 20 popMsg.show("CPU usage warning",p.name()+" spends a lot of cpu resource") elif getCPUusage(p) > 50: warnCountList[pid][0] -= 1 if warnCountList[pid][1] == 0 and getMemUsage(p) > 1024 * 1024 * 1024 and pid != 4: warnCountList[pid][1] = 20 popMsg.show("Memory usage warning",p.name()+" spends a lot of memory resource") elif getMemUsage(p) > 1024 * 1024 * 1024: warnCountList[pid][1] -= 1 except KeyError: warnCountList[pid] = [0, 0] except pu.NoSuchProcess: continue self.memUsage.setValue(memUsage()) self.cpuUsage.setValue(cpuUsage()) diskUsageList = diskIOcountEachProcess() readTotal = 0 writeTotal = 0 for diskUsage in diskUsageList: readTotal += diskUsageList[diskUsage][0] writeTotal += diskUsageList[diskUsage][1] self.diskUsage.setText( "Read: "+humanize(readTotal)+"/s"+" | Write: "+humanize(writeTotal)+"/s" ) networkUsageList = networkUsageEachDevice() sentTotal = 0 recvTotal = 0 for networkUsage in networkUsageList: sentTotal += networkUsage[1] recvTotal += networkUsage[2] self.netUsage.setText("Sent: "+humanize(sentTotal*2)+"/s | Recv: "+humanize(recvTotal*2)+"/s") def popExtend(self): self.w = DetailWindow() self.w.show() def netPopExtend(self): self.nw = GraphicsWindow() self.nw.setWindowTitle(_translate("MainWindow", "SMS", None)) self.nw.setWindowIcon(QtGui.QIcon(icon_path)) # Plot in chunks, adding one new plot curve for every 100 samples self.chunkSize = 10 # Remove chunks after we have 10 self.maxChunks = 20 self.startTime = pyqtgraph.ptime.time() self.nw.nextRow() self.p5 = self.nw.addPlot(colspan=2) self.p5.setLabel('bottom', 'Time', 's') self.p5.setXRange(-10, 0) self.p5.setYRange(0,1000) self.p5.setMouseEnabled(x=False,y=False) self.p5.setMenuEnabled(False) self.p5.showGrid(x=True,y=True,alpha=0.5) self.curves = [[],[]] self.data5 = np.empty((self.chunkSize+1,3)) self.ptr5 = 0 self.ntimer = QtCore.QTimer() self.ntimer.timeout.connect(self.updateNetGraph) self.ntimer.start(500) def updateNetGraph(self): now = pyqtgraph.ptime.time() global networkUsageList netUsage = networkUsageList for c in self.curves[0]: c.setPos(-(now-self.startTime), 0) for c in self.curves[1]: c.setPos(-(now-self.startTime), 0) i = self.ptr5 % self.chunkSize if i == 0: curve1 = self.p5.plot(pen=(0,2)) self.curves[0].append(curve1) curve2 = self.p5.plot(pen=(1,2)) self.curves[1].append(curve2) last = self.data5[-1] self.data5 = np.empty((self.chunkSize+1,3)) self.data5[0] = last while len(self.curves[0]) > self.maxChunks: c = self.curves[0].pop(0) self.p5.removeItem(c) d = self.curves[0].pop(0) self.p5.removeItem(d) else: curve1 = self.curves[0][-1] curve2 = self.curves[1][-1] self.data5[i+1,0] = now - self.startTime sentTotal = 0 recvTotal = 0 for networkUsage in networkUsageList: sentTotal += networkUsage[1] recvTotal += networkUsage[2] self.data5[i+1,1] = sentTotal*2/1024 self.data5[i+1,2] = recvTotal*2/1024 curve1.setData(x=self.data5[:i+2, 0], y=self.data5[:i+2, 1]) curve2.setData(x=self.data5[:i+2, 0], y=self.data5[:i+2, 2]) self.ptr5 += 1
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Apr 27 07:00:22 2020 @author: inox """ from pyaudio import paInt16, PyAudio from struct import unpack from pyqtgraph import GraphicsWindow, setConfigOptions, LegendItem, QtCore, QtGui from numpy import fft, arange, split, argmax, floor from scipy.signal import find_peaks as fpeaks #Inicialización de ventana gráfica con los cuadros de visualización y el dicionario de trazas a graficar win = GraphicsWindow(title="Analizador de Espectro") win.setWindowTitle("Analizador de Espectro") setConfigOptions(antialias=True, background='k') scope = win.addPlot(title="Waveform", row=1, col=1) span = win.addPlot(title="FFT", row=2, col=1) traces = dict() legend = LegendItem() #Actualización (o inicialización) de las trazas de los cuadros de visualización def set_plotdata(name, data_x, data_y): if name in traces: traces[name].setData(data_x, data_y) else: if name == 'waveform': traces[name] = scope.plot(pen='w', width=4) scope.setYRange(-2**(bits - 1), 2**(bits - 1), padding=0)