Kalman Filter

zzwon1212·2024년 8월 7일

무제

목록 보기
6/7

The Kalman filter assumes variables are random and Gaussian distributed. Each variable has a mean value μ\mu, which is the center of the random distribution (and its most likely state), and a variance σ2\sigma^2, which is the uncertainty.

State

x^k=[positionvelocity] Pk=[ΣppΣpvΣvpΣvv]\hat{\mathbf{x}}_k = \begin{bmatrix} \text{position} \\ \text{velocity} \\ \end{bmatrix} \\ \ \\ \mathbf{P}_k = \begin{bmatrix} \Sigma_{pp} & \Sigma_{pv} \\ \Sigma_{vp} & \Sigma_{vv} \\ \end{bmatrix}
  • predict the next state (at time kk) by using the current state (at time k1k-1):
    pk=pk1+Δtvk1vk=pk1+Δtvk1 x^k=[1Δt01]x^k1=Fkx^k1Pk=FkPk1FkT\begin{aligned} p_k &= p_{k-1} + \Delta{t} v_{k-1} \\ v_k &= \phantom{p_{k-1} + \Delta{t} } v_{k-1} \end{aligned} \\ \ \\ \begin{aligned} \hat{\mathbf{x}}_k &= \begin{bmatrix} 1 & \Delta{t} \\ 0 & 1 \\ \end{bmatrix} \hat{\mathbf{x}}_{k-1} \\ &= \mathbf{F}_k \hat{\mathbf{x}}_{k-1} \\ \mathbf{P}_k &= \mathbf{F}_k \mathbf{P}_{k-1} \mathbf{F}^T_k \end{aligned} \\
    • Fk\mathbf{F}_k: prediction matrix

Adding external influence

pk=pk1+Δtvk1+12aΔt2vk=pk1+Δtvk1+aΔt x^k=Fkx^k1+[Δt22Δt]a=Fkx^k1+Bkuk\begin{aligned} p_k &= p_{k-1} + \Delta{t} v_{k-1} + \frac{1}{2} a \Delta{t}^2 \\ v_k &= \phantom{p_{k-1} + \Delta{t} } v_{k-1} + a \Delta{t} \end{aligned} \\ \ \\ \begin{aligned} \hat{\mathbf{x}}_k &= \mathbf{F}_k \hat{\mathbf{x}}_{k-1} + \begin{bmatrix} \frac{\Delta{t^2}}{2} \\ \Delta{t} \\ \end{bmatrix} a \\ &= \mathbf{F}_k \hat{\mathbf{x}}_{k-1} + \mathbf{B}_k \vec{\mathbf{u}}_k \end{aligned} \\
  • aa: external influence (e.g. acceleration)
  • Bk\mathbf{B}_k: control matrix
  • uk\vec{\mathbf{u}}_k: control vector

Adding external uncertainty

  • Prediction step:

    x^k=Fkx^k1+BkukPk=FkPk1FkT+Qk(1)\begin{aligned} \hat{\mathbf{x}}_k &= \mathbf{F}_k \hat{\mathbf{x}}_{k-1} + \mathbf{B}_k \vec{\mathbf{u}}_k \\ \mathbf{P}_k &= \mathbf{F}_k \mathbf{P}_{k-1} \mathbf{F}^T_k + \mathbf{Q}_k \end{aligned} \tag{1}
    • Qk\mathbf{Q}_k: external uncertainty (noise covariance)
    • The new estimate is a prediction made from previous best estimate, plus a correction for known external influences.
    • The new uncertainty is predicted from the old uncertainty, with some additional uncertainty from the environment.
  • Now, we have a fuzzy estimate of where our system might be, given by x^k\hat{x}_k and Pk\mathbf{P}_k. What happens when we get some data from our sensors?

