kalman filter noise in matlab

main.m
% Define parameters for the Kalman filter
A = 1; % State transition matrix
H = 1; % Observation matrix
Q = 0.0001; % Process noise covariance
R = 1; % Measurement noise covariance
x = 0; % Initial state
P = 1; % Initial state covariance

% Generate process and measurement noise
processNoise = sqrt(Q) * randn(1, 100); 
measurementNoise = sqrt(R) * randn(1, 100);

% Initialize variables
x_hat = zeros(1, 100); % Estimated states
P_hat = zeros(1, 100); % Estimated state covariance

% Kalman filter iteration
for k = 1:100
    % Prediction
    x_hat_minus = A * x;
    P_minus = A * P * A' + Q;
    
    % Update
    K = P_minus * H' / (H * P_minus * H' + R);
    x = x_hat_minus + K * (measurementNoise(k) - H * x_hat_minus);
    P = (1 - K * H) * P_minus;
    
    % Store the estimated states
    x_hat(k) = x;
    P_hat(k) = P;
end

disp(x_hat); % Display the estimated states
881 chars
34 lines

related categories

gistlibby LogSnag