Engee documentation
Notebook

Identification of a dynamic system parameter using an extended Kalman filter

Introduction

Estimating the state of dynamic systems by noisy measurements is a fundamental task in control theory, navigation, and signal processing. For linear systems with Gaussian noise, the Kalman filter is the optimal solution, however, many real systems require simultaneous evaluation of both state variables and unknown model parameters.

In such cases, an extended Kalman filter (RFK) is used, based on the local linearization of nonlinear model functions through first-order Taylor series expansion. In this paper, the problem of parameter identification and evaluation of the state of the aperiodic link of the first order is considered.:

​,

where — unknown dynamic parameter, — known coefficient, — input impact, ​ — process noise. The solution uses the expansion of the state vector by including an unknown parameter in it. .

The purpose of the work is to demonstrate the implementation of RFCs for joint estimation, to investigate the convergence of the parameter estimation and to analyze the behavior of the error covariance matrix.

Required libraries

In this example, we will need the following libraries:

  • LinearAlgebra — for matrix operations;
  • Statistics — for generating noise.
In [ ]:
using LinearAlgebra, Statistics

Initial parameters of the model

Let's define the system parameters:

  • T_sim = 200 — number of simulation steps;
  • k_u = 1.0 — gain factor;
  • T_const = 10.0 — time constant.

Let's calculate the true parameters: and , describing an aperiodic link of the first order.

In [ ]:
# Model Parameters
T_sim = 200;
k_u = 1.0;
T_const = 10.0;
a_true = 1.0 - 1.0/T_const;
b_true = k_u / T_const;
display(a_true)
0.9

Let's define the initial parameter estimate a_init = 0.85 and the input signal u_d = 1.0. We will also define the noise in terms of standard deviations.: σ_v = √0.01 — process noise, σ_ζ = 0.001 — parameter noise (simulates parameter drift), σ_w = √0.01 — measurement noise.

In [ ]:
# Initial conditions
a_init = 0.85;
u_d = 1.0;

# Noise covariance
σ_v = sqrt(0.01);
σ_ζ = 0.001;
σ_w = sqrt(0.01);

Data generation

Creating a function generate_data(), which mimics the behavior of a real system. At each step, we generate process noise v and calculate the next state using the formula:

Then add the measurement noise.: .

The function returns arrays of true states and noisy measurements.

In [ ]:
# Generating a true process
function generate_data(T_sim, σ_v, a_true, b_true, u_d, σ_w)
    x_true = zeros(T_sim)
    y_meas = zeros(T_sim)
    x = 0.0
    
    for n in 1:T_sim
        v = σ_v * randn()
        x = a_true * x + b_true * u_d + b_true * v
        x_true[n] = x
        y_meas[n] = x + σ_w * randn()
    end
    
    return x_true, y_meas
end
Out[0]:
generate_data (generic function with 1 method)

The extended Kalman Filter function

Creating a function ekf_filter(), which implements an extended Kalman filter for joint evaluation of state x and parameter a. Initialize the extended state vector X = [x; a] = [0; 0.85] and the covariance matrix P = diag(1.0, 0.1).

At each step of the cycle:

  1. Matrix formation: — transition matrix, — the entry matrix, — the noise matrix.

  2. Condition forecast:

  3. Linearization: calculation of the Jacobi matrix , where the nonlinearity due to the product is taken into account .

  4. Prediction of covariance: , where .

  5. Correction: calculating the discrepancy ,

    innovation covariance , the Kalman coefficient .

  6. Update: ,

    .

