the first vision of peak detection. I try to detect the value max of a array that the previous min point is lower than a threshold given.
main.cpp@0:08dd14677b02, 2019-11-20 (annotated)
- Committer:
- castlefei
- Date:
- Wed Nov 20 11:56:33 2019 +0000
- Revision:
- 0:08dd14677b02
peak detection by wenchang Fei
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
castlefei | 0:08dd14677b02 | 1 | /* mbed Microcontroller Library |
castlefei | 0:08dd14677b02 | 2 | * Copyright (c) 2019 ARM Limited |
castlefei | 0:08dd14677b02 | 3 | * SPDX-License-Identifier: Apache-2.0 |
castlefei | 0:08dd14677b02 | 4 | */ |
castlefei | 0:08dd14677b02 | 5 | |
castlefei | 0:08dd14677b02 | 6 | #include "mbed.h" |
castlefei | 0:08dd14677b02 | 7 | #include "platform/mbed_thread.h" |
castlefei | 0:08dd14677b02 | 8 | #include <vector> |
castlefei | 0:08dd14677b02 | 9 | #include <iostream> |
castlefei | 0:08dd14677b02 | 10 | |
castlefei | 0:08dd14677b02 | 11 | // Blinking rate in milliseconds |
castlefei | 0:08dd14677b02 | 12 | #define BLINKING_RATE_MS 500 |
castlefei | 0:08dd14677b02 | 13 | using namespace std; |
castlefei | 0:08dd14677b02 | 14 | |
castlefei | 0:08dd14677b02 | 15 | void findPeaks(float *num,int count, float threshold) |
castlefei | 0:08dd14677b02 | 16 | { |
castlefei | 0:08dd14677b02 | 17 | vector<int> sign; |
castlefei | 0:08dd14677b02 | 18 | for(int i = 1;i<count;i++) |
castlefei | 0:08dd14677b02 | 19 | { |
castlefei | 0:08dd14677b02 | 20 | /*do difference between the near points: |
castlefei | 0:08dd14677b02 | 21 | *result<0,sign = -1 |
castlefei | 0:08dd14677b02 | 22 | *result>0,sign = 1 |
castlefei | 0:08dd14677b02 | 23 | *result=0,sign = 0 |
castlefei | 0:08dd14677b02 | 24 | */ |
castlefei | 0:08dd14677b02 | 25 | int diff = num[i] - num[i-1]; |
castlefei | 0:08dd14677b02 | 26 | if(diff>0) |
castlefei | 0:08dd14677b02 | 27 | { |
castlefei | 0:08dd14677b02 | 28 | sign.push_back(1); |
castlefei | 0:08dd14677b02 | 29 | } |
castlefei | 0:08dd14677b02 | 30 | else if(diff<0) |
castlefei | 0:08dd14677b02 | 31 | { |
castlefei | 0:08dd14677b02 | 32 | sign.push_back(-1); |
castlefei | 0:08dd14677b02 | 33 | } |
castlefei | 0:08dd14677b02 | 34 | else |
castlefei | 0:08dd14677b02 | 35 | { |
castlefei | 0:08dd14677b02 | 36 | sign.push_back(0); |
castlefei | 0:08dd14677b02 | 37 | } |
castlefei | 0:08dd14677b02 | 38 | } |
castlefei | 0:08dd14677b02 | 39 | //do difference between the near sign |
castlefei | 0:08dd14677b02 | 40 | //result < 0 save the max index |
castlefei | 0:08dd14677b02 | 41 | //result > 0 save the min index |
castlefei | 0:08dd14677b02 | 42 | vector<int> indMax; |
castlefei | 0:08dd14677b02 | 43 | vector<int> indMin; |
castlefei | 0:08dd14677b02 | 44 | int cnt = 1; |
castlefei | 0:08dd14677b02 | 45 | for(int j = 1;j<sign.size();j++) |
castlefei | 0:08dd14677b02 | 46 | { |
castlefei | 0:08dd14677b02 | 47 | int diff = sign[j]-sign[j-1]; |
castlefei | 0:08dd14677b02 | 48 | if(diff<0) |
castlefei | 0:08dd14677b02 | 49 | { |
castlefei | 0:08dd14677b02 | 50 | indMax.push_back(j); |
castlefei | 0:08dd14677b02 | 51 | } |
castlefei | 0:08dd14677b02 | 52 | else if(diff>0) |
castlefei | 0:08dd14677b02 | 53 | { |
castlefei | 0:08dd14677b02 | 54 | indMin.push_back(j); |
castlefei | 0:08dd14677b02 | 55 | } |
castlefei | 0:08dd14677b02 | 56 | } |
castlefei | 0:08dd14677b02 | 57 | //try to find the valid max (which the previous min is smaller than threshold) |
castlefei | 0:08dd14677b02 | 58 | //begin with index 1 so there will have +1 or -1 error |
castlefei | 0:08dd14677b02 | 59 | for(int j = 1;j<indMax.size();j++) |
castlefei | 0:08dd14677b02 | 60 | { |
castlefei | 0:08dd14677b02 | 61 | |
castlefei | 0:08dd14677b02 | 62 | for(int i =0; i <indMin.size()-1; i++) |
castlefei | 0:08dd14677b02 | 63 | { |
castlefei | 0:08dd14677b02 | 64 | if(indMin[i]<indMax[j] && indMin[i+1]>indMax[j] && num[indMin[i]]< threshold) |
castlefei | 0:08dd14677b02 | 65 | { |
castlefei | 0:08dd14677b02 | 66 | cnt = cnt +1; |
castlefei | 0:08dd14677b02 | 67 | printf("found a valid max, index:%d, value: %f \n\r", indMax[j],num[indMax[j]]); |
castlefei | 0:08dd14677b02 | 68 | } |
castlefei | 0:08dd14677b02 | 69 | } |
castlefei | 0:08dd14677b02 | 70 | |
castlefei | 0:08dd14677b02 | 71 | } |
castlefei | 0:08dd14677b02 | 72 | printf("num of max: %d\n\r", indMax.size()); |
castlefei | 0:08dd14677b02 | 73 | printf("num of valid max: %d\n\r", cnt); |
castlefei | 0:08dd14677b02 | 74 | |
castlefei | 0:08dd14677b02 | 75 | cout<<"max values:"<<endl; |
castlefei | 0:08dd14677b02 | 76 | for(int m = 0;m<indMax.size();m++) |
castlefei | 0:08dd14677b02 | 77 | { |
castlefei | 0:08dd14677b02 | 78 | cout<<num[indMax[m]]<<" "; |
castlefei | 0:08dd14677b02 | 79 | } |
castlefei | 0:08dd14677b02 | 80 | cout<<endl; |
castlefei | 0:08dd14677b02 | 81 | cout<<"min values:"<<endl; |
castlefei | 0:08dd14677b02 | 82 | for(int n = 0;n<indMin.size();n++) |
castlefei | 0:08dd14677b02 | 83 | { |
castlefei | 0:08dd14677b02 | 84 | cout<<num[indMin[n]]<<" "; |
castlefei | 0:08dd14677b02 | 85 | } |
castlefei | 0:08dd14677b02 | 86 | cout<<endl; |
castlefei | 0:08dd14677b02 | 87 | } |
castlefei | 0:08dd14677b02 | 88 | |
castlefei | 0:08dd14677b02 | 89 | int main() |
castlefei | 0:08dd14677b02 | 90 | { |
castlefei | 0:08dd14677b02 | 91 | // Initialise the digital pin LED1 as an output |
castlefei | 0:08dd14677b02 | 92 | DigitalOut led(LED1); |
castlefei | 0:08dd14677b02 | 93 | float a[] = {1,2,10,2,4,0,8,10,23,0,2, 0, 5}; |
castlefei | 0:08dd14677b02 | 94 | int len = sizeof(a)/sizeof(int); |
castlefei | 0:08dd14677b02 | 95 | while (true) { |
castlefei | 0:08dd14677b02 | 96 | //printf("len of a is %d \n\r", len); |
castlefei | 0:08dd14677b02 | 97 | findPeaks(a,len,1); |
castlefei | 0:08dd14677b02 | 98 | led = !led; |
castlefei | 0:08dd14677b02 | 99 | thread_sleep_for(BLINKING_RATE_MS); |
castlefei | 0:08dd14677b02 | 100 | } |
castlefei | 0:08dd14677b02 | 101 | } |