JKalman滤波的使用(java实现的卡尔曼滤波)

             JKalman是一个开源项目,利用java实现的卡尔曼滤波,本文主要记录在项目中的使用记录。

        1. 官网的源码下载:JKalman download | SourceForge.net

         保存到百度网盘的源码及卡尔曼介绍文档下载:JKalman-1.0.zip_免费高速下载|百度网盘-分享无限制

         解压:

          

       2. 我是用在安卓项目里,把src目录下的jama跟jkalman复制到自己的安卓项目里。

       

   

  3. 参照源码提供的测试程序KalmanTest.java,创建一个类去使用JKalman里的方法。

public class KalmanFilter {
    private JKalman mFilter;
    private Matrix mPredictValue;
    private Matrix mCorrectedValue;
    private Matrix mMeasurementValue;

    private final String TAG = "KalmanFilter";

    public void KalmanFilter(){
        // empty constructor.
    }

    public void initial(){
        try {
            mFilter = new JKalman(4, 2);

            double x = 0;
            double y = 0;

            // init
            mPredictValue = new Matrix(4, 1); // predict state [x, y, dx, dy, dxy]
            mCorrectedValue = new Matrix(4, 1); // corrected state [x, y, dx, dy, dxy]

            mMeasurementValue = new Matrix(2, 1); // measurement [x]
            mMeasurementValue.set(0, 0, x);
            mMeasurementValue.set(1, 0, y);

            // transitions for x, y, dx, dy
            double[][] tr = { {1, 0, 1, 0},
                    {0, 1, 0, 1},
                    {0, 0, 1, 0},
                    {0, 0, 0, 1} };
            mFilter.setTransition_matrix(new Matrix(tr));

            // 1s somewhere?
            mFilter.setError_cov_post(mFilter.getError_cov_post().identity());

            // Init first assumption similar to first observation (cheat :)
            // kalman.setState_post(kalman.getState_post());
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage());
        }
    }

    // 其他地方会调用这个方法,old是输入的值,newValue是经过过滤的值
    // 可以过滤掉跳变值。
    public void filter(Point oldValue, Point newValue) {
        // check state before
        mPredictValue = mFilter.Predict();
        mMeasurementValue.set(0, 0, oldValue.x);
        mMeasurementValue.set(1, 0, oldValue.y);

        // look better
        mCorrectedValue = mFilter.Correct(mMeasurementValue);

        newValue.x = (float)mPredictValue.get(0,0);
        newValue.y = (float)mPredictValue.get(1,0);
    }
}

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
卡尔曼滤波是一种常用的状态估计方法,可用于对系统状态进行预测和优化。在Java实现卡尔曼滤波,可以按照以下步骤进行: 1. 定义状态方程和观测方程 卡尔曼滤波的核心是状态方程和观测方程,它们用于描述系统的状态和观测值之间的关系。在Java中可以使用矩阵运算库如Apache Commons Math来编写这些方程。 2. 初始化状态和协方差矩阵 卡尔曼滤波需要初始化系统的状态和协方差矩阵。在Java中可以使用矩阵运算库来实现这一步骤。 3. 实现卡尔曼滤波算法 在Java中可以编写一个类来实现卡尔曼滤波算法。该类可以包括以下方法: - predict: 根据状态方程进行状态预测 - update: 根据观测方程进行状态更新 - getState: 获取当前状态 - getErrorCovariance: 获取当前协方差矩阵 4. 测试卡尔曼滤波算法 可以编写一个测试类来测试卡尔曼滤波算法的性能和准确度。可以使用一些已知的数据集来测试算法的效果。 下面是一个简单的Java实现卡尔曼滤波的例子: ```java import org.apache.commons.math3.linear.*; public class KalmanFilter { private RealMatrix state; // 系统状态 private RealMatrix stateCovariance; // 系统状态协方差矩阵 private RealMatrix transitionMatrix; // 状态转移矩阵 private RealMatrix observationMatrix; // 观测矩阵 private RealMatrix measurementNoise; // 观测噪声协方差矩阵 private RealMatrix processNoise; // 系统噪声协方差矩阵 public KalmanFilter(RealMatrix initialState, RealMatrix initialCovariance, RealMatrix transitionMatrix, RealMatrix observationMatrix, RealMatrix measurementNoise, RealMatrix processNoise) { this.state = initialState; this.stateCovariance = initialCovariance; this.transitionMatrix = transitionMatrix; this.observationMatrix = observationMatrix; this.measurementNoise = measurementNoise; this.processNoise = processNoise; } public void predict() { // 预测状态 state = transitionMatrix.multiply(state); // 预测协方差矩阵 stateCovariance = transitionMatrix.multiply(stateCovariance) .multiply(transitionMatrix.transpose()) .add(processNoise); } public void update(RealMatrix measurement) { // 计算观测残差 RealMatrix innovation = measurement.subtract(observationMatrix.multiply(state)); // 计算卡尔曼增益 RealMatrix kalmanGain = stateCovariance.multiply(observationMatrix.transpose()) .multiply((observationMatrix.multiply(stateCovariance) .multiply(observationMatrix.transpose()) .add(measurementNoise)).inverse()); // 更新状态 state = state.add(kalmanGain.multiply(innovation)); // 更新协方差矩阵 stateCovariance = (RealMatrix) MatrixUtils .createRealIdentityMatrix(stateCovariance.getRowDimension()) .subtract(kalmanGain.multiply(observationMatrix)) .multiply(stateCovariance); } public RealMatrix getState() { return state; } public RealMatrix getErrorCovariance() { return stateCovariance; } } ``` 以上代码实现了一个简单的卡尔曼滤波器,包括predict和update两个方法用于状态预测和状态更新。在使用时,需要提供系统状态和协方差矩阵的初始值,状态转移矩阵和观测矩阵,以及观测噪声协方差矩阵和系统噪声协方差矩阵。可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kevin@1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值