x = 34.1+0.35*sin(y/100 -pi/2).*exp(-y/200);
[t0_pos,s0_pos,t0_neg,s0_neg]= crossing_V7(x,y,threshold,'linear');
plot(x,y,'b',s0_pos,t0_pos,'*r',s0_neg,t0_neg,'dk','linewidth',2,'markersize',12);grid on
set(gca,'YDir','reverse')
legend('signal','signal positive slope crossing points','signal negative slope crossing points');
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
error(nargchk(1,4,nargin));
if nargin < 2 | isempty(t)
elseif length(t) ~= length(S)
error('t and S must be of identical length!');
S1 = S(1:end-1) .* S(2:end);
if strcmp(imeth,'linear')
if abs(S(ind(ii))) >= eps*abs(S(ind(ii)))
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
ind_pos = find(sign(slope_sign)>0);
ind_neg = find(sign(slope_sign)<0);