Keep maximum value with analog reading

Hi all :slight_smile:

I need to analyze an analog signal to keep only maximum values.
the signal is some kind of sinusoide curve.

I need to display only the maximum values reach at each peak.
What is the best way to do it?
the signal will be sample around 500Hz or 1kHz.
My first idea was to keep 10 or 20 last values in an array, compare any new measure and keep it if it's more than all in the array.
Doing this to avoid multiple peaks when the analog signal is reaching the maximum value, due to sensor accuracy.

Is it a good way to do it? do you have any better approach ?

Thank you for your replies

What is the best way to do it?

Use a variable.

My first idea was to keep 10 or 20 last values in an array, compare any new measure and keep it if it's more than all in the array.
Doing this to avoid multiple peaks when the analog signal is reaching the maximum value, due to sensor accuracy.

If you are looking for the maximum value, does it matter how many times the maximum occurs in some period of time?

do you have any better approach ?

Set a variable to zero
If the analogue input is greater than the variable then set the variable to the input value

No need for an array

I need to measure a pressure in a chamber, this pressure is going up and down around 250 times per second.

The measure is done around 1kHz, due to the accuracy of the sensor I'll have multiple close values when the pressure is reaching the maximum, and I guess some kind of up and down measures at this point.
So I need to keep only the real peak value over a short period of time, a simple variable will not do it.

M4vrick:
a simple variable will not do it.

Why not? Just read the current value, and if it's larger than the known max, replace known max with the new one, else don't.

oh yes! I understand.
Not comparing to the last value but to the known maximum value ...
And reseting when going down enough to ensure the pressure peak is behind.

It's so easy I've not think of it :lol:

Many thanks to all :slight_smile:

Not comparing to the last value but to the known maximum value ...

As I suggested earlier.....

Yes :wink:

English is not my first language... sometimes it's hard for me to understand.

Thank you.

The best way is to emulate a peak-detector circuit - you set the value to the reading
if the reading is larger, so you get the maximum. But otherwise you must gradually
decay the reading back down again so it can track a varying signal (but on a longer
timescale than the period of the waveform.

This suggests a simple low pass digital filter:

float peak_value = 0.0 ;  // must be float for digital filtering

float sample_peak (byte pin)
{
  int new_val = analogRead (pin) ;
  if (new_val > peak_value)
    peak_value = new_value ;
  else
    peak_value -= 0.001 * peak_value ; // simple exponential decay

  return peak_value * 5.0 / 1024 ;  // return as volts perhaps
}

Interesting idea :smiley:

I was thinking of something like this but more simple, I know the low level which can be used to trigger a new measure.
So simply comparing the value to the known maximum to have it, and comparing to the predefined low level to begin a new measure when going under.