Alex Leung
/
HealthTracker
Test version
algorithm.cpp@1:bd9f39f9d91c, 2018-03-20 (annotated)
- Committer:
- a2824256
- Date:
- Tue Mar 20 02:21:49 2018 +0000
- Revision:
- 1:bd9f39f9d91c
- Parent:
- 0:4be500de690c
repair main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a2824256 | 0:4be500de690c | 1 | #include "algorithm.h" |
a2824256 | 0:4be500de690c | 2 | #include "mbed.h" |
a2824256 | 0:4be500de690c | 3 | |
a2824256 | 0:4be500de690c | 4 | 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, |
a2824256 | 0:4be500de690c | 5 | int32_t *pn_heart_rate, int8_t *pch_hr_valid) |
a2824256 | 0:4be500de690c | 6 | /** |
a2824256 | 0:4be500de690c | 7 | * \brief Calculate the heart rate and SpO2 level |
a2824256 | 0:4be500de690c | 8 | * \par Details |
a2824256 | 0:4be500de690c | 9 | * By detecting peaks of PPG cycle and corresponding AC/DC of red/infra-red signal, the ratio for the SPO2 is computed. |
a2824256 | 0:4be500de690c | 10 | * Since this algorithm is aiming for Arm M0/M3. formaula for SPO2 did not achieve the accuracy due to register overflow. |
a2824256 | 0:4be500de690c | 11 | * Thus, accurate SPO2 is precalculated and save longo uch_spo2_table[] per each ratio. |
a2824256 | 0:4be500de690c | 12 | * |
a2824256 | 0:4be500de690c | 13 | * \param[in] *pun_ir_buffer - IR sensor data buffer |
a2824256 | 0:4be500de690c | 14 | * \param[in] n_ir_buffer_length - IR sensor data buffer length |
a2824256 | 0:4be500de690c | 15 | * \param[in] *pun_red_buffer - Red sensor data buffer |
a2824256 | 0:4be500de690c | 16 | * \param[out] *pn_spo2 - Calculated SpO2 value |
a2824256 | 0:4be500de690c | 17 | * \param[out] *pch_spo2_valid - 1 if the calculated SpO2 value is valid |
a2824256 | 0:4be500de690c | 18 | * \param[out] *pn_heart_rate - Calculated heart rate value |
a2824256 | 0:4be500de690c | 19 | * \param[out] *pch_hr_valid - 1 if the calculated heart rate value is valid |
a2824256 | 0:4be500de690c | 20 | * |
a2824256 | 0:4be500de690c | 21 | * \retval None |
a2824256 | 0:4be500de690c | 22 | */ |
a2824256 | 0:4be500de690c | 23 | { |
a2824256 | 0:4be500de690c | 24 | uint32_t un_ir_mean ,un_only_once ; |
a2824256 | 0:4be500de690c | 25 | int32_t k ,n_i_ratio_count; |
a2824256 | 0:4be500de690c | 26 | int32_t i, s, m, n_exact_ir_valley_locs_count ,n_middle_idx; |
a2824256 | 0:4be500de690c | 27 | int32_t n_th1, n_npks,n_c_min; |
a2824256 | 0:4be500de690c | 28 | int32_t an_ir_valley_locs[15] ; |
a2824256 | 0:4be500de690c | 29 | int32_t an_exact_ir_valley_locs[15] ; |
a2824256 | 0:4be500de690c | 30 | int32_t an_dx_peak_locs[15] ; |
a2824256 | 0:4be500de690c | 31 | int32_t n_peak_interval_sum; |
a2824256 | 0:4be500de690c | 32 | |
a2824256 | 0:4be500de690c | 33 | int32_t n_y_ac, n_x_ac; |
a2824256 | 0:4be500de690c | 34 | int32_t n_spo2_calc; |
a2824256 | 0:4be500de690c | 35 | int32_t n_y_dc_max, n_x_dc_max; |
a2824256 | 0:4be500de690c | 36 | int32_t n_y_dc_max_idx, n_x_dc_max_idx; |
a2824256 | 0:4be500de690c | 37 | int32_t an_ratio[5],n_ratio_average; |
a2824256 | 0:4be500de690c | 38 | int32_t n_nume, n_denom ; |
a2824256 | 0:4be500de690c | 39 | // remove DC of ir signal |
a2824256 | 0:4be500de690c | 40 | un_ir_mean =0; |
a2824256 | 0:4be500de690c | 41 | for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ; |
a2824256 | 0:4be500de690c | 42 | un_ir_mean =un_ir_mean/n_ir_buffer_length ; |
a2824256 | 0:4be500de690c | 43 | for (k=0 ; k<n_ir_buffer_length ; k++ ) an_x[k] = pun_ir_buffer[k] - un_ir_mean ; |
a2824256 | 0:4be500de690c | 44 | |
a2824256 | 0:4be500de690c | 45 | // 4 pt Moving Average |
a2824256 | 0:4be500de690c | 46 | for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){ |
a2824256 | 0:4be500de690c | 47 | n_denom= ( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3]); |
a2824256 | 0:4be500de690c | 48 | an_x[k]= n_denom/(int32_t)4; |
a2824256 | 0:4be500de690c | 49 | } |
a2824256 | 0:4be500de690c | 50 | |
a2824256 | 0:4be500de690c | 51 | // get difference of smoothed IR signal |
a2824256 | 0:4be500de690c | 52 | |
a2824256 | 0:4be500de690c | 53 | for( k=0; k<BUFFER_SIZE-MA4_SIZE-1; k++) |
a2824256 | 0:4be500de690c | 54 | an_dx[k]= (an_x[k+1]- an_x[k]); |
a2824256 | 0:4be500de690c | 55 | |
a2824256 | 0:4be500de690c | 56 | // 2-pt Moving Average to an_dx |
a2824256 | 0:4be500de690c | 57 | for(k=0; k< BUFFER_SIZE-MA4_SIZE-2; k++){ |
a2824256 | 0:4be500de690c | 58 | an_dx[k] = ( an_dx[k]+an_dx[k+1])/2 ; |
a2824256 | 0:4be500de690c | 59 | } |
a2824256 | 0:4be500de690c | 60 | |
a2824256 | 0:4be500de690c | 61 | // hamming window |
a2824256 | 0:4be500de690c | 62 | // flip wave form so that we can detect valley with peak detector |
a2824256 | 0:4be500de690c | 63 | for ( i=0 ; i<BUFFER_SIZE-HAMMING_SIZE-MA4_SIZE-2 ;i++){ |
a2824256 | 0:4be500de690c | 64 | s= 0; |
a2824256 | 0:4be500de690c | 65 | for( k=i; k<i+ HAMMING_SIZE ;k++){ |
a2824256 | 0:4be500de690c | 66 | s -= an_dx[k] *auw_hamm[k-i] ; |
a2824256 | 0:4be500de690c | 67 | } |
a2824256 | 0:4be500de690c | 68 | an_dx[i]= s/ (int32_t)1146; // divide by sum of auw_hamm |
a2824256 | 0:4be500de690c | 69 | } |
a2824256 | 0:4be500de690c | 70 | |
a2824256 | 0:4be500de690c | 71 | |
a2824256 | 0:4be500de690c | 72 | n_th1=0; // threshold calculation |
a2824256 | 0:4be500de690c | 73 | for ( k=0 ; k<BUFFER_SIZE-HAMMING_SIZE ;k++){ |
a2824256 | 0:4be500de690c | 74 | n_th1 += ((an_dx[k]>0)? an_dx[k] : ((int32_t)0-an_dx[k])) ; |
a2824256 | 0:4be500de690c | 75 | } |
a2824256 | 0:4be500de690c | 76 | n_th1= n_th1/ ( BUFFER_SIZE-HAMMING_SIZE); |
a2824256 | 0:4be500de690c | 77 | // peak location is acutally index for sharpest location of raw signal since we flipped the signal |
a2824256 | 0:4be500de690c | 78 | 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 |
a2824256 | 0:4be500de690c | 79 | |
a2824256 | 0:4be500de690c | 80 | n_peak_interval_sum =0; |
a2824256 | 0:4be500de690c | 81 | if (n_npks>=2){ |
a2824256 | 0:4be500de690c | 82 | for (k=1; k<n_npks; k++) |
a2824256 | 0:4be500de690c | 83 | n_peak_interval_sum += (an_dx_peak_locs[k]-an_dx_peak_locs[k -1]); |
a2824256 | 0:4be500de690c | 84 | n_peak_interval_sum=n_peak_interval_sum/(n_npks-1); |
a2824256 | 0:4be500de690c | 85 | *pn_heart_rate=(int32_t)(6000/n_peak_interval_sum);// beats per minutes |
a2824256 | 0:4be500de690c | 86 | *pch_hr_valid = 1; |
a2824256 | 0:4be500de690c | 87 | } |
a2824256 | 0:4be500de690c | 88 | else { |
a2824256 | 0:4be500de690c | 89 | *pn_heart_rate = -999; |
a2824256 | 0:4be500de690c | 90 | *pch_hr_valid = 0; |
a2824256 | 0:4be500de690c | 91 | } |
a2824256 | 0:4be500de690c | 92 | |
a2824256 | 0:4be500de690c | 93 | for ( k=0 ; k<n_npks ;k++) |
a2824256 | 0:4be500de690c | 94 | an_ir_valley_locs[k]=an_dx_peak_locs[k]+HAMMING_SIZE/2; |
a2824256 | 0:4be500de690c | 95 | |
a2824256 | 0:4be500de690c | 96 | |
a2824256 | 0:4be500de690c | 97 | // raw value : RED(=y) and IR(=X) |
a2824256 | 0:4be500de690c | 98 | // we need to assess DC and AC value of ir and red PPG. |
a2824256 | 0:4be500de690c | 99 | for (k=0 ; k<n_ir_buffer_length ; k++ ) { |
a2824256 | 0:4be500de690c | 100 | an_x[k] = pun_ir_buffer[k] ; |
a2824256 | 0:4be500de690c | 101 | an_y[k] = pun_red_buffer[k] ; |
a2824256 | 0:4be500de690c | 102 | } |
a2824256 | 0:4be500de690c | 103 | |
a2824256 | 0:4be500de690c | 104 | // find precise min near an_ir_valley_locs |
a2824256 | 0:4be500de690c | 105 | n_exact_ir_valley_locs_count =0; |
a2824256 | 0:4be500de690c | 106 | for(k=0 ; k<n_npks ;k++){ |
a2824256 | 0:4be500de690c | 107 | un_only_once =1; |
a2824256 | 0:4be500de690c | 108 | m=an_ir_valley_locs[k]; |
a2824256 | 0:4be500de690c | 109 | n_c_min= 16777216;//2^24; |
a2824256 | 0:4be500de690c | 110 | if (m+5 < BUFFER_SIZE-HAMMING_SIZE && m-5 >0){ |
a2824256 | 0:4be500de690c | 111 | for(i= m-5;i<m+5; i++) |
a2824256 | 0:4be500de690c | 112 | if (an_x[i]<n_c_min){ |
a2824256 | 0:4be500de690c | 113 | if (un_only_once >0){ |
a2824256 | 0:4be500de690c | 114 | un_only_once =0; |
a2824256 | 0:4be500de690c | 115 | } |
a2824256 | 0:4be500de690c | 116 | n_c_min= an_x[i] ; |
a2824256 | 0:4be500de690c | 117 | an_exact_ir_valley_locs[k]=i; |
a2824256 | 0:4be500de690c | 118 | } |
a2824256 | 0:4be500de690c | 119 | if (un_only_once ==0) |
a2824256 | 0:4be500de690c | 120 | n_exact_ir_valley_locs_count ++ ; |
a2824256 | 0:4be500de690c | 121 | } |
a2824256 | 0:4be500de690c | 122 | } |
a2824256 | 0:4be500de690c | 123 | if (n_exact_ir_valley_locs_count <2 ){ |
a2824256 | 0:4be500de690c | 124 | *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range |
a2824256 | 0:4be500de690c | 125 | *pch_spo2_valid = 0; |
a2824256 | 0:4be500de690c | 126 | return; |
a2824256 | 0:4be500de690c | 127 | } |
a2824256 | 0:4be500de690c | 128 | // 4 pt MA |
a2824256 | 0:4be500de690c | 129 | for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){ |
a2824256 | 0:4be500de690c | 130 | an_x[k]=( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3])/(int32_t)4; |
a2824256 | 0:4be500de690c | 131 | an_y[k]=( an_y[k]+an_y[k+1]+ an_y[k+2]+ an_y[k+3])/(int32_t)4; |
a2824256 | 0:4be500de690c | 132 | } |
a2824256 | 0:4be500de690c | 133 | |
a2824256 | 0:4be500de690c | 134 | //using an_exact_ir_valley_locs , find ir-red DC andir-red AC for SPO2 calibration ratio |
a2824256 | 0:4be500de690c | 135 | //finding AC/DC maximum of raw ir * red between two valley locations |
a2824256 | 0:4be500de690c | 136 | n_ratio_average =0; |
a2824256 | 0:4be500de690c | 137 | n_i_ratio_count =0; |
a2824256 | 0:4be500de690c | 138 | |
a2824256 | 0:4be500de690c | 139 | for(k=0; k< 5; k++) an_ratio[k]=0; |
a2824256 | 0:4be500de690c | 140 | for (k=0; k< n_exact_ir_valley_locs_count; k++){ |
a2824256 | 0:4be500de690c | 141 | if (an_exact_ir_valley_locs[k] > BUFFER_SIZE ){ |
a2824256 | 0:4be500de690c | 142 | *pn_spo2 = -999 ; // do not use SPO2 since valley loc is out of range |
a2824256 | 0:4be500de690c | 143 | *pch_spo2_valid = 0; |
a2824256 | 0:4be500de690c | 144 | return; |
a2824256 | 0:4be500de690c | 145 | } |
a2824256 | 0:4be500de690c | 146 | } |
a2824256 | 0:4be500de690c | 147 | // find max between two valley locations |
a2824256 | 0:4be500de690c | 148 | // and use ratio betwen AC compoent of Ir & Red and DC compoent of Ir & Red for SPO2 |
a2824256 | 0:4be500de690c | 149 | |
a2824256 | 0:4be500de690c | 150 | for (k=0; k< n_exact_ir_valley_locs_count-1; k++){ |
a2824256 | 0:4be500de690c | 151 | n_y_dc_max= -16777216 ; |
a2824256 | 0:4be500de690c | 152 | n_x_dc_max= - 16777216; |
a2824256 | 0:4be500de690c | 153 | if (an_exact_ir_valley_locs[k+1]-an_exact_ir_valley_locs[k] >10){ |
a2824256 | 0:4be500de690c | 154 | for (i=an_exact_ir_valley_locs[k]; i< an_exact_ir_valley_locs[k+1]; i++){ |
a2824256 | 0:4be500de690c | 155 | if (an_x[i]> n_x_dc_max) {n_x_dc_max =an_x[i];n_x_dc_max_idx =i; } |
a2824256 | 0:4be500de690c | 156 | if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i];n_y_dc_max_idx=i;} |
a2824256 | 0:4be500de690c | 157 | } |
a2824256 | 0:4be500de690c | 158 | 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 |
a2824256 | 0:4be500de690c | 159 | 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]) ; |
a2824256 | 0:4be500de690c | 160 | |
a2824256 | 0:4be500de690c | 161 | |
a2824256 | 0:4be500de690c | 162 | n_y_ac= an_y[n_y_dc_max_idx] - n_y_ac; // subracting linear DC compoenents from raw |
a2824256 | 0:4be500de690c | 163 | 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 |
a2824256 | 0:4be500de690c | 164 | 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]); |
a2824256 | 0:4be500de690c | 165 | n_x_ac= an_x[n_y_dc_max_idx] - n_x_ac; // subracting linear DC compoenents from raw |
a2824256 | 0:4be500de690c | 166 | n_nume=( n_y_ac *n_x_dc_max)>>7 ; //prepare X100 to preserve floating value |
a2824256 | 0:4be500de690c | 167 | n_denom= ( n_x_ac *n_y_dc_max)>>7; |
a2824256 | 0:4be500de690c | 168 | if (n_denom>0 && n_i_ratio_count <5 && n_nume != 0) |
a2824256 | 0:4be500de690c | 169 | { |
a2824256 | 0:4be500de690c | 170 | 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) ; |
a2824256 | 0:4be500de690c | 171 | n_i_ratio_count++; |
a2824256 | 0:4be500de690c | 172 | } |
a2824256 | 0:4be500de690c | 173 | } |
a2824256 | 0:4be500de690c | 174 | } |
a2824256 | 0:4be500de690c | 175 | |
a2824256 | 0:4be500de690c | 176 | maxim_sort_ascend(an_ratio, n_i_ratio_count); |
a2824256 | 0:4be500de690c | 177 | n_middle_idx= n_i_ratio_count/2; |
a2824256 | 0:4be500de690c | 178 | |
a2824256 | 0:4be500de690c | 179 | if (n_middle_idx >1) |
a2824256 | 0:4be500de690c | 180 | n_ratio_average =( an_ratio[n_middle_idx-1] +an_ratio[n_middle_idx])/2; // use median |
a2824256 | 0:4be500de690c | 181 | else |
a2824256 | 0:4be500de690c | 182 | n_ratio_average = an_ratio[n_middle_idx ]; |
a2824256 | 0:4be500de690c | 183 | |
a2824256 | 0:4be500de690c | 184 | if( n_ratio_average>2 && n_ratio_average <184){ |
a2824256 | 0:4be500de690c | 185 | n_spo2_calc= uch_spo2_table[n_ratio_average] ; |
a2824256 | 0:4be500de690c | 186 | *pn_spo2 = n_spo2_calc ; |
a2824256 | 0:4be500de690c | 187 | *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 |
a2824256 | 0:4be500de690c | 188 | } |
a2824256 | 0:4be500de690c | 189 | else{ |
a2824256 | 0:4be500de690c | 190 | *pn_spo2 = -999 ; // do not use SPO2 since signal ratio is out of range |
a2824256 | 0:4be500de690c | 191 | *pch_spo2_valid = 0; |
a2824256 | 0:4be500de690c | 192 | } |
a2824256 | 0:4be500de690c | 193 | } |
a2824256 | 0:4be500de690c | 194 | |
a2824256 | 0:4be500de690c | 195 | |
a2824256 | 0:4be500de690c | 196 | 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) |
a2824256 | 0:4be500de690c | 197 | /** |
a2824256 | 0:4be500de690c | 198 | * \brief Find peaks |
a2824256 | 0:4be500de690c | 199 | * \par Details |
a2824256 | 0:4be500de690c | 200 | * Find at most MAX_NUM peaks above MIN_HEIGHT separated by at least MIN_DISTANCE |
a2824256 | 0:4be500de690c | 201 | * |
a2824256 | 0:4be500de690c | 202 | * \retval None |
a2824256 | 0:4be500de690c | 203 | */ |
a2824256 | 0:4be500de690c | 204 | { |
a2824256 | 0:4be500de690c | 205 | maxim_peaks_above_min_height( pn_locs, pn_npks, pn_x, n_size, n_min_height ); |
a2824256 | 0:4be500de690c | 206 | maxim_remove_close_peaks( pn_locs, pn_npks, pn_x, n_min_distance ); |
a2824256 | 0:4be500de690c | 207 | *pn_npks = min( *pn_npks, n_max_num ); |
a2824256 | 0:4be500de690c | 208 | } |
a2824256 | 0:4be500de690c | 209 | |
a2824256 | 0:4be500de690c | 210 | 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) |
a2824256 | 0:4be500de690c | 211 | /** |
a2824256 | 0:4be500de690c | 212 | * \brief Find peaks above n_min_height |
a2824256 | 0:4be500de690c | 213 | * \par Details |
a2824256 | 0:4be500de690c | 214 | * Find all peaks above MIN_HEIGHT |
a2824256 | 0:4be500de690c | 215 | * |
a2824256 | 0:4be500de690c | 216 | * \retval None |
a2824256 | 0:4be500de690c | 217 | */ |
a2824256 | 0:4be500de690c | 218 | { |
a2824256 | 0:4be500de690c | 219 | int32_t i = 1, n_width; |
a2824256 | 0:4be500de690c | 220 | *pn_npks = 0; |
a2824256 | 0:4be500de690c | 221 | |
a2824256 | 0:4be500de690c | 222 | while (i < n_size-1){ |
a2824256 | 0:4be500de690c | 223 | if (pn_x[i] > n_min_height && pn_x[i] > pn_x[i-1]){ // find left edge of potential peaks |
a2824256 | 0:4be500de690c | 224 | n_width = 1; |
a2824256 | 0:4be500de690c | 225 | while (i+n_width < n_size && pn_x[i] == pn_x[i+n_width]) // find flat peaks |
a2824256 | 0:4be500de690c | 226 | n_width++; |
a2824256 | 0:4be500de690c | 227 | if (pn_x[i] > pn_x[i+n_width] && (*pn_npks) < 15 ){ // find right edge of peaks |
a2824256 | 0:4be500de690c | 228 | pn_locs[(*pn_npks)++] = i; |
a2824256 | 0:4be500de690c | 229 | // for flat peaks, peak location is left edge |
a2824256 | 0:4be500de690c | 230 | i += n_width+1; |
a2824256 | 0:4be500de690c | 231 | } |
a2824256 | 0:4be500de690c | 232 | else |
a2824256 | 0:4be500de690c | 233 | i += n_width; |
a2824256 | 0:4be500de690c | 234 | } |
a2824256 | 0:4be500de690c | 235 | else |
a2824256 | 0:4be500de690c | 236 | i++; |
a2824256 | 0:4be500de690c | 237 | } |
a2824256 | 0:4be500de690c | 238 | } |
a2824256 | 0:4be500de690c | 239 | |
a2824256 | 0:4be500de690c | 240 | |
a2824256 | 0:4be500de690c | 241 | void maxim_remove_close_peaks(int32_t *pn_locs, int32_t *pn_npks, int32_t *pn_x, int32_t n_min_distance) |
a2824256 | 0:4be500de690c | 242 | /** |
a2824256 | 0:4be500de690c | 243 | * \brief Remove peaks |
a2824256 | 0:4be500de690c | 244 | * \par Details |
a2824256 | 0:4be500de690c | 245 | * Remove peaks separated by less than MIN_DISTANCE |
a2824256 | 0:4be500de690c | 246 | * |
a2824256 | 0:4be500de690c | 247 | * \retval None |
a2824256 | 0:4be500de690c | 248 | */ |
a2824256 | 0:4be500de690c | 249 | { |
a2824256 | 0:4be500de690c | 250 | |
a2824256 | 0:4be500de690c | 251 | int32_t i, j, n_old_npks, n_dist; |
a2824256 | 0:4be500de690c | 252 | |
a2824256 | 0:4be500de690c | 253 | /* Order peaks from large to small */ |
a2824256 | 0:4be500de690c | 254 | maxim_sort_indices_descend( pn_x, pn_locs, *pn_npks ); |
a2824256 | 0:4be500de690c | 255 | |
a2824256 | 0:4be500de690c | 256 | for ( i = -1; i < *pn_npks; i++ ){ |
a2824256 | 0:4be500de690c | 257 | n_old_npks = *pn_npks; |
a2824256 | 0:4be500de690c | 258 | *pn_npks = i+1; |
a2824256 | 0:4be500de690c | 259 | for ( j = i+1; j < n_old_npks; j++ ){ |
a2824256 | 0:4be500de690c | 260 | n_dist = pn_locs[j] - ( i == -1 ? -1 : pn_locs[i] ); // lag-zero peak of autocorr is at index -1 |
a2824256 | 0:4be500de690c | 261 | if ( n_dist > n_min_distance || n_dist < -n_min_distance ) |
a2824256 | 0:4be500de690c | 262 | pn_locs[(*pn_npks)++] = pn_locs[j]; |
a2824256 | 0:4be500de690c | 263 | } |
a2824256 | 0:4be500de690c | 264 | } |
a2824256 | 0:4be500de690c | 265 | |
a2824256 | 0:4be500de690c | 266 | // Resort indices longo ascending order |
a2824256 | 0:4be500de690c | 267 | maxim_sort_ascend( pn_locs, *pn_npks ); |
a2824256 | 0:4be500de690c | 268 | } |
a2824256 | 0:4be500de690c | 269 | |
a2824256 | 0:4be500de690c | 270 | void maxim_sort_ascend(int32_t *pn_x,int32_t n_size) |
a2824256 | 0:4be500de690c | 271 | /** |
a2824256 | 0:4be500de690c | 272 | * \brief Sort array |
a2824256 | 0:4be500de690c | 273 | * \par Details |
a2824256 | 0:4be500de690c | 274 | * Sort array in ascending order (insertion sort algorithm) |
a2824256 | 0:4be500de690c | 275 | * |
a2824256 | 0:4be500de690c | 276 | * \retval None |
a2824256 | 0:4be500de690c | 277 | */ |
a2824256 | 0:4be500de690c | 278 | { |
a2824256 | 0:4be500de690c | 279 | int32_t i, j, n_temp; |
a2824256 | 0:4be500de690c | 280 | for (i = 1; i < n_size; i++) { |
a2824256 | 0:4be500de690c | 281 | n_temp = pn_x[i]; |
a2824256 | 0:4be500de690c | 282 | for (j = i; j > 0 && n_temp < pn_x[j-1]; j--) |
a2824256 | 0:4be500de690c | 283 | pn_x[j] = pn_x[j-1]; |
a2824256 | 0:4be500de690c | 284 | pn_x[j] = n_temp; |
a2824256 | 0:4be500de690c | 285 | } |
a2824256 | 0:4be500de690c | 286 | } |
a2824256 | 0:4be500de690c | 287 | |
a2824256 | 0:4be500de690c | 288 | void maxim_sort_indices_descend(int32_t *pn_x, int32_t *pn_indx, int32_t n_size) |
a2824256 | 0:4be500de690c | 289 | /** |
a2824256 | 0:4be500de690c | 290 | * \brief Sort indices |
a2824256 | 0:4be500de690c | 291 | * \par Details |
a2824256 | 0:4be500de690c | 292 | * Sort indices according to descending order (insertion sort algorithm) |
a2824256 | 0:4be500de690c | 293 | * |
a2824256 | 0:4be500de690c | 294 | * \retval None |
a2824256 | 0:4be500de690c | 295 | */ |
a2824256 | 0:4be500de690c | 296 | { |
a2824256 | 0:4be500de690c | 297 | int32_t i, j, n_temp; |
a2824256 | 0:4be500de690c | 298 | for (i = 1; i < n_size; i++) { |
a2824256 | 0:4be500de690c | 299 | n_temp = pn_indx[i]; |
a2824256 | 0:4be500de690c | 300 | for (j = i; j > 0 && pn_x[n_temp] > pn_x[pn_indx[j-1]]; j--) |
a2824256 | 0:4be500de690c | 301 | pn_indx[j] = pn_indx[j-1]; |
a2824256 | 0:4be500de690c | 302 | pn_indx[j] = n_temp; |
a2824256 | 0:4be500de690c | 303 | } |
a2824256 | 0:4be500de690c | 304 | } |