function [Zout,njump]=rmjmp(Zk,Zkm1,njump,jmp,tol)
% function [Zout,njump]=rmjmp(Zk,Zkm1,njump,jmp,tol)
%
% Removes jumps in time functions.
%
% Inputs:
% Zk and Zkm1 are consecutive values of function Z.
%    Zk=Z(k), Zkm1=Z(k-1) where k is the time index.
% njump is the jump counter. 
%    njump is both an input and output quantity.
% jmp: The nominal value of the jump is given by jmp.
%    For example, the nominal jump for the atan function is pi.
% tol is a tolerance on the actual size of the jump. 
%   Jumps of size |jmp-tol| are looked for.
%   Typically set tol=jmp/2.
%
% Outputs
% Zout is the output with the jumps removed and 
%   corresponds to time k.
% njump is updated if a jump was detected.
%   njump increases for downgoing jumps and
%   decreases for upgoing jumps.
%
% INITIALIZATIOV
%   This function is typically started with K=2.
%   Zout(1) should be initialized to Z(1).
%   njump should be in initialized to zero.



jump=abs(jmp-tol);
dz=Zk-Zkm1;
if dz<0
	% A down going jump is possible
	if abs(dz)>jump
		% a negative jump has occurred
		% disp('A negative jump has occurred')
		njump=njump+1;
		Zout=Zk+njump*jmp;
	else
		njump=njump;
		Zout=Zk+njump*jmp;
	end
elseif dz>0
	% An up going is possible
	if abs(dz)>jump
		% A positive jump has occurred
		% disp('A positive jump has occurred')
		njump=njump-1;
		Zout=Zk+njump*jmp;
	else
		njump=njump;
		Zout=Zk+njump*jmp;
	end
else
	% dz=0
	% disp('No jump has occurred')
	njump=njump;
	Zout=Zk+njump*jmp;
end

