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);
    }
}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