peak detection

Dependents:   mbed-test-i2c-PCA-biquad-peakdet Mix-code-v2 mbed-os-step-counting the-lastest-code

Revision:
1:d1dac3b2692a
Parent:
0:59ce24b43be2
Child:
2:6500ec04a6a1
--- a/peak.cpp	Mon Nov 25 14:26:44 2019 +0000
+++ b/peak.cpp	Mon Nov 25 23:36:28 2019 +0000
@@ -1,9 +1,15 @@
 #include "peak.h"
 #include <iostream>
+#include <algorithm>
+using namespace std;
+#define max(a, b)  (((a) > (b)) ? (a) : (b))
+#define min(a, b)  (((a) > (b)) ? (b) : (a))
 
-void PEAK::findPeaks(vector<float> num,int count, float threshold)  
+int PEAK::findPeaks(vector<float> num,int count, float &height)  
 {  
     vector<int> sign;  
+    float min;
+    float threshold;
     for(int i = 1;i<count;i++)  
     {  
         /*do difference between the near points: 
@@ -26,11 +32,13 @@
             sign.push_back(0);  
         }  
     }  
-    //do difference between the near sign
-    //result < 0 save the max index
-    //result > 0 save the min index 
+    /*do difference between the near sign
+     *result < 0 save the max index
+     *result > 0 save the min index 
+     */
     vector<int> indMax;  
-    vector<int> indMin;  
+    vector<int> indMin; 
+    vector<float> valMax;  
     int cnt = 0;
     for(int j = 1;j<sign.size();j++)  
     {  
@@ -63,16 +71,33 @@
     for(int j = 0; j < indMax.size(); j++)
     {
         
-        if(num[indMax[j]] > threshold)
+        if(num[indMax[j]] > height)
         {
             cnt = cnt +1;    
-            //printf("found a valid max, index:%d, value: %f \n\r", indMax[j],num[indMax[j]]);
+            //push the max values into the vector
+            valMax.push_back(num[indMax[j]]);
         }
     }
     
+    threshold = *min_element(valMax.begin(),valMax.end())* 0.3;
     
-    printf("num of max: %d\n\r", indMax.size());
-    printf("num of step: %d\n\r", cnt);
+    printf("height in peak: %f\n\r", height);
+    printf("threshold: %f\n\r", threshold);
+    
+    if(height > 0.1001)
+    {
+        threshold = min(threshold,height);
+        //printf("I am in !!!!!!\n\r");
+        
+    }
+    
+    threshold = max(threshold, 0.1);
+    threshold = min(threshold, 0.2);
+    height = threshold;
+    return cnt;
+    
+    //printf("num of max: %d\n\r", indMax.size());
+    //printf("num of step: %d\n\r", cnt);
     
    // cout<<"max values:"<<endl;
 //    for(int m = 0;m<indMax.size();m++)