Advertisement

python处理心电数据(ECG)-----heartpy库的应用

阅读量:

该文本介绍了如何使用Python库heartpy进行心率分析。主要步骤包括安装库、读取心率数据、设置采样频率和频谱方法,并通过process函数分析数据。代码中设置了freq_method='fft',并计算了多个心率相关指标,如BPM、SDNN、LF、HF和LF/HF比值。此外,文本还提到了波峰检测和结果可视化。

将原始数据+1,

数据概貌如下:

第三方软件包:https://python-heart-rate-analysis-toolkit.readthedocs.io/en/latest/quickstart.html

该库的官方文档已由Paulvangent于2016年3月15日发布于http://www.paulvangent.com/2016/03/15/analyzing-a-discrete-heart-rate-signal-using-python-part-1/

使用的第三方名叫:heartpy

安装该库:

复制代码
    python -m pip install heartpy

主要采用的函数process,其主要参数如下所示:

该函数接收多个参数进行数据处理,包括数据集、采样率、窗宽设置为0.75、报告时间开关、频率计算开关、频域方法设置为'welch'、插值裁剪开关、裁剪比例参数、插值阈值设定为1020、异常校正开关、最低心率设定为40、最高心率设定为180、逐段拒绝开关、高精度模式开关、高精度采样率设定为1000.0、需要计算的测量指标参数以及工作数据参数。

其中,freq_method:可选参数用于频谱提取的方法。该方法可选,包含以下几种:FFT(傅里叶分析)、periodogram(周期图)和Welch方法(Welch算法),其默认值为Welch方法。

代码如下:

复制代码
 #输出指标汇总

    
 #measures.keys===dict_keys(['bpm', 'ibi', 'sdnn', 'sdsd', 'rmssd', 'pnn20', 'pnn50', 
    
 #'hr_mad', 'breathingrate', 'frq', 'psd', 'lf', 'hf', 'lf/hf', 'interp_rr_function', 
    
 #'interp_rr_linspace'])
    
 #---------------------------------
    
 #working_data.keys===dict_keys(['hr', 'peaklist', 'ybeat', 'rolmean', 'RR_list', 'RR_diff', 
    
 #'RR_sqdiff', 'rrsd', 'best', 'peaklist_cor', 'removed_beats', 'removed_beats_y',
    
 #'binary_peaklist', 'RR_masklist', 'RR_list_cor', 'nn20', 'nn50'])
    
  
    
 import pandas as pd
    
 import matplotlib.pyplot as plt
    
 import numpy as np
    
 import math
    
 import heartpy as hp
    
  
    
 #读取文件数据
    
 hrdata = hp.get_data('C:/Users/xujinhua/Desktop/ecgtest/test2p.csv')
    
  
    
 #设置采集的频率,本项目是2000Hz
    
 fs=2000 
    
 #calc_freq=True此处要设置为 True
    
 working_data,measures = hp.process(hrdata,fs,calc_freq=True,freq_method='fft')
    
  
    
 #绘图,看查找到的峰值
    
 #hp.plotter(working_data, measures)
    
  
    
  
    
 print(measures.keys())
    
 print(working_data.keys())
    
 #The module dict now contains all the variables computed over our signal:
    
 print('bpm=',measures['bpm']) #returns BPM value
    
 print('SDNN=',measures['sdnn']) #输出SDNN
    
 print('LF=',measures['lf']) #输出LF
    
 print('HF=',measures['hf']) #输出HF
    
 print('LF/HF=',measures['lf/hf']) #输出LF/HF

找寻的波峰:

输出指标:

全部评论 (0)

还没有任何评论哟~