In case you don't have the Signal Processing Toolbox, or its findpeaks function is not general enough, you could try the following function - though I don't claim it's the the fastest way possible!
function peaks = peakfind(v, x)
pks = true;
va = v(x+1 : end);
for k = 1:x
vb = v(x+1-k : end-k);
pksa = va > vb;
pksb = vb > va;
pks = pks & pksa(1 : end-x) & pksb(1+k : end-x+k);
end
peaks = find(pks) + x;
end
One thing to note: if you define a peak to be an element that is greater than all its neighbours, repeated values in the input vector will never be seen as a peak. So [ 1 100 100 1] (with X=1) has no peaks using your definition. If you want to find such plateaux, you can do it by breaking the symmetry and finding one of the values. In the code above, you could replace "pksb = vb > va" with "pksb = ~pksa" (and amending the description) to achieve this.
Best Answer