尝试一个简单的卡尔曼滤波

最近翻看了这篇翻译的文章详解卡尔曼滤波原理-CSDN博客

萌发了找个简单例子帮助自己理解的想法, 卡尔曼的作用在于可以在任何含有不确定信息 的动态系统中使用滤波,对系统下一步的走向做出有根据的预测,即使伴随着各种干扰,卡尔曼滤波总是能指出真实发生的情况。

用一个线性随机波动的真实序列做卡尔曼滤波测试

import numpy as np
import matplotlib.pyplot as plt

# 定义卡尔曼滤波器
class KalmanFilter:
    def __init__(self, process_variance, measurement_variance, estimated_measurement_variance):
        self.process_variance = process_variance
        self.measurement_variance = measurement_variance
        self.estimated_measurement_variance = estimated_measurement_variance
        self.posteri_estimate = 0.0
        self.posteri_error_estimate = 1.0

    def update(self, measurement):
        # 预测步骤
        priori_estimate = self.posteri_estimate
        priori_error_estimate = self.posteri_error_estimate + self.process_variance

        # 更新步骤
        blending_factor = priori_error_estimate / (priori_error_estimate + self.measurement_variance)
        self.posteri_estimate = priori_estimate + blending_factor * (measurement - priori_estimate)
        self.posteri_error_estimate = (1 - blending_factor) * priori_error_estimate

        return self.posteri_estimate

# 模拟观测数据
np.random.seed(0)
true_values = np.linspace(0, 10, 100) + np.random.normal(0, 1, 100)

# 初始化滤波器
kf = KalmanFilter(process_variance=0.01, measurement_variance=1, estimated_measurement_variance=0.01)

# 对观测数据进行滤波
filtered_values = []
for measurement in true_values:
    filtered_values.append(kf.update(measurement))

# 绘制结果
plt.plot(range(100), true_values, label='True Values')
plt.plot(range(100), filtered_values, label='Filtered Values')
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Kalman Filter Demo')
plt.legend()
plt.show()

类的构造函数,用于初始化卡尔曼滤波器的参数。

  • process_variance:过程噪声的方差,表示系统自身的变化不确定性。
  • measurement_variance:测量噪声的方差,表示测量值的不确定性。
  • estimated_measurement_variance:预估的测量方差(在这个代码中未使用)。
  • posteri_estimate:后验估计值,初始化为 0.0,表示当前对系统状态的估计。
  • posteri_error_estimate:后验误差估计,初始化为 1.0,表示当前估计的误差。

update 方法:用于更新卡尔曼滤波器的状态。

  • measurement:当前测量值。
  • 预测步骤
    • priori_estimate:当前的后验估计值,即上一次更新后的估计。
    • priori_error_estimate:预测的误差估计,通过将后验误差加上过程噪声来获得。

更新步骤

  • blending_factor:混合因子,决定了预测值和测量值在最终估计中的权重。它的值在 0 到 1 之间,越接近 1 表示测量值越可信,越接近 0 表示预测值越可信。
  • self.posteri_estimate:更新后的后验估计值,通过将预测值与当前测量值结合来计算。
  • self.posteri_error_estimate:更新后的后验误差估计,反映了新的误差情况。

最后,该方法返回更新后的后验估计值,表示当前对系统状态的最新估计。

整体来说,KalmanFilter 类实现了卡尔曼滤波的基本逻辑,能够根据不断更新的测量值来优化对系统状态的估计。通过预测和更新步骤,它有效地结合了测量噪声和过程噪声,从而提供一个更准确的状态估计。

测试结果

相关推荐
m0_748245347 分钟前
python——Django 框架
开发语言·python·django
曼巴UE511 分钟前
UE5.3 C++ TArray系列(一)
开发语言·c++·ue5
熬夜苦读学习23 分钟前
Linux文件系统
linux·运维·服务器·开发语言·后端
菜鸟一枚在这30 分钟前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
java1234_小锋34 分钟前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
阿巴~阿巴~1 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
winfredzhang2 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写
奔跑吧邓邓子2 小时前
【Python爬虫(34)】Python多进程编程:开启高效并行世界的钥匙
开发语言·爬虫·python·多进程
Heris993 小时前
2.22 c++练习【operator运算符重载、封装消息队列、封装信号灯集】
开发语言·c++