SHA256
69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
import queue
|
|
import threading
|
|
|
|
import numpy as np
|
|
import pyqtgraph as pg
|
|
from PyQt5 import QtWidgets
|
|
from pyqtgraph.Qt import QtCore
|
|
|
|
|
|
def graficar(cola_clean, cola_filtrada):
|
|
app = QtWidgets.QApplication([])
|
|
win = pg.GraphicsLayoutWidget(show=True, title="Onda senoidal")
|
|
|
|
# Un solo plot con ambas curvas
|
|
p1 = win.addPlot(title="Señal cruda y filtrada")
|
|
p1.addLegend() # leyenda automática
|
|
curve1 = p1.plot(pen="r", name="Cruda")
|
|
curve2 = p1.plot(pen="b", name="Filtrada")
|
|
|
|
def update():
|
|
# Actualizar señal cruda
|
|
if not cola_clean.empty():
|
|
datos = cola_clean.get()
|
|
muestras = np.arange(len(datos))
|
|
curve1.setData(muestras, datos)
|
|
|
|
# Actualizar señal filtrada
|
|
if not cola_filtrada.empty():
|
|
datos_f = cola_filtrada.get()
|
|
muestras_f = np.arange(len(datos_f))
|
|
curve2.setData(muestras_f, datos_f)
|
|
|
|
timer = QtCore.QTimer()
|
|
timer.timeout.connect(update)
|
|
timer.start(100) # refresco cada 100 ms
|
|
|
|
app.exec_()
|
|
|
|
|
|
class websocket_cliente(threading.Thread):
|
|
def __init__(self, cola_clean, cola_filtada, buffer_size):
|
|
super().__init__()
|
|
self.cola_clean = cola_clean
|
|
self.cola_filtrado = cola_filtada
|
|
self.buffer_size = buffer_size
|
|
self.buffer_filtrado = [0] * self.buffer_size
|
|
self.buffer_limpio = [0] * self.buffer_size
|
|
self.idx_despla = self.buffer_size / 100
|
|
|
|
def run(self):
|
|
while True:
|
|
self.buffer_limpio.append(self.cola_clean.get())
|
|
try:
|
|
self.buffer_filtrado = self.cola_filtrado.get_nowait()
|
|
except queue.Empty:
|
|
pass
|
|
|
|
|
|
# tiempo real
|
|
if __name__ == "__main__":
|
|
cola_clean = queue.Queue()
|
|
cola_filtrada = queue.Queue()
|
|
|
|
cliente = websocket_cliente(cola_clean, cola_filtrada, buffer_size=1024)
|
|
cliente.daemon = True
|
|
cliente.start()
|
|
|
|
graficar(cola_clean, cola_filtrada)
|