Time to peak using findpeaks

12 views (last 30 days)
David Santos
David Santos on 14 Sep 2025
Commented: Star Strider on 16 Sep 2025
I'm using findpeaks to locate multiple peaks in my function but I want to know the rising time to the peak.
Findpeaks gives you the 'width' output but its not working because it suposses that the peak is in the middle of the valleys (and its based on the prominece, it would be nice to be referenced to lowestPoint/left valley) but most of these peaks are not symetric so its not working properly, any ideas?

Accepted Answer

Star Strider
Star Strider on 14 Sep 2025
I usually have findpeaks return the indices of the peaks.
Your definition of the start time point isnot obvious. I would use interp1 to define it as a function of its amplitude, then subtract the time returned from the time of the peak.
Example --
t = datetime('07:00:00', InputFormat='HH:mm:ss') : minutes(1) : datetime('08:00:00', InputFormat='HH:mm:ss');
t.Format = 'HH:mm:ss';
t = t(:);
mp = round(numel(t)/2);
f = exp(-minutes(t - t(mp)).^2/50) * 1000 + 2500; % Create Data (Curve)
[pks,locs] = findpeaks(f) % Peak & Index
pks = 3500
locs = 31
thrshld = 2550; % Amplitude Threshold
idxrng = 1 : locs; % Index Rnage For Interpolation
thrshld_time = interp1(f(idxrng), t(idxrng), thrshld) % Interpolate To Find Associated Time
thrshld_time = datetime
07:17:43
t_interval = t(locs) - thrshld_time
t_interval = duration
00:12:16
figure
plot(t, f)
hold on
plot([thrshld_time thrshld_time], [2550 2750], ":r")
plot([thrshld_time t(locs)], 2750*[1 1], '|-r')
hold off
grid
yline(thrshld, '--r')
xline(t(locs), '--r')
text(thrshld_time+t_interval/2, 2750, sprintf('\\leftarrow \\Delta = %s', t_interval), Rotation=45)
axis('padded')
.
  8 Comments
David Santos
David Santos on 16 Sep 2025
Thanks a lot this is a great work!
Star Strider
Star Strider on 16 Sep 2025
As always, my pleasure!
Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!