Paul Jeon
/
RD117_MBED_demo
MAX32625PICO supported
algorithm/algorithm.cpp@2:560e76e77544, 2016-04-21 (annotated)
- Committer:
- MaximGordon
- Date:
- Thu Apr 21 18:25:34 2016 +0000
- Revision:
- 2:560e76e77544
- Parent:
- 1:e88f22c6c1b0
- Child:
- 3:7c0fb55eb3ff
updated variable names
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MaximGordon | 1:e88f22c6c1b0 | 1 | /** \file algorithm.cpp ****************************************************** |
MaximGordon | 0:346a7fa07998 | 2 | * |
MaximGordon | 0:346a7fa07998 | 3 | * Project: MAXREFDES117# |
MaximGordon | 1:e88f22c6c1b0 | 4 | * Filename: algorithm.cpp |
MaximGordon | 0:346a7fa07998 | 5 | * Description: This module calculates the heart rate/SpO2 level |
MaximGordon | 0:346a7fa07998 | 6 | * |
MaximGordon | 0:346a7fa07998 | 7 | * Revision History: |
MaximGordon | 0:346a7fa07998 | 8 | *\n 1-18-2016 Rev 01.00 SK Initial release. |
MaximGordon | 0:346a7fa07998 | 9 | *\n |
MaximGordon | 0:346a7fa07998 | 10 | * |
MaximGordon | 0:346a7fa07998 | 11 | * -------------------------------------------------------------------- |
MaximGordon | 0:346a7fa07998 | 12 | * |
MaximGordon | 0:346a7fa07998 | 13 | * This code follows the following naming conventions: |
MaximGordon | 0:346a7fa07998 | 14 | * |
MaximGordon | 0:346a7fa07998 | 15 | *\n char ch_pmod_value |
MaximGordon | 0:346a7fa07998 | 16 | *\n char (array) s_pmod_s_string[16] |
MaximGordon | 0:346a7fa07998 | 17 | *\n float f_pmod_value |
MaximGordon | 0:346a7fa07998 | 18 | *\n int32_t n_pmod_value |
MaximGordon | 0:346a7fa07998 | 19 | *\n int32_t (array) an_pmod_value[16] |
MaximGordon | 0:346a7fa07998 | 20 | *\n int16_t w_pmod_value |
MaximGordon | 0:346a7fa07998 | 21 | *\n int16_t (array) aw_pmod_value[16] |
MaximGordon | 0:346a7fa07998 | 22 | *\n uint16_t uw_pmod_value |
MaximGordon | 0:346a7fa07998 | 23 | *\n uint16_t (array) auw_pmod_value[16] |
MaximGordon | 0:346a7fa07998 | 24 | *\n uint8_t uch_pmod_value |
MaximGordon | 0:346a7fa07998 | 25 | *\n uint8_t (array) auch_pmod_buffer[16] |
MaximGordon | 0:346a7fa07998 | 26 | *\n uint32_t un_pmod_value |
MaximGordon | 0:346a7fa07998 | 27 | *\n int32_t * pn_pmod_value |
MaximGordon | 0:346a7fa07998 | 28 | * |
MaximGordon | 0:346a7fa07998 | 29 | * ------------------------------------------------------------------------- */ |
MaximGordon | 0:346a7fa07998 | 30 | /******************************************************************************* |
MaximGordon | 0:346a7fa07998 | 31 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
MaximGordon | 0:346a7fa07998 | 32 | * |
MaximGordon | 0:346a7fa07998 | 33 | * Permission is hereby granted, free of charge, to any person obtaining a |
MaximGordon | 0:346a7fa07998 | 34 | * copy of this software and associated documentation files (the "Software"), |
MaximGordon | 0:346a7fa07998 | 35 | * to deal in the Software without restriction, including without limitation |
MaximGordon | 0:346a7fa07998 | 36 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MaximGordon | 0:346a7fa07998 | 37 | * and/or sell copies of the Software, and to permit persons to whom the |
MaximGordon | 0:346a7fa07998 | 38 | * Software is furnished to do so, subject to the following conditions: |
MaximGordon | 0:346a7fa07998 | 39 | * |
MaximGordon | 0:346a7fa07998 | 40 | * The above copyright notice and this permission notice shall be included |
MaximGordon | 0:346a7fa07998 | 41 | * in all copies or substantial portions of the Software. |
MaximGordon | 0:346a7fa07998 | 42 | * |
MaximGordon | 0:346a7fa07998 | 43 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
MaximGordon | 0:346a7fa07998 | 44 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
MaximGordon | 0:346a7fa07998 | 45 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
MaximGordon | 0:346a7fa07998 | 46 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
MaximGordon | 0:346a7fa07998 | 47 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
MaximGordon | 0:346a7fa07998 | 48 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
MaximGordon | 0:346a7fa07998 | 49 | * OTHER DEALINGS IN THE SOFTWARE. |
MaximGordon | 0:346a7fa07998 | 50 | * |
MaximGordon | 0:346a7fa07998 | 51 | * Except as contained in this notice, the name of Maxim Integrated |
MaximGordon | 0:346a7fa07998 | 52 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
MaximGordon | 0:346a7fa07998 | 53 | * Products, Inc. Branding Policy. |
MaximGordon | 0:346a7fa07998 | 54 | * |
MaximGordon | 0:346a7fa07998 | 55 | * The mere transfer of this software does not imply any licenses |
MaximGordon | 0:346a7fa07998 | 56 | * of trade secrets, proprietary technology, copyrights, patents, |
MaximGordon | 0:346a7fa07998 | 57 | * trademarks, maskwork rights, or any other form of intellectual |
MaximGordon | 0:346a7fa07998 | 58 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
MaximGordon | 0:346a7fa07998 | 59 | * ownership rights. |
MaximGordon | 0:346a7fa07998 | 60 | ******************************************************************************* |
MaximGordon | 0:346a7fa07998 | 61 | */ |
MaximGordon | 0:346a7fa07998 | 62 | #include "algorithm.h" |
MaximGordon | 0:346a7fa07998 | 63 | #include "mbed.h" |
MaximGordon | 0:346a7fa07998 | 64 | |
MaximGordon | 0:346a7fa07998 | 65 | void maxim_heart_rate_and_oxygen_saturation(uint32_t *un_ir_buffer , int32_t n_ir_buffer_length, uint32_t *un_red_buffer , int32_t *n_spo2, int8_t *ch_spo2_valid , |
MaximGordon | 0:346a7fa07998 | 66 | int32_t *n_heart_rate , int8_t *ch_hr_valid) |
MaximGordon | 0:346a7fa07998 | 67 | /** |
MaximGordon | 0:346a7fa07998 | 68 | * \brief Calculate the heart rate and SpO2 level |
MaximGordon | 0:346a7fa07998 | 69 | * \par Details |
MaximGordon | 0:346a7fa07998 | 70 | * By detecting peaks of PPG cycle and corresponding AC/DC of red/infra-red signal, the ratio for the SPO2 is computed. |
MaximGordon | 0:346a7fa07998 | 71 | * Since this algorithm is aiming for Arm M0/M3. formaula for SPO2 did not achieve the accuracy due to register overflow. |
MaximGordon | 0:346a7fa07998 | 72 | * Thus, accurate SPO2 is precalculated and save longo uch_spo2_table[] per each ratio. |
MaximGordon | 0:346a7fa07998 | 73 | * |
MaximGordon | 0:346a7fa07998 | 74 | * \param[in] *un_ir_buffer - IR sensor data buffer |
MaximGordon | 0:346a7fa07998 | 75 | * \param[in] n_ir_buffer_length - IR sensor data buffer length |
MaximGordon | 0:346a7fa07998 | 76 | * \param[in] *un_red_buffer - Red sensor data buffer |
MaximGordon | 0:346a7fa07998 | 77 | * \param[out] *n_spo2 - Calculated SpO2 value |
MaximGordon | 0:346a7fa07998 | 78 | * \param[out] *ch_spo2_valid - 1 if the calculated SpO2 value is valid |
MaximGordon | 0:346a7fa07998 | 79 | * \param[out] *n_heart_rate - Calculated heart rate value |
MaximGordon | 0:346a7fa07998 | 80 | * \param[out] *ch_hr_valid - 1 if the calculated heart rate value is valid |
MaximGordon | 0:346a7fa07998 | 81 | * |
MaximGordon | 0:346a7fa07998 | 82 | * \retval None |
MaximGordon | 0:346a7fa07998 | 83 | */ |
MaximGordon | 0:346a7fa07998 | 84 | { |
MaximGordon | 0:346a7fa07998 | 85 | |
MaximGordon | 0:346a7fa07998 | 86 | |
MaximGordon | 2:560e76e77544 | 87 | uint32_t un_ir_mean ,un_only_once ; |
MaximGordon | 2:560e76e77544 | 88 | int32_t k ,n_i_ratio_count; |
MaximGordon | 2:560e76e77544 | 89 | int32_t i,s ,m, n_exact_ir_valley_locs_count ,n_middle_idx; |
MaximGordon | 2:560e76e77544 | 90 | int32_t n_th1, n_npks,n_c_min; |
MaximGordon | 2:560e76e77544 | 91 | int32_t n_ir_valley_locs[15] ; |
MaximGordon | 2:560e76e77544 | 92 | int32_t n_exact_ir_valley_locs[15] ; |
MaximGordon | 2:560e76e77544 | 93 | int32_t n_dx_peak_locs[15] ; |
MaximGordon | 2:560e76e77544 | 94 | int32_t n_peak_interval_sum; |
MaximGordon | 0:346a7fa07998 | 95 | |
MaximGordon | 2:560e76e77544 | 96 | int32_t n_y_ac, n_x_ac; |
MaximGordon | 2:560e76e77544 | 97 | int32_t n_spo2_calc; |
MaximGordon | 2:560e76e77544 | 98 | int32_t n_y_dc_max, n_x_dc_max; |
MaximGordon | 2:560e76e77544 | 99 | int32_t n_y_dc_max_idx, n_x_dc_max_idx; |
MaximGordon | 2:560e76e77544 | 100 | int32_t n_ratio[5],n_ratio_average; |
MaximGordon | 2:560e76e77544 | 101 | int32_t n_nume, n_denom ; |
MaximGordon | 0:346a7fa07998 | 102 | // remove DC of ir signal |
MaximGordon | 2:560e76e77544 | 103 | un_ir_mean =0; |
MaximGordon | 2:560e76e77544 | 104 | for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += un_ir_buffer[k] ; |
MaximGordon | 2:560e76e77544 | 105 | un_ir_mean =un_ir_mean/n_ir_buffer_length ; |
MaximGordon | 2:560e76e77544 | 106 | for (k=0 ; k<n_ir_buffer_length ; k++ ) n_x[k] = un_ir_buffer[k] - un_ir_mean ; |
MaximGordon | 0:346a7fa07998 | 107 | |
MaximGordon | 0:346a7fa07998 | 108 | // 4 pt Moving Average |
MaximGordon | 0:346a7fa07998 | 109 | for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){ |
MaximGordon | 2:560e76e77544 | 110 | n_denom= ( n_x[k]+n_x[k+1]+ n_x[k+2]+ n_x[k+3]); |
MaximGordon | 2:560e76e77544 | 111 | n_x[k]= n_denom/(int32_t)4; |
MaximGordon | 0:346a7fa07998 | 112 | } |
MaximGordon | 0:346a7fa07998 | 113 | |
MaximGordon | 0:346a7fa07998 | 114 | // get difference of smoothed IR signal |
MaximGordon | 0:346a7fa07998 | 115 | |
MaximGordon | 0:346a7fa07998 | 116 | for( k=0; k<BUFFER_SIZE-MA4_SIZE-1; k++) |
MaximGordon | 0:346a7fa07998 | 117 | n_dx[k]= (n_x[k+1]- n_x[k]); |
MaximGordon | 0:346a7fa07998 | 118 | |
MaximGordon | 0:346a7fa07998 | 119 | // 2-pt Moving Average to n_dx |
MaximGordon | 0:346a7fa07998 | 120 | for(k=0; k< BUFFER_SIZE-MA4_SIZE-2; k++){ |
MaximGordon | 0:346a7fa07998 | 121 | n_dx[k] = ( n_dx[k]+n_dx[k+1])/2 ; |
MaximGordon | 0:346a7fa07998 | 122 | } |
MaximGordon | 0:346a7fa07998 | 123 | |
MaximGordon | 0:346a7fa07998 | 124 | // hamming window |
MaximGordon | 0:346a7fa07998 | 125 | // flip wave form so that we can detect valley with peak detector |
MaximGordon | 0:346a7fa07998 | 126 | for ( i=0 ; i<BUFFER_SIZE-HAMMING_SIZE-MA4_SIZE-2 ;i++){ |
MaximGordon | 0:346a7fa07998 | 127 | s= 0; |
MaximGordon | 0:346a7fa07998 | 128 | for( k=i; k<i+ HAMMING_SIZE ;k++){ |
MaximGordon | 0:346a7fa07998 | 129 | s -= n_dx[k] *uw_hamm[k-i] ; |
MaximGordon | 0:346a7fa07998 | 130 | } |
MaximGordon | 0:346a7fa07998 | 131 | n_dx[i]= s/ (int32_t)1146; // divide by sum of uw_hamm |
MaximGordon | 0:346a7fa07998 | 132 | } |
MaximGordon | 0:346a7fa07998 | 133 | |
MaximGordon | 0:346a7fa07998 | 134 | |
MaximGordon | 2:560e76e77544 | 135 | n_th1=0; // threshold calculation |
MaximGordon | 0:346a7fa07998 | 136 | for ( k=0 ; k<BUFFER_SIZE-HAMMING_SIZE ;k++){ |
MaximGordon | 2:560e76e77544 | 137 | n_th1 += ((n_dx[k]>0)? n_dx[k] : ((int32_t)0-n_dx[k])) ; |
MaximGordon | 0:346a7fa07998 | 138 | } |
MaximGordon | 2:560e76e77544 | 139 | n_th1= n_th1/ ( BUFFER_SIZE-HAMMING_SIZE); |
MaximGordon | 0:346a7fa07998 | 140 | // peak location is acutally index for sharpest location of raw signal since we flipped the signal |
MaximGordon | 2:560e76e77544 | 141 | maxim_find_peaks( n_dx_peak_locs, &n_npks, n_dx, BUFFER_SIZE-HAMMING_SIZE, n_th1, 8, 5 );//peak_height, peak_distance, max_num_peaks |
MaximGordon | 0:346a7fa07998 | 142 | |
MaximGordon | 2:560e76e77544 | 143 | n_peak_interval_sum =0; |
MaximGordon | 0:346a7fa07998 | 144 | if (n_npks>=2){ |
MaximGordon | 0:346a7fa07998 | 145 | for (k=1; k<n_npks; k++) |
MaximGordon | 2:560e76e77544 | 146 | n_peak_interval_sum += (n_dx_peak_locs[k] -n_dx_peak_locs[k -1] ) ; |
MaximGordon | 2:560e76e77544 | 147 | n_peak_interval_sum =n_peak_interval_sum/(n_npks-1); |
MaximGordon | 2:560e76e77544 | 148 | *n_heart_rate =(int32_t)( 6000/ n_peak_interval_sum );// beats per minutes |
MaximGordon | 0:346a7fa07998 | 149 | //prlongf(">>> *n_heart_rate= %d \n", *n_heart_rate) ; |
MaximGordon | 0:346a7fa07998 | 150 | *ch_hr_valid = 1; |
MaximGordon | 0:346a7fa07998 | 151 | } |
MaximGordon | 0:346a7fa07998 | 152 | else { |
MaximGordon | 0:346a7fa07998 | 153 | *n_heart_rate = -999; |
MaximGordon | 0:346a7fa07998 | 154 | *ch_hr_valid = 0; |
MaximGordon | 0:346a7fa07998 | 155 | } |
MaximGordon | 0:346a7fa07998 | 156 | |
MaximGordon | 0:346a7fa07998 | 157 | for ( k=0 ; k<n_npks ;k++) |
MaximGordon | 2:560e76e77544 | 158 | n_ir_valley_locs[k]= n_dx_peak_locs[k] +HAMMING_SIZE /2; |
MaximGordon | 0:346a7fa07998 | 159 | |
MaximGordon | 0:346a7fa07998 | 160 | |
MaximGordon | 0:346a7fa07998 | 161 | // raw value : RED(=y) and IR(=X) |
MaximGordon | 0:346a7fa07998 | 162 | // we need to assess DC and AC value of ir and red PPG. |
MaximGordon | 0:346a7fa07998 | 163 | for (k=0 ; k<n_ir_buffer_length ; k++ ) { |
MaximGordon | 0:346a7fa07998 | 164 | n_x[k] = un_ir_buffer[k] ; |
MaximGordon | 0:346a7fa07998 | 165 | n_y[k] = un_red_buffer[k] ; |
MaximGordon | 0:346a7fa07998 | 166 | } |
MaximGordon | 0:346a7fa07998 | 167 | |
MaximGordon | 2:560e76e77544 | 168 | // find precise min near n_ir_valley_locs |
MaximGordon | 2:560e76e77544 | 169 | n_exact_ir_valley_locs_count =0; |
MaximGordon | 0:346a7fa07998 | 170 | for ( k=0 ; k<n_npks ;k++){ |
MaximGordon | 2:560e76e77544 | 171 | un_only_once =1; |
MaximGordon | 2:560e76e77544 | 172 | m=n_ir_valley_locs[k]; |
MaximGordon | 2:560e76e77544 | 173 | n_c_min= 16777216;//2^24; |
MaximGordon | 0:346a7fa07998 | 174 | if (m+5 < BUFFER_SIZE-HAMMING_SIZE && m-5 >0){ |
MaximGordon | 0:346a7fa07998 | 175 | |
MaximGordon | 0:346a7fa07998 | 176 | for(i= m-5;i<m+5; i++) |
MaximGordon | 2:560e76e77544 | 177 | if (n_x[i]<n_c_min){ |
MaximGordon | 2:560e76e77544 | 178 | if (un_only_once >0){ |
MaximGordon | 2:560e76e77544 | 179 | un_only_once =0; |
MaximGordon | 0:346a7fa07998 | 180 | } |
MaximGordon | 2:560e76e77544 | 181 | n_c_min= n_x[i] ; |
MaximGordon | 2:560e76e77544 | 182 | n_exact_ir_valley_locs[k]=i; |
MaximGordon | 0:346a7fa07998 | 183 | } |
MaximGordon | 2:560e76e77544 | 184 | if (un_only_once ==0) n_exact_ir_valley_locs_count ++ ; |
MaximGordon | 0:346a7fa07998 | 185 | } |
MaximGordon | 0:346a7fa07998 | 186 | } |
MaximGordon | 2:560e76e77544 | 187 | if (n_exact_ir_valley_locs_count <2 ){ |
MaximGordon | 0:346a7fa07998 | 188 | *n_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range |
MaximGordon | 0:346a7fa07998 | 189 | *ch_spo2_valid = 0; |
MaximGordon | 0:346a7fa07998 | 190 | return; |
MaximGordon | 0:346a7fa07998 | 191 | } |
MaximGordon | 0:346a7fa07998 | 192 | // 4 pt MA |
MaximGordon | 0:346a7fa07998 | 193 | for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){ |
MaximGordon | 0:346a7fa07998 | 194 | n_x[k]=( n_x[k]+n_x[k+1]+ n_x[k+2]+ n_x[k+3])/(int32_t)4; |
MaximGordon | 0:346a7fa07998 | 195 | n_y[k]=( n_y[k]+n_y[k+1]+ n_y[k+2]+ n_y[k+3])/(int32_t)4; |
MaximGordon | 0:346a7fa07998 | 196 | |
MaximGordon | 0:346a7fa07998 | 197 | } |
MaximGordon | 0:346a7fa07998 | 198 | |
MaximGordon | 2:560e76e77544 | 199 | //using n_exact_ir_valley_locs , find ir-red DC andir-red AC for SPO2 calibration ratio |
MaximGordon | 0:346a7fa07998 | 200 | //finding AC/DC maximum of raw ir * red between two valley locations |
MaximGordon | 2:560e76e77544 | 201 | n_ratio_average =0; |
MaximGordon | 2:560e76e77544 | 202 | n_i_ratio_count = 0; |
MaximGordon | 0:346a7fa07998 | 203 | |
MaximGordon | 2:560e76e77544 | 204 | for(k=0; k< 5; k++) n_ratio[k]=0; |
MaximGordon | 2:560e76e77544 | 205 | for (k=0; k< n_exact_ir_valley_locs_count; k++){ |
MaximGordon | 2:560e76e77544 | 206 | if (n_exact_ir_valley_locs[k] > BUFFER_SIZE ){ |
MaximGordon | 0:346a7fa07998 | 207 | *n_spo2 = -999 ; // do not use SPO2 since valley loc is out of range |
MaximGordon | 0:346a7fa07998 | 208 | *ch_spo2_valid = 0; |
MaximGordon | 0:346a7fa07998 | 209 | return; |
MaximGordon | 0:346a7fa07998 | 210 | } |
MaximGordon | 0:346a7fa07998 | 211 | } |
MaximGordon | 0:346a7fa07998 | 212 | // find max between two valley locations |
MaximGordon | 0:346a7fa07998 | 213 | // and use ratio betwen AC compoent of Ir & Red and DC compoent of Ir & Red for SPO2 |
MaximGordon | 0:346a7fa07998 | 214 | |
MaximGordon | 2:560e76e77544 | 215 | for (k=0; k< n_exact_ir_valley_locs_count-1; k++){ |
MaximGordon | 2:560e76e77544 | 216 | n_y_dc_max= -16777216 ; |
MaximGordon | 2:560e76e77544 | 217 | n_x_dc_max= - 16777216; |
MaximGordon | 2:560e76e77544 | 218 | // printf("range=%d: %d\n ", n_exact_ir_valley_locs[k], n_exact_ir_valley_locs[k+1]); |
MaximGordon | 2:560e76e77544 | 219 | if (n_exact_ir_valley_locs[k+1]-n_exact_ir_valley_locs[k] >10){ |
MaximGordon | 2:560e76e77544 | 220 | for (i=n_exact_ir_valley_locs[k]; i< n_exact_ir_valley_locs[k+1]; i++){ |
MaximGordon | 0:346a7fa07998 | 221 | |
MaximGordon | 2:560e76e77544 | 222 | if (n_x[i]> n_x_dc_max) {n_x_dc_max =n_x[i];n_x_dc_max_idx =i; } |
MaximGordon | 2:560e76e77544 | 223 | if (n_y[i]> n_y_dc_max) {n_y_dc_max =n_y[i];n_y_dc_max_idx=i;} |
MaximGordon | 0:346a7fa07998 | 224 | } |
MaximGordon | 2:560e76e77544 | 225 | n_y_ac= (n_y[n_exact_ir_valley_locs[k+1]] - n_y[n_exact_ir_valley_locs[k] ] )*(n_y_dc_max_idx -n_exact_ir_valley_locs[k]); //red |
MaximGordon | 2:560e76e77544 | 226 | n_y_ac= n_y[n_exact_ir_valley_locs[k]] + n_y_ac/ (n_exact_ir_valley_locs[k+1] - n_exact_ir_valley_locs[k]) ; |
MaximGordon | 0:346a7fa07998 | 227 | |
MaximGordon | 0:346a7fa07998 | 228 | |
MaximGordon | 2:560e76e77544 | 229 | n_y_ac= n_y[n_y_dc_max_idx] - n_y_ac; // subracting linear DC compoenents from raw |
MaximGordon | 2:560e76e77544 | 230 | n_x_ac= (n_x[n_exact_ir_valley_locs[k+1]] - n_x[n_exact_ir_valley_locs[k] ] )*(n_x_dc_max_idx -n_exact_ir_valley_locs[k]); // ir |
MaximGordon | 2:560e76e77544 | 231 | n_x_ac= n_x[n_exact_ir_valley_locs[k]] + n_x_ac/ (n_exact_ir_valley_locs[k+1] - n_exact_ir_valley_locs[k]); |
MaximGordon | 2:560e76e77544 | 232 | n_x_ac= n_x[n_y_dc_max_idx] - n_x_ac; // subracting linear DC compoenents from raw |
MaximGordon | 2:560e76e77544 | 233 | n_nume=( n_y_ac *n_x_dc_max)>>7 ; //prepare X100 to preserve floating value |
MaximGordon | 2:560e76e77544 | 234 | n_denom= ( n_x_ac *n_y_dc_max)>>7; |
MaximGordon | 2:560e76e77544 | 235 | if (n_denom>0 && n_i_ratio_count <5 && n_nume != 0) |
MaximGordon | 0:346a7fa07998 | 236 | { |
MaximGordon | 2:560e76e77544 | 237 | n_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) ; |
MaximGordon | 2:560e76e77544 | 238 | n_i_ratio_count++; |
MaximGordon | 0:346a7fa07998 | 239 | } |
MaximGordon | 0:346a7fa07998 | 240 | } |
MaximGordon | 0:346a7fa07998 | 241 | |
MaximGordon | 2:560e76e77544 | 242 | // prlongf("n_ratio[%d]= %d n_exact_ir_valley_locs[k] =%d , n_exact_ir_valley_locs[%d] =%d \n",k, n_ratio[k] ,n_exact_ir_valley_locs[k] ,k+1, n_exact_ir_valley_locs[k+1] ) ; |
MaximGordon | 2:560e76e77544 | 243 | // prlongf("n_nume= %d ,n_denom= %d n_y_ac = %d, n_x_dc_max = %d, n_x_ac= %d, n_y_dc_max = %d\n",n_nume, n_denom, n_y_ac ,n_x_dc_max ,n_x_ac ,n_y_dc_max ); |
MaximGordon | 0:346a7fa07998 | 244 | |
MaximGordon | 0:346a7fa07998 | 245 | } |
MaximGordon | 0:346a7fa07998 | 246 | |
MaximGordon | 2:560e76e77544 | 247 | maxim_sort_ascend(n_ratio, n_i_ratio_count); |
MaximGordon | 2:560e76e77544 | 248 | n_middle_idx= n_i_ratio_count/2; |
MaximGordon | 0:346a7fa07998 | 249 | |
MaximGordon | 2:560e76e77544 | 250 | if (n_middle_idx >1) |
MaximGordon | 2:560e76e77544 | 251 | n_ratio_average =( n_ratio[n_middle_idx-1] +n_ratio[n_middle_idx])/2; // use median |
MaximGordon | 0:346a7fa07998 | 252 | else |
MaximGordon | 2:560e76e77544 | 253 | n_ratio_average = n_ratio[n_middle_idx ]; |
MaximGordon | 0:346a7fa07998 | 254 | |
MaximGordon | 2:560e76e77544 | 255 | if( n_ratio_average>2 && n_ratio_average <184){ |
MaximGordon | 2:560e76e77544 | 256 | n_spo2_calc= uch_spo2_table[n_ratio_average] ; |
MaximGordon | 2:560e76e77544 | 257 | *n_spo2 = n_spo2_calc ; |
MaximGordon | 2:560e76e77544 | 258 | *ch_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 |
MaximGordon | 0:346a7fa07998 | 259 | } |
MaximGordon | 0:346a7fa07998 | 260 | else{ |
MaximGordon | 0:346a7fa07998 | 261 | *n_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range |
MaximGordon | 0:346a7fa07998 | 262 | *ch_spo2_valid = 0; |
MaximGordon | 0:346a7fa07998 | 263 | } |
MaximGordon | 0:346a7fa07998 | 264 | |
MaximGordon | 0:346a7fa07998 | 265 | |
MaximGordon | 0:346a7fa07998 | 266 | } |
MaximGordon | 0:346a7fa07998 | 267 | |
MaximGordon | 0:346a7fa07998 | 268 | |
MaximGordon | 0:346a7fa07998 | 269 | void maxim_find_peaks( int32_t *n_locs, int32_t *n_npks, int32_t *n_x, int32_t n_size, int32_t n_min_height, int32_t n_min_distance, int32_t n_max_num ) |
MaximGordon | 0:346a7fa07998 | 270 | /** |
MaximGordon | 0:346a7fa07998 | 271 | * \brief Find peaks |
MaximGordon | 0:346a7fa07998 | 272 | * \par Details |
MaximGordon | 0:346a7fa07998 | 273 | * Find at most MAX_NUM peaks above MIN_HEIGHT separated by at least MIN_DISTANCE |
MaximGordon | 0:346a7fa07998 | 274 | * |
MaximGordon | 0:346a7fa07998 | 275 | * \retval None |
MaximGordon | 0:346a7fa07998 | 276 | */ |
MaximGordon | 0:346a7fa07998 | 277 | { |
MaximGordon | 0:346a7fa07998 | 278 | maxim_peaks_above_min_height( n_locs, n_npks, n_x, n_size, n_min_height ); |
MaximGordon | 0:346a7fa07998 | 279 | maxim_remove_close_peaks( n_locs, n_npks, n_x, n_min_distance ); |
MaximGordon | 0:346a7fa07998 | 280 | *n_npks = min( *n_npks, n_max_num ); |
MaximGordon | 0:346a7fa07998 | 281 | } |
MaximGordon | 0:346a7fa07998 | 282 | |
MaximGordon | 0:346a7fa07998 | 283 | void maxim_peaks_above_min_height( int32_t *n_locs, int32_t *n_npks, int32_t *n_x, int32_t n_size, int32_t n_min_height ) |
MaximGordon | 0:346a7fa07998 | 284 | /** |
MaximGordon | 0:346a7fa07998 | 285 | * \brief Find peaks above n_min_height |
MaximGordon | 0:346a7fa07998 | 286 | * \par Details |
MaximGordon | 0:346a7fa07998 | 287 | * Find all peaks above MIN_HEIGHT |
MaximGordon | 0:346a7fa07998 | 288 | * |
MaximGordon | 0:346a7fa07998 | 289 | * \retval None |
MaximGordon | 0:346a7fa07998 | 290 | */ |
MaximGordon | 0:346a7fa07998 | 291 | { |
MaximGordon | 2:560e76e77544 | 292 | int32_t i = 1, n_width; |
MaximGordon | 0:346a7fa07998 | 293 | *n_npks = 0; |
MaximGordon | 0:346a7fa07998 | 294 | |
MaximGordon | 0:346a7fa07998 | 295 | while (i < n_size-1){ |
MaximGordon | 0:346a7fa07998 | 296 | if (n_x[i] > n_min_height && n_x[i] > n_x[i-1]){ // find left edge of potential peaks |
MaximGordon | 2:560e76e77544 | 297 | n_width = 1; |
MaximGordon | 2:560e76e77544 | 298 | while (i+n_width < n_size && n_x[i] == n_x[i+n_width]) // find flat peaks |
MaximGordon | 2:560e76e77544 | 299 | n_width++; |
MaximGordon | 2:560e76e77544 | 300 | if (n_x[i] > n_x[i+n_width] && (*n_npks) < 15 ){ // find right edge of peaks |
MaximGordon | 0:346a7fa07998 | 301 | n_locs[(*n_npks)++] = i; |
MaximGordon | 0:346a7fa07998 | 302 | // for flat peaks, peak location is left edge |
MaximGordon | 2:560e76e77544 | 303 | i += n_width+1; |
MaximGordon | 0:346a7fa07998 | 304 | } |
MaximGordon | 0:346a7fa07998 | 305 | else |
MaximGordon | 2:560e76e77544 | 306 | i += n_width; |
MaximGordon | 0:346a7fa07998 | 307 | } |
MaximGordon | 0:346a7fa07998 | 308 | else |
MaximGordon | 0:346a7fa07998 | 309 | i++; |
MaximGordon | 0:346a7fa07998 | 310 | } |
MaximGordon | 0:346a7fa07998 | 311 | } |
MaximGordon | 0:346a7fa07998 | 312 | |
MaximGordon | 0:346a7fa07998 | 313 | |
MaximGordon | 0:346a7fa07998 | 314 | void maxim_remove_close_peaks( int32_t *n_locs, int32_t *n_npks, int32_t *n_x, int32_t n_min_distance ) |
MaximGordon | 0:346a7fa07998 | 315 | /** |
MaximGordon | 0:346a7fa07998 | 316 | * \brief Remove peaks |
MaximGordon | 0:346a7fa07998 | 317 | * \par Details |
MaximGordon | 0:346a7fa07998 | 318 | * Remove peaks separated by less than MIN_DISTANCE |
MaximGordon | 0:346a7fa07998 | 319 | * |
MaximGordon | 0:346a7fa07998 | 320 | * \retval None |
MaximGordon | 0:346a7fa07998 | 321 | */ |
MaximGordon | 0:346a7fa07998 | 322 | { |
MaximGordon | 0:346a7fa07998 | 323 | |
MaximGordon | 2:560e76e77544 | 324 | int32_t i, j, n_old_npks, n_dist; |
MaximGordon | 0:346a7fa07998 | 325 | |
MaximGordon | 0:346a7fa07998 | 326 | /* Order peaks from large to small */ |
MaximGordon | 0:346a7fa07998 | 327 | maxim_sort_indices_descend( n_x, n_locs, *n_npks ); |
MaximGordon | 0:346a7fa07998 | 328 | |
MaximGordon | 0:346a7fa07998 | 329 | for ( i = -1; i < *n_npks; i++ ){ |
MaximGordon | 2:560e76e77544 | 330 | n_old_npks = *n_npks; |
MaximGordon | 0:346a7fa07998 | 331 | *n_npks = i+1; |
MaximGordon | 2:560e76e77544 | 332 | for ( j = i+1; j < n_old_npks; j++ ){ |
MaximGordon | 2:560e76e77544 | 333 | n_dist = n_locs[j] - ( i == -1 ? -1 : n_locs[i] ); // lag-zero peak of autocorr is at index -1 |
MaximGordon | 2:560e76e77544 | 334 | if ( n_dist > n_min_distance || n_dist < -n_min_distance ) |
MaximGordon | 0:346a7fa07998 | 335 | n_locs[(*n_npks)++] = n_locs[j]; |
MaximGordon | 0:346a7fa07998 | 336 | } |
MaximGordon | 0:346a7fa07998 | 337 | } |
MaximGordon | 0:346a7fa07998 | 338 | |
MaximGordon | 0:346a7fa07998 | 339 | // Resort indices longo ascending order |
MaximGordon | 0:346a7fa07998 | 340 | maxim_sort_ascend( n_locs, *n_npks ); |
MaximGordon | 0:346a7fa07998 | 341 | } |
MaximGordon | 0:346a7fa07998 | 342 | |
MaximGordon | 0:346a7fa07998 | 343 | void maxim_sort_ascend(int32_t *n_x, int32_t n_size) |
MaximGordon | 0:346a7fa07998 | 344 | /** |
MaximGordon | 0:346a7fa07998 | 345 | * \brief Sort array |
MaximGordon | 0:346a7fa07998 | 346 | * \par Details |
MaximGordon | 0:346a7fa07998 | 347 | * Sort array in ascending order (insertion sort algorithm) |
MaximGordon | 0:346a7fa07998 | 348 | * |
MaximGordon | 0:346a7fa07998 | 349 | * \retval None |
MaximGordon | 0:346a7fa07998 | 350 | */ |
MaximGordon | 0:346a7fa07998 | 351 | { |
MaximGordon | 2:560e76e77544 | 352 | int32_t i, j, n_temp; |
MaximGordon | 0:346a7fa07998 | 353 | for (i = 1; i < n_size; i++) { |
MaximGordon | 2:560e76e77544 | 354 | n_temp = n_x[i]; |
MaximGordon | 2:560e76e77544 | 355 | for (j = i; j > 0 && n_temp < n_x[j-1]; j--) |
MaximGordon | 0:346a7fa07998 | 356 | n_x[j] = n_x[j-1]; |
MaximGordon | 2:560e76e77544 | 357 | n_x[j] = n_temp; |
MaximGordon | 0:346a7fa07998 | 358 | } |
MaximGordon | 0:346a7fa07998 | 359 | } |
MaximGordon | 0:346a7fa07998 | 360 | |
MaximGordon | 0:346a7fa07998 | 361 | void maxim_sort_indices_descend( int32_t *n_x, int32_t *n_indx, int32_t n_size) |
MaximGordon | 0:346a7fa07998 | 362 | /** |
MaximGordon | 0:346a7fa07998 | 363 | * \brief Sort indices |
MaximGordon | 0:346a7fa07998 | 364 | * \par Details |
MaximGordon | 0:346a7fa07998 | 365 | * Sort indices according to descending order (insertion sort algorithm) |
MaximGordon | 0:346a7fa07998 | 366 | * |
MaximGordon | 0:346a7fa07998 | 367 | * \retval None |
MaximGordon | 0:346a7fa07998 | 368 | */ |
MaximGordon | 0:346a7fa07998 | 369 | { |
MaximGordon | 2:560e76e77544 | 370 | int32_t i, j, n_temp; |
MaximGordon | 0:346a7fa07998 | 371 | for (i = 1; i < n_size; i++) { |
MaximGordon | 2:560e76e77544 | 372 | n_temp = n_indx[i]; |
MaximGordon | 2:560e76e77544 | 373 | for (j = i; j > 0 && n_x[n_temp] > n_x[n_indx[j-1]]; j--) |
MaximGordon | 0:346a7fa07998 | 374 | n_indx[j] = n_indx[j-1]; |
MaximGordon | 2:560e76e77544 | 375 | n_indx[j] = n_temp; |
MaximGordon | 0:346a7fa07998 | 376 | } |
MaximGordon | 0:346a7fa07998 | 377 | } |
MaximGordon | 0:346a7fa07998 | 378 |