Initial Commit.
This commit is contained in:
71
func/utils/detect_Rpeak.py
Normal file
71
func/utils/detect_Rpeak.py
Normal file
@ -0,0 +1,71 @@
|
||||
from ecgdetectors import Detectors
|
||||
from numpy import quantile, delete, array, argmax, full, nan
|
||||
|
||||
from func.BCGDataset import BCG_Operation
|
||||
|
||||
def refinement( data, peak):
|
||||
if len(data) == 0 or len(peak) <=2 : return None
|
||||
firstPeak = peak[0]
|
||||
lastPeak = peak[-1]
|
||||
meanPeak = quantile( data[peak[1:-1]], 0.2 )
|
||||
if data[firstPeak] < meanPeak * 0.6 :
|
||||
peak = delete(peak, 0)
|
||||
if data[lastPeak] < meanPeak * 0.6 :
|
||||
peak = delete(peak, -1)
|
||||
return array(peak)
|
||||
|
||||
def find_TPeak(data,peaks,th=50):
|
||||
"""
|
||||
找出真实的J峰或R峰
|
||||
:param data: BCG或ECG数据
|
||||
:param peaks: 初步峰值(从label中导出的location_R)
|
||||
:param th: 范围阈值
|
||||
:return: 真实峰值
|
||||
"""
|
||||
return_peak = []
|
||||
for peak in peaks:
|
||||
if peak>len(data):continue
|
||||
min_win,max_win = max(0,int(peak-th)),min(len(data),int(peak+th))
|
||||
return_peak.append(argmax(data[min_win:max_win])+min_win)
|
||||
return array(return_peak)
|
||||
|
||||
def Rpeak_Detection(raw_ecg,fs,low_cut,high_cut,th1,detector_method):
|
||||
detectors = Detectors(sampling_frequency=fs)
|
||||
method_dic = {'pt': detectors.pan_tompkins_detector,
|
||||
'ta': detectors.two_average_detector,
|
||||
"Engzee": detectors.engzee_detector,
|
||||
"Wt": detectors.swt_detector,
|
||||
"Christov": detectors.christov_detector,
|
||||
"Hamilton": detectors.hamilton_detector
|
||||
}
|
||||
detectormethods = method_dic[detector_method]
|
||||
|
||||
# raw_ecg = raw_ecg[200*sample_rate:]
|
||||
preprocessing = BCG_Operation(sample_rate=fs) # 对ECG做了降采样处理
|
||||
raw_ecg = preprocessing.Butterworth(raw_ecg, "bandpass", low_cut=low_cut, high_cut=high_cut, order=3) * 4
|
||||
#######################限制幅值处理############################################
|
||||
# for i in range(len(raw_ecg)):
|
||||
# if raw_ecg[i] > 300 or raw_ecg[i] < -300:
|
||||
# raw_ecg[i] = 0
|
||||
##############################################################################
|
||||
|
||||
R_peak = array(detectormethods(raw_ecg)) - 100
|
||||
# R_peak = np.array(detectors.pan_tompkins_detector(raw_ecg))-100
|
||||
|
||||
R_peak = find_TPeak(raw_ecg, R_peak, th=int(th1 * fs / 1000))
|
||||
R_peak = refinement(raw_ecg, R_peak)
|
||||
|
||||
RR_Interval = full(len(R_peak) - 1, nan)
|
||||
|
||||
for i in range(len(R_peak) - 1):
|
||||
RR_Interval[i] = R_peak[i + 1] - R_peak[i]
|
||||
|
||||
RRIV = full(len(RR_Interval) - 1, nan)
|
||||
for i in range(len(RR_Interval) - 1):
|
||||
RRIV[i] = RR_Interval[i + 1] - RR_Interval[i]
|
||||
|
||||
Interval = full(len(raw_ecg), nan)
|
||||
for i in range(len(R_peak) - 1):
|
||||
Interval[R_peak[i]: R_peak[i + 1]] = R_peak[i + 1] - R_peak[i]
|
||||
|
||||
return R_peak, Interval, RRIV
|
||||
Reference in New Issue
Block a user