MAXREFDES117# Code Documentation  V01.00
Heart Rate / SpO2 Monitor
 All Data Structures Files Functions Variables Typedefs Macros Pages
algorithm.cpp
Go to the documentation of this file.
1 
27 /*******************************************************************************
28 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included
38 * in all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
41 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
44 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
45 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46 * OTHER DEALINGS IN THE SOFTWARE.
47 *
48 * Except as contained in this notice, the name of Maxim Integrated
49 * Products, Inc. shall not be used except as stated in the Maxim Integrated
50 * Products, Inc. Branding Policy.
51 *
52 * The mere transfer of this software does not imply any licenses
53 * of trade secrets, proprietary technology, copyrights, patents,
54 * trademarks, maskwork rights, or any other form of intellectual
55 * property whatsoever. Maxim Integrated Products, Inc. retains all
56 * ownership rights.
57 *******************************************************************************
58 */
59 
60 #include "algorithm.h"
61 #include "arduino.h"
62 
63 #if defined(ARDUINO_AVR_UNO)
64 //Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
65 //To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
66 void maxim_heart_rate_and_oxygen_saturation(uint16_t *pun_ir_buffer, int32_t n_ir_buffer_length, uint16_t *pun_red_buffer, int32_t *pn_spo2, int8_t *pch_spo2_valid,
67  int32_t *pn_heart_rate, int8_t *pch_hr_valid)
68 #else
69 void maxim_heart_rate_and_oxygen_saturation(uint32_t *pun_ir_buffer, int32_t n_ir_buffer_length, uint32_t *pun_red_buffer, int32_t *pn_spo2, int8_t *pch_spo2_valid,
70  int32_t *pn_heart_rate, int8_t *pch_hr_valid)
71 #endif
72 
89 {
90  uint32_t un_ir_mean,un_only_once ;
91  int32_t k, n_i_ratio_count;
92  int32_t i, s, m, n_exact_ir_valley_locs_count, n_middle_idx;
93  int32_t n_th1, n_npks, n_c_min;
94  int32_t an_ir_valley_locs[15] ;
95  int32_t n_peak_interval_sum;
96 
97  int32_t n_y_ac, n_x_ac;
98  int32_t n_spo2_calc;
99  int32_t n_y_dc_max, n_x_dc_max;
100  int32_t n_y_dc_max_idx, n_x_dc_max_idx;
101  int32_t an_ratio[5], n_ratio_average;
102  int32_t n_nume, n_denom ;
103 
104  // calculates DC mean and subtract DC from ir
105  un_ir_mean =0;
106  for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ;
107  un_ir_mean =un_ir_mean/n_ir_buffer_length ;
108 
109  // remove DC and invert signal so that we can use peak detector as valley detector
110  for (k=0 ; k<n_ir_buffer_length ; k++ )
111  an_x[k] = -1*(pun_ir_buffer[k] - un_ir_mean) ;
112 
113  // 4 pt Moving Average
114  for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){
115  an_x[k]=( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3])/(int)4;
116  }
117  // calculate threshold
118  n_th1=0;
119  for ( k=0 ; k<BUFFER_SIZE ;k++){
120  n_th1 += an_x[k];
121  }
122  n_th1= n_th1/ ( BUFFER_SIZE);
123  if( n_th1<30) n_th1=30; // min allowed
124  if( n_th1>60) n_th1=60; // max allowed
125 
126  for ( k=0 ; k<15;k++) an_ir_valley_locs[k]=0;
127  // since we flipped signal, we use peak detector as valley detector
128  maxim_find_peaks( an_ir_valley_locs, &n_npks, an_x, BUFFER_SIZE, n_th1, 4, 15 );//peak_height, peak_distance, max_num_peaks
129  n_peak_interval_sum =0;
130  if (n_npks>=2){
131  for (k=1; k<n_npks; k++) n_peak_interval_sum += (an_ir_valley_locs[k] -an_ir_valley_locs[k -1] ) ;
132  n_peak_interval_sum =n_peak_interval_sum/(n_npks-1);
133  *pn_heart_rate =(int32_t)( (FS*60)/ n_peak_interval_sum );
134  *pch_hr_valid = 1;
135  }
136  else {
137  *pn_heart_rate = -999; // unable to calculate because # of peaks are too small
138  *pch_hr_valid = 0;
139  }
140 
141  // load raw value again for SPO2 calculation : RED(=y) and IR(=X)
142  for (k=0 ; k<n_ir_buffer_length ; k++ ) {
143  an_x[k] = pun_ir_buffer[k] ;
144  an_y[k] = pun_red_buffer[k] ;
145  }
146 
147  // find precise min near an_ir_valley_locs
148  n_exact_ir_valley_locs_count =n_npks;
149 
150  //using exact_ir_valley_locs , find ir-red DC andir-red AC for SPO2 calibration an_ratio
151  //finding AC/DC maximum of raw
152 
153  n_ratio_average =0;
154  n_i_ratio_count = 0;
155  for(k=0; k< 5; k++) an_ratio[k]=0;
156  for (k=0; k< n_exact_ir_valley_locs_count; k++){
157  if (an_ir_valley_locs[k] > BUFFER_SIZE ){
158  *pn_spo2 = -999 ; // do not use SPO2 since valley loc is out of range
159  *pch_spo2_valid = 0;
160  return;
161  }
162  }
163  // find max between two valley locations
164  // and use an_ratio betwen AC compoent of Ir & Red and DC compoent of Ir & Red for SPO2
165  for (k=0; k< n_exact_ir_valley_locs_count-1; k++){
166  n_y_dc_max= -16777216 ;
167  n_x_dc_max= -16777216;
168  if (an_ir_valley_locs[k+1]-an_ir_valley_locs[k] >3){
169  for (i=an_ir_valley_locs[k]; i< an_ir_valley_locs[k+1]; i++){
170  if (an_x[i]> n_x_dc_max) {n_x_dc_max =an_x[i]; n_x_dc_max_idx=i;}
171  if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i]; n_y_dc_max_idx=i;}
172  }
173  n_y_ac= (an_y[an_ir_valley_locs[k+1]] - an_y[an_ir_valley_locs[k] ] )*(n_y_dc_max_idx -an_ir_valley_locs[k]); //red
174  n_y_ac= an_y[an_ir_valley_locs[k]] + n_y_ac/ (an_ir_valley_locs[k+1] - an_ir_valley_locs[k]) ;
175  n_y_ac= an_y[n_y_dc_max_idx] - n_y_ac; // subracting linear DC compoenents from raw
176  n_x_ac= (an_x[an_ir_valley_locs[k+1]] - an_x[an_ir_valley_locs[k] ] )*(n_x_dc_max_idx -an_ir_valley_locs[k]); // ir
177  n_x_ac= an_x[an_ir_valley_locs[k]] + n_x_ac/ (an_ir_valley_locs[k+1] - an_ir_valley_locs[k]);
178  n_x_ac= an_x[n_y_dc_max_idx] - n_x_ac; // subracting linear DC compoenents from raw
179  n_nume=( n_y_ac *n_x_dc_max)>>7 ; //prepare X100 to preserve floating value
180  n_denom= ( n_x_ac *n_y_dc_max)>>7;
181  if (n_denom>0 && n_i_ratio_count <5 && n_nume != 0)
182  {
183  an_ratio[n_i_ratio_count]= (n_nume*100)/n_denom ; //formular is ( n_y_ac *n_x_dc_max) / ( n_x_ac *n_y_dc_max) ;
184  n_i_ratio_count++;
185  }
186  }
187  }
188  // choose median value since PPG signal may varies from beat to beat
189  maxim_sort_ascend(an_ratio, n_i_ratio_count);
190  n_middle_idx= n_i_ratio_count/2;
191 
192  if (n_middle_idx >1)
193  n_ratio_average =( an_ratio[n_middle_idx-1] +an_ratio[n_middle_idx])/2; // use median
194  else
195  n_ratio_average = an_ratio[n_middle_idx ];
196 
197  if( n_ratio_average>2 && n_ratio_average <184){
198  n_spo2_calc= uch_spo2_table[n_ratio_average] ;
199  *pn_spo2 = n_spo2_calc ;
200  *pch_spo2_valid = 1;// float_SPO2 = -45.060*n_ratio_average* n_ratio_average/10000 + 30.354 *n_ratio_average/100 + 94.845 ; // for comparison with table
201  }
202  else{
203  *pn_spo2 = -999 ; // do not use SPO2 since signal an_ratio is out of range
204  *pch_spo2_valid = 0;
205  }
206 }
207 
208 
209 void maxim_find_peaks( int32_t *pn_locs, int32_t *n_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height, int32_t n_min_distance, int32_t n_max_num )
217 {
218  maxim_peaks_above_min_height( pn_locs, n_npks, pn_x, n_size, n_min_height );
219  maxim_remove_close_peaks( pn_locs, n_npks, pn_x, n_min_distance );
220  *n_npks = min( *n_npks, n_max_num );
221 }
222 
223 void maxim_peaks_above_min_height( int32_t *pn_locs, int32_t *n_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height )
231 {
232  int32_t i = 1, n_width;
233  *n_npks = 0;
234 
235  while (i < n_size-1){
236  if (pn_x[i] > n_min_height && pn_x[i] > pn_x[i-1]){ // find left edge of potential peaks
237  n_width = 1;
238  while (i+n_width < n_size && pn_x[i] == pn_x[i+n_width]) // find flat peaks
239  n_width++;
240  if (pn_x[i] > pn_x[i+n_width] && (*n_npks) < 15 ){ // find right edge of peaks
241  pn_locs[(*n_npks)++] = i;
242  // for flat peaks, peak location is left edge
243  i += n_width+1;
244  }
245  else
246  i += n_width;
247  }
248  else
249  i++;
250  }
251 }
252 
253 void maxim_remove_close_peaks(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_min_distance)
261 {
262 
263  int32_t i, j, n_old_npks, n_dist;
264 
265  /* Order peaks from large to small */
266  maxim_sort_indices_descend( pn_x, pn_locs, *pn_npks );
267 
268  for ( i = -1; i < *pn_npks; i++ ){
269  n_old_npks = *pn_npks;
270  *pn_npks = i+1;
271  for ( j = i+1; j < n_old_npks; j++ ){
272  n_dist = pn_locs[j] - ( i == -1 ? -1 : pn_locs[i] ); // lag-zero peak of autocorr is at index -1
273  if ( n_dist > n_min_distance || n_dist < -n_min_distance )
274  pn_locs[(*pn_npks)++] = pn_locs[j];
275  }
276  }
277 
278  // Resort indices int32_to ascending order
279  maxim_sort_ascend( pn_locs, *pn_npks );
280 }
281 
282 void maxim_sort_ascend(int32_t *pn_x, int32_t n_size)
283 
290 {
291  int32_t i, j, n_temp;
292  for (i = 1; i < n_size; i++) {
293  n_temp = pn_x[i];
294  for (j = i; j > 0 && n_temp < pn_x[j-1]; j--)
295  pn_x[j] = pn_x[j-1];
296  pn_x[j] = n_temp;
297  }
298 }
299 
300 void maxim_sort_indices_descend( int32_t *pn_x, int32_t *pn_indx, int32_t n_size)
308 {
309  int32_t i, j, n_temp;
310  for (i = 1; i < n_size; i++) {
311  n_temp = pn_indx[i];
312  for (j = i; j > 0 && pn_x[n_temp] > pn_x[pn_indx[j-1]]; j--)
313  pn_indx[j] = pn_indx[j-1];
314  pn_indx[j] = n_temp;
315  }
316 }
317 
318 
319 
320