determining time interval between peaks using a for loop

20 views (last 30 days)
i have a set of data for volume flow rate (vfr) and plotted a graph vfr against time. i have to find the time interval between peaks using a for loop but am unsure how to write this loop
  3 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 15 Nov 2018
Edited: Adam Danz on 15 Nov 2018
I agree with jonas, a for-loop is unneeded. I also agree that anyone would need a sample dataset to determine the best approach. In the mean time, here's a general approach assuming your data are stored in a vector named vfr.
1) find the peaks using findpeaks() or whatever methed you're already using
[~, loc] = findpeaks(vfr);
loc will be an index vector that identifies which values in vfr were identified as peaks.
2) Assuming the data in vfr were sampled at a fixed interval 'intv' (say, every 10 ms), all you need to do is determine the number of values between each peak and multiply that number by the sample interval.
timeBetweenPeaks = diff(loc) * intv;
timeBetweenPeaks is a vector with a length one shorter than loc and is the time between each detected peak.
It's fairly straight forward to turn that into a for-loop but not necessary.
  2 Comments
Adam Danz
Adam Danz on 15 Nov 2018
Edited: Adam Danz on 15 Nov 2018
I'll explain it with words so you can explore some coding which is the best way to learn. But please note that this method is really silly.
loc is a vector of indices such as [10 23 42 99]. That means the 10th , 23rd, etc element of vfr has been identified as a peak. Using a for-loop
for i = __ : __
[your code]
end
you want to loop through the 2nd element of loc to the end of loc. That is, your loops will be 2 to length(loc). Within each loop, you'll need to get the distance between peak i and peak i-1 and you'll need to store the result in a new vector timeBetweenPeaks. The distance is merely (loc(i)-loc(i-1)) * intv.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!