create extended kalman filter in matlab

To create an Extended Kalman Filter in Matlab, follow these steps:

  1. Define your system model and measurement model equations.
    • System Model: x_k = f(x_{k-1},u_{k-1}) + w_{k-1}
    • Measurement Model: z_k = h(x_k) + v_k
  2. Initialize the state vector, process noise covariance matrix, and measurement noise covariance matrix.
    main.m
    x = [initial_state_values];
    Q = [process_noise_covariance_matrix];
    R = [measurement_noise_covariance_matrix];
    
    110 chars
    4 lines
  3. Create the extended Kalman filter object.
    main.m
    ekf = extendedKalmanFilter(@systemModel,@measurementModel,x,Q,R);
    
    66 chars
    2 lines
  4. Define the system model and measurement model functions.
    main.m
    function x_k = systemModel(x_k_prev,u_k_prev)
       % system model equations
    end
    
    function z_k = measurementModel(x_k)
       % measurement model equations
    end
    
    153 chars
    8 lines
  5. Input the measurement data and run the filter.
    main.m
    z = [measurement_data];
    [x_est,P_est] = predictAndUpdate(ekf,z);
    
    65 chars
    3 lines
  6. Repeat Step 5 for each new measurement.

Here is an example implementation of an extended Kalman filter in Matlab:

main.m
% Define the system model equations
function x_k = systemModel(x_k_prev,u_k_prev)
   x_k = [x_k_prev(1) + cos(x_k_prev(3))*u_k_prev(1);
          x_k_prev(2) + sin(x_k_prev(3))*u_k_prev(1);
          x_k_prev(3) + u_k_prev(2)];
end

% Define the measurement model equations
function z_k = measurementModel(x_k)
   z_k = [sqrt(x_k(1)^2 + x_k(2)^2);
          atan2(x_k(2),x_k(1))];
end

% Initialize the state vector, process noise covariance matrix, and measurement noise covariance matrix
x = [0;0;0];
Q = diag([0.1,0.1,0.01]);
R = diag([0.1,0.1]);

% Create the extended Kalman filter object
ekf = extendedKalmanFilter(@systemModel,@measurementModel,x,Q,R);

% Input the measurement data and run the filter
measurements = [1.2, 0.5;
                1.6, 1.2;
                2.1, 2.0;
                2.6, 3.0;
                3.2, 4.5];
num_measurements = size(measurements,1);
x_est = zeros(3,num_measurements);
P_est = zeros(3,3,num_measurements);
for k = 1:num_measurements
   z = measurements(k,:)';
   [x,P] = predictAndUpdate(ekf,z);
   x_est(:,k) = x;
   P_est(:,:,k) = P;
end

% Plot the results
t = 1:num_measurements;
figure
subplot(311)
plot(t,x_est(1,:),'-b',t,sqrt(measurements(:,1).^2 + measurements(:,2).^2),'--r')
ylabel('Position (m)')
legend('Estimated','True')
subplot(312)
plot(t,x_est(2,:),'-b',t,atan2(measurements(:,2),measurements(:,1)),'--r')
ylabel('Heading (rad)')
legend('Estimated','True')
subplot(313)
plot(t,x_est(3,:),'-b')
ylabel('Speed (m/s)')
xlabel('Time (s)')
1500 chars
53 lines

In this implementation, the system model equations describe a simple motion model for a robot moving in a 2D plane with a constant speed and rotational velocity. The measurement model equations describe how the robot's position and heading can be measured using range and bearing sensors. The extended Kalman filter object is created using the system model and measurement model functions, and is then run using a series of measurements. The results are plotted to show how the filter estimates the robot's position, heading, and speed over time.

gistlibby LogSnag