I have a sensor which I am recording samples from. I am trying to find local max's within 256 samples of the data. I am storing the samples into an array and looking through the array to identify the local max's. unfortunately it is returning the same max's when the value next to it is equal (see below).
IE: Next max of 456 at index 45
Next max of 456 at index 46
Next max of 456 at index 47
Next max of 456 at index 48
Next max of 456 at index 49
I am looking for a way to compare those values and somehow return the value in the middle and only that value along with its corresponding index. (Next max of 456 at index 47) and when there are even amount of equal values the index can be index.5 or whatever is possible. What can I do?
int inp[256];
void setup()
{
Serial.begin(9600); // for debugging
}
void loop()
{
int max_i = 0;
int max_v = 0;
for (int i = 0; i < 256; i++)
{
if ( inp[i] > max_v )
{
max_v = inp[i];
max_i = i;
}
}
Serial.println((String)"\nGlobal Max is " + max_v + " at index " + max_i);
//Serial.println(max_v);
int i;
for (i = 0 ; i < 256 ; i ++) // save 256 samples
{
inp[i] = analogRead(A0);
delay(10);
} // close for
int N = 15; // loc max neighborhood size
for (int i = N-1; i < 255-N; i++)
{
bool loc = true;
for (int j = 1; j < N; j++) // look N-1 back and N-1 ahead
{
if (inp[i] < inp[i-j] || inp[i] < inp[i+j]) loc = false;
}
if (loc == true)
{
Serial.println((String)"Next max of " + inp[i] + " at index " + i);
}
//Serial.println(inp[i]);
}
Serial.println("-------------");
} // close main loop