function y = dShiftRecursive (x, tol)
% dShiftRecursiv - splits data into segments and recursively calls dShiftEliminate
%	y = dShiftRecursive (x, tol)
%	x - column vector of data
%	tol - tolerance of discontinuity (optional)
%	y - output vector
% Program removes discontinuities caused by sudden shift of the signal
% by constant value


if (nargin<2), tol = 2; end;

n = length(x);

dx = diff(x); 	ddx = diff(dx);
s  = std(ddx);
I = find( abs(ddx) > tol*s );

if ( n < 800 | length(find(diff(I)~=1))<2 ),
	y = dShiftEliminate(x,tol);
	return;
end;

[m,a] = max(diff(I));
t0 = round(0.5*( I(a) + I(a+1) ));
disp([n, t0, n-t0]);

y1=	flipud(dShiftRecursive(flipud(x(1:t0)),tol)) ;
y2=	dShiftRecursive(x(t0+1:n),tol);

y = [ y1 ; y2 ];

return;


