x = 0 : pi/20 : 5*pi;
y = sin(x) + x/3 + randn(1,length(x))/4;
z = smooth(y,20)';

maxL = [1, z(2:end) > z(1:end-1)];
maxR = [z(1:end-1) > z(2:end), 1];
localMax = maxL & maxR;
px = x(localMax);
px(2,:) = 0;
px(3,:) = NaN;
pz = z(localMax);
pz(2,:) = z(localMax);
pz(3,:) = NaN;

clf, hold on
plot(x,y,'bo:')
plot(x,z,'m','LineWidth',3)
plot(px,pz,'r')

