1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <vector>
using namespace std;
// Prototypes
vector<int> getData();
void findPeaks( const vector<int> & );
//======================================================================
int main()
{
vector<int> amp = getData();
findPeaks( amp );
}
//======================================================================
vector<int> getData()
{
// Hard-code for testing; better read from file in future
// Data from original post
// vector<int> amp = { 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 1, 9, 35, 88, 115, 212, 240, 237, 200,
// 145, 87, 42, 18, 12, 13, 14, 15, 15, 14, 13, 10, 8, 8, 8, 8, 7, 6, 6, 4, 4, 4,
// 3, 4, 5, 6, 4, 4, 3, 2, 2, 1, 1, 0, 1, 2, 3, 4, 4, 2 };
// Keskiverto data
vector<int> amp = { 0, 1, 2, 2, 2, 1, 1, 1, 4, 1, 1, 0 };
return amp;
}
//======================================================================
void findPeaks( const vector<int> & )
{
const int NOISE = -1; // Level up to and including which peaks will be excluded
int wideStart = -1; // The start of any current wide peak
int grad = -1; // Sign of gradient (almost)
// = 1 for increasing
// = 0 for level AND PREVIOUSLY INCREASING (so potential wide peak)
// = -1 for decreasing OR level, but previously decreasing
// A sharp peak is identified by grad=1 -> grad=-1
// A wide peak is identified by grad=0 -> grad=-1
for ( int i = 0; i < amp.size() - 1; i++ )
{
if ( amp[i+1] < amp[i] ) // Only possibility of a peak
{
if ( grad == 1 && amp[i] > NOISE )
{
cout << "Sharp peak of " << amp[i] << " at i = " << i << '\n';
}
else if ( grad == 0 && amp[i] > NOISE )
{
cout << "Wide peak of " << amp[i] << " from i = " << wideStart << " to " << i << '\n';
}
grad = -1;
}
else if ( amp[i+1] == amp[i] ) // Check for start of a wide peak
{
if ( grad == 1 )
{
wideStart = i;
grad = 0;
}
}
else
{
grad = 1;
}
}
}
//======================================================================
|