determining time interval between peaks using a for loop

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 个评论

Do you HAVE to use a loop? Becuse it would be really simple to solve it without a loop. It would also be easier to help you if you upload the data.
Nothing I can do if you do not upload the data.

请先登录,再进行评论。

 采纳的回答

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 个评论

how would i go about changing it into a for loop
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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by