Refining the estimate with measurements

  • Notice that the units and scale of the reading might not be the same as the units and scale of the state we’re keeping track of. You might be able to guess where this is going: We’ll model the sensors with a matrix, Hk\mathbf{H}_k.

  • We can figure out the distribution of sensor readings we'd expect to see in the usual way:

    μexpected=Hkx^kΣexpected=HkPkHkT\begin{aligned} \vec{\mu}_{\text{expected}} &= \mathbf{H}_k \hat{\mathbf{x}}_k \\ \Sigma_{\text{expected}} &= \mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T \end{aligned}
  • We’ll call the covariance of the sensor noise (uncertainty) Rk\mathbf{R}_k. The distribution has a mean equal to the reading we observed, which we’ll call zk\vec{\mathbf{z}}_k.

  • So now we have two Gaussian blobs: One surrounding the mean of our transformed prediction, and one surrounding the actual sensor reading we got. We must try to reconcile our guess about the readings we’d see based on the predicted state (pink) with a different guess based on our sensor readings (green) that we actually observed:

  • The Product of Two Univariate Gaussian PDFs is a scaled Gaussian PDF.

    N(μ0,σ02)N(μ1,σ12)=N(μ,σ2) =N(μ0σ12+μ1σ02σ02+σ12,σ02σ12σ02+σ12) =N(μ0+σ02(μ1μ0)σ02+σ12,σ02σ04σ02+σ12)\mathcal{N}(\mu_0, \sigma_0^2) \cdot \mathcal{N}(\mu_1, \sigma_1^2) = \mathcal{N}(\mu', {\sigma'}^2) \\ \ \\ = \mathcal{N}(\frac{\mu_0 \sigma_1^2 + \mu_1 \sigma_0^2}{\sigma_0^2 + \sigma_1^2}, \frac{\sigma_0^2 \sigma_1^2}{\sigma_0^2 + \sigma_1^2}) \\ \ \\ = \mathcal{N}(\mu_0 + \frac{\sigma_0^2 (\mu_1 - \mu_0)}{\sigma_0^2 + \sigma_1^2}, \sigma_0^2 - \frac{\sigma_0^4}{\sigma_0^2 + \sigma_1^2})
    • We can simplify by factoring out a little piece and calling it k\mathbf{k}:
      k=σ02σ02+σ12 μ=μ0+k(μ1μ0) σ2=σ02kσ02\mathbf{k} = \frac{\sigma_0^2}{\sigma_0^2 + \sigma_1^2} \\ \ \\ \begin{aligned} \mu' &= \mu_0 + \mathbf{k}(\mu_1 - \mu_0) \\ \ \\ {\sigma'}^2 &= \sigma_0^2 - \mathbf{k} \sigma_0^2 \end{aligned}
    • We can extend this formula to matrix version.
      K=Σ0(Σ0+Σ1)1 μ=μ0+K(μ1μ0)Σ=Σ0KΣ0\mathbf{K} = \Sigma_0(\Sigma_0 + \Sigma_1)^{-1} \\ \ \\ \begin{aligned} \vec{\mu}' &= \vec{\mu}_0 + \mathbf{K}(\vec{\mu}_1 - \vec{\mu}_0) \\ \Sigma' &= \Sigma_0 - \mathbf{K} \Sigma_0 \end{aligned}
      • K\mathbf{K} is called the Kalman gain.
    • As a result, we can take our previous estimate and add something to make a new estimate.

Update step

  • We have two distributions: The predicted measurement with (μ0,Σ0)=(Hkx^k,HkPkHkT)(\mu_0,\Sigma_0)=(\mathbf{H}_k \hat{\mathbf{x}}_k, \mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T), and the observed measurement with (μ1,Σ1)=(zk,Rk)(\mu_1, \Sigma_1)=(\vec{\mathbf{z}}_k, \mathbf{R}_k).

  • Plug these into above formula.

    Hkx^k=Hkx^k      +K(zkHkx^k)HkPkHkT=HkPkHkTKHkPkHkT\begin{aligned} \mathbf{H}_k \hat{\mathbf{x}}_k' &= \mathbf{H}_k \hat{\mathbf{x}}_k \ \ \ \ \ \ + \mathbf{K} (\vec{\mathbf{z}}_k - \mathbf{H}_k \hat{\mathbf{x}}_k) \\ \mathbf{H}_k \mathbf{P}_k' \mathbf{H}_k^T &= \mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T - \mathbf{K} \mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T \\ \end{aligned} \\
    • where the Kalman gain is:
      K=HkPkHkT(HkPkHkT+Rk)1\mathbf{K} = \mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T (\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T + \mathbf{R}_k)^{-1}
  • We can knock an Hk\mathbf{H}_k and an HkT\mathbf{H}_k^T off the front and the end of terms properly.

    x^k=x^k+K(zkHkx^k)Pk=PkKHkPk(2)\begin{aligned} \hat{\mathbf{x}}_k' &= \hat{\mathbf{x}}_k + \mathbf{K}' (\vec{\mathbf{z}}_k - \mathbf{H}_k \hat{\mathbf{x}}_k) \\ \mathbf{P}_k' &= \mathbf{P}_k - \mathbf{K}' \mathbf{H}_k \mathbf{P}_k \end{aligned} \tag{2}
    K=PkHkT(HkPkHkT+Rk)1(3)\mathbf{K}' = \mathbf{P}_k \mathbf{H}_k^T (\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T + \mathbf{R}_k)^{-1} \tag{3}
  • x^k\hat{\mathbf{x}}_k' is our new best estimate. We can go on and feed it (along with Pk\mathbf{P}_k' ) back into another round of predict or update as many times as we like.

Of all the math above, all we need to implement are equations (1)(1), (2)(2), and (3)(3).


📙 참고

profile
JUST DO IT.

0개의 댓글