In [ ]:
function ekf_filter(y_meas::Vector{Float64}, a_init::Float64, b_true::Float64, u_d::Float64, σ_v::Float64, σ_ζ::Float64, σ_w::Float64)
    T_sim = length(y_meas)
    
    # Extended state vector: [x; a]
    X = [0.0; a_init]
    
    # Error covariance matrix
    P = diagm([1.0, 0.1])
    
    # The measurement matrix
    C = [1.0 0.0]
    
    # Covariance matrix of process noise
    M = diagm([σ_v^2, σ_ζ^2])
    
    # The 2x2 identity matrix
    I2 = diagm(ones(2))
    
    # Storing results
    x_est = zeros(T_sim)
    a_est = zeros(T_sim)
    P_hist = zeros(2, 2, T_sim)
    
    for n in 1:T_sim
        x_n = X[1]
        a_n = X[2]
        
        # Condition forecast
        # Transition matrix A = [a0; 0 1]
        A = [a_n  0.0;
             0.0  1.0]
        
        # Control matrix B = [b; 0] (b is a constant)
        B = [b_true; 0.0]
        
        X_pred = A * X + B * u_d
        
        # Linearization
        F = [a_n  x_n;
             0.0  1.0]
        
        # Process noise matrix V = [b0; 0 1]
        V_mat = [b_true  0.0;
                 0.0      1.0]
        
        # Prediction of covariance
        P_pred = F * P * F' + V_mat * M * V_mat'
        
        # Correction
        y_pred = (C * X_pred)[1]
        innovation = y_meas[n] - y_pred
        
        S = (C * P_pred * C')[1, 1] + σ_w^2
        K = P_pred * C' / S
        
        X = X_pred + K * innovation
        P = (I2 - K * C) * P_pred
        
        # Conservation
        x_est[n] = X[1]
        a_est[n] = X[2]
        P_hist[:, :, n] = P
    end
    
    return x_est, a_est, P_hist
end
Out[0]:
ekf_filter (generic function with 1 method)

Running the simulation

Let's run the simulation: generate the data and apply the filter.

In [ ]:
x_true, y_meas = generate_data(T_sim, σ_v, a_true, b_true, u_d, σ_w);
x_est, a_est, P_hist = ekf_filter(y_meas, a_init, b_true, u_d, σ_v, σ_ζ, σ_w);

For visual inspection, we will output the first and last values of the evaluation vector of the extended Kalman filter.

In [ ]:
x_est
Out[0]:
200-element Vector{Float64}:
 0.14310081619978648
 0.23915330583528285
 0.20957303927390009
 0.26607767068000626
 0.3884568888337616
 0.5100903333114909
 0.5344283013016132
 0.5692036761074832
 0.6351143666385379
 0.6772130891330526
 0.7201782083990114
 0.700702597158715
 0.7237308724987693
 ⋮
 1.007264754012645
 1.011002756933732
 0.9996417060121864
 1.007551076756899
 1.0070630128722802
 1.011825792285365
 1.011063881429158
 0.9942177135507816
 0.991925762549218
 1.0136964386882734
 1.0224545667226046
 1.0082160811729803

Visualization of results

Let's build three graphs. In the first one, we will show the true state (blue line), noisy measurements (orange dots) and the filter estimate (green line). The second one shows the true value of the parameter (black dotted line) and its estimate (orange line), which starts with and converges to the true value . The third step is the convergence of the uncertainty of the parameter estimate. .

In [ ]:
p1 = plot(1:T_sim, x_true, label="The true state", lw=2)
plot!(p1, 1:T_sim, y_meas, label="Measurements", alpha=0.4, marker=(:circle, 2))
plot!(p1, 1:T_sim, x_est, label="RFK assessment", lw=2)
xlabel!(""); ylabel!("x")
title!("Assessment of the condition")

p2 = plot(1:T_sim, a_true*ones(T_sim), label="The true parameter is a", lw=2, color=:black, ls=:dash)
plot!(p2, 1:T_sim, a_est, label="Evaluation of parameter a", lw=2)
xlabel!(""); ylabel!("a")
title!("Parameter evaluation")

P_a = [P_hist[2, 2, n] for n in 1:T_sim]  
p3 = plot(1:T_sim, P_a, 
          label="Variance of parameter a", 
          lw=2, 
          color=:purple,
          fillrange=0, 
          fillalpha=0.2,
          legend=:topright)
xlabel!("Step")
ylabel!("Variance")
title!("Convergence of uncertainty in the estimation of parameter a")

plot(p1, p2, p3, layout=(3,1), size=(800, 750))
Out[0]:

Conclusion

In the course of the work, an extended Kalman filter was implemented to jointly evaluate the state and unknown parameter of a linear dynamic object. The simulation showed:

  1. Convergence of the parameter estimate. Despite the deliberate deviation of the initial estimate, the filter quickly corrected it, and after the first dozen iterations, the estimate stabilized in the vicinity of the true value.

  2. Convergence of uncertainty. The variance of the parameter estimation error decreased monotonously, which indicates the accumulation of information and increased confidence of the algorithm in the estimation.

  3. The quality of the assessment of the condition. The reconstructed trajectory is in good agreement with the true one, despite the noise of the process and measurements.

It is important to emphasize that the correct operation of the RFK directly depends on the consistency of the model with the real process, and if there is a discrepancy, the parameter estimate is shifted.

The considered approach is widely used in real-world tasks.:

  • Adaptive control — evaluation of object parameters (time constants, gain coefficients) in real time to build adaptive controllers.

  • Navigation and localization — joint refinement of coordinates, orientation and calibration parameters of sensors in inertial systems, GPS/GLONASS.

  • Energy — assessment of the parameters of electric machines and the condition of batteries (charge, capacity, wear).

  • Medicine and Biotechnology — assessment of pharmacokinetic parameters based on measurement data, processing of EEG and ECG signals.

  • Meteorology is the assimilation of data into numerical weather models, where the atmospheric state and model parameters are simultaneously refined.

Thus, the demonstrated scheme of joint estimation of the state and parameters based on RFK is a universal tool for working with incomplete or noisy data on dynamic systems.