MATLAB: Determining time interval between peaks using a for loop

matlab 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

Best Answer

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.