function y = dShiftEliminate( x, tol )
% dShiftEliminate - eliminates spike noise in 1D arrays
%	y = dSpikeEliminate( x, MaxPeakWidth )
%	x - 1D column vector of signal data

%	tol - tolerance, relative ratio of the second derivative to its std

if (nargin<2), tol = 2; end;
dx = diff(x); 	ddx = diff(dx);
s  = std(ddx);

I = find( abs(ddx) > tol*s );
if ( isempty(I) ),
	y = x;
	return;
end;
J = find( diff(I) > 1 );
if ( isempty(J) ),
 	t = I;	
else,
	t = I(1):I(J(1));	%the first peaks
end;

ddy = ddx;
ddy(t) = mean(ddx(t))*ones(size(t));
l = length(J);
for (i=1:l-1),
	t = I(J(i)+1):I(J(i+1));
	ddy(t) = mean(ddx(t))*ones(size(t));
end;
if ( ~isempty(J) ),
	t = I(J(l)+1):I(length(I)); %the last peaks
	ddy(t) = mean(ddx(t))*ones(size(t));
end;

y = cumsum([x(1) ; cumsum([ dx(1); ddy ])]);

return;
