algorithm1.c 14 KB

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