近期由于需要,开始关注bayes++库的使用,Bayes++是一个开源的C++类库。这些类表示并实现了用于离散系统的贝叶斯滤波的各种数值算法。该库中的类提供测试和一致的数值方法,并且用层次明确的结构表明了各种滤波算法和系统模型类型。
参考网站:Bayes++ Library入门学习
bayes++库官方网站
1.准备
编译环境:kdevelop
系统:ubuntu14.04
插一句,如果想要安装kdevelop,直接在终端输入sudo apt-get kdevelop即可,kdevelop还是一个比较好用的轻便编译器的。
从bayes++官网下载下来的,可以看到bayes的实现是通过大量类的相互继承来实现的,从核心文件夹bayes就可以看出来。
2.库制作
从整个bayes++库可以看到,它是设计使用在Windows环境下的,所以要通过cmake制作一个共享库,原理参见博客:
多目录工程的CmakeLists.txt编写(自动添加多目录下的文件)
将核心文件夹BayesFilter里的文件单独复制出来一份,制作一个共享库。
在里面新建一个cmakelist文件,在里面写入
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. BayesFilter_SRCS)
add_library(BayesFilter ${BayesFilter_SRCS})
这个库就算制作完成了。如果不成功的话,也可以去这里下载:
https://download.csdn.net/download/qq_36355662/10385056
3.实验
以bayes++库中自带的一个简单例子参考,用kdevelop编译环境来使用制作好的bayes++库。
建立工程如下所示,
在main.cpp文件中写入:
#include <iostream>
/*
* Bayes++ the Bayesian Filtering Library
* Copyright (c) 2002 Michael Stevens
* See accompanying Bayes++.htm for terms and conditions of use.
*
* $Id$
*/
/*
* Example of using Bayesian Filter Class to solve a simple problem.
* 使用贝叶斯过滤器类来解决一个简单问题的例子。
* A linear filter with one state and constant noises
* 具有一个状态和不变噪声的线性滤波器。
*/
// Use the Unscented filter scheme from Bayes++
//使用Bayes ++中的无迹过滤器方案。
#include "BayesFilter/unsFlt.hpp"
#include <iostream>
#include <boost/numeric/ublas/io.hpp>
using namespace std;
using namespace Bayesian_filter;
using namespace Bayesian_filter_matrix;
/*
* Simple Prediction model
* 简单的预测模型
*/
class Simple_predict : public Linear_predict_model
{
public:
Simple_predict() : Linear_predict_model(1,1)
// Construct a constant model构造一个常量模型
{
// Stationary Prediction model (Identity)固定预测模型
Fx(0,0) = 1.;
// Constant Noise model with a large variance具有较大方差的恒定噪声模型
q[0] = 2.;//噪声方差(总是密集的,使用耦合来表示稀疏性)
G(0,0) = 1.;//噪声耦合
}
};
/*
* Simple Observation model
* 简单的观测模型
*/
class Simple_observe : public Linear_uncorrelated_observe_model
{
public:
Simple_observe () : Linear_uncorrelated_observe_model(1,1)
// Construct a constant model构造一个常量模型
{
// Linear model线性模型
Hx(0,0) = 1;
// Constant Observation Noise model with variance of one方差为1的常数观测噪声模型
Zv[0] = 1.;//噪声方差
}
};
/*
* Filter a simple example
*/
int main(int argc, char **argv)
{
// Global setup for test output
cout.flags(ios::fixed); //以定点形式显示浮点数
cout.precision(4);//设定输出值以新的浮点数精度值显示,即小数点后保留4位。
// Construct simple Prediction and Observation models
//构建简单的预测和观测模型
Simple_predict my_predict;
Simple_observe my_observe;
// Use an 'Unscented' filter scheme with one state
//在一种状态下使用“无迹”过滤器方案
Unscented_scheme my_filter(1);
// Setup the initial state and covariance
//设置初始状态和协方差
Vec x_init (1);
SymMatrix X_init (1, 1);
x_init[0] = 10.; // Start at 10 with no uncertainty从10开始,没有不确定性
X_init(0,0) = 0.;
//无迹滤波器初始化
my_filter.init_kalman (x_init, X_init);
cout << "Unscented_filter Initial : x = " << my_filter.x <<" X = "<< my_filter.X << endl;
// Predict the filter forward前向滤波器预测
my_filter.predict (my_predict);
//更新过滤器,所以状态和协方差可用
my_filter.update(); // Update the filter, so state and covariance are available
cout << "Unscented_filter Predict : x = " << my_filter.x <<" X = "<< my_filter.X << endl;
// Make an observation做个观察
Vec z(1);
z[0] = 11.; // Observe that we should be at 11
my_filter.observe (my_observe, z);
//更新过滤器,所以状态和协方差可用
my_filter.update(); // Update the filter to state and covariance are available
cout << "Unscented_filter Filtered :x = " << my_filter.x <<" X = "<< my_filter.X << endl;
return 0;
}
在cmakelist文件中写入:
cmake_minimum_required(VERSION 2.8)
project(simpleExample)
# Add header file include directories
include_directories( ./BayesFilter)
# Add block directories
add_subdirectory(BayesFilter)
# Target
#add_executable(bayestest ${DIRSRCS})
add_executable(simpleExample main.cpp)
target_link_libraries(simpleExample BayesFilter )
install(TARGETS simpleExample RUNTIME DESTINATION bin)
编译之后,运行就能得出结果了。