pts = 0:pi/160:12*pi;

data = sin(pts) + sin(0.3*pts) + cos(3*pts);

clf, hold on, plot(data)

thresh = mean(data) + std(data);
b = data > thresh;

bstart = ~b(1:end-1) & b(2:end);
bend = b(1:end-1) & ~b(2:end);

pstart = find(bstart);
pend = 1+find(bend);

xcoords = [pstart; pend; NaN*pstart];
ycoords = repmat(thresh,size(xcoords));

plot(xcoords(:),ycoords(:),'r','Linewidth',3)
