Calculate duration of Peaks using findpeaks function
8 views (last 30 days)
I am currently working on an analysis of storm events. I am interested in finding the duration of storms defined by windspeeds that exceed a particular threshold:
Currently, I have used the findpeaks() function to identify the location (timestamps) and maximum intensity (peak value) of the windspeeds. I have done this as follows:
[PKS,LOCS] = findpeaks(Windspeed,time,'MinPeakHeight',th)
Here, windspeed is a vector containing hourly windspeed measurements, time is vector with corresponding datenum values for the measurements of the windspeed. Finally, th is the thresholds for when windspeeds are considered a storm events.
Now, I also want to know the duration of the storm events. That is, the time between when the windspeed first exceeds th until it agains drops below th.
I have tried using 'WidthReference', however, this only gives the options 'halfprom' and 'halfheight' and both don't give the values I am looking for.
Can anyone help me with this. Many thanks in advance!
Star Strider on 10 Feb 2022
Try something like this —
t = linspace(0, 24*30-1, 2*30); % Simulate Time
wv = 2 + sin(2*pi*t/30) .* cos(2*pi*t/24); % Simulate Data
th = 2.4; % Set Threshold
idx = find(diff(sign(wv - th))); % Approximate Indices Where 'wv' Crosses 'th'
for k = 1:numel(idx)
idxrng = max([1,idx(k)-1]) : min([numel(t),idx(k)+1]); % Index Range For Interpolation
t_th(k,:) = interp1(wv(idxrng), t(idxrng), th); % More Precise Time Estimates For 'th' Crossings
t_th = t_th(1:2*floor(numel(t_th)/2)); % Delete Incomplete Events
t_thm = reshape(t_th, 2, ); % Matrix Of Paired Events
EventDuration = diff(t_thm)
plot(t, ones(size(t))*th, ':k')
plot(t_thm, ones(size(t_thm))*th, '|-r')
text(mean(t_thm), ones(size(mean(t_thm)))*th, compose('Duration = %.2f\\rightarrow', EventDuration), 'Horiz','right', 'Vert','bottom', 'Rotation',-60)
It may be necessary for you to adapt this to your data. It may work as written, however it should be straightforward to change it if required.