Deniz Dosluoglu / Mbed 2 deprecated RD117_MBED_mod

Dependencies:   ToScreen mbed

Fork of RD117_MBED by Maxim Integrated

Revision:
5:27d600e46926
Parent:
4:5273ab1085ab
diff -r 5273ab1085ab -r 27d600e46926 algorithm/algorithm.cpp
--- a/algorithm/algorithm.cpp	Thu May 19 22:04:06 2016 +0000
+++ b/algorithm/algorithm.cpp	Tue Apr 10 03:36:39 2018 +0000
@@ -96,7 +96,7 @@
     int32_t n_nume,  n_denom ;
     // remove DC of ir signal    
     un_ir_mean =0; 
-    for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ;
+    for (k=0 ; k<n_ir_buffer_length ; k++ ) un_ir_mean += pun_ir_buffer[k] ; //take ir signal, sum everything
     un_ir_mean =un_ir_mean/n_ir_buffer_length ;
     for (k=0 ; k<n_ir_buffer_length ; k++ )  an_x[k] =  pun_ir_buffer[k] - un_ir_mean ; 
     
@@ -118,7 +118,7 @@
     
     // hamming window
     // flip wave form so that we can detect valley with peak detector
-    for ( i=0 ; i<BUFFER_SIZE-HAMMING_SIZE-MA4_SIZE-2 ;i++){
+    for ( i=0 ; i<BUFFER_SIZE-HAMMING_SIZE-MA4_SIZE-2 ;i++){ //0 to 500-5-4-2
         s= 0;
         for( k=i; k<i+ HAMMING_SIZE ;k++){
             s -= an_dx[k] *auw_hamm[k-i] ; 
@@ -129,12 +129,12 @@
  
     n_th1=0; // threshold calculation
     for ( k=0 ; k<BUFFER_SIZE-HAMMING_SIZE ;k++){
-        n_th1 += ((an_dx[k]>0)? an_dx[k] : ((int32_t)0-an_dx[k])) ;
+        n_th1 += ((an_dx[k]>0)? an_dx[k] : ((int32_t)0-an_dx[k])) ; //if signal is positive add, if negative flip size then add
     }
-    n_th1= n_th1/ ( BUFFER_SIZE-HAMMING_SIZE);
+    n_th1= n_th1/ ( BUFFER_SIZE-HAMMING_SIZE); //take avg for threshold
     // peak location is acutally index for sharpest location of raw signal since we flipped the signal         
     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 
-
+//find peaks 8 samples apart, 5 highest samples
     n_peak_interval_sum =0;
     if (n_npks>=2){
         for (k=1; k<n_npks; k++)
@@ -178,12 +178,12 @@
                 n_exact_ir_valley_locs_count ++ ;
         }
     }
-    if (n_exact_ir_valley_locs_count <2 ){
+    if (n_exact_ir_valley_locs_count <2 ){ //if location of ir valley locations is less than 2
        *pn_spo2 =  -999 ; // do not use SPO2 since signal ratio is out of range
        *pch_spo2_valid  = 0; 
        return;
     }
-    // 4 pt MA
+    // 4 pt MA = moving average //take four point moving avg again, why
     for(k=0; k< BUFFER_SIZE-MA4_SIZE; k++){
         an_x[k]=( an_x[k]+an_x[k+1]+ an_x[k+2]+ an_x[k+3])/(int32_t)4;
         an_y[k]=( an_y[k]+an_y[k+1]+ an_y[k+2]+ an_y[k+3])/(int32_t)4;
@@ -211,7 +211,7 @@
         if (an_exact_ir_valley_locs[k+1]-an_exact_ir_valley_locs[k] >10){
             for (i=an_exact_ir_valley_locs[k]; i< an_exact_ir_valley_locs[k+1]; i++){
                 if (an_x[i]> n_x_dc_max) {n_x_dc_max =an_x[i];n_x_dc_max_idx =i; }
-                if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i];n_y_dc_max_idx=i;}
+                if (an_y[i]> n_y_dc_max) {n_y_dc_max =an_y[i];n_y_dc_max_idx=i; }
             }
             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
             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])  ; 
@@ -223,7 +223,7 @@
             n_x_ac=  an_x[n_y_dc_max_idx] - n_x_ac;      // subracting linear DC compoenents from raw 
             n_nume=( n_y_ac *n_x_dc_max)>>7 ; //prepare X100 to preserve floating value
             n_denom= ( n_x_ac *n_y_dc_max)>>7;
-            if (n_denom>0  && n_i_ratio_count <5 &&  n_nume != 0)
+            if (n_denom>0  && n_i_ratio_count <5 &&  n_nume != 0) //take averages of ratios
             {   
                 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) ;
                 n_i_ratio_count++;
@@ -262,7 +262,7 @@
 {
     maxim_peaks_above_min_height( pn_locs, pn_npks, pn_x, n_size, n_min_height );
     maxim_remove_close_peaks( pn_locs, pn_npks, pn_x, n_min_distance );
-    *pn_npks = min( *pn_npks, n_max_num );
+    *pn_npks = min( *pn_npks, n_max_num ); // find number of peaks
 }
 
 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)