ECG Data Analysis on a real-time signal in Python

2020-07-27 05:29发布

问题:

I am using Python to produce an electrocardiogram (ECG) from signals obtained by an Arduino. I want to perform some analysis on it, what type of analysis I do not know yet that is something I have yet to decide. However my question is, is it possible to do this analysis on a real time flow of data coming through the serial port, or is it easier/better to save the data first to suppose a text file and then perform analysis on it. Right now I can't wrap my head round how to do it. An extra note: I would at the very minimum like to detect the peaks of the signal (R wave) and the R-R interval (so I can measure the beats per minute).

Here is what I have on Python thus far:

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
import matplotlib.figure as mfig
import PyQt4.QtGui as gui, PyQt4.QtCore as core
import collections
import time
import random

import serial
ser = serial.Serial('/dev/tty.usbmodem1411', 57600)

start_byte = 'S'
end_byte = 'F'


refreshMillis = 50
N = 200
xs = collections.deque(maxlen=N)
ys = collections.deque(maxlen=N)

app = gui.QApplication([])

fig = mfig.Figure()
canvas = FigureCanvasQTAgg(fig)

ax = fig.add_subplot(111)
ax.set_ylim([0,5])
line2D, = ax.plot(xs,ys)
canvas.show()

def process_line():

    line = ser.readline()
    data = map(float,line.split(" "))
    xs.append(data[0])
    ys.append(data[1])
    line2D.set_data(xs,ys)
    print data
    xmin, xmax = min(xs),max(xs)
    if xmin == xmax:
        ax.set_xlim([xmin,xmin+1])
    else:
        ax.set_xlim([xmin,xmax])
    canvas.draw()

timer = core.QTimer()
timer.timeout.connect(process_line)
timer.start(refreshMillis)

app.exec_()

ser.flush()
ser.close()

回答1:

Sure it's possible. It's easier to save it at first and then analyse the data later, but it's also no problem to do this on a definded chunk of data. The real question is, what kind of analysis do you want to do! Do you need all the data? or x seconds of data? How much data do you need to find reliable R and R-R values? In your case, I would first dump some data away and play with it to see what you need. You can then later build a version which does this on-the-fly with the discovered parameters of your algorithms.