Main Content

distance

Confidence value of measurement

Description

d = distance(kalmanFilter,zmatrix) computes a distance between the location of a detected object and the predicted location by the Kalman filter object. This distance computation takes into account the covariance of the predicted state and the process noise. The distance function can only be called after the predict function.

Use the distance function to find the best matches. The computed distance values describe how a set of measurements matches the Kalman filter. You can thus select a measurement that best fits the filter. This strategy can be used for matching object detections against object tracks in a multiobject tracking problem. This distance computation takes into account the covariance of the predicted state and the process noise.

example

Examples

collapse all

Track the location of a physical object moving in one direction.

Generate synthetic data which mimics the 1-D location of a physical object moving at a constant speed.

detectedLocations = num2cell(2*randn(1,40) + (1:40));

Simulate missing detections by setting some elements to empty.

detectedLocations{1} = [];
  for idx = 16: 25 
      detectedLocations{idx} = []; 
  end

Create a figure to show the location of detections and the results of using the Kalman filter for tracking.

figure;
hold on;
ylabel('Location');
ylim([0,50]); 
xlabel('Time');
xlim([0,length(detectedLocations)]);

Figure contains an axes object. The axes object with xlabel Time, ylabel Location is empty.

Create a 1-D, constant speed Kalman filter when the physical object is first detected. Predict the location of the object based on previous states. If the object is detected at the current time step, use its location to correct the states.

kalman = []; 
for idx = 1: length(detectedLocations) 
   location = detectedLocations{idx}; 
   if isempty(kalman)
     if ~isempty(location) 
       
       stateModel = [1 1;0 1]; 
       measurementModel = [1 0]; 
       kalman = vision.KalmanFilter(stateModel,measurementModel,'ProcessNoise',1e-4,'MeasurementNoise',4);
      kalman.State = [location, 0]; 
     end 
   else
     trackedLocation = predict(kalman);
     if ~isempty(location) 
       plot(idx, location,'k+');
      d = distance(kalman,location); 
       title(sprintf('Distance:%f', d));
       trackedLocation = correct(kalman,location); 
     else 
       title('Missing detection'); 
     end 
     pause(0.2);
     plot(idx,trackedLocation,'ro'); 
   end 
 end 
legend('Detected locations','Predicted/corrected locations');

Figure contains an axes object. The axes object with title Distance:3.126145, xlabel Time, ylabel Location contains 66 objects of type line. One or more of the lines displays its values using only markers These objects represent Detected locations, Predicted/corrected locations.

Use Kalman filter to remove noise from a random signal corrupted by a zero-mean Gaussian noise.

Synthesize a random signal that has value of 1 and is corrupted by a zero-mean Gaussian noise with standard deviation of 0.1.

x = 1;
len = 100;
z = x + 0.1 * randn(1,len);

Remove noise from the signal by using a Kalman filter. The state is expected to be constant, and the measurement is the same as state.

stateTransitionModel = 1;
measurementModel = 1;
obj = vision.KalmanFilter(stateTransitionModel,measurementModel,'StateCovariance',1,'ProcessNoise',1e-5,'MeasurementNoise',1e-2);

z_corr = zeros(1,len);
for idx = 1: len
 predict(obj);
 z_corr(idx) = correct(obj,z(idx));
end

Plot results.

figure, plot(x * ones(1,len),'g-'); 
hold on;
plot(1:len,z,'b+',1:len,z_corr,'r-');
legend('Original signal','Noisy signal','Filtered signal');

Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Original signal, Noisy signal, Filtered signal.

Input Arguments

collapse all

Kalman filter object.

Location of a detected object, specified as an N-column matrix. Each row matrix contains a measurement vector. The distance function returns a row vector where each distance element corresponds to the measurement input.

More About

collapse all

Distance Equation

d(z)=(zHx)T1(zHx)+ln||

Where Σ=HPHT+R and |Σ| is the determinant of Σ. You can then find the best matches by examining the returned distance values.

Version History

Introduced in R2012b