心拍・酸素飽和度モニタモジュール MAXREFDES117から取得した心拍の値でmicro:bit上のLEDに表示したハートを鼓動させるプログラムです。

Dependencies:   microbit

Fork of microbit-Heartbeat-serial by Junichi Katsu

Committer:
JKsoft_main
Date:
Tue Apr 17 12:11:39 2018 +0000
Revision:
2:d04db7a8d012
Parent:
1:83ace7df2c63
??

Who changed what in which revision?

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