Code to log MAX30102 sensor data to phone application (Based on Nexpaq)

Dependencies:   ds3231 maxim_dev nexpaq_mdk MAX30102

Fork of IO_Demo by nexpaq

Committer:
shettypavank
Date:
Sun Dec 18 09:43:33 2016 +0000
Revision:
2:9057c54ae6e9
Code to log max30102 sensor data to phone app

Who changed what in which revision?

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