![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Changes added for the DISCO-L476VG
algorithm/algorithm.cpp@0:346a7fa07998, 2016-04-20 (annotated)
- Committer:
- MaximGordon
- Date:
- Wed Apr 20 21:46:06 2016 +0000
- Revision:
- 0:346a7fa07998
- Child:
- 1:e88f22c6c1b0
Complete
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MaximGordon | 0:346a7fa07998 | 1 | /** \file algorithm.h ****************************************************** |
MaximGordon | 0:346a7fa07998 | 2 | * |
MaximGordon | 0:346a7fa07998 | 3 | * Project: MAXREFDES117# |
MaximGordon | 0:346a7fa07998 | 4 | * Filename: algorithm.c |
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 | 0:346a7fa07998 | 87 | uint32_t irMean ,onlyOnce ; |
MaximGordon | 0:346a7fa07998 | 88 | int32_t k ,iRatioCount; |
MaximGordon | 0:346a7fa07998 | 89 | int32_t i,s ,m, exact_ir_valley_locs_count ,middleIdx; |
MaximGordon | 0:346a7fa07998 | 90 | int32_t th1, n_npks,cMin; |
MaximGordon | 0:346a7fa07998 | 91 | int32_t ir_valley_locs[15] ; |
MaximGordon | 0:346a7fa07998 | 92 | int32_t exact_ir_valley_locs[15] ; |
MaximGordon | 0:346a7fa07998 | 93 | int32_t dx_peak_locs[15] ; |
MaximGordon | 0:346a7fa07998 | 94 | int32_t peakintervalSum; |
MaximGordon | 0:346a7fa07998 | 95 | |
MaximGordon | 0:346a7fa07998 | 96 | int32_t yAC, xAC; |
MaximGordon | 0:346a7fa07998 | 97 | int32_t spo2calc; |
MaximGordon | 0:346a7fa07998 | 98 | int32_t yDCmax, xDCmax; |
MaximGordon | 0:346a7fa07998 | 99 | int32_t yDCmaxIdx, xDCmaxIdx; |
MaximGordon | 0:346a7fa07998 | 100 | int32_t ratio[5],ratioAverage; |
MaximGordon | 0:346a7fa07998 | 101 | int32_t nume, denom ; |
MaximGordon | 0:346a7fa07998 | 102 | // remove DC of ir signal |
MaximGordon | 0:346a7fa07998 | 103 | irMean =0; |
MaximGordon | 0:346a7fa07998 | 104 | for (k=0 ; k<n_ir_buffer_length ; k++ ) irMean += un_ir_buffer[k] ; |
MaximGordon | 0:346a7fa07998 | 105 | irMean =irMean/n_ir_buffer_length ; |
MaximGordon | 0:346a7fa07998 | 106 | for (k=0 ; k<n_ir_buffer_length ; k++ ) n_x[k] = un_ir_buffer[k] - irMean ; |
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 | 0:346a7fa07998 | 110 | denom= ( n_x[k]+n_x[k+1]+ n_x[k+2]+ n_x[k+3]); |
MaximGordon | 0:346a7fa07998 | 111 | n_x[k]= 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 | 0:346a7fa07998 | 135 | th1=0; // threshold calculation |
MaximGordon | 0:346a7fa07998 | 136 | for ( k=0 ; k<BUFFER_SIZE-HAMMING_SIZE ;k++){ |
MaximGordon | 0:346a7fa07998 | 137 | th1 += ((n_dx[k]>0)? n_dx[k] : ((int32_t)0-n_dx[k])) ; |
MaximGordon | 0:346a7fa07998 | 138 | } |
MaximGordon | 0:346a7fa07998 | 139 | th1= 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 | 0:346a7fa07998 | 141 | maxim_find_peaks( dx_peak_locs, &n_npks, n_dx, BUFFER_SIZE-HAMMING_SIZE, th1, 8, 5 );//peak_height, peak_distance, max_num_peaks |
MaximGordon | 0:346a7fa07998 | 142 | |
MaximGordon | 0:346a7fa07998 | 143 | peakintervalSum =0; |
MaximGordon | 0:346a7fa07998 | 144 | if (n_npks>=2){ |
MaximGordon | 0:346a7fa07998 | 145 | for (k=1; k<n_npks; k++) |
MaximGordon | 0:346a7fa07998 | 146 | peakintervalSum += (dx_peak_locs[k] -dx_peak_locs[k -1] ) ; |
MaximGordon | 0:346a7fa07998 | 147 | peakintervalSum =peakintervalSum/(n_npks-1); |
MaximGordon | 0:346a7fa07998 | 148 | *n_heart_rate =(int32_t)( 6000/ peakintervalSum );// 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 | 0:346a7fa07998 | 158 | ir_valley_locs[k]= 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 | 0:346a7fa07998 | 168 | // find precise min near ir_valley_locs |
MaximGordon | 0:346a7fa07998 | 169 | exact_ir_valley_locs_count =0; |
MaximGordon | 0:346a7fa07998 | 170 | for ( k=0 ; k<n_npks ;k++){ |
MaximGordon | 0:346a7fa07998 | 171 | onlyOnce =1; |
MaximGordon | 0:346a7fa07998 | 172 | m=ir_valley_locs[k]; |
MaximGordon | 0:346a7fa07998 | 173 | cMin= 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 | 0:346a7fa07998 | 177 | if (n_x[i]<cMin){ |
MaximGordon | 0:346a7fa07998 | 178 | if (onlyOnce >0){ |
MaximGordon | 0:346a7fa07998 | 179 | onlyOnce =0; |
MaximGordon | 0:346a7fa07998 | 180 | } |
MaximGordon | 0:346a7fa07998 | 181 | cMin= n_x[i] ; |
MaximGordon | 0:346a7fa07998 | 182 | exact_ir_valley_locs[k]=i; |
MaximGordon | 0:346a7fa07998 | 183 | } |
MaximGordon | 0:346a7fa07998 | 184 | if (onlyOnce ==0) exact_ir_valley_locs_count ++ ; |
MaximGordon | 0:346a7fa07998 | 185 | } |
MaximGordon | 0:346a7fa07998 | 186 | } |
MaximGordon | 0:346a7fa07998 | 187 | if (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 | 0:346a7fa07998 | 199 | //using 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 | 0:346a7fa07998 | 201 | ratioAverage =0; |
MaximGordon | 0:346a7fa07998 | 202 | iRatioCount = 0; |
MaximGordon | 0:346a7fa07998 | 203 | |
MaximGordon | 0:346a7fa07998 | 204 | for(k=0; k< 5; k++) ratio[k]=0; |
MaximGordon | 0:346a7fa07998 | 205 | for (k=0; k< exact_ir_valley_locs_count; k++){ |
MaximGordon | 0:346a7fa07998 | 206 | if (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 | 0:346a7fa07998 | 215 | for (k=0; k< exact_ir_valley_locs_count-1; k++){ |
MaximGordon | 0:346a7fa07998 | 216 | yDCmax= -16777216 ; |
MaximGordon | 0:346a7fa07998 | 217 | xDCmax= - 16777216; |
MaximGordon | 0:346a7fa07998 | 218 | // printf("range=%d: %d\n ", exact_ir_valley_locs[k], exact_ir_valley_locs[k+1]); |
MaximGordon | 0:346a7fa07998 | 219 | if (exact_ir_valley_locs[k+1]-exact_ir_valley_locs[k] >10){ |
MaximGordon | 0:346a7fa07998 | 220 | for (i=exact_ir_valley_locs[k]; i< exact_ir_valley_locs[k+1]; i++){ |
MaximGordon | 0:346a7fa07998 | 221 | |
MaximGordon | 0:346a7fa07998 | 222 | if (n_x[i]> xDCmax) {xDCmax =n_x[i];xDCmaxIdx =i; } |
MaximGordon | 0:346a7fa07998 | 223 | if (n_y[i]> yDCmax) {yDCmax =n_y[i];yDCmaxIdx=i;} |
MaximGordon | 0:346a7fa07998 | 224 | } |
MaximGordon | 0:346a7fa07998 | 225 | yAC= (n_y[exact_ir_valley_locs[k+1]] - n_y[exact_ir_valley_locs[k] ] )*(yDCmaxIdx -exact_ir_valley_locs[k]); //red |
MaximGordon | 0:346a7fa07998 | 226 | yAC= n_y[exact_ir_valley_locs[k]] + yAC/ (exact_ir_valley_locs[k+1] - exact_ir_valley_locs[k]) ; |
MaximGordon | 0:346a7fa07998 | 227 | |
MaximGordon | 0:346a7fa07998 | 228 | |
MaximGordon | 0:346a7fa07998 | 229 | yAC= n_y[yDCmaxIdx] - yAC; // subracting linear DC compoenents from raw |
MaximGordon | 0:346a7fa07998 | 230 | xAC= (n_x[exact_ir_valley_locs[k+1]] - n_x[exact_ir_valley_locs[k] ] )*(xDCmaxIdx -exact_ir_valley_locs[k]); // ir |
MaximGordon | 0:346a7fa07998 | 231 | xAC= n_x[exact_ir_valley_locs[k]] + xAC/ (exact_ir_valley_locs[k+1] - exact_ir_valley_locs[k]); |
MaximGordon | 0:346a7fa07998 | 232 | xAC= n_x[yDCmaxIdx] - xAC; // subracting linear DC compoenents from raw |
MaximGordon | 0:346a7fa07998 | 233 | nume=( yAC *xDCmax)>>7 ; //prepare X100 to preserve floating value |
MaximGordon | 0:346a7fa07998 | 234 | denom= ( xAC *yDCmax)>>7; |
MaximGordon | 0:346a7fa07998 | 235 | if (denom>0 && iRatioCount <5 && nume != 0) |
MaximGordon | 0:346a7fa07998 | 236 | { |
MaximGordon | 0:346a7fa07998 | 237 | ratio[iRatioCount]= (nume*100)/denom ; //formular is ( yAC *xDCmax) / ( xAC *yDCmax) ; |
MaximGordon | 0:346a7fa07998 | 238 | iRatioCount++; |
MaximGordon | 0:346a7fa07998 | 239 | } |
MaximGordon | 0:346a7fa07998 | 240 | } |
MaximGordon | 0:346a7fa07998 | 241 | |
MaximGordon | 0:346a7fa07998 | 242 | // prlongf("ratio[%d]= %d exact_ir_valley_locs[k] =%d , exact_ir_valley_locs[%d] =%d \n",k, ratio[k] ,exact_ir_valley_locs[k] ,k+1, exact_ir_valley_locs[k+1] ) ; |
MaximGordon | 0:346a7fa07998 | 243 | // prlongf("nume= %d ,denom= %d yAC = %d, xDCmax = %d, xAC= %d, yDCmax = %d\n",nume, denom, yAC ,xDCmax ,xAC ,yDCmax ); |
MaximGordon | 0:346a7fa07998 | 244 | |
MaximGordon | 0:346a7fa07998 | 245 | } |
MaximGordon | 0:346a7fa07998 | 246 | |
MaximGordon | 0:346a7fa07998 | 247 | maxim_sort_ascend(ratio, iRatioCount); |
MaximGordon | 0:346a7fa07998 | 248 | middleIdx= iRatioCount/2; |
MaximGordon | 0:346a7fa07998 | 249 | |
MaximGordon | 0:346a7fa07998 | 250 | if (middleIdx >1) |
MaximGordon | 0:346a7fa07998 | 251 | ratioAverage =( ratio[middleIdx-1] +ratio[middleIdx])/2; // use median |
MaximGordon | 0:346a7fa07998 | 252 | else |
MaximGordon | 0:346a7fa07998 | 253 | ratioAverage = ratio[middleIdx ]; |
MaximGordon | 0:346a7fa07998 | 254 | |
MaximGordon | 0:346a7fa07998 | 255 | if( ratioAverage>2 && ratioAverage <184){ |
MaximGordon | 0:346a7fa07998 | 256 | spo2calc= uch_spo2_table[ratioAverage] ; |
MaximGordon | 0:346a7fa07998 | 257 | *n_spo2 = spo2calc ; |
MaximGordon | 0:346a7fa07998 | 258 | *ch_spo2_valid = 1;// float_SPO2 = -45.060*ratioAverage* ratioAverage/10000 + 30.354 *ratioAverage/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 | 0:346a7fa07998 | 292 | int32_t i = 1, 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 | 0:346a7fa07998 | 297 | width = 1; |
MaximGordon | 0:346a7fa07998 | 298 | while (i+width < n_size && n_x[i] == n_x[i+width]) // find flat peaks |
MaximGordon | 0:346a7fa07998 | 299 | width++; |
MaximGordon | 0:346a7fa07998 | 300 | if (n_x[i] > n_x[i+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 | 0:346a7fa07998 | 303 | i += width+1; |
MaximGordon | 0:346a7fa07998 | 304 | } |
MaximGordon | 0:346a7fa07998 | 305 | else |
MaximGordon | 0:346a7fa07998 | 306 | i += 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 | 0:346a7fa07998 | 324 | int32_t i, j, old_npks, 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 | 0:346a7fa07998 | 330 | old_npks = *n_npks; |
MaximGordon | 0:346a7fa07998 | 331 | *n_npks = i+1; |
MaximGordon | 0:346a7fa07998 | 332 | for ( j = i+1; j < old_npks; j++ ){ |
MaximGordon | 0:346a7fa07998 | 333 | dist = n_locs[j] - ( i == -1 ? -1 : n_locs[i] ); // lag-zero peak of autocorr is at index -1 |
MaximGordon | 0:346a7fa07998 | 334 | if ( dist > n_min_distance || 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 | 0:346a7fa07998 | 352 | int32_t i, j, temp; |
MaximGordon | 0:346a7fa07998 | 353 | for (i = 1; i < n_size; i++) { |
MaximGordon | 0:346a7fa07998 | 354 | temp = n_x[i]; |
MaximGordon | 0:346a7fa07998 | 355 | for (j = i; j > 0 && temp < n_x[j-1]; j--) |
MaximGordon | 0:346a7fa07998 | 356 | n_x[j] = n_x[j-1]; |
MaximGordon | 0:346a7fa07998 | 357 | n_x[j] = 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 | 0:346a7fa07998 | 370 | int32_t i, j, temp; |
MaximGordon | 0:346a7fa07998 | 371 | for (i = 1; i < n_size; i++) { |
MaximGordon | 0:346a7fa07998 | 372 | temp = n_indx[i]; |
MaximGordon | 0:346a7fa07998 | 373 | for (j = i; j > 0 && n_x[temp] > n_x[n_indx[j-1]]; j--) |
MaximGordon | 0:346a7fa07998 | 374 | n_indx[j] = n_indx[j-1]; |
MaximGordon | 0:346a7fa07998 | 375 | n_indx[j] = temp; |
MaximGordon | 0:346a7fa07998 | 376 | } |
MaximGordon | 0:346a7fa07998 | 377 | } |
MaximGordon | 0:346a7fa07998 | 378 |