function [z_meas] = ParseRange( z )

persistent z_history;
persistent n; 
persistent InvalidPointCtr;

PointCountTimeout = 20; 
N = 4;
epsilon  = .1;

if(isempty(z_history))  %First time through.
  n = 0;
  z_history = NaN(1,N);
  InvalidPointCtr = 0;
end
  
if(n<N)
    n = n+1;
else
    n = 1;
end
z_history(n) = z;

delta = NaN(1,N-1);
k = n;
for i = 1:N-1  %Compute delta(1) as most recent measurement delta, delta(2) is the second most recent measurement delta, etc.
  if(k>1)
    k_minus = k-1;
  else
    k_minus = N;
  end
  delta(i) = abs(z_history(k)-z_history(k_minus));
k = k_minus;    
end

idx = (delta > epsilon);

if((idx(1) == 0 && idx(2) == 1 && idx(3) == 1))
  z_meas = z;
  InvalidPointCtr = 0;
else
  z_meas = NaN;
  InvalidPointCtr = InvalidPointCtr+1;
end

if(InvalidPointCtr > PointCountTimeout)   %If a "valid" point hasn't been found after 2 seconds, ram may not be moving so just output most recent measurement.
    z_meas = z;
    InvalidPointCtr = 0;
end

end

