[英]Real-time ECG filtering
I am dealing with raspi3B+ and python 2.7. 我正在处理raspi3B +和python 2.7。
I am using AD8232 heart rate sensor and MCP3008 analog-digital converter. 我正在使用AD8232心率传感器和MCP3008模数转换器。
I am plotting the sensor data with matplotlib lib but it is too noisy. 我正在用matplotlib lib绘制传感器数据,但是太吵了。
I have to filter to ECG data but I did not know how to I apply. 我必须过滤ECG数据,但我不知道该如何申请。
Can I do filter real-time or I have to save the data into the txt file and then apply filtering,after filtering plotting data into the new txt file? 我可以实时过滤还是将绘图数据过滤到新的txt文件中之后将数据保存到txt文件中,然后应用过滤?
I did not save the data into the txt or csv file. 我没有将数据保存到txt或csv文件中。 How can I filter? 我该如何过滤?
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()
Your script didnt work for me, so I will just add code, which you need to understand. 您的脚本对我没有用,所以我只添加您需要理解的代码。
Your script doesnt perform real-time, but rather process data in batch of size 500 values. 您的脚本不会实时执行,而是批量处理500个值的数据。 I changed the script to perform graph updating in realtime. 我更改了脚本以实时执行图形更新。
I used deque. 我用双端队列。 I created from deque a buffer of size 500, so if you keep appending it from left, it will automaticly pop old values so if you keep ploting it, it will look realtime. 我从双端队列创建了一个大小为500的缓冲区,因此,如果您一直从左开始追加缓冲区,它将自动弹出旧值,因此,如果您继续绘制它,它将看起来是实时的。
After you aqcuire new value and before you plot the data every cycle, you have time to perform filtering - for example with Coarse graining method (you need to program it) or you just keep doing average based on previous few values. 确定新值之后,在每个周期绘制数据之前,您有时间执行过滤-例如使用粗粒度方法(您需要对其进行编程),或者只是继续基于先前的几个值进行平均。
Your modified code 您修改的代码
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.