我正在使用AD8232心率传感器和MCP3008模数转换器。
我正在使用matplotlib lib绘制传感器数据,但是太吵了。
我必须过滤ECG数据,但我不知道该如何申请。
我可以实时过滤吗?还是必须在将绘图数据过滤到新的txt文件之后将数据保存到txt文件中,然后应用过滤?
我没有将数据保存到txt或csv文件中。我该如何过滤?
import matplotlib.pyplot as plt
import numpy as np
import spidev, time
import RPi.GPIO as GPIO
# read adc function
def analog_read(channel):
r = spi.xfer2([1,(8+channel)<<4,0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
spi = spidev.SpiDev()
spi.open(0,0)
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(500)
y=[0 for iii range(500)]
li, = ax.plot(x, y)
plt.ylim([0,3.3])
fig.canvas.draw()
plt.show(block=False)
# convert adc function
def St():
for ii in range(1,501):
reading = analog_read(0)
voltage = reading * 3.3 / 4096
time.sleep(0.005)
vv=("%3.3f" % (voltage))
x[ii-1]=ii-1
y[ii-1]=vv
while True:
St()
li.set_ydata(y)
fig.canvas.draw()
答案 0 :(得分:0)
您的脚本对我不起作用,所以我只添加您需要理解的代码。
您的脚本不执行实时操作,而是批量处理500个值的数据。我更改了脚本以实时执行图形更新。
我使用了双端队列。我从双端队列创建了一个大小为500的缓冲区,因此,如果您继续从左开始添加缓冲区,它将自动弹出旧值,因此如果您继续绘制它,它将看起来是实时的。
在获取新值之后以及在每个周期绘制数据之前,您有时间执行过滤-例如使用粗粒度方法(您需要对其进行编程),或者只是根据以前的几个值进行平均。 / p>
您修改的代码
import matplotlib.pyplot as plt
import numpy as np
import spidev, time
import RPi.GPIO as GPIO
from collections import deque
# read adc function
def analog_read(channel):
r = spi.xfer2([1,(8+channel)<<4,0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
spi = spidev.SpiDev()
spi.open(0,0)
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(500)
x = x.tolist()
y=[0 for iii in range(500)]
li, = ax.plot(x, y)
plt.ylim([0,3.3])
fig.canvas.draw()
plt.show(block=False)
# convert adc function
voltage_series = deque([],maxlen=500)
x_axis = x.tolist()
#fill it
for i in range(500):
voltage_series.append(0)
# Y-axis = voltage_series
# X-axis = x_axis
counter = 0
while True: # grapg updates every read
time.sleep(0.05)
reading = analog_read(0)
voltage = reading * 3.3 / 4096
vv=("%3.3f" % (voltage))
voltage_series.appendleft(voltage)
if counter%500==0:
### Time to realtime filtering
voltage_for_filtering = list(voltage_series)
# transfor to numpy for example np.array(voltage_for_filtering)
# or do standard method for smoothing noise - coarse graining
# voltage_for_filtering = coarse_grain(voltage_for_filtering) -->> example
li.set_ydata(voltage_for_filtering)
fig.canvas.draw()