MATLAB: Zone identification on curve

identificationstrairs

How can I automatically identify the start and end of a staircase function?
As in the following figure, I would like to automatically identify the beginning and the end (the whole part in red). It is important to note that there are several areas to identify on the same vector.
I have tried with the function "find" and "min" and "max" but it does not work perfectly when the data changes.
Should a function be able to identify a staircase function?

Best Answer

Try this:
D = load('identification.mat');
var = D.var;
x = 0:numel(var)-1;
varfilt = sgolayfilt(var, 5, 271);
Lmx = find(islocalmax(varfilt, 'MinSeparation',900));
Lmn = find(islocalmin(varfilt, 'MinSeparation',900));
figure
plot(x, var)
hold on
% plot(x, varfilt)
for k = 1:numel(Lmn)
idxrng = Lmn(k):Lmx(k);
plot(x(idxrng), var(Lmn(k):Lmx(k)),'-r', 'LineWidth',2)
end
% plot(x(Lmx), var(Lmx), '^r')
% plot(x(Lmn), var(Lmn), 'vr')
hold off
grid
producing this plot:
That is likely as good as it is possible to get. I left other graphics calls in the code (commented-out) so if you un-comment them, you can see how the code works in more detail.
.