dogukan barkan / Mbed 2 deprecated HSP_SpO2

Dependencies:   mbed MAX14720 USBDevice

Committer:
douqan93
Date:
Thu Mar 28 08:07:54 2019 +0000
Revision:
0:af4d13000e95
This program for MAX32620HSP's onboard SpO2 sensor (MAX30101). Algorithm is not perfect but it works.

Who changed what in which revision?

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