简体   繁体   中英

Real-time ECG filtering

在此处输入图片说明 I am dealing with raspi3B+ and python 2.7.

I am using AD8232 heart rate sensor and MCP3008 analog-digital converter.

I am plotting the sensor data with matplotlib lib but it is too noisy.

I have to filter to ECG data but I did not know how to I apply.

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?

I did not save the data into the txt or csv file. 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. 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM