algorithm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /** \file algorithm.cpp ******************************************************
  2. *
  3. * Project: MAXREFDES117#
  4. * Filename: algorithm.cpp
  5. * Description: This module calculates the heart rate/SpO2 level
  6. *
  7. *
  8. * --------------------------------------------------------------------
  9. *
  10. * This code follows the following naming conventions:
  11. *
  12. * char ch_pmod_value
  13. * char (array) s_pmod_s_string[16]
  14. * float f_pmod_value
  15. * int32_t n_pmod_value
  16. * int32_t (array) an_pmod_value[16]
  17. * int16_t w_pmod_value
  18. * int16_t (array) aw_pmod_value[16]
  19. * uint16_t uw_pmod_value
  20. * uint16_t (array) auw_pmod_value[16]
  21. * uint8_t uch_pmod_value
  22. * uint8_t (array) auch_pmod_buffer[16]
  23. * uint32_t un_pmod_value
  24. * int32_t * pn_pmod_value
  25. *
  26. * ------------------------------------------------------------------------- */
  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. #include "algorithm.h"
  60. const uint16_t auw_hamm[31]={ 41, 276, 512, 276, 41 }; //Hamm= long16(512* hamming(5)');
  61. //uch_spo2_table is computed as -45.060*ratioAverage* ratioAverage + 30.354 *ratioAverage + 94.845 ;
  62. const uint8_t uch_spo2_table[184]={ 95, 95, 95, 96, 96, 96, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 99, 99, 99, 99,
  63. 99, 99, 99, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  64. 100, 100, 100, 100, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 98, 98, 98, 97, 97,
  65. 97, 97, 96, 96, 96, 96, 95, 95, 95, 94, 94, 94, 93, 93, 93, 92, 92, 92, 91, 91,
  66. 90, 90, 89, 89, 89, 88, 88, 87, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, 81, 81,
  67. 80, 80, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, 70, 69, 69, 68, 67,
  68. 66, 66, 65, 64, 63, 62, 62, 61, 60, 59, 58, 57, 56, 56, 55, 54, 53, 52, 51, 50,
  69. 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 31, 30, 29,
  70. 28, 27, 26, 25, 23, 22, 21, 20, 19, 17, 16, 15, 14, 12, 11, 10, 9, 7, 6, 5,
  71. 3, 2, 1 } ;
  72. static int32_t an_dx[ BUFFER_SIZE-MA4_SIZE]; // delta
  73. static int32_t an_x[ BUFFER_SIZE]; //ir
  74. static int32_t an_y[ BUFFER_SIZE]; //red
  75. 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,
  76. int32_t *pn_heart_rate, int8_t *pch_hr_valid)
  77. /**
  78. * \brief Calculate the heart rate and SpO2 level
  79. * \par Details
  80. * By detecting peaks of PPG cycle and corresponding AC/DC of red/infra-red signal, the ratio for the SPO2 is computed.
  81. * Since this algorithm is aiming for Arm M0/M3. formaula for SPO2 did not achieve the accuracy due to register overflow.
  82. * Thus, accurate SPO2 is precalculated and save longo uch_spo2_table[] per each ratio.
  83. *
  84. * \param[in] *pun_ir_buffer - IR sensor data buffer
  85. * \param[in] n_ir_buffer_length - IR sensor data buffer length
  86. * \param[in] *pun_red_buffer - Red sensor data buffer
  87. * \param[out] *pn_spo2 - Calculated SpO2 value
  88. * \param[out] *pch_spo2_valid - 1 if the calculated SpO2 value is valid
  89. * \param[out] *pn_heart_rate - Calculated heart rate value
  90. * \param[out] *pch_hr_valid - 1 if the calculated heart rate value is valid
  91. *
  92. * \retval None
  93. */
  94. {
  95. uint32_t un_ir_mean ,un_only_once ;
  96. int32_t k ,n_i_ratio_count;
  97. int32_t i, s, m, n_exact_ir_valley_locs_count ,n_middle_idx;
  98. int32_t n_th1, n_npks,n_c_min;
  99. int32_t an_ir_valley_locs[15] ;
  100. int32_t an_exact_ir_valley_locs[15] ;
  101. int32_t an_dx_peak_locs[15] ;
  102. int32_t n_peak_interval_sum;
  103. int32_t n_y_ac, n_x_ac;
  104. int32_t n_spo2_calc;
  105. int32_t n_y_dc_max, n_x_dc_max;
  106. int32_t n_y_dc_max_idx, n_x_dc_max_idx;
  107. int32_t an_ratio[5],n_ratio_average;
  108. int32_t n_nume, n_denom ;
  109. // remove DC of ir signal
  110. un_ir_mean =0;
  111. for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ;
  112. un_ir_mean =un_ir_mean/n_ir_buffer_length ;
  113. for (k=0 ; k<n_ir_buffer_length ; k++ ) an_x[k] = pun_ir_buffer[k] - un_ir_mean ;
  114. // 4 pt Moving Average
  115. for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){
  116. n_denom= ( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3]);
  117. an_x[k]= n_denom/(int32_t)4;
  118. }
  119. // get difference of smoothed IR signal
  120. for( k=0; k<BUFFER_SIZE-MA4_SIZE-1; k++)
  121. an_dx[k]= (an_x[k+1]- an_x[k]);
  122. // 2-pt Moving Average to an_dx
  123. for(k=0; k< BUFFER_SIZE-MA4_SIZE-2; k++){
  124. an_dx[k] = ( an_dx[k]+an_dx[k+1])/2 ;
  125. }
  126. // hamming window
  127. // flip wave form so that we can detect valley with peak detector
  128. for ( i=0 ; i<BUFFER_SIZE-HAMMING_SIZE-MA4_SIZE-2 ;i++){
  129. s= 0;
  130. for( k=i; k<i+ HAMMING_SIZE ;k++){
  131. s -= an_dx[k] *auw_hamm[k-i] ;
  132. }
  133. an_dx[i]= s/ (int32_t)1146; // divide by sum of auw_hamm
  134. }
  135. n_th1=0; // threshold calculation
  136. for ( k=0 ; k<BUFFER_SIZE-HAMMING_SIZE ;k++){
  137. n_th1 += ((an_dx[k]>0)? an_dx[k] : ((int32_t)0-an_dx[k])) ;
  138. }
  139. n_th1= n_th1/ ( BUFFER_SIZE-HAMMING_SIZE);
  140. // peak location is acutally index for sharpest location of raw signal since we flipped the signal
  141. maxim_find_peaks( an_dx_peak_locs, &n_npks, an_dx, BUFFER_SIZE-HAMMING_SIZE, n_th1, 8, 5 );//peak_height, peak_distance, max_num_peaks
  142. n_peak_interval_sum =0;
  143. if (n_npks>=2){
  144. for (k=1; k<n_npks; k++)
  145. n_peak_interval_sum += (an_dx_peak_locs[k]-an_dx_peak_locs[k -1]);
  146. n_peak_interval_sum=n_peak_interval_sum/(n_npks-1);
  147. *pn_heart_rate=(int32_t)(6000/n_peak_interval_sum);// beats per minutes
  148. *pch_hr_valid = 1;
  149. }
  150. else {
  151. *pn_heart_rate = -999;
  152. *pch_hr_valid = 0;
  153. }
  154. for ( k=0 ; k<n_npks ;k++)
  155. an_ir_valley_locs[k]=an_dx_peak_locs[k]+HAMMING_SIZE/2;
  156. // raw value : RED(=y) and IR(=X)
  157. // we need to assess DC and AC value of ir and red PPG.
  158. for (k=0 ; k<n_ir_buffer_length ; k++ ) {
  159. an_x[k] = pun_ir_buffer[k] ;
  160. an_y[k] = pun_red_buffer[k] ;
  161. }
  162. // find precise min near an_ir_valley_locs
  163. n_exact_ir_valley_locs_count =0;
  164. for(k=0 ; k<n_npks ;k++){
  165. un_only_once =1;
  166. m=an_ir_valley_locs[k];
  167. n_c_min= 16777216;//2^24;
  168. if (m+5 < BUFFER_SIZE-HAMMING_SIZE && m-5 >0){
  169. for(i= m-5;i<m+5; i++)
  170. if (an_x[i]<n_c_min){
  171. if (un_only_once >0){
  172. un_only_once =0;
  173. }
  174. n_c_min= an_x[i] ;
  175. an_exact_ir_valley_locs[k]=i;
  176. }
  177. if (un_only_once ==0)
  178. n_exact_ir_valley_locs_count ++ ;
  179. }
  180. }
  181. if (n_exact_ir_valley_locs_count <2 ){
  182. *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range
  183. *pch_spo2_valid = 0;
  184. return;
  185. }
  186. // 4 pt MA
  187. for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){
  188. an_x[k]=( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3])/(int32_t)4;
  189. an_y[k]=( an_y[k]+an_y[k+1]+ an_y[k+2]+ an_y[k+3])/(int32_t)4;
  190. }
  191. //using an_exact_ir_valley_locs , find ir-red DC andir-red AC for SPO2 calibration ratio
  192. //finding AC/DC maximum of raw ir * red between two valley locations
  193. n_ratio_average =0;
  194. n_i_ratio_count =0;
  195. for(k=0; k< 5; k++) an_ratio[k]=0;
  196. for (k=0; k< n_exact_ir_valley_locs_count; k++){
  197. if (an_exact_ir_valley_locs[k] > BUFFER_SIZE ){
  198. *pn_spo2 = -999 ; // do not use SPO2 since valley loc is out of range
  199. *pch_spo2_valid = 0;
  200. return;
  201. }
  202. }
  203. // find max between two valley locations
  204. // and use ratio betwen AC compoent of Ir & Red and DC compoent of Ir & Red for SPO2
  205. for (k=0; k< n_exact_ir_valley_locs_count-1; k++){
  206. n_y_dc_max= -16777216 ;
  207. n_x_dc_max= - 16777216;
  208. if (an_exact_ir_valley_locs[k+1]-an_exact_ir_valley_locs[k] >10){
  209. for (i=an_exact_ir_valley_locs[k]; i< an_exact_ir_valley_locs[k+1]; i++){
  210. if (an_x[i]> n_x_dc_max) {n_x_dc_max =an_x[i];n_x_dc_max_idx =i; }
  211. if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i];n_y_dc_max_idx=i;}
  212. }
  213. n_y_ac= (an_y[an_exact_ir_valley_locs[k+1]] - an_y[an_exact_ir_valley_locs[k] ] )*(n_y_dc_max_idx -an_exact_ir_valley_locs[k]); //red
  214. n_y_ac= an_y[an_exact_ir_valley_locs[k]] + n_y_ac/ (an_exact_ir_valley_locs[k+1] - an_exact_ir_valley_locs[k]) ;
  215. n_y_ac= an_y[n_y_dc_max_idx] - n_y_ac; // subracting linear DC compoenents from raw
  216. n_x_ac= (an_x[an_exact_ir_valley_locs[k+1]] - an_x[an_exact_ir_valley_locs[k] ] )*(n_x_dc_max_idx -an_exact_ir_valley_locs[k]); // ir
  217. n_x_ac= an_x[an_exact_ir_valley_locs[k]] + n_x_ac/ (an_exact_ir_valley_locs[k+1] - an_exact_ir_valley_locs[k]);
  218. n_x_ac= an_x[n_y_dc_max_idx] - n_x_ac; // subracting linear DC compoenents from raw
  219. n_nume=( n_y_ac *n_x_dc_max)>>7 ; //prepare X100 to preserve floating value
  220. n_denom= ( n_x_ac *n_y_dc_max)>>7;
  221. if (n_denom>0 && n_i_ratio_count <5 && n_nume != 0)
  222. {
  223. an_ratio[n_i_ratio_count]= (n_nume*20)/n_denom ; //formular is ( n_y_ac *n_x_dc_max) / ( n_x_ac *n_y_dc_max) ;
  224. n_i_ratio_count++;
  225. }
  226. }
  227. }
  228. maxim_sort_ascend(an_ratio, n_i_ratio_count);
  229. n_middle_idx= n_i_ratio_count/2;
  230. if (n_middle_idx >1)
  231. n_ratio_average =( an_ratio[n_middle_idx-1] +an_ratio[n_middle_idx])/2; // use median
  232. else
  233. n_ratio_average = an_ratio[n_middle_idx ];
  234. if( n_ratio_average>2 && n_ratio_average <184){
  235. n_spo2_calc= uch_spo2_table[n_ratio_average] ;
  236. *pn_spo2 = n_spo2_calc ;
  237. *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
  238. }
  239. else{
  240. *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range
  241. *pch_spo2_valid = 0;
  242. }
  243. }
  244. void maxim_find_peaks(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height, int32_t n_min_distance, int32_t n_max_num)
  245. /**
  246. * \brief Find peaks
  247. * \par Details
  248. * Find at most MAX_NUM peaks above MIN_HEIGHT separated by at least MIN_DISTANCE
  249. *
  250. * \retval None
  251. */
  252. {
  253. maxim_peaks_above_min_height( pn_locs, pn_npks, pn_x, n_size, n_min_height );
  254. maxim_remove_close_peaks( pn_locs, pn_npks, pn_x, n_min_distance );
  255. *pn_npks = min( *pn_npks, n_max_num );
  256. }
  257. void maxim_peaks_above_min_height(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_size, int32_t n_min_height)
  258. /**
  259. * \brief Find peaks above n_min_height
  260. * \par Details
  261. * Find all peaks above MIN_HEIGHT
  262. *
  263. * \retval None
  264. */
  265. {
  266. int32_t i = 1, n_width;
  267. *pn_npks = 0;
  268. while (i < n_size-1){
  269. if (pn_x[i] > n_min_height && pn_x[i] > pn_x[i-1]){ // find left edge of potential peaks
  270. n_width = 1;
  271. while (i+n_width < n_size && pn_x[i] == pn_x[i+n_width]) // find flat peaks
  272. n_width++;
  273. if (pn_x[i] > pn_x[i+n_width] && (*pn_npks) < 15 ){ // find right edge of peaks
  274. pn_locs[(*pn_npks)++] = i;
  275. // for flat peaks, peak location is left edge
  276. i += n_width+1;
  277. }
  278. else
  279. i += n_width;
  280. }
  281. else
  282. i++;
  283. }
  284. }
  285. void maxim_remove_close_peaks(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_min_distance)
  286. /**
  287. * \brief Remove peaks
  288. * \par Details
  289. * Remove peaks separated by less than MIN_DISTANCE
  290. *
  291. * \retval None
  292. */
  293. {
  294. int32_t i, j, n_old_npks, n_dist;
  295. /* Order peaks from large to small */
  296. maxim_sort_indices_descend( pn_x, pn_locs, *pn_npks );
  297. for ( i = -1; i < *pn_npks; i++ ){
  298. n_old_npks = *pn_npks;
  299. *pn_npks = i+1;
  300. for ( j = i+1; j < n_old_npks; j++ ){
  301. n_dist = pn_locs[j] - ( i == -1 ? -1 : pn_locs[i] ); // lag-zero peak of autocorr is at index -1
  302. if ( n_dist > n_min_distance || n_dist < -n_min_distance )
  303. pn_locs[(*pn_npks)++] = pn_locs[j];
  304. }
  305. }
  306. // Resort indices longo ascending order
  307. maxim_sort_ascend( pn_locs, *pn_npks );
  308. }
  309. void maxim_sort_ascend(int32_t *pn_x,int32_t n_size)
  310. /**
  311. * \brief Sort array
  312. * \par Details
  313. * Sort array in ascending order (insertion sort algorithm)
  314. *
  315. * \retval None
  316. */
  317. {
  318. int32_t i, j, n_temp;
  319. for (i = 1; i < n_size; i++) {
  320. n_temp = pn_x[i];
  321. for (j = i; j > 0 && n_temp < pn_x[j-1]; j--)
  322. pn_x[j] = pn_x[j-1];
  323. pn_x[j] = n_temp;
  324. }
  325. }
  326. void maxim_sort_indices_descend(int32_t *pn_x, int32_t *pn_indx, int32_t n_size)
  327. /**
  328. * \brief Sort indices
  329. * \par Details
  330. * Sort indices according to descending order (insertion sort algorithm)
  331. *
  332. * \retval None
  333. */
  334. {
  335. int32_t i, j, n_temp;
  336. for (i = 1; i < n_size; i++) {
  337. n_temp = pn_indx[i];
  338. for (j = i; j > 0 && pn_x[n_temp] > pn_x[pn_indx[j-1]]; j--)
  339. pn_indx[j] = pn_indx[j-1];
  340. pn_indx[j] = n_temp;
  341. }
  342. }