def _Peak_detection(filt_sig_sample, fs, n): if n == 1: NN_index_sig = np.array(signal.argrelextrema(filt_sig_sample, np.greater)).reshape(1,-1)[0] f, ppg_den = signal.periodogram(filt_sig_sample, fs) min_f = np.where(f >= 0.6)[0][0] max_f = np.where(f >= 3.0)[0][0] ppgHRfreq = ppg_den[min_f:max_f] HRfreq = f[min_f:max_f] HRf = HRfreq[np.argmax(ppgHRfreq)] boundary = 0.5 if HRf - boundary > 0.6: HRfmin = HRf - boundary else: HRfmin = 0.6 if HRf + boundary < 3.0: HRfmax = HRf + boundary else: HRfmax = 3.0 filtered = _ButterFilt(filt_sig_sample,fs,np.array([HRfmin,HRfmax]),5,'bandpass') NN_index_filtered = np.array(signal.argrelextrema(filtered, np.greater)).reshape(1,-1)[0] rpeak = np.array([]).astype(int) for i in NN_index_filtered: rpeak = np.append(rpeak,NN_index_sig[np.abs(i - NN_index_sig).argmin()]) rpeak = np.unique(rpeak) elif n==2: rol_mean = rolling_mean(filt_sig_sample, windowsize = 0.75, sample_rate = fs) working_data = hp.peakdetection.fit_peaks(filt_sig_sample, rol_mean, fs) rpeak = working_data['peaklist'] elif n==3: rpeak = argrelmax(np.array(filt_sig_sample))[0] return(rpeak)
def process_data(x, fps=30): print(x.shape) x = preprocessing.scale(x) x = bandpass_butter(x, cut_low=1, cut_high=2, rate=fps, order=2) rol_mean = rolling_mean(x, windowsize=5, sample_rate=fps) wd = heartpy.peakdetection.detect_peaks(x, rol_mean, ma_perc=20, sample_rate=fps) peaks = wd['peaklist'] rri = np.diff(peaks) rri = rri * 1 / fps * 1000 hr = 6e4 / rri # df = pd.DataFrame() # df['rri'] = [rri] # res = apply_model_df(df) # print(f"res: {res}") # m, wd = heartpy.analysis.calc_breathing(wd['RR_list'], x, sample_rate=fps) # print(f"breath rate: {round(m['breathingrate'], 3)}") fig, axs = plt.subplots(3, 1, figsize=(17, 9)) axs = axs.ravel() axs[0].plot(x, '-b', label='x') axs[0].plot(peaks, x[peaks], 'r.', label='peaks') axs[1].plot(rri, '-r.', label='rri') axs[2].plot(hr, '-r.', label='hr') for ax in axs: ax.legend() plt.show()
def get_all_features_hrva(data_sample, sample_rate=100, rpeak_method=0): """ :param data_sample: :param sample_rate: :param rpeak_method: :return: """ if rpeak_method in [1, 2, 3, 4]: detector = PeakDetector() peak_list = detector.ppg_detector(data_sample, rpeak_method)[0] else: rol_mean = rolling_mean(data_sample, windowsize=0.75, sample_rate=100.0) peaks_wd = detect_peaks(data_sample, rol_mean, ma_perc=20, sample_rate=100.0) peak_list = peaks_wd["peaklist"] rr_list = np.diff(peak_list) * (1000 / sample_rate) #1000 milisecond nn_list = get_nn_intervals(rr_list) nn_list_non_na = np.copy(nn_list) nn_list_non_na[np.where(np.isnan(nn_list_non_na))[0]] = -1 time_domain_features = get_time_domain_features(rr_list) frequency_domain_features = get_frequency_domain_features(rr_list) geometrical_features = get_geometrical_features(rr_list) csi_cvi_features = get_csi_cvi_features(rr_list) return time_domain_features,frequency_domain_features,\ geometrical_features,csi_cvi_features
def run_algo(algorithm: str, sig: numpy.ndarray, freq_sampling: int) -> List[int]: """ run a qrs detector on a signal :param algorithm: name of the qrs detector to use :type algorithm: str :param sig: values of the sampled signal to study :type sig: ndarray :param freq_sampling: value of sampling frequency of the signal :type freq_sampling: int :return: localisations of qrs detections :rtype: list(int) """ detectors = Detectors(freq_sampling) if algorithm == 'Pan-Tompkins-ecg-detector': qrs_detections = detectors.pan_tompkins_detector(sig) elif algorithm == 'Hamilton-ecg-detector': qrs_detections = detectors.hamilton_detector(sig) elif algorithm == 'Christov-ecg-detector': qrs_detections = detectors.christov_detector(sig) elif algorithm == 'Engelse-Zeelenberg-ecg-detector': qrs_detections = detectors.engzee_detector(sig) elif algorithm == 'SWT-ecg-detector': qrs_detections = detectors.swt_detector(sig) elif algorithm == 'Matched-filter-ecg-detector' and freq_sampling == 360: qrs_detections = detectors.matched_filter_detector( sig, 'templates/template_360hz.csv') elif algorithm == 'Matched-filter-ecg-detector' and freq_sampling == 250: qrs_detections = detectors.matched_filter_detector( sig, 'templates/template_250hz.csv') elif algorithm == 'Two-average-ecg-detector': qrs_detections = detectors.two_average_detector(sig) elif algorithm == 'Hamilton-biosppy': qrs_detections = bsp_ecg.ecg(signal=sig, sampling_rate=freq_sampling, show=False)[2] elif algorithm == 'Christov-biosppy': order = int(0.3 * freq_sampling) filtered, _, _ = bsp_tools.filter_signal(signal=sig, ftype='FIR', band='bandpass', order=order, frequency=[3, 45], sampling_rate=freq_sampling) rpeaks, = bsp_ecg.christov_segmenter(signal=filtered, sampling_rate=freq_sampling) rpeaks, = bsp_ecg.correct_rpeaks(signal=filtered, rpeaks=rpeaks, sampling_rate=freq_sampling, tol=0.05) _, qrs_detections = bsp_ecg.extract_heartbeats( signal=filtered, rpeaks=rpeaks, sampling_rate=freq_sampling, before=0.2, after=0.4) elif algorithm == 'Engelse-Zeelenberg-biosppy': order = int(0.3 * freq_sampling) filtered, _, _ = bsp_tools.filter_signal(signal=sig, ftype='FIR', band='bandpass', order=order, frequency=[3, 45], sampling_rate=freq_sampling) rpeaks, = bsp_ecg.engzee_segmenter(signal=filtered, sampling_rate=freq_sampling) rpeaks, = bsp_ecg.correct_rpeaks(signal=filtered, rpeaks=rpeaks, sampling_rate=freq_sampling, tol=0.05) _, qrs_detections = bsp_ecg.extract_heartbeats( signal=filtered, rpeaks=rpeaks, sampling_rate=freq_sampling, before=0.2, after=0.4) elif algorithm == 'Gamboa-biosppy': order = int(0.3 * freq_sampling) filtered, _, _ = bsp_tools.filter_signal(signal=sig, ftype='FIR', band='bandpass', order=order, frequency=[3, 45], sampling_rate=freq_sampling) rpeaks, = bsp_ecg.gamboa_segmenter(signal=filtered, sampling_rate=freq_sampling) rpeaks, = bsp_ecg.correct_rpeaks(signal=filtered, rpeaks=rpeaks, sampling_rate=freq_sampling, tol=0.05) _, qrs_detections = bsp_ecg.extract_heartbeats( signal=filtered, rpeaks=rpeaks, sampling_rate=freq_sampling, before=0.2, after=0.4) elif algorithm == 'mne-ecg': qrs_detections = mne_ecg.qrs_detector(freq_sampling, sig) elif algorithm == 'heartpy': rol_mean = rolling_mean(sig, windowsize=0.75, sample_rate=100.0) qrs_detections = hp_pkdetection.detect_peaks( sig, rol_mean, ma_perc=20, sample_rate=100.0)['peaklist'] elif algorithm == 'gqrs-wfdb': qrs_detections = processing.qrs.gqrs_detect(sig=sig, fs=freq_sampling) elif algorithm == 'xqrs-wfdb': qrs_detections = processing.xqrs_detect(sig=sig, fs=freq_sampling) else: raise ValueError( f'Sorry... unknown algorithm. Please check the list {algorithms_list}' ) cast_qrs_detections = [int(element) for element in qrs_detections] return cast_qrs_detections