贝叶斯滤波与卡尔曼滤波
递归状态估计先用运动模型预测状态分布,再用新观测更新后验。KF、EKF 与粒子滤波采用不同的分布表示和近似方式。
2. 基础与定义
Section titled “2. 基础与定义”2.1 状态与观测
Section titled “2.1 状态与观测”状态 、输入 、观测 由转移分布 与观测分布 连接。估计目标是 。
2.2 预测与更新
Section titled “2.2 预测与更新”3. 核心原理
Section titled “3. 核心原理”3.1 线性高斯滤波
Section titled “3.1 线性高斯滤波”对 、,过程与观测噪声独立,协方差为 。预测为
创新 、创新协方差 ,更新为
最后一式是 Joseph 协方差更新。线性高斯条件下,均值与协方差完整描述精确后验。
卡尔曼增益推导
误差更新为 。独立零均值误差给出 Joseph 形式。对 关于 求导并令零:,因此 。实现中求解线性系统即可。
3.2 非线性线性化
Section titled “3.2 非线性线性化”EKF 对 、 在当前估计处计算雅可比 。均值使用非线性函数传播,协方差使用局部线性模型。迭代 EKF 在固定预测先验下重新线性化观测。
3.3 粒子近似
Section titled “3.3 粒子近似”粒子滤波用 表示后验。以转移先验为提议分布时,先采样状态,再更新
有效样本数 可用于触发重采样。对数权重与归一化可减少数值下溢。
离散状态递推
有限状态下,转移矩阵 每列和为一。令先验概率向量为 ,观测似然向量为 ,则 ,。归一化分母为零表示观测与当前模型没有共同支持。
4. 实现与应用
Section titled “4. 实现与应用”4.1 估计流程
Section titled “4.1 估计流程”时间同步 → 预测均值与协方差 → 构造创新 → 更新状态 → 检查创新统计及协方差。
线性 KF 的 Eigen 实现
输入矩阵维度匹配, 半正定、 正定,观测噪声与先验独立。
#include <Eigen/Dense>
class KalmanFilter {private: Eigen::VectorXd x; Eigen::MatrixXd P; Eigen::MatrixXd F, B, H, Q, R, I;
public: const Eigen::VectorXd& state() const { return x; } const Eigen::MatrixXd& covariance() const { return P; }
KalmanFilter(const Eigen::VectorXd& x0, const Eigen::MatrixXd& P0) : x(x0), P(P0) {}
void setMatrices(const Eigen::MatrixXd& F_, const Eigen::MatrixXd& B_, const Eigen::MatrixXd& H_, const Eigen::MatrixXd& Q_, const Eigen::MatrixXd& R_) { F = F_; B = B_; H = H_; Q = Q_; R = R_; I = Eigen::MatrixXd::Identity(P.rows(), P.cols()); }
void predict(const Eigen::VectorXd& u) { x = F * x + B * u; P = F * P * F.transpose() + Q; }
void update(const Eigen::VectorXd& z) { Eigen::MatrixXd S = H * P * H.transpose() + R; Eigen::MatrixXd K = S.ldlt().solve(H * P).transpose();
Eigen::VectorXd y = z - H * x; x = x + K * y; Eigen::MatrixXd A = I - K * H; P = A * P * A.transpose() + K * R * K.transpose(); }};EKF 的 Eigen 实现
该示例采用欧氏状态和加性噪声,调用方提供函数及其雅可比。
#include <Eigen/Dense>#include <functional>
class ExtendedKalmanFilter {private: Eigen::VectorXd x; Eigen::MatrixXd P; Eigen::MatrixXd Q, R, I;
public: const Eigen::VectorXd& state() const { return x; } const Eigen::MatrixXd& covariance() const { return P; }
ExtendedKalmanFilter(const Eigen::VectorXd& x0, const Eigen::MatrixXd& P0, const Eigen::MatrixXd& Q_, const Eigen::MatrixXd& R_) : x(x0), P(P0), Q(Q_), R(R_) { I = Eigen::MatrixXd::Identity(P.rows(), P.cols()); }
void predict(const Eigen::VectorXd& u, std::function<Eigen::VectorXd(Eigen::VectorXd, Eigen::VectorXd)> f, std::function<Eigen::MatrixXd(Eigen::VectorXd, Eigen::VectorXd)> jacobian_f) { Eigen::MatrixXd F = jacobian_f(x, u); x = f(x, u); P = F * P * F.transpose() + Q; }
void update(const Eigen::VectorXd& z, std::function<Eigen::VectorXd(Eigen::VectorXd)> h, std::function<Eigen::MatrixXd(Eigen::VectorXd)> jacobian_h) { Eigen::MatrixXd H = jacobian_h(x); Eigen::MatrixXd S = H * P * H.transpose() + R; Eigen::MatrixXd K = S.ldlt().solve(H * P).transpose();
Eigen::VectorXd y = z - h(x); x = x + K * y; Eigen::MatrixXd A = I - K * H; P = A * P * A.transpose() + K * R * K.transpose(); }};5. 分析与总结
Section titled “5. 分析与总结”| 方法 | 后验表示 | 主要近似 |
|---|---|---|
| 离散 Bayes | 有限状态概率 | 状态离散化 |
| KF | 高斯 | 线性高斯下闭式精确 |
| EKF | 高斯 | 非线性一阶线性化 |
| 粒子滤波 | 加权样本 | 有限采样与重采样 |
噪声模型、时间同步和可观测性共同决定估计质量。