def test_ppg_findpeaks(): sampling_rate = 500 ppg = nk.ppg_simulate( duration=30, sampling_rate=sampling_rate, heart_rate=60, frequency_modulation=0.01, ibi_randomness=0.1, drift=1, motion_amplitude=0.5, powerline_amplitude=0.1, burst_amplitude=1, burst_number=5, random_state=42, show=True, ) ppg_cleaned_elgendi = nk.ppg_clean(ppg, sampling_rate=sampling_rate, method="elgendi") info_elgendi = nk.ppg_findpeaks(ppg_cleaned_elgendi, sampling_rate=sampling_rate, show=True) peaks = info_elgendi["PPG_Peaks"] assert peaks.size == 29 assert peaks.sum() == 219763
def find_feature_points(signal, sampling_rate=50, threshold=0.5): """ 寻找峰值以及槽点 :param signal: 初始数据 已经去噪 :param sampling_rate: 采样率 :param threshold: 过近的点可以认为是异常值 给予一个threshold去除异常识别的峰值点 :return: """ origin_signal = signal.copy() peaks = nk.ppg_findpeaks(signal, sampling_rate=sampling_rate) # 先找到峰值点 locmax, props = spsg.find_peaks(signal) notches = [] other_notches = [] # 两种方法找到的共同峰值点 all_peaks = np.intersect1d(locmax, peaks["PPG_Peaks"]) mean_T = np.mean(np.diff(all_peaks)) # 真正的峰值点 true_peaks = [all_peaks[0]] # 周期 T = [] for i in range(len(all_peaks) - 1): if all_peaks[i + 1] - all_peaks[i] < threshold * mean_T: continue true_peaks.append(all_peaks[i + 1]) for i in range(len(true_peaks) - 1): notches.append(true_peaks[i] + np.argmin(origin_signal[true_peaks[i]:true_peaks[i + 1]])) T.append(true_peaks[i + 1] - true_peaks[i]) for i in range(len(locmax) - 1): other_notches.append(locmax[i] + np.argmin(origin_signal[locmax[i]:locmax[i + 1]])) other_peaks = list(set(locmax) - set(true_peaks)) other_notches = list(set(other_notches) - set(notches)) other_peaks = [i for i in other_peaks if origin_signal[i] > 0] other_notches = [i for i in other_notches if origin_signal[i] > 0] return { "Peaks": true_peaks, "Other Peaks": other_peaks, "Other Notches": other_notches, "Notches": notches, "T": T, "Cleaned Pulses": origin_signal }
def neurokit_index(request): data = pd.read_csv( "/Users/siyuqian/Study/django-docker/712AF22B_Mar11_14-07-59.csv") # Generate 15 seconds of PPG signal (recorded at 250 samples / second) # ppg = nk.ppg_simulate(duration=15, sampling_rate=250, heart_rate=70) ppg = nk.ppg_process(data['PPG'], sampling_rate=50) # Clear the noise ppg_clean = nk.ppg_clean(ppg) # Peaks peaks = nk.ppg_findpeaks(ppg_clean, sampling_rate=100) # Compute HRV indices hrv_indices = nk.hrv(peaks, sampling_rate=100, show=True) result = hrv_indices.to_json() parsed = json.loads(result) context = {'response': json.dumps(parsed)} return render(request, 'neurokit/neurokit_index.html', context)