python 均线斜率怎么计算_Python分析离散心率信号(上)

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

import math

dataset = pd.read_csv("data.csv")

#Calculate moving average with 0.75s

in both directions, then append do dataset

hrw = 0.75 #One-sided

window size, as proportion of the sampling frequency

fs = 100 #The example

dataset was recorded at 100Hz

mov_avg = dataset['hart'].rolling(int(hrw*fs)).mean() #Calculate

moving average

#Impute where moving average function

returns NaN, which is the beginning of the signal where x hrw

avg_hr = (np.mean(dataset.hart))

mov_avg = [avg_hr if math.isnan(x) else x for x in mov_avg]

mov_avg = [x*1.2 for x in mov_avg] #For now we

raise the average by 20% to prevent the secondary heart contraction from

interfering, in part 2 we will do this dynamically

dataset['hart_rollingmean'] = mov_avg #Append the

moving average to the dataframe

#Mark regions of interest

window = []

peaklist = []

listpos = 0 #We use a

counter to move over the different data columns

for datapoint in dataset.hart:

rollingmean = dataset.hart_rollingmean[listpos] #Get local

mean

if (datapoint < rollingmean) and (len(window) < 1): #If no

detectable R-complex activity -> do nothing

listpos += 1

elif (datapoint > rollingmean): #If signal

comes above local mean, mark ROI

window.append(datapoint)

listpos += 1

else: #If signal

drops below local mean -> determine highest point

maximum = max(window)

beatposition = listpos - len(window) + (window.index(max(window))) #Notate the

position of the point on the X-axis

peaklist.append(beatposition) #Add detected

peak to list

window = [] #Clear marked

ROI

listpos += 1

ybeat = [dataset.hart[x] for x in peaklist] #Get the

y-value of all peaks for plotting purposes

plt.title("Detected

peaks in signal")

plt.xlim(0,2500)

plt.plot(dataset.hart, alpha=0.5, color='blue') #Plot

semi-transparent HR

plt.plot(mov_avg, color ='green') #Plot moving

average

plt.scatter(peaklist, ybeat, color='red') #Plot detected

peaks

plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值