# problem of using findpeaks

4 views (last 30 days)
jie hu on 28 Nov 2023
Answered: Chunru on 28 Nov 2023
I have two vectors of time (datetime formate) and elevation, but there is an error to use findpeaks(elev,time), saying "Expected X to be strictly increasing."

Star Strider on 28 Nov 2023
My guess is that the datetime values are not considered to be monotonically increasing because they span multiple years.
Try this as a work-around —
elev = LD.elev;
timing = LD.timing;
[pks,plocs] = findpeaks(elev);
[vys,vlocs] = findpeaks(-elev);
figure
plot(timing, elev, 'DisplayName','Data')
hold on
plot(timing(plocs), pks, '^r', 'MarkerSize',2.5, 'DisplayName','Peaks')
plot(timing(vlocs), -vys, 'vg', 'MarkerSize',2.5, 'DisplayName','Valleys')
hold off
grid
xlabel('timing')
ylabel('elev')
legend('Location','best')
figure
plot(timing, elev, 'DisplayName','Data')
hold on
plot(timing(plocs), pks, '^r', 'DisplayName','Peaks')
plot(timing(vlocs), -vys, 'vg', 'DisplayName','Valleys')
hold off
grid
xlabel('timing')
ylabel('elev')
legend('Location','best')
xlim([timing(1) timing(500)])
.

Chunru on 28 Nov 2023
% It seems that timing contains duplcate data.
% fild peaks require x-axis to be monotonically increasing.
% Workaround:
% sort the data
[timing, i] = sort(timing);
elev = elev(i);
% find the peaks and location
[pks, locs] = findpeaks(elev, MinPeakProminence=1);
plot(timing, elev);
hold on
plot(timing(locs), pks, 'rv', 'Markersize', 5)