Test program for a Max30102 heartrate sensor on a STM32L476RG specific board ENSMM
Dependencies: mbed
These two pictures show how to connect the Mheuve Sensor to the ST-Link debugger (don't forget to disable ST-link jumpers and JP5 on the board! ):
These two pictures show how to connect the Mheuve Sensor TX RX to the ST-Link debugger (don't forget to cross TX and RX, it means Mheuve sensor TX on ST-Link RX and ST-Link TX on Mheuve sensor RX ):
The Mheuve sensor board needs to be powered by an external battery.
The result appears on the terminal, speed config is 115200 bds.
algorithm/algorithm.cpp@3:7c0fb55eb3ff, 2016-04-21 (annotated)
- Committer:
- MaximGordon
- Date:
- Thu Apr 21 19:38:17 2016 +0000
- Revision:
- 3:7c0fb55eb3ff
- Parent:
- 2:560e76e77544
- Child:
- 4:5273ab1085ab
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 | 3:7c0fb55eb3ff | 65 | 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 , |
MaximGordon | 3:7c0fb55eb3ff | 66 | int32_t *pn_heart_rate , int8_t *pch_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 | 3:7c0fb55eb3ff | 74 | * \param[in] *pun_ir_buffer - IR sensor data buffer |
MaximGordon | 0:346a7fa07998 | 75 | * \param[in] n_ir_buffer_length - IR sensor data buffer length |
MaximGordon | 3:7c0fb55eb3ff | 76 | * \param[in] *pun_red_buffer - Red sensor data buffer |
MaximGordon | 3:7c0fb55eb3ff | 77 | * \param[out] *pn_spo2 - Calculated SpO2 value |
MaximGordon | 3:7c0fb55eb3ff | 78 | * \param[out] *pch_spo2_valid - 1 if the calculated SpO2 value is valid |
MaximGordon | 3:7c0fb55eb3ff | 79 | * \param[out] *pn_heart_rate - Calculated heart rate value |
MaximGordon | 3:7c0fb55eb3ff | 80 | * \param[out] *pch_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 | 3:7c0fb55eb3ff | 91 | int32_t an_ir_valley_locs[15] ; |
MaximGordon | 3:7c0fb55eb3ff | 92 | int32_t an_exact_ir_valley_locs[15] ; |
MaximGordon | 3:7c0fb55eb3ff | 93 | int32_t an_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 | 3:7c0fb55eb3ff | 100 | int32_t an_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 | 3:7c0fb55eb3ff | 104 | for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ; |
MaximGordon | 2:560e76e77544 | 105 | un_ir_mean =un_ir_mean/n_ir_buffer_length ; |
MaximGordon | 3:7c0fb55eb3ff | 106 | for (k=0 ; k<n_ir_buffer_length ; k++ ) an_x[k] = pun_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 | 3:7c0fb55eb3ff | 110 | n_denom= ( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3]); |
MaximGordon | 3:7c0fb55eb3ff | 111 | an_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 | 3:7c0fb55eb3ff | 117 | an_dx[k]= (an_x[k+1]- an_x[k]); |
MaximGordon | 0:346a7fa07998 | 118 | |
MaximGordon | 3:7c0fb55eb3ff | 119 | // 2-pt Moving Average to an_dx |
MaximGordon | 0:346a7fa07998 | 120 | for(k=0; k< BUFFER_SIZE-MA4_SIZE-2; k++){ |
MaximGordon | 3:7c0fb55eb3ff | 121 | an_dx[k] = ( an_dx[k]+an_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 | 3:7c0fb55eb3ff | 129 | s -= an_dx[k] *auw_hamm[k-i] ; |
MaximGordon | 0:346a7fa07998 | 130 | } |
MaximGordon | 3:7c0fb55eb3ff | 131 | an_dx[i]= s/ (int32_t)1146; // divide by sum of auw_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 | 3:7c0fb55eb3ff | 137 | n_th1 += ((an_dx[k]>0)? an_dx[k] : ((int32_t)0-an_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 | 3:7c0fb55eb3ff | 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 |
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 | 3:7c0fb55eb3ff | 146 | n_peak_interval_sum += (an_dx_peak_locs[k] -an_dx_peak_locs[k -1] ) ; |
MaximGordon | 2:560e76e77544 | 147 | n_peak_interval_sum =n_peak_interval_sum/(n_npks-1); |
MaximGordon | 3:7c0fb55eb3ff | 148 | *pn_heart_rate =(int32_t)( 6000/ n_peak_interval_sum );// beats per minutes |
MaximGordon | 3:7c0fb55eb3ff | 149 | //prlongf(">>> *pn_heart_rate= %d \n", *pn_heart_rate) ; |
MaximGordon | 3:7c0fb55eb3ff | 150 | *pch_hr_valid = 1; |
MaximGordon | 0:346a7fa07998 | 151 | } |
MaximGordon | 0:346a7fa07998 | 152 | else { |
MaximGordon | 3:7c0fb55eb3ff | 153 | *pn_heart_rate = -999; |
MaximGordon | 3:7c0fb55eb3ff | 154 | *pch_hr_valid = 0; |
MaximGordon | 0:346a7fa07998 | 155 | } |
MaximGordon | 0:346a7fa07998 | 156 | |
MaximGordon | 0:346a7fa07998 | 157 | for ( k=0 ; k<n_npks ;k++) |
MaximGordon | 3:7c0fb55eb3ff | 158 | an_ir_valley_locs[k]= an_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 | 3:7c0fb55eb3ff | 164 | an_x[k] = pun_ir_buffer[k] ; |
MaximGordon | 3:7c0fb55eb3ff | 165 | an_y[k] = pun_red_buffer[k] ; |
MaximGordon | 0:346a7fa07998 | 166 | } |
MaximGordon | 0:346a7fa07998 | 167 | |
MaximGordon | 3:7c0fb55eb3ff | 168 | // find precise min near an_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 | 3:7c0fb55eb3ff | 172 | m=an_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 | 3:7c0fb55eb3ff | 177 | if (an_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 | 3:7c0fb55eb3ff | 181 | n_c_min= an_x[i] ; |
MaximGordon | 3:7c0fb55eb3ff | 182 | an_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 | 3:7c0fb55eb3ff | 188 | *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range |
MaximGordon | 3:7c0fb55eb3ff | 189 | *pch_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 | 3:7c0fb55eb3ff | 194 | an_x[k]=( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3])/(int32_t)4; |
MaximGordon | 3:7c0fb55eb3ff | 195 | an_y[k]=( an_y[k]+an_y[k+1]+ an_y[k+2]+ an_y[k+3])/(int32_t)4; |
MaximGordon | 0:346a7fa07998 | 196 | |
MaximGordon | 0:346a7fa07998 | 197 | } |
MaximGordon | 0:346a7fa07998 | 198 | |
MaximGordon | 3:7c0fb55eb3ff | 199 | //using an_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 | 3:7c0fb55eb3ff | 204 | for(k=0; k< 5; k++) an_ratio[k]=0; |
MaximGordon | 2:560e76e77544 | 205 | for (k=0; k< n_exact_ir_valley_locs_count; k++){ |
MaximGordon | 3:7c0fb55eb3ff | 206 | if (an_exact_ir_valley_locs[k] > BUFFER_SIZE ){ |
MaximGordon | 3:7c0fb55eb3ff | 207 | *pn_spo2 = -999 ; // do not use SPO2 since valley loc is out of range |
MaximGordon | 3:7c0fb55eb3ff | 208 | *pch_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 | 3:7c0fb55eb3ff | 218 | // printf("range=%d: %d\n ", an_exact_ir_valley_locs[k], an_exact_ir_valley_locs[k+1]); |
MaximGordon | 3:7c0fb55eb3ff | 219 | if (an_exact_ir_valley_locs[k+1]-an_exact_ir_valley_locs[k] >10){ |
MaximGordon | 3:7c0fb55eb3ff | 220 | for (i=an_exact_ir_valley_locs[k]; i< an_exact_ir_valley_locs[k+1]; i++){ |
MaximGordon | 0:346a7fa07998 | 221 | |
MaximGordon | 3:7c0fb55eb3ff | 222 | if (an_x[i]> n_x_dc_max) {n_x_dc_max =an_x[i];n_x_dc_max_idx =i; } |
MaximGordon | 3:7c0fb55eb3ff | 223 | if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i];n_y_dc_max_idx=i;} |
MaximGordon | 0:346a7fa07998 | 224 | } |
MaximGordon | 3:7c0fb55eb3ff | 225 | 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 |
MaximGordon | 3:7c0fb55eb3ff | 226 | 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]) ; |
MaximGordon | 0:346a7fa07998 | 227 | |
MaximGordon | 0:346a7fa07998 | 228 | |
MaximGordon | 3:7c0fb55eb3ff | 229 | n_y_ac= an_y[n_y_dc_max_idx] - n_y_ac; // subracting linear DC compoenents from raw |
MaximGordon | 3:7c0fb55eb3ff | 230 | 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 |
MaximGordon | 3:7c0fb55eb3ff | 231 | 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]); |
MaximGordon | 3:7c0fb55eb3ff | 232 | n_x_ac= an_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 | 3:7c0fb55eb3ff | 237 | 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) ; |
MaximGordon | 2:560e76e77544 | 238 | n_i_ratio_count++; |
MaximGordon | 0:346a7fa07998 | 239 | } |
MaximGordon | 0:346a7fa07998 | 240 | } |
MaximGordon | 0:346a7fa07998 | 241 | |
MaximGordon | 3:7c0fb55eb3ff | 242 | // prlongf("an_ratio[%d]= %d an_exact_ir_valley_locs[k] =%d , an_exact_ir_valley_locs[%d] =%d \n",k, an_ratio[k] ,an_exact_ir_valley_locs[k] ,k+1, an_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 | 3:7c0fb55eb3ff | 247 | maxim_sort_ascend(an_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 | 3:7c0fb55eb3ff | 251 | n_ratio_average =( an_ratio[n_middle_idx-1] +an_ratio[n_middle_idx])/2; // use median |
MaximGordon | 0:346a7fa07998 | 252 | else |
MaximGordon | 3:7c0fb55eb3ff | 253 | n_ratio_average = an_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 | 3:7c0fb55eb3ff | 257 | *pn_spo2 = n_spo2_calc ; |
MaximGordon | 3:7c0fb55eb3ff | 258 | *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 |
MaximGordon | 0:346a7fa07998 | 259 | } |
MaximGordon | 0:346a7fa07998 | 260 | else{ |
MaximGordon | 3:7c0fb55eb3ff | 261 | *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range |
MaximGordon | 3:7c0fb55eb3ff | 262 | *pch_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 | 3:7c0fb55eb3ff | 269 | 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 ) |
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 | 3:7c0fb55eb3ff | 278 | maxim_peaks_above_min_height( pn_locs, pn_npks, pn_x, n_size, n_min_height ); |
MaximGordon | 3:7c0fb55eb3ff | 279 | maxim_remove_close_peaks( pn_locs, pn_npks, pn_x, n_min_distance ); |
MaximGordon | 3:7c0fb55eb3ff | 280 | *pn_npks = min( *pn_npks, n_max_num ); |
MaximGordon | 0:346a7fa07998 | 281 | } |
MaximGordon | 0:346a7fa07998 | 282 | |
MaximGordon | 3:7c0fb55eb3ff | 283 | 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 ) |
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 | 3:7c0fb55eb3ff | 293 | *pn_npks = 0; |
MaximGordon | 0:346a7fa07998 | 294 | |
MaximGordon | 0:346a7fa07998 | 295 | while (i < n_size-1){ |
MaximGordon | 3:7c0fb55eb3ff | 296 | if (pn_x[i] > n_min_height && pn_x[i] > pn_x[i-1]){ // find left edge of potential peaks |
MaximGordon | 2:560e76e77544 | 297 | n_width = 1; |
MaximGordon | 3:7c0fb55eb3ff | 298 | while (i+n_width < n_size && pn_x[i] == pn_x[i+n_width]) // find flat peaks |
MaximGordon | 2:560e76e77544 | 299 | n_width++; |
MaximGordon | 3:7c0fb55eb3ff | 300 | if (pn_x[i] > pn_x[i+n_width] && (*pn_npks) < 15 ){ // find right edge of peaks |
MaximGordon | 3:7c0fb55eb3ff | 301 | pn_locs[(*pn_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 | 3:7c0fb55eb3ff | 314 | void maxim_remove_close_peaks( int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_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 | 3:7c0fb55eb3ff | 327 | maxim_sort_indices_descend( pn_x, pn_locs, *pn_npks ); |
MaximGordon | 0:346a7fa07998 | 328 | |
MaximGordon | 3:7c0fb55eb3ff | 329 | for ( i = -1; i < *pn_npks; i++ ){ |
MaximGordon | 3:7c0fb55eb3ff | 330 | n_old_npks = *pn_npks; |
MaximGordon | 3:7c0fb55eb3ff | 331 | *pn_npks = i+1; |
MaximGordon | 2:560e76e77544 | 332 | for ( j = i+1; j < n_old_npks; j++ ){ |
MaximGordon | 3:7c0fb55eb3ff | 333 | n_dist = pn_locs[j] - ( i == -1 ? -1 : pn_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 | 3:7c0fb55eb3ff | 335 | pn_locs[(*pn_npks)++] = pn_locs[j]; |
MaximGordon | 0:346a7fa07998 | 336 | } |
MaximGordon | 0:346a7fa07998 | 337 | } |
MaximGordon | 0:346a7fa07998 | 338 | |
MaximGordon | 0:346a7fa07998 | 339 | // Resort indices longo ascending order |
MaximGordon | 3:7c0fb55eb3ff | 340 | maxim_sort_ascend( pn_locs, *pn_npks ); |
MaximGordon | 0:346a7fa07998 | 341 | } |
MaximGordon | 0:346a7fa07998 | 342 | |
MaximGordon | 3:7c0fb55eb3ff | 343 | void maxim_sort_ascend(int32_t *pn_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 | 3:7c0fb55eb3ff | 354 | n_temp = pn_x[i]; |
MaximGordon | 3:7c0fb55eb3ff | 355 | for (j = i; j > 0 && n_temp < pn_x[j-1]; j--) |
MaximGordon | 3:7c0fb55eb3ff | 356 | pn_x[j] = pn_x[j-1]; |
MaximGordon | 3:7c0fb55eb3ff | 357 | pn_x[j] = n_temp; |
MaximGordon | 0:346a7fa07998 | 358 | } |
MaximGordon | 0:346a7fa07998 | 359 | } |
MaximGordon | 0:346a7fa07998 | 360 | |
MaximGordon | 3:7c0fb55eb3ff | 361 | void maxim_sort_indices_descend( int32_t *pn_x, int32_t *pn_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 | 3:7c0fb55eb3ff | 372 | n_temp = pn_indx[i]; |
MaximGordon | 3:7c0fb55eb3ff | 373 | for (j = i; j > 0 && pn_x[n_temp] > pn_x[pn_indx[j-1]]; j--) |
MaximGordon | 3:7c0fb55eb3ff | 374 | pn_indx[j] = pn_indx[j-1]; |
MaximGordon | 3:7c0fb55eb3ff | 375 | pn_indx[j] = n_temp; |
MaximGordon | 0:346a7fa07998 | 376 | } |
MaximGordon | 0:346a7fa07998 | 377 | } |
MaximGordon | 0:346a7fa07998 | 378 |