Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
prudhomm committed Nov 21, 2024
1 parent 61f077e commit 843f83e
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* xref:index.adoc[Introduction]
* xref:quickstart.adoc[]
* xref:overview.adoc[]
* xref:tps/tp-assim-1.adoc[The Kalman Filter]
//* Homework
//** xref:homework/problem-set-1.adoc[Homework 1]
//** xref:homework/problem-set-2.adoc[Homework 2]
Expand Down
7 changes: 4 additions & 3 deletions docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Introduction:: xref::attachment$lecture-rbobm-beamer-l1-2024.pdf[Notes > pdf]
Ingredients:: xref::attachment$lecture-rbobm-beamer-notations.pdf[Notes > pdf]
Reduced Basis:: xref::attachment$lecture-rbobm-beamer-approx.pdf[Notes > pdf]
//A priori:: xref::attachment$lecture-rbobm-beamer-apriori.pdf[Notes > pdf]
/Error Bounds:: TODO
Extenstion to Non Compliant problems:: xref::attachment$lecture-rbobm-beamer-l4.pdf[Notes > pdf]
Parabolic(time dependent problems):: xref::attachment$lecture-rbobm-beamer-parabolic.pdf[Notes > pdf]
//Error Bounds:: TODO
Extension to Non Compliant problems:: xref::attachment$lecture-rbobm-beamer-l4.pdf[Notes > pdf]
Extension to Parabolic problems(time dependent problems):: xref::attachment$lecture-rbobm-beamer-parabolic.pdf[Notes > pdf]
Extension to Non-affine problems:: xref::attachment$lecture-rbobm-beamer-non-affine.pdf[Notes > pdf]
//
Homework 1:: xref::attachment$problem-set-1.pdf[> pdf]
//Homework 2:: xref::attachment$problem-set-2.pdf[> pdf]
Expand Down
213 changes: 213 additions & 0 deletions docs/modules/ROOT/pages/tps/tp-assim-1.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
= Practical Session on Implementing the Kalman Filter Method
:stem: latexmath
Duration: Approximately 30-40 minutes

== Introduction

The Kalman filter is a powerful mathematical tool used for estimating the state of a dynamic system from a series of noisy measurements. It is widely applied in areas like control systems, robotics, navigation, and finance. This session will guide you through implementing the Kalman filter with two simple examples:

1. One-Dimensional Position Tracking
2. Two-Dimensional Position and Velocity Estimation

By the end of this session, you’ll understand how the Kalman filter works and how to implement it in practice.

== Prerequisites

- Basic knowledge of linear algebra and probability theory
- Familiarity with programming in Python (or a similar language)
- Basic understanding of dynamic systems and state estimation

== Example 1: One-Dimensional Position Tracking

=== Problem Statement

Imagine a vehicle moving along a straight line at a constant velocity. We receive position measurements at regular intervals, but these measurements are noisy. Our goal is to estimate the vehicle’s true position over time using the Kalman filter.

=== Kalman Filter Overview

The Kalman filter operates in a two-step process:

1. *Predict*: Estimate the current state and uncertainty from the previous state.
2. *Update*: Correct the predicted state using the new measurement.

=== Implementation Steps

1. Define the System Model:
- *State Variable (x)*: Represents the true position.
- *Process Model*: latexmath:[x_k = x_{k-1} + v * dt + w], where stem:[v] is velocity, stem:[dt] is the time step, and stem:[w] is the process noise (assumed to be Gaussian with zero mean).
- *Measurement Model*: stem:[z_k = x_k + v], where stem:[v] is the measurement noise (also Gaussian).

2. Initialize Variables:
- Initial State Estimate (stem:[x_est])
- Initial Estimate Uncertainty (stem:[P]): Represents confidence in the initial estimate.
- Process Noise Covariance (stem:[Q]): Represents uncertainty in the process model.
- Measurement Noise Covariance (stem:[R]): Represents measurement noise level.

3. Implement the Kalman Filter Equations:
- Predict Step
- Update Step

// [source,python]
// ----
// import numpy as np
// import matplotlib.pyplot as plt
//
// # Simulation parameters
// dt = 1.0 # Time step
// num_steps = 50 # Number of time steps
// true_velocity = 1.0 # Constant velocity
//
// # Generate true positions
// x_true = np.arange(0, num_steps * dt, dt) * true_velocity
//
// # Generate noisy measurements
// measurement_noise_std = 2.0
// z_measurements = x_true + np.random.normal(0, measurement_noise_std, size=num_steps)
//
// # Kalman filter initialization
// x_est = np.zeros(num_steps) # Estimated position
// P = np.zeros(num_steps) # Estimate uncertainty
// x_est[0] = 0.0 # Initial position estimate
// P[0] = 1.0 # Initial estimate uncertainty
//
// # Define noise covariances
// Q = 0.0001 # Process noise covariance
// R = measurement_noise_std ** 2 # Measurement noise covariance
//
// # Kalman filter loop
// for k in range(1, num_steps):
// # Predict step
// x_pred = x_est[k-1] + true_velocity * dt
// P_pred = P[k-1] + Q
//
// # Update step
// K = P_pred / (P_pred + R)
// x_est[k] = x_pred + K * (z_measurements[k] - x_pred)
// P[k] = (1 - K) * P_pred
//
// # Plotting the results
// plt.figure(figsize=(12, 6))
// plt.plot(x_true, label='True Position')
// plt.scatter(range(num_steps), z_measurements, color='red', label='Measurements', alpha=0.5)
// plt.plot(x_est, label='Kalman Filter Estimate', linestyle='--')
// plt.xlabel('Time Step')
// plt.ylabel('Position')
// plt.title('One-Dimensional Position Tracking using Kalman Filter')
// plt.legend()
// plt.show()
// ----

