SunDou commited on
Commit
8da08ab
·
verified ·
1 Parent(s): 1576568

Upload data/dataset_Biosensors.csv with huggingface_hub

Browse files
Files changed (1) hide show
  1. data/dataset_Biosensors.csv +1614 -0
data/dataset_Biosensors.csv ADDED
@@ -0,0 +1,1614 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "keyword","repo_name","file_path","file_extension","file_size","line_count","content","language"
2
+ "Biosensors","Cassey2016/PPG_Peak_Detection","main.m",".m","3040","58","% =========================================================================
3
+ % Below functions are the implementation for the comparison methods in
4
+ % paper:
5
+ % Han, Dong, Syed K. Bashar, Jesús Lázaro, Fahimeh Mohagheghian,
6
+ % Andrew Peitzsch, Nishat Nishita, Eric Ding, Emily L. Dickson,
7
+ % Danielle DiMezza, Jessica Scott, Cody Whitcomb, Timothy P. Fitzgibbons,
8
+ % David D. McManus, and Ki H. Chon. 2022.
9
+ % ""A Real-Time PPG Peak Detection Method for Accurate Determination of
10
+ % Heart Rate during Sinus Rhythm and Cardiac Arrhythmia""
11
+ % Biosensors 12, no. 2: 82. https://doi.org/10.3390/bios12020082
12
+ %
13
+ % Please cite our paper if you used our implementation code. Thank you.
14
+ % Author: Dong Han (dong.han@uconn.edu), 01/31/2022.
15
+ % =========================================================================
16
+
17
+ % -------------------------------------------------------------------------
18
+ % Input:
19
+ % PPG_raw_buffer: should be 30-sec segment.
20
+ % fs_PPG_raw: the sampling frequency of the PPG_raw_buffer.
21
+ % -------------------------------------------------------------------------
22
+ %% Preparation of PPG signal:
23
+ addpath('.\func')
24
+ [PPG_buffer,fs_PPG] = my_func_prep_PPG_buffer(PPG_raw_buffer,fs_PPG_raw);
25
+
26
+ %% Method 1: implemented method 1-a
27
+ V_max_flag = true; % true == upper peak detection.
28
+ addpath('.\method_01_and_02');
29
+ output_upper_Shin_2009 = my_peak_compare_Shin_2009(PPG_buffer,fs_PPG,V_max_flag); % Implementation of Shin 2009 paper.
30
+
31
+ %% Method 2: implemented method 1-b
32
+ V_max_flag = false; % false == lower peak detection.
33
+ output_lower_Shin_2009 = my_peak_compare_Shin_2009(PPG_buffer,fs_PPG,V_max_flag); % Implementation of Shin 2009 paper.
34
+
35
+ %% Method 3 & 4: implemented method 2, it has two output peaks in ""output_Elgendi_1_2013""
36
+ delta = 0.5; % it was 0.1 as mentioned in the paper. But I think 0.5 works better (0.5 is in the billauer's website).
37
+ addpath('.\method_03_and_04');
38
+ [output_Elgendi_1_2013] = my_Elgendi_2013_method_I_peakdet(PPG_buffer, delta, fs_PPG);
39
+
40
+ %% Method 5: first derivative and adaptive thresholding method in Li et al. [4] and Elgendi's paper [3]
41
+ abpsig = resample(PPG_buffer,fs_abpsig,fs_PPG_buffer); % upsampling it to 125 Hz.
42
+ addpath('.\method_05');
43
+ [output_Elgendi_2_2013] = my_func_ppg_peakdet_method_05_Elgendi_2013_method_II(abpsig,fs_abpsig);
44
+
45
+ %% Method 6: implemented method 4
46
+ fs_abp = 250; % Hz.
47
+ abp = resample(PPG_buffer,fs_abp,fs_PPG); % upsampling it to 125 Hz.
48
+ addpath('.\method_06');
49
+ [output_Elgendi_3_2013] = my_Elgendi_2013_method_III_peakdet(abp,fs_abp);
50
+
51
+ %% Method 7: event-related moving averages with dynamic threshold method in Elgendi et al.'s paper [3]
52
+ addpath('.\method_07');
53
+ [output_Elgendi_4_2013] = my_func_ppg_peakdet_method_07_Elgendi_2013_method_IV(-PPG_raw_buffer,fs_PPG_raw);
54
+
55
+ %% Method 8 & 9: peak detection on Stationary Wavelet Transform of PPG signal
56
+ fs_swt = 125; % Hz.
57
+ PPG_swt = resample(PPG_buffer,fs_swt,fs_PPG); % upsampling it to 125 Hz.
58
+ addpath('.\method_08_and_09');
59
+ [output_Vadrevu_1_2019,output_Vadrevu_2_2019] = my_Vadrevu_2019_peakdet(PPG_swt,fs_swt);","MATLAB"
60
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_07/my_func_ppg_peakdet_method_07_Elgendi_2013_method_IV.m",".m","6505","156","function output_Elgendi_4_2013 = my_func_ppg_peakdet_method_07_Elgendi_2013_method_IV(raw_PPG,fs_PPG)
61
+ % =========================================================================
62
+ % This is my implementation of the method IV in this paper:
63
+ % Elgendi, Mohamed, et al.
64
+ % ""Systolic peak detection in acceleration photoplethysmograms measured from
65
+ % emergency responders in tropical conditions."" PLoS One 8.10 (2013): e76585.
66
+ %
67
+ % Implemented by Dong Han on 03/02/2020.
68
+ %
69
+ % Please cite our paper if you used this code:
70
+ % Han, Dong, Syed K. Bashar, Jesús Lázaro, Fahimeh Mohagheghian,
71
+ % Andrew Peitzsch, Nishat Nishita, Eric Ding, Emily L. Dickson,
72
+ % Danielle DiMezza, Jessica Scott, Cody Whitcomb, Timothy P. Fitzgibbons,
73
+ % David D. McManus, and Ki H. Chon. 2022.
74
+ % ""A Real-Time PPG Peak Detection Method for Accurate Determination of
75
+ % Heart Rate during Sinus Rhythm and Cardiac Arrhythmia""
76
+ % Biosensors 12, no. 2: 82. https://doi.org/10.3390/bios12020082
77
+ %
78
+ % Please cite our paper if you used our code. Thank you.
79
+ % =========================================================================
80
+ %% pre-processing - bandpass filtering
81
+ [b, a] = butter(2,[0.5 8]/(fs_PPG/2)); % 2nd order bandpass filter 0.5-8Hz;
82
+ filtered_PPG = filtfilt(b, a, raw_PPG); % zero-phase filter.
83
+ filtered_PPG = filtered_PPG ./ std(filtered_PPG); % normalizing data is very important for my peak detection.
84
+ filtered_PPG = filtered_PPG - mean(filtered_PPG);
85
+
86
+ debugging_plot_flag = false; % only for plotting debugging figures.
87
+
88
+ % clip the signal by keeping the signal above zero.
89
+ % I do not want to do this, so i will move all signal above zero.
90
+ S_n = filtered_PPG;
91
+ % ---- Not following the paper to clip signal but move all signal above zero:
92
+ % if min(S_n) < 0
93
+ % Z_n = S_n - min(S_n); % elevate signal above zero.
94
+ % else
95
+ % % the minimum of S_n is still above zero, so do nothing.
96
+ % Z_n = S_n;
97
+ % end
98
+ % ---- Following the paper: only keep the positive value:
99
+ Z_n = S_n;
100
+ Z_n(Z_n < 0) = 0;
101
+ %% pre-processing - squaring
102
+ y_n = (Z_n).^2; % element-wise power.
103
+ %% feature extraction - generating potential blocks using two moving averages
104
+ W_1 = round(0.111 * fs_PPG); % mentioned as the paper by brute-force search.
105
+ % first moving average:
106
+ % MA_peak = y_n; % for the beginning and ending signal, use the original signal.
107
+ % for nn = 1+round(W_1/2):length(raw_PPG)-round(W_1/2)
108
+ % temp_range = (nn-round(W_1/2)):(nn+round(W_1/2));
109
+ % MA_peak(nn) = sum(y_n(temp_range))/W_1;
110
+ % end
111
+ MA_peak = movmean(y_n,W_1);
112
+
113
+ % second moving average:
114
+ W_2 = round(0.667 * fs_PPG);
115
+ % MA_beat = y_n;
116
+ % for nn = 1+round(W_2/2):length(raw_PPG)-round(W_2/2)
117
+ % temp_range = (nn-round(W_2/2)):(nn+round(W_2/2));
118
+ % MA_beat(nn) = sum(y_n(temp_range))/W_2;
119
+ % end
120
+ MA_beat = movmean(y_n,W_2);
121
+ %% classification - thresholding
122
+ beta = 0.02; % from the paper, by brute force search.
123
+ z_bar = mean(y_n);
124
+ alpha = beta * z_bar; % offset level.
125
+ THR_1 = MA_beat + alpha;
126
+
127
+ Blocks_Of_Interest = zeros(size(MA_peak)); % I initial it as zero.
128
+ for nn = 1:length(MA_peak)
129
+ if MA_peak(nn) > THR_1(nn) % I think it is THR_1(nn).
130
+ Blocks_Of_Interest(nn) = 0.1;
131
+ else
132
+ % since I inital block of interest as zero, so I do not need to
133
+ % assign zero again.
134
+ end
135
+ end
136
+
137
+ % searh for onset and offset of each block.
138
+ count_blocks = 0;
139
+ block_onset = NaN(size(MA_peak));
140
+ block_offset = NaN(size(MA_peak));
141
+ if any(Blocks_Of_Interest > 0) % there is a block exist.
142
+ for nn = 1:length(MA_peak)
143
+ if nn == 1 && Blocks_Of_Interest(nn) > 0
144
+ % the first point is a block;
145
+ count_blocks = count_blocks + 1; % since the block start from zero, I have to add the counter first.
146
+ block_onset(count_blocks,1) = nn;
147
+ elseif nn == length(MA_peak) && Blocks_Of_Interest(nn) > 0
148
+ % end with a block:
149
+ % no need to add count_blocks;
150
+ block_offset(count_blocks,1) = nn;
151
+ else
152
+ if nn > 1
153
+ if Blocks_Of_Interest(nn-1) == 0 && Blocks_Of_Interest(nn) > 0 % a jump means a new block.
154
+ count_blocks = count_blocks + 1;
155
+ block_onset(count_blocks,1) = nn;
156
+ elseif Blocks_Of_Interest(nn-1) > 0 && Blocks_Of_Interest(nn) == 0 % a drop means the end of previous block.
157
+ block_offset(count_blocks,1) = nn;
158
+ end
159
+ end
160
+ end
161
+ end
162
+ else
163
+ % there is no block existed. Check why.
164
+ % keyboard;
165
+ HR_Elgendi_4_2013 = 0; % there is no peak location.
166
+ S_peaks = 1;
167
+ output_Elgendi_4_2013 = struct('filtered_PPG_Elgendi_4_2013',S_n,...
168
+ 'PPG_peak_loc_Elgendi_4_2013',S_peaks,...
169
+ 'HR_Elgendi_4_2013',HR_Elgendi_4_2013);
170
+ return
171
+ end
172
+
173
+ block_onset(isnan(block_onset)) = []; % remove extra elements.
174
+ block_offset(isnan(block_offset)) = []; % remove extra elements.
175
+ if size(block_onset,1) ~= size(block_offset,1)
176
+ % not same number of onset and offset, check here.
177
+ keyboard;
178
+ end
179
+
180
+ if size(block_onset,1) ~= count_blocks
181
+ keyboard;
182
+ end
183
+ S_peaks = NaN(count_blocks,1);
184
+ THR_2 = W_1;
185
+
186
+ for jj = 1:count_blocks
187
+ block_idx = [block_onset(jj,1):block_offset(jj,1)];
188
+ [~,I] = max(y_n(block_idx));
189
+ S_peaks(jj,1) = block_onset(jj,1) + I - 1;
190
+ end
191
+
192
+ if debugging_plot_flag
193
+ figure;
194
+ plot(filtered_PPG);hold on;
195
+ plot(S_peaks,y_n(S_peaks),'r.','markersize',10);
196
+ plot(y_n);
197
+ plot(MA_peak,'k:');
198
+ plot(MA_beat,'r--');
199
+ plot(THR_1,'g.-');
200
+ plot(Blocks_Of_Interest*max(y_n)*10,'color',[0.5,0.5,0.5]); % grey color. I want to make block more obvious.
201
+
202
+ legend('filtered PPG','peaks', 'squared PPG with clip to zero', 'MA peak', 'MA beat','THR 1', 'Blocks of Interest');
203
+ end
204
+
205
+ if isempty(S_peaks)
206
+ HR_Elgendi_4_2013 = 0; % there is no peak location.
207
+ S_peaks = 1;
208
+ else
209
+ HR_Elgendi_4_2013 = 60 * fs_PPG ./ diff(S_peaks); % calculate the HR.
210
+ end
211
+
212
+ output_Elgendi_4_2013 = struct('filtered_PPG_Elgendi_4_2013',S_n,...
213
+ 'PPG_peak_loc_Elgendi_4_2013',S_peaks,...
214
+ 'HR_Elgendi_4_2013',HR_Elgendi_4_2013);
215
+ end","MATLAB"
216
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_01_and_02/my_peak_compare_Shin_2009.m",".m","21523","388","function [output_Shin_2009] = my_peak_compare_Shin_2009(raw_PPG,fs_PPG,V_max_flag)
217
+ % =========================================================================
218
+ % This function is the implementation of this paper:
219
+ % Shin, Hang Sik, Chungkeun Lee, and Myoungho Lee.
220
+ % ""Adaptive threshold method for the peak detection of
221
+ % photoplethysmographic waveform.""
222
+ % Computers in biology and medicine
223
+ % 39.12 (2009): 1145-1152.
224
+ %
225
+ % Implemented by: Dong Han, on 02/10/2020.
226
+ %
227
+ % Please cite our paper if you used this code:
228
+ % Han, Dong, Syed K. Bashar, Jesús Lázaro, Fahimeh Mohagheghian,
229
+ % Andrew Peitzsch, Nishat Nishita, Eric Ding, Emily L. Dickson,
230
+ % Danielle DiMezza, Jessica Scott, Cody Whitcomb, Timothy P. Fitzgibbons,
231
+ % David D. McManus, and Ki H. Chon. 2022.
232
+ % ""A Real-Time PPG Peak Detection Method for Accurate Determination of
233
+ % Heart Rate during Sinus Rhythm and Cardiac Arrhythmia""
234
+ % Biosensors 12, no. 2: 82. https://doi.org/10.3390/bios12020082
235
+ %
236
+ % Please cite our paper if you used our code. Thank you.
237
+ % =========================================================================
238
+ debugging_plot_flag = false; % debugging plot. Can be false if don't want to plot anything.
239
+ %% Section 2.4 PPG frequency analysis and filtering.
240
+
241
+ % (1): high pass >= 0.5 Hz.
242
+ [b, a] = butter(6,[0.5 20]/(fs_PPG/2)); % bandpass filter 0.5-10Hz, changed from 0.5-20 to 0.5-9 Hz at 11/21/2018
243
+ raw_PPG = filtfilt(b, a, raw_PPG); % -> AC component
244
+ raw_PPG = raw_PPG ./ std(raw_PPG); % normalizing data is very important for my peak detection.
245
+ raw_PPG = raw_PPG - mean(raw_PPG);
246
+ %% Section 2.5 & 2.6 Peak detection algorithm & Adaptive threshold detection
247
+
248
+ % (1): bandpass filtering, no moving average filter or wavelet
249
+ % decomposition.
250
+ filtered_PPG = raw_PPG;
251
+ Fs = fs_PPG;
252
+
253
+ % % ===== interpolation to 1kHz of PPG: =====
254
+ % x = 1:length(filtered_PPG);
255
+ % v = filtered_PPG;
256
+ %
257
+ % upsample_Fs = 250;
258
+ % xq = 1:Fs/upsample_Fs:length(filtered_PPG);
259
+ % vq1 = interp1(x,v,xq);
260
+ %
261
+ % filtered_PPG = vq1;
262
+ % Fs = upsample_Fs; % upsampled to 1000 Hz.
263
+
264
+ % figure
265
+ % plot(x,v,'o',xq,vq1,':.');
266
+ % xlim([0 max(xq)]);
267
+ % title('(Default) Linear Interpolation');
268
+
269
+ % (2): V_max
270
+ % slope_k: k-th slope amplitude;
271
+ % s_r: slope changing rate (empirically: V_max = -0.6);
272
+ % V_n_1: previous peak amplitude;
273
+ % std_PPG: standard deviation of entire PPG signal;
274
+ % Fs: sampling frequency.
275
+
276
+ filtered_PPG = filtered_PPG(:);
277
+ slope_k = NaN(size(filtered_PPG)); % should be a column vector.
278
+ peak_loc = NaN(size(filtered_PPG)); % the array to store PPG peak index.
279
+ pk_idx = 1; % the counter of peaks.
280
+ %% Section 2.7: Peak Correction
281
+ refractory_period = 0.6 * Fs; % sec * sampling frequency, initial refractory period is 0.6 sec.
282
+
283
+ temp_win_left = round(0.15 * Fs); % sec * sampling frequency. This is the search region for local minima or maxima detection. chose 0.15 sec because 0.3 sec == 200 BPM.
284
+ temp_win_right = round(0.15 * Fs);
285
+
286
+
287
+ if V_max_flag % doing upper peak detection.
288
+ s_r = -0.6;
289
+ else
290
+ s_r = 0.6;%0.6; % not positive because my signal is zero mean.
291
+ % I need to make all bottom signal positive, so I am moving them up.
292
+ % move_filter_amp = min(filtered_PPG) * (-1);
293
+ % filtered_PPG = filtered_PPG + move_filter_amp + std(raw_PPG); % move the lowest value more than zero.
294
+ end
295
+
296
+
297
+ slope_meet_PPG_flag = false; % mark if the slope meet PPG.
298
+ slope_lower_PPG_flag = false; % mark if slope is lower than PPG, once PPG amp is lower than slope, mark it back.
299
+ prev_slope = NaN; % First, I want to test not decreasing with PPG amplitude.
300
+ if debugging_plot_flag % debugging plot
301
+ figure;
302
+ plot(filtered_PPG);
303
+ hold on;
304
+ end
305
+ for kk = 1:length(filtered_PPG)
306
+ % this is for debugging:
307
+ if kk == 2
308
+ my_stop = 1;
309
+ end
310
+ if kk == 1 % initial the slope value
311
+ if V_max_flag
312
+ slope_k(1,1) = 0.2 * max(filtered_PPG);
313
+ std_PPG = std(filtered_PPG);
314
+ else
315
+ slope_k(1,1) = 0.2 * min(filtered_PPG); % since my signal is zero mean, I start from the negative amp. % I added what I moved.
316
+ std_PPG = -std(filtered_PPG);
317
+ end
318
+ % std_PPG = std(filtered_PPG);
319
+ V_n_1 = slope_k(1,1);
320
+ else
321
+ if slope_meet_PPG_flag % slope has met PPG before.
322
+ slope_k(kk,1) = filtered_PPG(kk,1);
323
+ if V_max_flag % upper peak detection.
324
+ if kk < 2 % in the second point of signal
325
+ turn_point_flag = (slope_k(kk,1) < slope_k(kk-1,1)); % we met local maximum.
326
+ else
327
+ turn_point_flag = (slope_k(kk,1) < slope_k(kk-1,1)) & (slope_k(kk - 1,1) > slope_k(kk-2,1)); % we met local maximum.
328
+ end
329
+ else
330
+ if kk < 2 % in the second point of signal
331
+ turn_point_flag = (slope_k(kk,1) > slope_k(kk-1,1)); % we met local minimum.
332
+ else
333
+ turn_point_flag = (slope_k(kk,1) > slope_k(kk-1,1)) & (slope_k(kk - 1,1) < slope_k(kk-2,1)); % we met local minimum.
334
+ end
335
+ end
336
+
337
+ if turn_point_flag % there is a turning point.
338
+ if pk_idx > 1 % not the first peak
339
+ % check local maxima or minima:
340
+ if (kk - temp_win_left) < 1
341
+ temp_left = 1;
342
+ else
343
+ temp_left = kk - temp_win_left;
344
+ end
345
+
346
+ if (kk + temp_win_right) > length(filtered_PPG)
347
+ temp_right = length(filtered_PPG);
348
+ else
349
+ temp_right = kk + temp_win_right;
350
+ end
351
+ temp_win = temp_left:temp_right;
352
+ local_m_check = filtered_PPG(temp_win);
353
+ if V_max_flag
354
+ temp_m_idx = find(local_m_check > slope_k(kk - 1,1)); % check if there is another maximum than detected, remember use k-1.
355
+ else
356
+ temp_m_idx = find(local_m_check < slope_k(kk - 1,1)); % check if there is another minimum than detected
357
+ end
358
+
359
+ if isempty(temp_m_idx) % there is no more max or min than this peak
360
+ if (kk - peak_loc(pk_idx-1,1) > refractory_period) % it is not the first peak, and the second peak is outside refractory period. It should be kk, because I have not assign the peak to the array.
361
+ peak_loc(pk_idx,1) = kk-1;
362
+ V_n_1 = filtered_PPG(peak_loc(pk_idx-1,1),1);% previous peak amplitude %slope_k(kk-1,1);
363
+ % update refractory period:
364
+ refractory_period = 0.6 * (kk - peak_loc(pk_idx-1,1)); % current index minus peak location. update the refractory peroid before updating the peak counting.
365
+ pk_idx = pk_idx + 1;
366
+
367
+ % reset slope meet flag:
368
+ slope_meet_PPG_flag = false;
369
+ slope_k(kk,1) = slope_k(kk - 1,1) + s_r * ((V_n_1 + std_PPG) / Fs);
370
+
371
+ % ---- for checking lower slope -------
372
+ temp_slope_check = s_r * ((V_n_1 + std_PPG) / Fs);
373
+ if V_max_flag
374
+ if temp_slope_check > 0 % upper peaks should be decreasing with negative slope.
375
+ temp_slope_check = -s_r * ((V_n_1 + std_PPG) / Fs);%-temp_slope_check;
376
+ slope_k(kk,1) = slope_k(kk - 1,1) + temp_slope_check;
377
+ end
378
+ else
379
+ if temp_slope_check < 0 % upper peaks should be decreasing with negative slope.
380
+ temp_slope_check = -s_r * ((V_n_1 + std_PPG) / Fs);%-temp_slope_check;
381
+ slope_k(kk,1) = slope_k(kk - 1,1) + temp_slope_check;
382
+ end
383
+ end
384
+ % -------------------------------------------
385
+ if V_max_flag
386
+ temp_slope_below_PPG_flag = slope_k(kk,1) < filtered_PPG(kk,1); % upper peak detection, so slope below signal.
387
+ else
388
+ temp_slope_below_PPG_flag = slope_k(kk,1) > filtered_PPG(kk,1); % lower peak detection, so slope above signal.
389
+ end
390
+ if temp_slope_below_PPG_flag % if slope is below PPG signal, we will reset slope value to PPG amplitude.
391
+ slope_lower_PPG_flag = true; % slope is lower than PPG signal.
392
+ prev_slope = slope_k(kk,1); % store the slope value now.
393
+ slope_k(kk,1) = filtered_PPG(kk,1);
394
+ end
395
+ if debugging_plot_flag % debugging plot
396
+ plot(kk,slope_k(kk,1),'r.');
397
+ end
398
+
399
+ else
400
+ if (kk - peak_loc(pk_idx-1,1) <= refractory_period) % it is because of the refractory period that cause the no peak. It should be kk, because I have not assign the peak to the array.
401
+ slope_k(kk,1) = filtered_PPG(kk,1);% from the fig.3(c) in the paper, I see they are using the signal amplitude, not slope.
402
+ % no need to reset slope meet flag, waiting for
403
+ % next turning point.
404
+ if debugging_plot_flag % debugging plot
405
+ plot(kk,slope_k(kk,1),'r.');
406
+ end
407
+ end
408
+ end
409
+ else % there are more peaks higher then current kk peak.
410
+ if debugging_plot_flag % debugging plot
411
+ plot(kk,slope_k(kk,1),'r.');
412
+ end
413
+ end
414
+ else % the first peak, no need to check refractory period.
415
+ % check local maxima or minima:
416
+ if (kk - temp_win_left) < 1
417
+ temp_left = 1;
418
+ else
419
+ temp_left = kk - temp_win_left;
420
+ end
421
+
422
+ if (kk + temp_win_right) > length(filtered_PPG)
423
+ temp_right = length(filtered_PPG);
424
+ else
425
+ temp_right = kk + temp_win_right;
426
+ end
427
+ temp_win = temp_left:temp_right;
428
+ local_m_check = filtered_PPG(temp_win);
429
+ if V_max_flag
430
+ temp_m_idx = find(local_m_check > slope_k(kk-1,1)); % check if there is another maximum than detected, always detect previous peak.
431
+ else
432
+ temp_m_idx = find(local_m_check < slope_k(kk-1,1)); % check if there is another minimum than detected
433
+ end
434
+
435
+ if isempty(temp_m_idx)
436
+ peak_loc(pk_idx,1) = kk-1;
437
+ if pk_idx > 1
438
+ V_n_1 = filtered_PPG(peak_loc(pk_idx-1,1),1);
439
+ else
440
+ V_n_1 = slope_k(kk-1,1);% previous peak amplitude %slope_k(kk-1,1);
441
+ end
442
+ pk_idx = pk_idx + 1;
443
+
444
+ % reset slope meet flag:
445
+ slope_meet_PPG_flag = false;
446
+ slope_k(kk,1) = slope_k(kk - 1,1) + s_r * ((V_n_1 + std_PPG) / Fs);
447
+ % ---- for checking lower slope -------
448
+ temp_slope_check = s_r * ((V_n_1 + std_PPG) / Fs);
449
+ if V_max_flag
450
+ if temp_slope_check > 0 % upper peaks should be decreasing with negative slope.
451
+ temp_slope_check = -s_r * ((V_n_1 + std_PPG) / Fs);%-temp_slope_check;
452
+ slope_k(kk,1) = slope_k(kk - 1,1) + temp_slope_check;
453
+ end
454
+ else
455
+ if temp_slope_check < 0 % upper peaks should be decreasing with negative slope.
456
+ temp_slope_check = -s_r * ((V_n_1 + std_PPG) / Fs);%-temp_slope_check;
457
+ slope_k(kk,1) = slope_k(kk - 1,1) + temp_slope_check;
458
+ end
459
+ end
460
+ % -------------------------------------------
461
+ if V_max_flag
462
+ temp_slope_below_PPG_flag = slope_k(kk,1) < filtered_PPG(kk,1); % upper peak detection, so slope below signal.
463
+ else
464
+ temp_slope_below_PPG_flag = slope_k(kk,1) > filtered_PPG(kk,1); % lower peak detection, so slope above signal.
465
+ end
466
+
467
+ if temp_slope_below_PPG_flag % if slope is below PPG signal, we will reset slope value to PPG amplitude.
468
+ slope_k(kk,1) = filtered_PPG(kk,1);
469
+ end
470
+ if debugging_plot_flag % debugging plot
471
+ plot(kk,slope_k(kk,1),'r.');
472
+ end
473
+ else % there are more peaks higher then current kk peak.
474
+ if debugging_plot_flag % debugging plot
475
+ plot(kk,slope_k(kk,1),'r.');
476
+ end
477
+ end
478
+ % no need to calculate refractory period, because there is only one peak, at least two peaks can give this correctly:
479
+ end
480
+ else
481
+ % turning point did not meet, so keep decreasing or
482
+ % increasing the slope.
483
+ % slope_k(kk,1) = slope_k(kk - 1,1) + s_r * ((V_n_1 + std_PPG) / Fs);
484
+ if debugging_plot_flag % debugging plot
485
+ plot(kk,slope_k(kk,1),'r.');
486
+ end
487
+ end
488
+ else % slope has not met PPG before. Keep decresing or increasing according to 'V_max_flag'.
489
+ % if slope_lower_PPG_flag % if there is a slope lower than PPG before:
490
+ % slope_k(kk,1) = prev_slope;
491
+ % else
492
+ slope_k(kk,1) = slope_k(kk - 1,1) + s_r * ((V_n_1 + std_PPG) / Fs);
493
+ % ---- for checking lower slope -------
494
+ temp_slope_check = s_r * ((V_n_1 + std_PPG) / Fs);
495
+ if V_max_flag
496
+ if temp_slope_check > 0 % upper peaks should be decreasing with negative slope.
497
+ temp_slope_check = -s_r * ((V_n_1 + std_PPG) / Fs);%-temp_slope_check;
498
+ slope_k(kk,1) = slope_k(kk - 1,1) + temp_slope_check;
499
+ end
500
+ else
501
+ if temp_slope_check < 0 % upper peaks should be decreasing with negative slope.
502
+ temp_slope_check = -s_r * ((V_n_1 + std_PPG) / Fs);%-temp_slope_check;
503
+ slope_k(kk,1) = slope_k(kk - 1,1) + temp_slope_check;
504
+ end
505
+ end
506
+ % -------------------------------------------
507
+
508
+ % end
509
+ % if slope_k(kk,1) < filtered_PPG(kk,1) % if slope is below PPG signal, we will reset slope value to PPG amplitude.
510
+ % slope_lower_PPG_flag = true; % slope is lower than PPG signal.
511
+ % prev_slope = slope_k(kk,1); % store the slope value now.
512
+ % slope_k(kk,1) = filtered_PPG(kk,1);
513
+ % elseif slope_k(kk,1) > filtered_PPG(kk,1) % slope is higher.
514
+ % slope_lower_PPG_flag = false;
515
+ % prev_slope = NaN; % reset the prev value.
516
+
517
+ % end
518
+
519
+ % if slope_lower_PPG_flag ~= 1 % if slope was not lower than PPG.
520
+ % % -------------- Check if two lines will meet -----------------
521
+ % PPG_x1 = kk - 1;
522
+ % PPG_x2 = kk;
523
+ % PPG_y1 = filtered_PPG(kk-1,1);
524
+ % PPG_y2 = filtered_PPG(kk,1);
525
+ % slope = s_r;
526
+ % slope_y2 = slope_k(kk,1);
527
+ % slope_y1 = slope_k(kk-1,1);
528
+ % [meet_x] = my_slope_meet_PPG(PPG_x1,PPG_x2,PPG_y1,PPG_y2,slope,slope_y2,slope_y1);
529
+ %
530
+ % slope_meet_PPG_flag = (ceil(meet_x) == kk);%(slope_k(kk,1) - filtered_PPG(kk,1)) < 0.1; % 0.3 is a testing value. %slope_k(kk,1) == filtered_PPG(kk,1) % slope meets the PPG signal.
531
+ % end
532
+ if V_max_flag
533
+ slope_meet_PPG_flag = ((slope_k(kk,1) < filtered_PPG(kk,1)) & slope_k(kk - 1,1) > filtered_PPG(kk - 1,1));
534
+ else
535
+ slope_meet_PPG_flag = ((slope_k(kk,1) > filtered_PPG(kk,1)) & slope_k(kk - 1,1) < filtered_PPG(kk - 1,1)); % lower peak use inverse amplitude.
536
+ end
537
+ % -------------------------------------------------------------
538
+ % I found I cannot use equal, because the PPG sampling
539
+ % frequency is not so high.
540
+ if slope_meet_PPG_flag
541
+ slope_k(kk,1) = filtered_PPG(kk,1); % starts from the next index, slope == PPG amplitude.
542
+ else
543
+ % don't need to do anything.
544
+ if slope_lower_PPG_flag ~= 1 % there was no slope lower than PPG before.
545
+ if V_max_flag
546
+ slope_lower_PPG_flag = ((slope_k(kk,1) < filtered_PPG(kk,1)) & slope_k(kk - 1,1) == filtered_PPG(kk - 1,1)); % beginning part has same amplitude, but the ending part slope is lower.
547
+ else
548
+ slope_lower_PPG_flag = ((slope_k(kk,1) > filtered_PPG(kk,1)) & slope_k(kk - 1,1) == filtered_PPG(kk - 1,1)); % lower peak use inverse amplitude.
549
+ end
550
+ if slope_lower_PPG_flag
551
+ prev_slope = slope_k(kk,1); % store the slope value now.
552
+ slope_k(kk,1) = filtered_PPG(kk,1); % starts from the next index, slope == PPG amplitude.
553
+ end
554
+ else % there was slope lower than PPG before.
555
+
556
+ if V_max_flag
557
+ temp_PPG_below_slope_flag = filtered_PPG(kk,1) < prev_slope; % upper peak detection, so PPG below slope.
558
+ else
559
+ temp_PPG_below_slope_flag = filtered_PPG(kk,1) > prev_slope; % lower peak detection, so PPG above slope.
560
+ end
561
+
562
+ if temp_PPG_below_slope_flag % PPG is lower than prev slope.
563
+ slope_k(kk,1) = prev_slope; % stop tracking PPG amp.
564
+ slope_lower_PPG_flag = false; % reset the lower PPG flag.
565
+ prev_slope = NaN;
566
+ else
567
+ slope_k(kk,1) = filtered_PPG(kk,1); % keep tracking PPG amp.
568
+ end
569
+ end
570
+ end
571
+ if debugging_plot_flag % debugging plot
572
+ plot(kk,slope_k(kk,1),'r.');
573
+ end
574
+ end
575
+ end
576
+
577
+ end
578
+ % ================== IMPORTANT: clean up NaN value ========================
579
+ peak_loc(isnan(peak_loc)) = []; % remove empty peak loc.
580
+ if V_max_flag % doing upper peak detection.
581
+
582
+ else
583
+ % moving signal back.
584
+ % filtered_PPG = filtered_PPG - move_filter_amp - std(raw_PPG); % move the lowest value more than zero.
585
+ % slope_k = slope_k - move_filter_amp - std(raw_PPG); % move the slope as well.
586
+ end
587
+
588
+ if debugging_plot_flag % debugging plot
589
+ plot(peak_loc,filtered_PPG(peak_loc),'ko');
590
+ end
591
+
592
+ if isempty(peak_loc)
593
+ HR_Shin_2009 = 0; % there is no peak location.
594
+ peak_loc = 1;
595
+ else
596
+ HR_Shin_2009 = 60 * Fs ./ diff(peak_loc); % calculate the HR.
597
+ end
598
+
599
+ output_Shin_2009 = struct('PPG_peak_loc_Shin_2009',peak_loc,...
600
+ 'slope_Shin_2009',slope_k,...
601
+ 'filtered_PPG_Shin_2009',filtered_PPG,...
602
+ 'HR_Shin_2009',HR_Shin_2009);
603
+ end","MATLAB"
604
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_05/my_func_ppg_peakdet_method_05_Elgendi_2013_method_II.m",".m","11993","412","function [output_Elgendi_2_2013] = my_func_ppg_peakdet_method_05_Elgendi_2013_method_II(raw_PPG,fs_PPG)
605
+ % -------------------------------------------------------------------------
606
+ % This peak detection function was mentioned in this paper:
607
+ % Elgendi, Mohamed, et al.
608
+ % ""Systolic peak detection in acceleration photoplethysmograms measured from
609
+ % emergency responders in tropical conditions."" PLoS One 8.10 (2013): e76585.
610
+ %
611
+ [onsetp,peakp,dicron,abpsig] = delineator(raw_PPG,fs_PPG);
612
+ % -------------------------------------------------------------------------
613
+
614
+ if isempty(peakp) % there is no peak detected:
615
+ HR_Elgendi_2_2013 = 0; % there is no peak location.
616
+ peakp = 1;
617
+ else
618
+ HR_Elgendi_2_2013 = 60 * fs_PPG ./ diff(peakp); % calculate the HR.
619
+ end
620
+
621
+
622
+ output_Elgendi_2_2013 = struct('PPG_peak_loc_Elgendi_2_2013',peakp,...
623
+ 'HR_Elgendi_2_2013',HR_Elgendi_2_2013,...
624
+ 'filtered_PPG_Elgendi_2_2013',abpsig);
625
+ end
626
+
627
+ function [onsetp,peakp,dicron,abpsig] = delineator(abpsig,abpfreq)
628
+ % Below was copied from Mathwords File Exchange ""Pulse Waveform Delineator"":
629
+ % https://www.mathworks.com/matlabcentral/fileexchange/29484-pulse-waveform-delineator
630
+
631
+ % This program is intended to delineate the fiducial points of pulse waveforms
632
+ % Inputs:
633
+ % abpsig: input as original pulse wave signals;
634
+ % abpfreq: input as the sampling frequency;
635
+ % Outputs:
636
+ % onsetp: output fiducial points as the beginning of each beat;
637
+ % peakp: output fiducial points as systolic peaks;
638
+ % dicron: output fiducial points as dicrotic notches;
639
+
640
+ % Its delineation is based on the self-adaptation in pulse waveforms, but
641
+ % not in the differentials.
642
+
643
+ % Reference:
644
+ % BN Li, MC Dong & MI Vai (2010)
645
+ % On an automatic delineator for arterial blood pressure waveforms
646
+ % Biomedical Signal Processing and Control 5(1) 76-81.
647
+
648
+ % LI Bing Nan @ University of Macau, Feb 2007
649
+ % Revision 2.0.5, Apr 2009
650
+
651
+ %Initialization
652
+ peakIndex=0;
653
+ onsetIndex=0;
654
+ dicroIndex=0;
655
+ stepWin=2*abpfreq;
656
+ closeWin=floor(0.1*abpfreq); %invalide for pulse beat > 200BPM
657
+
658
+ sigLen=length(abpsig);
659
+
660
+ peakp=[];
661
+ onsetp=[];
662
+ dicron=[];
663
+
664
+ %lowpass filter at first
665
+ coh=25; %cutoff frequency is 25Hz
666
+ coh=coh*2/abpfreq;
667
+ od=3; %3rd order bessel filter
668
+ [B,A]=besself(od,coh);
669
+ abpsig=filter(B,A,abpsig);
670
+ abpsig=10*abpsig;
671
+
672
+ abpsig=smooth(abpsig);
673
+
674
+ %Compute differentials
675
+ ttp=diff(abpsig);
676
+ diff1(2:sigLen)=ttp;
677
+ diff1(1)=diff1(2);
678
+ diff1=100*diff1;
679
+ clear ttp;
680
+ diff1=smooth(diff1);
681
+
682
+ if sigLen>12*abpfreq
683
+ tk=10;
684
+ elseif sigLen>7*abpfreq
685
+ tk=5;
686
+ elseif sigLen>4*abpfreq
687
+ tk=2;
688
+ else
689
+ tk=1;
690
+ end
691
+
692
+ %Seek avaerage threshold in original signal
693
+ if tk>1 %self-learning threshold with interval sampling
694
+ tatom=floor(sigLen/(tk+2));
695
+ for ji=1:tk %search the slopes of abp waveforms
696
+ sigIndex=ji*tatom;
697
+ tempIndex=sigIndex+abpfreq;
698
+ [tempMin,jk,tempMax,jl]=seeklocales(abpsig,sigIndex,tempIndex);
699
+ tempTH(ji)=tempMax-tempMin;
700
+ end
701
+ abpMaxTH=mean(tempTH);
702
+ else
703
+ [tempMin,jk,tempMax,jl]=seeklocales(abpsig,closeWin,sigLen);
704
+ abpMaxTH=tempMax-tempMin;
705
+ end
706
+ clear j*;
707
+ clear t*;
708
+
709
+ abpMaxLT=0.4*abpMaxTH;
710
+
711
+ %Seek pulse beats by MinMax method
712
+ % diffIndex=1;
713
+ diffIndex=closeWin; %Avoid filter distortion
714
+
715
+ while diffIndex<sigLen
716
+ tempMin=abpsig(diffIndex); %Initialization
717
+ tempMax=abpsig(diffIndex);
718
+ tempIndex=diffIndex;
719
+ tpeakp=diffIndex; %Avoid initial error
720
+ tonsetp=diffIndex; %Avoid initial error
721
+
722
+ while tempIndex<sigLen
723
+ %If no pulses within 2s, then adjust threshold and retry
724
+ if (tempIndex-diffIndex)>stepWin
725
+ % tempIndex=diffIndex-closeWin;
726
+ tempIndex=diffIndex;
727
+ abpMaxTH=0.6*abpMaxTH;
728
+ if abpMaxTH<=abpMaxLT
729
+ abpMaxTH=2.5*abpMaxLT;
730
+ end
731
+ break;
732
+ end
733
+
734
+ if (diff1(tempIndex-1)*diff1(tempIndex+1))<=0 %Candidate fiducial points
735
+ if (tempIndex+5)<=sigLen
736
+ jk=tempIndex+5;
737
+ else
738
+ jk=sigLen;
739
+ end
740
+ if (tempIndex-5)>=1
741
+ jj=tempIndex-5;
742
+ else
743
+ jj=1;
744
+ end
745
+
746
+ %Artifacts of oversaturated or signal loss?
747
+ if (jk-tempIndex)>=5
748
+ for ttk=tempIndex:jk
749
+ if diff1(ttk)~=0
750
+ break;
751
+ end
752
+ end
753
+ if ttk==jk
754
+ break; %Confirm artifacts
755
+ end
756
+ end
757
+
758
+ if diff1(jj)<0 %Candidate onset
759
+ if diff1(jk)>0
760
+ [tempMini,tmin,ta,tb]=seeklocales(abpsig,jj,jk);
761
+ if abs(tmin-tempIndex)<=2
762
+ tempMin=tempMini;
763
+ tonsetp=tmin;
764
+ end
765
+ end
766
+ elseif diff1(jj)>0 %Candidate peak
767
+ if diff1(jk)<0
768
+ [tc,td,tempMaxi,tmax]=seeklocales(abpsig,jj,jk);
769
+ if abs(tmax-tempIndex)<=2
770
+ tempMax=tempMaxi;
771
+ tpeakp=tmax;
772
+ end
773
+ end
774
+ end
775
+
776
+ if ((tempMax-tempMin)>0.4*abpMaxTH) %evaluation
777
+ if ((tempMax-tempMin)<2*abpMaxTH)
778
+ if tpeakp>tonsetp
779
+ %If more zero-crossing points, further refine!
780
+ ttempMin=abpsig(tonsetp);
781
+ ttonsetp=tonsetp;
782
+ for ttk=tpeakp:-1:(tonsetp+1)
783
+ if abpsig(ttk)<ttempMin
784
+ ttempMin=abpsig(ttk);
785
+ ttonsetp=ttk;
786
+ end
787
+ end
788
+ tempMin=ttempMin;
789
+ tonsetp=ttonsetp;
790
+
791
+ if peakIndex>0
792
+ %If pulse period less than eyeclose, then artifact
793
+ if (tonsetp-peakp(peakIndex))<(3*closeWin)
794
+ %too many fiducial points, then reset
795
+ tempIndex=diffIndex;
796
+ abpMaxTH=2.5*abpMaxLT;
797
+ break;
798
+ end
799
+
800
+ %If pulse period bigger than 2s, then artifact
801
+ if (tpeakp-peakp(peakIndex))>stepWin
802
+ peakIndex=peakIndex-1;
803
+ onsetIndex=onsetIndex-1;
804
+ if dicroIndex>0
805
+ dicroIndex=dicroIndex-1;
806
+ end
807
+ end
808
+
809
+ if peakIndex>0
810
+ %new pulse beat
811
+ peakIndex=peakIndex+1;
812
+ peakp(peakIndex)=tpeakp;
813
+ onsetIndex=onsetIndex+1;
814
+ onsetp(onsetIndex)=tonsetp;
815
+
816
+ tf=onsetp(peakIndex)-onsetp(peakIndex-1);
817
+
818
+ to=floor(abpfreq./20); %50ms
819
+ tff=floor(0.1*tf);
820
+ if tff<to
821
+ to=tff;
822
+ end
823
+ to=peakp(peakIndex-1)+to;
824
+
825
+ te=floor(abpfreq./2); %500ms
826
+ tff=floor(0.5*tf);
827
+ if tff<te
828
+ te=tff;
829
+ end
830
+ te=peakp(peakIndex-1)+te;
831
+ % Dong added on 05/07/2020:
832
+ % For MIMIC III PACPVC 3_2, ii = 25.
833
+ if te > length(diff1)
834
+ te = length(diff1);
835
+ end
836
+ tff=seekdicrotic(diff1(to:te));
837
+ if tff==0
838
+ tff=te-peakp(peakIndex-1);
839
+ tff=floor(tff/3);
840
+ end
841
+ dicroIndex=dicroIndex+1;
842
+ dicron(dicroIndex)=to+tff;
843
+
844
+ tempIndex=tempIndex+closeWin;
845
+ break;
846
+ end
847
+ end
848
+
849
+ if peakIndex==0 %new pulse beat
850
+ peakIndex=peakIndex+1;
851
+ peakp(peakIndex)=tpeakp;
852
+ onsetIndex=onsetIndex+1;
853
+ onsetp(onsetIndex)=tonsetp;
854
+
855
+ tempIndex=tempIndex+closeWin;
856
+ break;
857
+ end
858
+ end
859
+ end
860
+ end
861
+ end
862
+
863
+ tempIndex=tempIndex+1; %step forward
864
+ end
865
+
866
+ % diffIndex=tempIndex+closeWin; %for a new beat
867
+ diffIndex=tempIndex+1;
868
+ end
869
+
870
+ if isempty(peakp),return;end
871
+ %Compensate the offsets of lowpass filter
872
+ sigLen=length(peakp);
873
+ for diffIndex=1:sigLen %avoid edge effect
874
+ tempp(diffIndex)=peakp(diffIndex)-od;
875
+ end
876
+ ttk=tempp(1);
877
+ if ttk<=0
878
+ tempp(1)=1;
879
+ end
880
+ clear peakp;
881
+ peakp=tempp;
882
+ clear tempp;
883
+
884
+ sigLen=length(onsetp);
885
+ for diffIndex=1:sigLen
886
+ tempp(diffIndex)=onsetp(diffIndex)-od;
887
+ end
888
+ ttk=tempp(1);
889
+ if ttk<=0
890
+ tempp(1)=1;
891
+ end
892
+ clear onsetp;
893
+ onsetp=tempp;
894
+ clear tempp;
895
+
896
+ if isempty(dicron),return;end
897
+ sigLen=length(dicron);
898
+ for diffIndex=1:sigLen
899
+ if dicron(diffIndex)~=0
900
+ tempp(diffIndex)=dicron(diffIndex)-od;
901
+ else
902
+ tempp(diffIndex)=0;
903
+ end
904
+ end
905
+ clear dicron;
906
+ dicron=tempp;
907
+ clear tempp;
908
+ end
909
+
910
+ function [mini,minip,maxi,maxip]=seeklocales(tempsig,tempbegin,tempend)
911
+ tempMin=tempsig(tempbegin);
912
+ tempMax=tempsig(tempbegin);
913
+ minip=tempbegin;
914
+ maxip=tempbegin;
915
+ for j=tempbegin:tempend
916
+ if tempsig(j)>tempMax
917
+ tempMax=tempsig(j);
918
+ maxip=j;
919
+ elseif tempsig(j)<tempMin
920
+ tempMin=tempsig(j);
921
+ minip=j;
922
+ end
923
+ end
924
+
925
+ mini=tempMin;
926
+ maxi=tempMax;
927
+ end
928
+
929
+ function [dicron]=seekdicrotic(tempdiff)
930
+ izcMin=0;
931
+ izcMax=0;
932
+ itemp=3;
933
+ tempLen=length(tempdiff)-3;
934
+
935
+ dicron=0;
936
+
937
+ tempdiff=smooth(tempdiff);
938
+
939
+ while itemp<=tempLen
940
+ if (tempdiff(itemp)*tempdiff(itemp+1))<=0
941
+ if tempdiff(itemp-2)<0
942
+ if tempdiff(itemp+2)>=0
943
+ izcMin=izcMin+1;
944
+ tzcMin(izcMin)=itemp;
945
+ end
946
+ end
947
+
948
+ % if tempdiff(itemp-2)>0
949
+ % if tempdiff(itemp+2)<=0
950
+ % izcMax=izcMax+1;
951
+ % tzcMax(izcMax)=itemp;
952
+ % end
953
+ % end
954
+ end
955
+
956
+ itemp=itemp+1;
957
+ end
958
+
959
+ if izcMin==0 %big inflection
960
+ itemp=3;
961
+ tempMin=tempdiff(itemp);
962
+ itempMin=itemp;
963
+
964
+ while itemp<tempLen
965
+ if tempdiff(itemp)<tempMin
966
+ tempMin=tempdiff(itemp);
967
+ itempMin=itemp;
968
+ end
969
+ itemp=itemp+1;
970
+ end
971
+
972
+ itemp=itempMin+1;
973
+ while itemp<tempLen
974
+ if tempdiff(itemp+1)<=tempdiff(itemp-1)
975
+ dicron=itemp;
976
+ return;
977
+ end
978
+ itemp=itemp+1;
979
+ end
980
+ elseif izcMin==1
981
+ dicron=tzcMin(izcMin);
982
+ return;
983
+ else
984
+ itemp=tzcMin(1);
985
+ tempMax=tempdiff(itemp);
986
+ itempMax=itemp;
987
+
988
+ while itemp<tempLen
989
+ if tempdiff(itemp)>tempMax
990
+ tempMax=tempdiff(itemp);
991
+ itempMax=itemp;
992
+ end
993
+ itemp=itemp+1;
994
+ end
995
+
996
+ for itemp=izcMin:-1:1
997
+ if tzcMin(itemp)<itempMax
998
+ dicron=tzcMin(itemp);
999
+ return;
1000
+ end
1001
+ end
1002
+ end
1003
+ end
1004
+
1005
+ function [diap]=seekdiap(tempabp)
1006
+ diap=0;
1007
+
1008
+ [tt,ti]=max(tempabp);
1009
+ if ti==0
1010
+ diap=floor(length(tempabp)./2);
1011
+ else
1012
+ diap=ti;
1013
+ end
1014
+ end
1015
+ ","MATLAB"
1016
+ "Biosensors","Cassey2016/PPG_Peak_Detection","func/my_func_standardizing_PPG.m",".m","422","11","function PPG_buffer = my_func_standardizing_PPG(PPG_buffer)
1017
+ % Standardizing PPG into zero-mean and uni-variance.
1018
+ var_sig_PPG = var(PPG_buffer);
1019
+ if var_sig_PPG == 0
1020
+ univar_sig_PPG = PPG_buffer;
1021
+ else
1022
+ univar_sig_PPG = sqrt(1/var_sig_PPG) * PPG_buffer;
1023
+ end
1024
+ zeromean_sig_PPG = univar_sig_PPG - mean(univar_sig_PPG);
1025
+ PPG_buffer = zeromean_sig_PPG; % univariance for PPG 30 sec segment
1026
+ end","MATLAB"
1027
+ "Biosensors","Cassey2016/PPG_Peak_Detection","func/my_func_prep_PPG_buffer.m",".m","554","17","function [PPG_buffer,fs_PPG] = my_func_prep_PPG_buffer(PPG_raw_buffer,fs_PPG)
1028
+ % Resample PPG to 50 Hz.
1029
+ if fs_PPG ~= 50 % Hz
1030
+ PPG_down = resample(PPG_raw_buffer,50,fs_PPG);
1031
+ fs_PPG = 50;
1032
+ else
1033
+ PPG_down = PPG_raw_buffer;
1034
+ end
1035
+
1036
+ PPG_buffer = PPG_down(:); % Make sure PPG is column vector
1037
+ % Standardizing PPG in sub-function.
1038
+ PPG_buffer = my_func_standardizing_PPG(PPG_buffer);
1039
+
1040
+ % Filter signal.
1041
+ [b, a] = butter(6,[0.5 20]/(fs_PPG/2)); % Bandpass filter.
1042
+ PPG_buffer = filtfilt(b, a, PPG_buffer);
1043
+ end","MATLAB"
1044
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_06/my_revise_run_wabp.m",".m","4330","109","function [r,ssf,my_avg0,A] = my_revise_run_wabp(abp,fs_abp)
1045
+ % Below was copied from Erick Andres Perez Alday's Github repository
1046
+ % "" physionetchallenges / matlab-classifier-2020 "":
1047
+ % https://github.com/physionetchallenges/matlab-classifier-2020/blob/master/Tools/PhysioNet-Cardiovascular-Signal-Toolbox-master/Tools/BP_Tools/run_wabp.m
1048
+ % WABP ABP waveform onset detector.
1049
+ % r = run_wabp(abp) obtains the onset time (in samples)
1050
+ % of each beat in the ABP waveform.
1051
+ %
1052
+ % In: ABP (125Hz sampled)
1053
+ % Out: Onset sample time
1054
+ %
1055
+ % Usage:
1056
+ % - ABP waveform must have units of mmHg
1057
+ %
1058
+ % Written by James Sun (xinsun@mit.edu) on Nov 19, 2005. This ABP onset
1059
+ % detector is adapted from Dr. Wei Zong's wabp.c.
1060
+ %
1061
+ % LICENSE:
1062
+ % This software is offered freely and without warranty under
1063
+ % the GNU (v3 or later) public license. See license file for
1064
+ % more information
1065
+
1066
+ % Dong changed: input should be 250 Hz for filtering.
1067
+ %% Input checks
1068
+ % if nargin ~=1
1069
+ % error('exactly 1 argment needed');
1070
+ % end
1071
+
1072
+ if size(abp,2)~=1
1073
+ error('Input must be a <nx1> vector');
1074
+ end
1075
+
1076
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1077
+
1078
+ % scale physiologic ABP
1079
+ offset = 1600;
1080
+ scale = 20;
1081
+ Araw = abp*scale-offset;
1082
+
1083
+ % LPF
1084
+ A = filter([1 0 0 0 0 -2 0 0 0 0 1],[1 -2 1],Araw)/24+30;
1085
+ A = (A(4:end)+offset)/scale; % Takes care of 4 sample group delay
1086
+
1087
+ % ------- Dong changed this: -------
1088
+ A = A ./ std(A); % normalizing data is very important for my peak detection.
1089
+ A = A - mean(A);
1090
+
1091
+ % Slope-sum function
1092
+ dypos = diff(A);
1093
+ dypos(dypos<0) = 0;
1094
+ % ssf = [0; 0; conv(ones(16,1),dypos)];
1095
+ w = 16/125*fs_abp; % 125 Hz to 250 Hz.
1096
+ ssf = [0; 0; conv(ones(w,1),dypos)];
1097
+
1098
+ % Decision rule
1099
+ first_8sec = 8*fs_abp;
1100
+ % avg0 = sum(ssf(1:1000))/1000; % average of 1st 8 seconds (1000 samples) of SSF
1101
+ avg0 = sum(ssf(1:first_8sec))/first_8sec;
1102
+ Threshold0 = 3*avg0; % initial decision threshold
1103
+
1104
+ % ignoring ""learning period"" for now
1105
+ lockout = 0; % lockout >0 means we are in refractory
1106
+ timer = 0;
1107
+ % z = zeros(100000,1);
1108
+ z = zeros(fs_abp*800,1);
1109
+ counter = 0;
1110
+
1111
+ % Dong: copied from wabp.c, 02/27/2020. % Dong change here. 02/27/2020.
1112
+ TmDEF = 0.25; %5;% Dong change here. 02/27/2020.
1113
+ max_min_thres = 0.1; %10;% Dong change here. 02/27/2020.
1114
+ my_avg0 = zeros(size(abp));% Dong change here. 02/27/2020.
1115
+ step_adjust_thres = 0.025; % it was 0.1 % Dong change here. 02/27/2020.
1116
+ % for t = 50:length(ssf)-17
1117
+ for t = round(0.4*fs_abp):length(ssf)-w-1
1118
+ lockout = lockout - 1;
1119
+ timer = timer + 1; % Timer used for counting time after previous ABP pulse
1120
+
1121
+ if (lockout<1) & (ssf(t)>avg0+TmDEF) %(ssf(t)>avg0+5) % Not in refractory and SSF has exceeded threshold here % Dong change here. 02/27/2020.
1122
+ timer = 0;
1123
+ maxSSF = max(ssf(t:t+w)); % Find local max of SSF
1124
+ minSSF = min(ssf(t-w:t)); % Find local min of SSF
1125
+ if maxSSF > (minSSF + max_min_thres) %(minSSF + 10)% Dong change here. 02/27/2020.
1126
+ onset = 0.01*maxSSF ; % Onset is at the time in which local SSF just exceeds 0.01*maxSSF
1127
+
1128
+ tt = t-w:t;
1129
+ dssf = ssf(tt) - ssf(tt-1);
1130
+ BeatTime = find(dssf<onset,1,'last')+t-w-1;
1131
+ counter = counter+1;
1132
+
1133
+ if isempty(BeatTime)
1134
+ counter = counter-1;
1135
+ else
1136
+ z(counter) = BeatTime;
1137
+ end
1138
+ Threshold0 = Threshold0 + step_adjust_thres*(maxSSF - Threshold0); % adjust threshold
1139
+ avg0 = Threshold0 / 3; % adjust avg
1140
+
1141
+ lockout = round(32/125*fs_abp); % lock so prevent sensing right after detection (refractory period)
1142
+ end
1143
+ end
1144
+
1145
+ if timer > round(312/125*fs_abp) % Lower threshold if no pulse detection for a while
1146
+ Threshold0 = Threshold0 - 0.1; %Threshold0 - 1; % Dong change here. 02/27/2020.
1147
+ avg0 = Threshold0/3;
1148
+ end
1149
+ my_avg0(t,1) = avg0+TmDEF; % % Dong change here. 02/27/2020.
1150
+ end
1151
+ r = z(find(z))-2;
1152
+ end","MATLAB"
1153
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_06/my_Elgendi_2013_method_III_peakdet.m",".m","1028","22","function [output_Elgendi_3_2013] = my_Elgendi_2013_method_III_peakdet(raw_PPG,fs_PPG)
1154
+ % -------------------------------------------------------------------------
1155
+ % This peak detection function was mentioned in this paper:
1156
+ % Elgendi, Mohamed, et al.
1157
+ % ""Systolic peak detection in acceleration photoplethysmograms measured from
1158
+ % emergency responders in tropical conditions."" PLoS One 8.10 (2013): e76585.
1159
+ %
1160
+ [r,ssf,my_avg0,A] = my_revise_run_wabp(raw_PPG,fs_PPG);
1161
+ % -------------------------------------------------------------------------
1162
+ if isempty(r)
1163
+ HR_Elgendi_3_2013 = 0; % there is no peak location.
1164
+ r = 1;
1165
+ else
1166
+ HR_Elgendi_3_2013 = 60 * fs_PPG ./ diff(r); % calculate the HR.
1167
+ end
1168
+ A = [A;0;0;0;]; % add zero
1169
+ A(1:6) = A(7); % first six plots are all high amplitude.
1170
+ output_Elgendi_3_2013 = struct('PPG_peak_loc_Elgendi_3_2013',r,...
1171
+ 'HR_Elgendi_3_2013',HR_Elgendi_3_2013,...
1172
+ 'filtered_PPG_Elgendi_3_2013',A,...
1173
+ 'thres_Elgendi_3_2013',my_avg0);
1174
+ end","MATLAB"
1175
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_03_and_04/my_Elgendi_2013_method_I_peakdet.m",".m","3762","111","function [output_Elgendi_1_2013] = my_Elgendi_2013_method_I_peakdet(raw_PPG, delta, fs_PPG)
1176
+ % -------------------------------------------------------------------------
1177
+ % Dong add this on 02/25/2020, based on this paper:
1178
+ % Elgendi, Mohamed, et al.
1179
+ % ""Systolic peak detection in acceleration photoplethysmograms measured from
1180
+ % emergency responders in tropical conditions."" PLoS One 8.10 (2013): e76585.
1181
+ %
1182
+ % (1): bandpass filter (0.5-8Hz)
1183
+ [b, a] = butter(6,[0.5 8]/(fs_PPG/2)); % bandpass filter 0.5-10Hz, changed from 0.5-20 to 0.5-9 Hz at 11/21/2018
1184
+ raw_PPG = filtfilt(b, a, raw_PPG); % -> AC component
1185
+ raw_PPG = raw_PPG ./ std(raw_PPG); % normalizing data is very important for my peak detection.
1186
+ raw_PPG = raw_PPG - mean(raw_PPG);
1187
+
1188
+ debugging_plot_flag = false; % only for plotting debugging figures.
1189
+ % -------------------------------------------------------------------------
1190
+ % Below code is copied from: http://billauer.co.il/peakdet.html
1191
+ % PEAKDET Detect peaks in a vector
1192
+ % [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
1193
+ % maxima and minima (""peaks"") in the vector V.
1194
+ % MAXTAB and MINTAB consists of two columns. Column 1
1195
+ % contains indices in V, and column 2 the found values.
1196
+ %
1197
+ % With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices
1198
+ % in MAXTAB and MINTAB are replaced with the corresponding
1199
+ % X-values.
1200
+ %
1201
+ % A point is considered a maximum peak if it has the maximal
1202
+ % value, and was preceded (to the left) by a value lower by
1203
+ % DELTA.
1204
+
1205
+ % Eli Billauer, 3.4.05 (Explicitly not copyrighted).
1206
+ % This function is released to the public domain; Any use is allowed.
1207
+ maxtab = [];
1208
+ mintab = [];
1209
+
1210
+ raw_PPG = raw_PPG(:); % Just in case this wasn't a proper vector
1211
+
1212
+ % if nargin < 3
1213
+ x = (1:length(raw_PPG))';
1214
+ % else
1215
+ % x = x(:);
1216
+ % if length(raw_PPG)~= length(x)
1217
+ % error('Input vectors v and x must have same length');
1218
+ % end
1219
+ % end
1220
+
1221
+ if (length(delta(:)))>1
1222
+ error('Input argument DELTA must be a scalar');
1223
+ end
1224
+
1225
+ if delta <= 0
1226
+ error('Input argument DELTA must be positive');
1227
+ end
1228
+
1229
+ mn = Inf; mx = -Inf;
1230
+ mnpos = NaN; mxpos = NaN;
1231
+
1232
+ lookformax = 1;
1233
+
1234
+ for i=1:length(raw_PPG)
1235
+ this = raw_PPG(i);
1236
+ if this > mx, mx = this; mxpos = x(i); end
1237
+ if this < mn, mn = this; mnpos = x(i); end
1238
+
1239
+ if lookformax
1240
+ if this < mx-delta
1241
+ maxtab = [maxtab ; mxpos mx];
1242
+ mn = this; mnpos = x(i);
1243
+ lookformax = 0;
1244
+ end
1245
+ else
1246
+ if this > mn+delta
1247
+ mintab = [mintab ; mnpos mn];
1248
+ mx = this; mxpos = x(i);
1249
+ lookformax = 1;
1250
+ end
1251
+ end
1252
+ end
1253
+
1254
+
1255
+
1256
+ if isempty(maxtab)
1257
+ HR_Elgendi_1_max_2009 = 0; % there is no peak location.
1258
+ peak_loc_max = 1;
1259
+ else
1260
+ peak_loc_max = maxtab(:,1);
1261
+ HR_Elgendi_1_max_2009 = 60 * fs_PPG ./ diff(peak_loc_max); % calculate the HR.
1262
+ end
1263
+
1264
+ if isempty(mintab)
1265
+ HR_Elgendi_1_min_2009 = 0; % there is no peak location.
1266
+ peak_loc_min = 1;
1267
+ else
1268
+ peak_loc_min = mintab(:,1);
1269
+ HR_Elgendi_1_min_2009 = 60 * fs_PPG ./ diff(peak_loc_min); % calculate the HR.
1270
+ end
1271
+
1272
+ output_Elgendi_1_2013 = struct('filtered_PPG_Elgendi_1_2013',raw_PPG,...
1273
+ 'PPG_peak_loc_Elgendi_1_max_2013',peak_loc_max,...
1274
+ 'PPG_peak_loc_Elgendi_1_min_2013',peak_loc_min,...
1275
+ 'HR_Elgendi_1_max_2013',HR_Elgendi_1_max_2009,...
1276
+ 'HR_Elgendi_1_min_2013',HR_Elgendi_1_min_2009);
1277
+
1278
+
1279
+ if debugging_plot_flag % debugging plot
1280
+ figure;
1281
+ plot(x,raw_PPG);hold on;
1282
+ plot(peak_loc_max,raw_PPG(peak_loc_max),'ro');
1283
+ plot(peak_loc_min,raw_PPG(peak_loc_min),'go');
1284
+ end
1285
+ end","MATLAB"
1286
+ "Biosensors","Cassey2016/PPG_Peak_Detection","method_08_and_09/my_Vadrevu_2019_peakdet.m",".m","10598","329","function [output_Vadrevu_1_2019,output_Vadrevu_2_2019] = my_Vadrevu_2019_peakdet(PPG_buffer,fs_PPG)
1287
+ % =========================================================================
1288
+ % This is my implementation of this paper:
1289
+ %
1290
+ % Vadrevu, Simhadri, and M. Sabarimalai Manikandan.
1291
+ % ""A robust pulse onset and peak detection method for automated PPG signal
1292
+ % analysis system."" IEEE Transactions on Instrumentation and Measurement
1293
+ % 68.3 (2018): 807-817.
1294
+ %
1295
+ % Implemented by Dong Han on 05/03/2020.
1296
+ %
1297
+ % Please cite our paper if you used this code:
1298
+ % Han, Dong, Syed K. Bashar, Jesús Lázaro, Fahimeh Mohagheghian,
1299
+ % Andrew Peitzsch, Nishat Nishita, Eric Ding, Emily L. Dickson,
1300
+ % Danielle DiMezza, Jessica Scott, Cody Whitcomb, Timothy P. Fitzgibbons,
1301
+ % David D. McManus, and Ki H. Chon. 2022.
1302
+ % ""A Real-Time PPG Peak Detection Method for Accurate Determination of
1303
+ % Heart Rate during Sinus Rhythm and Cardiac Arrhythmia""
1304
+ % Biosensors 12, no. 2: 82. https://doi.org/10.3390/bios12020082
1305
+ %
1306
+ % Please cite our paper if you used our code. Thank you.
1307
+ % =========================================================================
1308
+ debug_flag = false; % decide to plot the paper figure or not.
1309
+ %% A. Stationary Wavelet Transform of PPG signal.
1310
+
1311
+ % first, for the input length, you can know the maximum wavelet
1312
+ % decomposition level you can get:
1313
+ TYPE = '1D'; % extension method.
1314
+ MODE = 'zpd'; % zero extension.
1315
+ X = PPG_buffer;
1316
+
1317
+ % based on your input signal length, you have to extend your input signal
1318
+ % to MATLAB suggested length.
1319
+ LEN = 45;%18; % 18 for fs_PPG 50, 45 for fs_PPG 125; for 30 sec input.
1320
+ YEXT = wextend(TYPE,MODE,X,LEN); % required by swt.
1321
+ sig = YEXT;
1322
+ % s = PPG_buffer;
1323
+ sLen = length(sig);
1324
+ wname = 'bior1.5';
1325
+ L = wmaxlev(sLen,wname);
1326
+
1327
+ % [swa,swd] = swt(s,3,'bior1.5'); % the author mentioned wavelet biorthogonal 1.5 (bior1.5)
1328
+ [swa,swd] = swt(sig,L,wname); % the author mentioned wavelet biorthogonal 1.5 (bior1.5)
1329
+ s1 = swd(3,:) + swd(4,:);
1330
+ s1 = s1(:); % make sure it is column vector.
1331
+ s2 = swd(5,:) + swd(6,:) + swd(7,:);
1332
+ s2 = s2(:); % make sure it is column vector.
1333
+
1334
+ if debug_flag
1335
+ % if you want to debug the result.
1336
+ figure;
1337
+ t_plot = [1:length(sig)]'./fs_PPG; %
1338
+ subplot(5,1,1);
1339
+ plot(t_plot,sig);
1340
+ xlim([0 t_plot(end)])
1341
+ ylabel('Orig');
1342
+ title('Fig.3 in TIM 2019 paper');
1343
+
1344
+ subplot(5,1,2)
1345
+ plot(t_plot,(swd(1,:) + swd(2,:)))
1346
+ xlim([0 t_plot(end)])
1347
+ ylabel('s_0');
1348
+
1349
+ subplot(5,1,3);
1350
+ plot(t_plot,s1);
1351
+ xlim([0 t_plot(end)])
1352
+ ylabel('s_1');
1353
+
1354
+ subplot(5,1,4);
1355
+ plot(t_plot,s2);
1356
+ xlim([0 t_plot(end)])
1357
+ ylabel('s_2');
1358
+
1359
+ subplot(5,1,5);
1360
+ plot(t_plot,swa(7,:));
1361
+ xlim([0 t_plot(end)])
1362
+ ylabel('a_7');
1363
+ end
1364
+ %% B. Multiscale Sum and Products:
1365
+ p = s1 .* s2;
1366
+ p = p(:);
1367
+
1368
+ if debug_flag
1369
+ % if you want to debug the result.
1370
+ figure;
1371
+ ax(1) = subplot(4,1,1);
1372
+ plot(t_plot,sig);
1373
+ xlim([0 t_plot(end)])
1374
+ ylabel('Orig');
1375
+ title('Fig.4 in TIM 2019 paper');
1376
+
1377
+ ax(2) = subplot(4,1,2);
1378
+ p1 = swd(1,:) .* swd(2,:) .* swd(3,:) .* swd(4,:) .* swd(5,:) .* swd(6,:) .* swd(7,:);
1379
+ plot(t_plot,p1);
1380
+ xlim([0 t_plot(end)])
1381
+ ylabel('p_1');
1382
+
1383
+ ax(3) = subplot(4,1,3);
1384
+ p1 = swd(3,:) .* swd(4,:) .* swd(5,:) .* swd(6,:) .* swd(7,:);
1385
+ plot(t_plot,p1);
1386
+ xlim([0 t_plot(end)])
1387
+ ylabel('p_2');
1388
+
1389
+ ax(4) = subplot(4,1,4);
1390
+ plot(t_plot,p);
1391
+ xlim([0 t_plot(end)])
1392
+ ylabel('p');
1393
+
1394
+ linkaxes(ax,'x');
1395
+ end
1396
+ %% C. Shannon Entropy Envelope Extraction
1397
+ eta = 0.01 + std(p);
1398
+ p_tilda = abs(p);
1399
+ p_tilda(p_tilda < eta) = 0;
1400
+ p_tilda = p_tilda(:);
1401
+
1402
+ % normalize p_tilda:
1403
+ norm_p_tilda = (p_tilda - min(p_tilda)) ./ (max(p_tilda) - min(p_tilda));
1404
+ norm_p_tilda = norm_p_tilda(:);
1405
+
1406
+ se = NaN(size(norm_p_tilda));
1407
+
1408
+ for tttt = 1:size(norm_p_tilda,1)
1409
+ if norm_p_tilda(tttt) == 0
1410
+ % from MATLAB page: https://www.mathworks.com/help/wavelet/ref/wentropy.html
1411
+ % log(0) = 0
1412
+ % 0log(0) = 0.
1413
+ se(tttt) = 0;
1414
+ else
1415
+ se(tttt) = -1 * norm_p_tilda(tttt) .* log(norm_p_tilda(tttt));
1416
+ end
1417
+ end
1418
+
1419
+ % % method 1: CONV twice:
1420
+ filt_Len = floor(0.2 * fs_PPG); % 0.4 is better. 05/04/2020.
1421
+ % h = ones(filt_Len,1)/filt_Len; % A third-order filter has length 4
1422
+ % s = conv(se,h,'same'); % return the same size as se
1423
+ % s = conv(s,h,'same'); % conv twice
1424
+
1425
+ % method 2: FILTFILT.
1426
+ % for 4020, ii = 2, PPG is zero.
1427
+ if any(isnan(se))
1428
+ % any sample is NaN.
1429
+ new_se = se;
1430
+ new_se(isnan(new_se)) = [];
1431
+ if isempty(new_se)
1432
+ % nothing left after removing NaN.
1433
+
1434
+ HR_Vadrevu_1_2019 = 0; % there is no peak location.
1435
+ onset_zx = 1;
1436
+
1437
+ HR_Vadrevu_2_2019 = 0; % there is no peak location.
1438
+ peak_zx = 1;
1439
+
1440
+ filter_PPG = PPG_buffer;
1441
+ output_Vadrevu_1_2019 = struct('filtered_PPG_Vadrevu_2019',filter_PPG,...
1442
+ 'PPG_peak_loc_Vadrevu_1_2019',onset_zx,...
1443
+ 'HR_Vadrevu_1_2019',HR_Vadrevu_1_2019);
1444
+
1445
+ output_Vadrevu_2_2019 = struct('filtered_PPG_Vadrevu_2019',filter_PPG,...
1446
+ 'PPG_peak_loc_Vadrevu_2_2019',peak_zx,...
1447
+ 'HR_Vadrevu_2_2019',HR_Vadrevu_2_2019);
1448
+ return
1449
+ else
1450
+ % part of data is NaN, maybe I should fill zeros in it?
1451
+ keyboard;
1452
+ end
1453
+ end
1454
+ b = ones(filt_Len,1);
1455
+ a = -1;
1456
+ s = filtfilt(b, a, se); % -> AC component
1457
+
1458
+
1459
+ %% D. Pulse Peak and Onset Determination.
1460
+ % 1. Gaussian derivative kernel:
1461
+ sigma_1 = floor(0.05 * fs_PPG); % 0.05 mentioned in the paper.
1462
+ M = floor(2 * fs_PPG); % 2 mentioned in the paper.
1463
+ g = gausswin(M,sigma_1); % size should be 250 if Fs = 125.
1464
+ h_d = diff(g); % g(m+1) - g(m).
1465
+ z = conv(s,h_d,'same');
1466
+
1467
+ % % My conv function did not work.
1468
+ % temp_z = zeros(size(s,1),1);
1469
+ % for nnnn = 1:size(s,1)
1470
+ % for mmmm = 1:size(g,1)-1
1471
+ % if (nnnn-mmmm+1 > 0)
1472
+ % % h_d(mmmm) = g(mmmm+1) - g(mmmm);
1473
+ % temp_z(nnnn) = temp_z(nnnn) + s(mmmm) * h_d(nnnn-mmmm+1);
1474
+ % end
1475
+ % end
1476
+ % end
1477
+
1478
+ DownZCi = @(v) find(v(1:end-1) >= 0 & v(2:end) < 0); % Returns Down Zero-Crossing Indices. https://www.mathworks.com/matlabcentral/answers/267222-easy-way-of-finding-zero-crossing-of-a-function
1479
+ zx = DownZCi(z); % negative zero crossing point.
1480
+
1481
+ % peak correction algorithm for onset:
1482
+ search_intv = floor(0.1 * fs_PPG / 2); % w/2
1483
+ onset_zx = NaN(size(zx));
1484
+ for zz = 1:size(zx,1)
1485
+ temp_zx = zx(zz);
1486
+ if temp_zx - search_intv > 0 % not exceed signal limit.
1487
+ if temp_zx + search_intv <= size(sig,1)
1488
+ temp_PPG = sig(temp_zx - search_intv : temp_zx + search_intv);
1489
+ [~,I] = min(temp_PPG);
1490
+ if isempty(I) ~= 1
1491
+ adj_loc = temp_zx - search_intv + I - 1;
1492
+ else
1493
+ % no local minimum.
1494
+ adj_loc = temp_zx;
1495
+ end
1496
+ onset_zx(zz) = adj_loc;
1497
+ else
1498
+ % right interval exceed signal length.
1499
+ onset_zx(zz) = zx(zz);
1500
+ end
1501
+ else
1502
+ % left interval exceed index 1.
1503
+ onset_zx(zz) = zx(zz);
1504
+ end
1505
+ end
1506
+
1507
+ % find peak:
1508
+ peak_zx = NaN(size(onset_zx,1)-1,1); % one sample smaller.
1509
+ for zz = 2:size(onset_zx,1)
1510
+ temp_onset_1 = onset_zx(zz-1);
1511
+ temp_onset_2 = onset_zx(zz);
1512
+ temp_PPG = sig(temp_onset_1:temp_onset_2);
1513
+ [~,I] = max(temp_PPG);
1514
+ if isempty(I) ~= 1
1515
+ peak_zx(zz-1) = temp_onset_1 + I - 1; % peak is one sample size smaller.
1516
+ else
1517
+ peak_zx(zz-1) = onset_zx(zz);
1518
+ end
1519
+ end
1520
+
1521
+ % prepare to output signal:
1522
+ filter_PPG = z(LEN+1:end-LEN);
1523
+ remove_left = find(onset_zx < LEN+1);
1524
+ if isempty(remove_left) ~= 1
1525
+ onset_zx(remove_left) = [];
1526
+ end
1527
+ remove_right = find(onset_zx > size(z,1) - LEN);
1528
+ if isempty(remove_right) ~= 1
1529
+ onset_zx(remove_right) = [];
1530
+ end
1531
+ onset_zx = onset_zx - LEN; % shifted.
1532
+
1533
+ remove_left = find(peak_zx < LEN+1);
1534
+ if isempty(remove_left) ~= 1
1535
+ peak_zx(remove_left) = [];
1536
+ end
1537
+ remove_right = find(peak_zx > size(z,1) - LEN);
1538
+ if isempty(remove_right) ~= 1
1539
+ peak_zx(remove_right) = [];
1540
+ end
1541
+ peak_zx = peak_zx - LEN;
1542
+
1543
+ if debug_flag
1544
+ % if you want to debug the result.
1545
+ figure;
1546
+ ax(1) = subplot(7,1,1);
1547
+ plot(t_plot,sig);
1548
+ xlim([0 t_plot(end)])
1549
+ ylabel('Orig');
1550
+ title('Fig.5 in TIM 2019 paper');
1551
+
1552
+ ax(2) = subplot(7,1,2);
1553
+ plot(t_plot,p);
1554
+ xlim([0 t_plot(end)])
1555
+ ylabel('p');
1556
+
1557
+ ax(3) = subplot(7,1,3);
1558
+ plot(t_plot,norm_p_tilda);
1559
+ xlim([0 t_plot(end)])
1560
+ ylabel('p_th');
1561
+
1562
+ ax(4) = subplot(7,1,4);
1563
+ plot(t_plot,se);
1564
+ xlim([0 t_plot(end)])
1565
+ ylabel('se');
1566
+
1567
+ ax(5) = subplot(7,1,5);
1568
+ plot(t_plot,s);
1569
+ xlim([0 t_plot(end)])
1570
+ ylabel('s');
1571
+
1572
+ ax(6) = subplot(7,1,6);
1573
+ plot(t_plot,z);
1574
+ hold on;
1575
+ plot(t_plot(zx),z(zx),'ro');
1576
+ xlim([0 t_plot(end)]);
1577
+ ylabel('z');
1578
+
1579
+
1580
+ ax(7) = subplot(7,1,7);
1581
+ plot(t_plot,sig);
1582
+ hold on;
1583
+ plot(t_plot(onset_zx),sig(onset_zx),'go');
1584
+ plot(t_plot(peak_zx),sig(peak_zx),'ro');
1585
+ xlim([0 t_plot(end)])
1586
+ ylabel('orig with peak');
1587
+
1588
+ linkaxes(ax,'x');
1589
+ end
1590
+
1591
+ if isempty(onset_zx)
1592
+ HR_Vadrevu_1_2019 = 0; % there is no peak location.
1593
+ onset_zx = 1;
1594
+ else
1595
+ HR_Vadrevu_1_2019 = 60 * fs_PPG ./ diff(onset_zx); % calculate the HR.
1596
+ end
1597
+
1598
+ if isempty(peak_zx)
1599
+ HR_Vadrevu_2_2019 = 0; % there is no peak location.
1600
+ peak_zx = 1;
1601
+ else
1602
+ HR_Vadrevu_2_2019 = 60 * fs_PPG ./ diff(peak_zx); % calculate the HR.
1603
+ end
1604
+
1605
+ output_Vadrevu_1_2019 = struct('filtered_PPG_Vadrevu_2019',filter_PPG,...
1606
+ 'PPG_peak_loc_Vadrevu_1_2019',onset_zx,...
1607
+ 'HR_Vadrevu_1_2019',HR_Vadrevu_1_2019);
1608
+
1609
+ output_Vadrevu_2_2019 = struct('filtered_PPG_Vadrevu_2019',filter_PPG,...
1610
+ 'PPG_peak_loc_Vadrevu_2_2019',peak_zx,...
1611
+ 'HR_Vadrevu_2_2019',HR_Vadrevu_2_2019);
1612
+
1613
+ end
1614
+ ","MATLAB"