How to find time interval between peaks

Hi All,

I want to find the time interval between two adjacent peaks of an analog signal.Then find the same for the next 10 peaks.Please help me to find how to get the time value of each peak.Thanks in advance.

Please help me to find how to get the time value of each peak.

What does your code look like for determining that a given reading is a peak? In that if statement, it is trivial to call micros() to determine when the peak happened.

Define a peak.

First you'll have to define "peak" precisely - typically you'd need two previous readings and
compare the middle one against the one before it and the current - if its bigger than either
it is a peak.

However this isn't going to detect a flat peak - you'd need to do a little thinking about that. And
you might have to think about how to handle the first few readings (when you don't have two previous
readings)

Once you can identify a peak, you record the time it was detected. Then subtract the current
and previous times for peaks to get the interval.

micros() or millis()

(but the time at “a=c” isn’t a peak by any definition)… :slight_smile:

I got the peak values.

No. You got some values. You haven't a clue whether they represent peaks, troughs, or somewhere in between.

Reposting without a lot of white space:

for (i = 0; i < 200; i++) {
  c = analogRead(A0);
  if (c > a)
  {
    a = c;  // this happens whenever the signal is greater than a previous maximum
  }
  else if (c < b) {
    b = c;
  }
}

Unrelated variables and code removed for clarity.

If a sine wave was fed to this algorithm, it would record points along the first half cycle, and then ignore subsequent cycles. Hardly a peak detector.

If you are SURE that they are peaks and/or troughs, why is is so damned difficult to call micros() when you have discovered one?

Your assumption that one of the reads will happen to occur exactly when the input reaches a peak is not warranted.

Reposting without a lot of white space:

but inconsistent brace usage...

PaulS:
but inconsistent brace usage...

There is a limit to what ctrl-T will do.

bisni:
I want to find the time interval between two adjacent peaks of an analog signal.

What type of analog signal?

A pure sine wave form? A pure triangle form?

If it is a sine or triangle wave form, you could watch out for the slope of the curve between two points:
value is rising ==> slope positive
value is falling ==> slope negative

And the "peak" would then detected be when the slope becomes negative after being positive before.

Using peak detector algorithm we can find the maximum values.

If you record the time you get each value, then when you've found the maximum value, you know when that value happened.

bisni:
May I Know how to record the time?

Arduino provides two timing functions "millis()" and "micros()".

Use the one that fits your timing needs!

EVERY time you take a reading, record the time.Until you do this, you are wasting your time and ours. And I am sick of you wasting my time. Engage your brain for a while before posting again.