Bluetooth Heart Rate Monitor using Maxim MAX32630FTHR & MAXREFDES117

Fork of mbed-os-example-ble-HeartRate by mbed-os-examples

Committer:
Brakkion
Date:
Mon Dec 11 16:22:50 2017 +0000
Revision:
49:5be03f287022
Parent:
47:8ae30057edc0
Created using MAX32630FTHR & MAXREFDES117

Who changed what in which revision?

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