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 件のコメント

jonas
jonas 2018 年 11 月 15 日
編集済み: jonas 2018 年 11 月 15 日
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.
samah khalil
samah khalil 2018 年 11 月 15 日
yeah i have to use a for loop
jonas
jonas 2018 年 11 月 15 日
Nothing I can do if you do not upload the data.

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2018 年 11 月 15 日
編集済み: Adam Danz 2018 年 11 月 15 日

0 投票

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 件のコメント

samah khalil
samah khalil 2018 年 11 月 15 日
how would i go about changing it into a for loop
Adam Danz
Adam Danz 2018 年 11 月 15 日
編集済み: Adam Danz 2018 年 11 月 15 日
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 ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 11 月 15 日

編集済み:

2018 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by