pico is recognized

Dependencies:   max32625pico mbed SerialInterface USBDevice

Committer:
mjoun
Date:
Fri Jan 31 18:27:53 2020 +0000
Revision:
9:42ab3b212395
Parent:
4:5273ab1085ab
changed

Who changed what in which revision?

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