=== Explanation

- *Predict Step*: We predict the next position based on the current estimate and known velocity.
- *Update Step*: We adjust our prediction using the new measurement.
- *Kalman Gain (K)*: Determines the weight given to the prediction and the measurement.

== Example 2: Two-Dimensional Position and Velocity Estimation

=== Problem Statement

Consider an object moving in a plane with constant velocity. We can measure its position in both the x and y directions, but these measurements are noisy. Our goal is to estimate both the position and velocity over time.

=== Implementation Steps

1. Define the System Model:
- *State Vector (x)*: Includes both position and velocity.
- *State Transition Matrix (F)*: Describes the evolution of the state over time.
- *Observation Matrix (H)*: Maps the state to the observed measurements.

2. Initialize Variables:
- Initial State Estimate (x_est)
- Initial Estimate Covariance (P)
- Process Noise Covariance (Q)
- Measurement Noise Covariance (R)

3. Implement the Kalman Filter Equations:
- Predict Step
- Update Step

// [source,python]
// ----
// import numpy as np
// import matplotlib.pyplot as plt
//
// # Simulation parameters
// dt = 1.0 # Time step
// num_steps = 50 # Number of time steps
//
// # True initial state [x, vx, y, vy]
// x_true = np.zeros((4, num_steps))
// x_true[:, 0] = [0, 1, 0, 1] # Initial position and velocity
//
// # State transition matrix
// F = np.array([
// [1, dt, 0, 0],
// [0, 1, 0, 0],
// [0, 0, 1, dt],
// [0, 0, 0, 1]
// ])
//
// # Observation matrix
// H = np.array([
// [1, 0, 0, 0],
// [0, 0, 1, 0]
// ])
//
// # Process and measurement noise covariances
// Q = np.eye(4) * 0.0001 # Small process noise
// R = np.eye(2) * 1.0 # Measurement noise
//
// # Generate true positions and measurements
// z_measurements = np.zeros((2, num_steps))
// for k in range(1, num_steps):
// # True state update
// x_true[:, k] = F @ x_true[:, k-1] + np.random.multivariate_normal(np.zeros(4), Q)
// # Measurements
// z_measurements[:, k] = H @ x_true[:, k] + np.random.multivariate_normal(np.zeros(2), R)
//
// # Kalman filter initialization
// x_est = np.zeros((4, num_steps)) # State estimates
// P = np.zeros((4, 4, num_steps)) # Estimate covariances
// x_est[:, 0] = [0, 0.5, 0, 0.5] # Initial state estimate
// P[:, :, 0] = np.eye(4) # Initial covariance estimate
//
// # Kalman filter loop
// for k in range(1, num_steps):
// # Predict step
// x_pred = F @ x_est[:, k-1]
// P_pred = F @ P[:, :, k-1] @ F.T + Q
//
// # Update step
// y = z_measurements[:, k] - H @ x_pred # Measurement residual
// S = H @ P_pred @ H.T + R # Residual covariance
// K = P_pred @ H.T @ np.linalg.inv(S) # Kalman gain
// x_est[:, k] = x_pred + K @ y
// P[:, :, k] = (np.eye(4) - K @ H) @ P_pred
//
// # Plotting the results
// plt.figure(figsize=(12, 6))
// plt.plot(x_true[0, :], x_true[2, :], label='True Position', color='green')
// plt.scatter(z_measurements[0, :], z_measurements[1, :], color='red', label='Measurements', alpha=0.5)
// plt.plot(x_est[0, :], x_est[2, :], label='Kalman Filter Estimate', linestyle='--', color='blue')
// plt.xlabel('Position X')
// plt.ylabel('Position Y')
// plt.title('Two-Dimensional Position and Velocity Estimation using Kalman Filter')
// plt.legend()
// plt.show()
// ----
//
=== Explanation

- *State Vector*: Includes both position and velocity in x and y directions.
- *Predict Step*: Estimates the next state based on the previous state and the system model.
- *Update Step*: Refines the prediction using the new measurements.
- *Kalman Gain (K)*: Balances the confidence between the prediction and the measurement.

== Conclusion

In this practical session, we’ve:
- Explored the fundamental concepts behind the Kalman filter.
- Implemented the Kalman filter for one-dimensional position tracking.
- Extended the implementation to two-dimensional position and velocity estimation.

These examples demonstrate the versatility and power of the Kalman filter in estimating the state of dynamic systems in the presence of noise.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ scipy
pandas < 2.2
tabulate
plotly
nbformat >= 4.2.0
scikit-learn

0 comments on commit 843f83e

Please sign in to comment.