Try this —
LD1 = load('t.mat');
ty = LD1.ty.';
LD2 = load('y.mat');
sh1 = LD2.sh1;
y = sh1.';
[pks,plocs] = findpeaks(y, 'MinPeakProminence',0.5);
[vys,vlocs] = findpeaks(-y, 'MinPeakProminence',0.0000001);
for k = 1:numel(pks)
vlcs(1,k) = max(vlocs(vlocs<=plocs(k)));
vlcs(2,k) = min(vlocs(vlocs>=plocs(k)));
end
deltat = -diff(ty(vlcs));
figure
plot(ty,sh1)
hold on
plot(ty(plocs), pks, '^r')
plot(ty(vlcs), sh1(vlcs),'.r')
hold off
grid
xlabel('Time')
ylabel('Amplitude')
text(mean(ty(vlcs)),zeros(size(plocs)), compose('\\leftarrow \\Delta t = %.6f',deltat), 'Horiz','left', 'Vert','middle', 'Rotation',60)
producing —
EDIT — (9 Jul 2022 at 17:24)
Minor edit to my original code to make it more robust and more efficient. It produces the same reault. I have no idea how to make it more robust with respect to other data sets, since I have only one data set to work with here, and it works correctly. It will be necessary to experiment with it to get it to work with other data sets. One possibiity is to experiment with the name-value pair arguments in the second findpeaks call to be certain that it detects the beginning and end ‘valleys’.for each peak, and rejects any noise that may be in the data. My code requires that each peak has valleys on either side of it, since they are in the posted data.
EDIT — (9 Jul 2022 at 22:58)
This slightly revised code works for both sets of files —
LD1 = load('Abdelhakim Souidi (2) t.mat');
ty = LD1.ty.';
LD2 = load('Abdelhakim Souidi(2) y.mat');
sh1 = LD2.sh1;
y = sh1.';
[pks,plocs] = findpeaks(y, 'MinPeakProminence',0.1, 'MinPeakDistance',250);
[vys,vlocs] = findpeaks(-y, 'MinPeakProminence',0.0001, 'MinPeakDistance',500);
for k = 1:numel(pks)
vlcs(1,k) = max(vlocs(vlocs<=plocs(k)));
vlcs(2,k) = min(vlocs(vlocs>=plocs(k)));
end
deltat = diff(ty(vlcs));
figure
plot(ty,sh1)
hold on
plot(ty(plocs), pks, '^r')
plot(ty(vlcs), sh1(vlcs),'.r')
hold off
grid
xlabel('Time')
ylabel('Amplitude')
text(mean(ty(vlcs)),zeros(size(plocs)), compose('\\leftarrow \\Delta t = %.6f',deltat), 'Horiz','left', 'Vert','middle', 'Rotation',60)
producing —
It matters not which peak of the double-peak envelopes are identified as the actual peak, since the baseline duration is the desired result, and that simply depends on the relation of individual ‘plocs’ indices and the nearest ‘vlocs’ indices.
.