peak detection

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

peak.cpp

Committer:
castlefei
Date:
2019-11-25
Revision:
2:6500ec04a6a1
Parent:
1:d1dac3b2692a

File content as of revision 2:6500ec04a6a1:

#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))

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: 
         *result<0,sign = -1 
         *result>0,sign = 1 
         *result=0,sign = 0 
         */  
        float diff = num[i] - num[i-1];  
        //printf("difference:%f",diff);
        if(diff>0)  
        {  
            sign.push_back(1);  
        }  
        else if(diff<0)  
        {  
            sign.push_back(-1);  
        }  
        else  
        {  
            sign.push_back(0);  
        }  
    }  
    /*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<float> valMax;  
    int cnt = 0;
    for(int j = 1;j<sign.size();j++)  
    {  
        int diff = sign[j]-sign[j-1];  
        if(diff<0)  
        {  
            indMax.push_back(j);  
        }  
        else if(diff>0)  
        {  
            indMin.push_back(j);  
        }  
    }  
    //try to find the valid max (which the previous min is smaller than threshold)
    //begin with index 1 so there will have +1 or -1 error
    //for(int j = 1;j<indMax.size();j++)  
//    {
//        
//        for(int i =0; i <indMin.size()-1; i++)
//        {
//            if(indMin[i]<indMax[j] && indMin[i+1]>indMax[j] && num[indMin[i]]< threshold)
//            {
//                cnt = cnt +1;    
//                printf("found a valid max, index:%d, value: %f \n\r", indMax[j],num[indMax[j]]);
//            }    
//        }
//        
//    }
    
    for(int j = 0; j < indMax.size(); j++)
    {
        
        if(num[indMax[j]] > height)
        {
            cnt = cnt +1;    
            //push the max values into the vector
            valMax.push_back(num[indMax[j]]);
        }
    }
    
    threshold = *min_element(valMax.begin(),valMax.end())* 0.3;
    
    //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++)  
//    {  
//        printf("index:%d, value: %f ", indMax[m],num[indMax[m]]);
//    }  
//    cout<<endl;  
//    cout<<"min values:"<<endl;  
//    for(int n = 0;n<indMin.size();n++)  
//    {  
//        cout<<num[indMin[n]]<<"  ";  
//    }  
//    cout<<endl;
}