我正在处理raspi3B +和python 2.7。
我正在使用AD8232心率传感器和MCP3008模数转换器。
我正在绘制来自MCP3008的过滤后的传感器数据。
我想确定R个峰。因此,我正在使用scipy.signal.find_peaks模块。
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html
但是有一个问题,因为在每个循环后所有峰都在绘制。我只想显示一个周期中的峰,然后将新峰更新至下一个周期。
import matplotlib.pyplot as plt
import numpy as np
import spidev, time
import RPi.GPIO as GPIO
import numpy as np
from scipy import signal
spi = spidev.SpiDev()
spi.open(0,0)
b, a = signal.butter(2, 0.2,btype='lowpass',analog=False)
fig = plt.figure('ECG PLOTTING')
ax = fig.add_subplot(111)
x=np.arange(500)
y = np.zeros((500,))
li, = ax.plot(x,y)
plt.ylim([0,0.1])
plt.title('Real-Time ECG Plotting')
plt.xlabel('Sample')
plt.ylabel('Voltage')
plt.show(block=False)
# read adc function
def analog_read(channel):
spi.max_speed_hz=1350000
r = spi.xfer2([1,(8+channel)<<4,0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
def St():
ylist = []
for _ in range(500):
reading = analog_read(0)
voltage = reading * 3.3 / 1024
time.sleep(0.005)
ylist.append(voltage)
return np.array(ylist)
while True:
y=St()
zi = signal.lfilter_zi(b, a)
z, _ = signal.lfilter(b, a, y, zi=zi*y[0])
out = signal.filtfilt(b, a, z) #filtered data
peaks,_=signal.find_peaks(out,distance=120,height=0.05) #find peaks
li.set_ydata(out) #show filtered ECG plotting
ax.plot(peaks,out[peaks],"x") #adding the peaks point
fig.canvas.draw()