EKF优化:协方差coff计算公式、意义、Code优化
复习!复习!
原文链接:http://blog.csdn.net/goodshot/article/details/8611178
1.代码:
Matlab相关系数的意义:
- Eigen::MatrixXf correlation_matrix = corrcoef( LocM );
对行向量求相关系数 , 与列数无关,返回 cols()*cols() 矩阵...
翻译成Eigen:
还是自己写个函数吧
//1.求协方差
- Eigen::MatrixXf CIcSearchM::cov(Eigen::MatrixXf &d1, Eigen::MatrixXf &d2)
- {
- Eigen::MatrixXf CovM(1,1);
- assert(1 ==d1.cols() && 1 ==d2.cols() &&d1.cols()==d2.cols() );
- //求协方差
- float Ex =0;float Ey=0;
- for (int i=0;i< d1.rows();++i){
- Ex +=d1(i);
- Ey +=d2(i);
- }
- Ex /=d1.rows();
- Ey /=d2.rows();
- for (int i=0;i< d1.rows();++i){
- CovM(0) += (d1(i)-Ex)*(d2(i)-Ey);
- }
- CovM(0) /= d1.rows() -1;
- return CovM;
- }
//2.写入方差矩阵
- //求矩阵的相关系数!
- //返回矩阵A的列向量的相关系数矩阵//对行向量求相关系数 , 与行数无关,返回 cols()*cols() 矩阵...
- Eigen::MatrixXf CIcSearchM::corrcoef(Eigen::MatrixXf &M)
- {
- // C(i,j)/SQRT(C(i,i)*C(j,j)).//C is the covariation Matrix
- int Row= M.rows();
- int Col= M.cols();
- int Order= Col;//int Order= (std::max)(Row,Col);
- Eigen::MatrixXf Coef(Order,Order);
- for (int i=0;i<Order;++i){
- for (int j=0;j<Order;++j){
- Coef(i,j)= cov((Eigen::MatrixXf)M.col(i),(Eigen::MatrixXf)M.col(j))(0);
- }
- }
- return Coef;
- }
2.优化的代码
使用Eigen计算1000维的方阵大概需要200ms的时间,相对于matlab默认开启GPU加速,时间上消耗的太多了。
参考:比较OpenBLAS、Matlab、MKL、Eigen的基础计算性能
优化的代码:
- //求矩阵的相关系数!一个原始公式的简化算法/优化算法
- //返回矩阵A的列向量的相关系数矩阵//对行向量求相关系数 , 与行数无关,返回 cols()*cols() 矩阵...
- Eigen::MatrixXf CIcSearchM::CorrcoefOpm(Eigen::MatrixXf &MI)
- {
- Eigen::MatrixXf M =MI;
- // C(i,j)/SQRT(C(i,i)*C(j,j)).//C is the covariation Matrix
- //公式:
- //temp = mysample - repmat(mean(mysample), 10, 1);
- //result = temp' * temp ./ (size(mysample, 1) - 1)
- int Row= M.rows();
- int Col= M.cols();
- int Order= Col;//int Order= (std::max)(Row,Col);
- SYSTEMTIME sysP;
- GetLocalTime( &sysP );
- int MileTsp = sysP.wSecond;
- int MileTP = sysP.wMilliseconds;
- Eigen::MatrixXf CovM(Order,Order);//(1,Col);
- Eigen::MatrixXf E_M(1,Col);
- //减去每一个维度的均值;确定一列为一个维度。
- //std::cout<< "Mat Src :"<<std::endl;m_Testor.print_EigenMat( M);
- for (int i =0;i< Col;++i)
- {
- //求均值
- E_M(i) =M.col(i).sum()/M.rows();
- //std::cout<< "E_M(i)" << E_M(i)<< std::endl;
- M.col(i) = M.col(i)- E_M(i);
- //
- }
- //SYSTEMTIME sysP2;
- //GetLocalTime( &sysP );
- //int MileTsp2 = sysP.wSecond;
- //int MileTP2 = sysP.wMilliseconds;
- //int DetaTp = MileTP2 - MileTP;
- //int DetaTsp = MileTsp2 -MileTsp;
- //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
- //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
- //std::cout<< "Mat E_M :"<<std::endl;m_Testor.print_EigenMat( M);
- CovM = M.transpose();
- //GetLocalTime( &sysP );
- //MileTsp2 = sysP.wSecond;
- //MileTP2 = sysP.wMilliseconds;
- //DetaTp = MileTP2 - MileTP;
- //DetaTsp = MileTsp2 -MileTsp;
- //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
- //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
- //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
- CovM = CovM * M ;
- //GetLocalTime( &sysP );
- //MileTsp2 = sysP.wSecond;
- //MileTP2 = sysP.wMilliseconds;
- //DetaTp = MileTP2 - MileTP;
- //DetaTsp = MileTsp2 -MileTsp;
- //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
- //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
- //实现 ./ 函数 数值计算没有区别
- CovM = CovM /(Order-1)/(Order-1);
- //GetLocalTime( &sysP );
- //MileTsp2 = sysP.wSecond;
- //MileTP2 = sysP.wMilliseconds;
- //DetaTp = MileTP2 - MileTP;
- //DetaTsp = MileTsp2 -MileTsp;
- //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
- //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
- //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
- //GetLocalTime( &sysP );
- //MileTsp2 = sysP.wSecond;
- //MileTP2 = sysP.wMilliseconds;
- //DetaTp = MileTP2 - MileTP;
- //DetaTsp = MileTsp2 -MileTsp;
- //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
- //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
- //遍历一次
- for (int i=0;i< Order;++i){
- for (int j=0;j<Order;++j){
- CovM(i,j) = sqrt(CovM(i,i)*CovM(j,j) );
- }
- }
- //GetLocalTime( &sysP );
- //MileTsp2 = sysP.wSecond;
- //MileTP2 = sysP.wMilliseconds;
- //DetaTp = MileTP2 - MileTP;
- //DetaTsp = MileTsp2 -MileTsp;
- //std::cout<< "The Process time is :"<< DetaTsp<<"S"<< std::endl;
- //std::cout<< "The Process time is :"<< DetaTp<<"mS"<< std::endl;
- //std::cout<< "Mat CovM :"<<std::endl;m_Testor.print_EigenMat( CovM);
- return CovM;
- }
稀疏矩阵可以加速到3ms,我去!终于可以实用了.....
EKF优化:协方差coff计算公式、意义、Code优化的更多相关文章
- Linux启动时间优化-内核和用户空间启动优化实践
关键词:initcall.bootgraph.py.bootchartd.pybootchart等. 启动时间的优化,分为两大部分,分别是内核部分和用户空间两大部分. 从内核timestamp 0.0 ...
- 基于Oracle的SQL优化(社区万众期待 数据库优化扛鼎巨著)
基于Oracle的SQL优化(社区万众期待数据库优化扛鼎巨著) 崔华 编 ISBN 978-7-121-21758-6 2014年1月出版 定价:128.00元 856页 16开 编辑推荐 本土O ...
- mysql数据库优化课程---18、mysql服务器优化
mysql数据库优化课程---18.mysql服务器优化 一.总结 一句话总结: 1.四种字符集问题:字符集都设置为utf-82.slow log慢查询日志问题3.root密码丢失 1.mysql存在 ...
- ORACLE性能优化- Buffer cache 的调整与优化
Buffer Cache是SGA的重要组成部分,主要用于缓存数据块,其大小也直接影响系统的性能.当Buffer Cache过小的时候,将会造成更多的 free buffer waits事件. 下面将具 ...
- 转 cocos2d-x 优化(纹理渲染优化、资源缓存、内存优化)
概述 包括以下5种优化:引擎底层优化.纹理优化.渲染优化.资源缓存.内存优化 引擎优化 2.0版本比1.0版本在算法上有所优化,效率更高.2.0版本使用OpenGl ES 2.0图形库,1.0版本 ...
- Android开发优化之——对Bitmap的内存优化
http://blog.csdn.net/arui319/article/details/7953690 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitm ...
- MySql数据库3【优化2】sql语句的优化
1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十二)数据层优化-explain关键字及慢sql优化
本文提要 从编码角度来优化数据层的话,我首先会去查一下项目中运行的sql语句,定位到瓶颈是否出现在这里,首先去优化sql语句,而慢sql就是其中的主要优化对象,对于慢sql,顾名思义就是花费较多执行时 ...
- 数据库性能优化(database tuning)性能优化绝不仅仅只是索引
一毕业就接触优化方面的问题,专业做优化也有至少5年之多的时间了,可现在还是经常听到很多人认为优化很简单,就是建索引的问题,这确实不能怪大家,做这行20多年的时间里,在职业生涯的每个阶段,几乎都能听到这 ...
随机推荐
- 学习记录--让我打开另一种思路的SQL
1.显示文章.提交人和最后回复时间 select a.title,a.username,b.adddate from table a, (select max(adddate) adddate fro ...
- Python网络请求urllib和urllib3详解
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...
- 【POJ 1860】Currency Exchange
[题目链接]:http://poj.org/problem?id=1860 [题意] 给你n种货币,m种货币之间的交换信息; 交换信息以 A,B,RA,CA,RB,CB的形式给出; 即A换B的话假设A ...
- [POJ1226]Substrings(后缀数组)
传送门 给定 n 个字符串,求出现或反转后出现在每个字符串中的最长子串. 算法分析: 这题不同的地方在于要判断是否在反转后的字符串中出现.其实这并没有加大题目的难度. 只需要先将每个字符串都反过来写一 ...
- 清北学堂模拟赛d3t3 c
分析:一开始拿到这道题真的是无从下手,暴力都很难打出来.但是基本的方向还是要有的,题目问的是方案数,dp不行就考虑数学方法.接下来比较难想.其实对于每一行或者每一列,我们任意打乱顺序其实对答案是没有影 ...
- 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?
这里的疑问在于pageContext和request都是JSP中的内置对象之一,为什么不直接用${request.contextPath}来获取项目路径? 出现这种疑问,其实是将JSP的内置对象和EL ...
- R语言 PCA
1.关键点 综述:主成分分析 因子分析 典型相关分析,三种方法的共同点主要是用来对数据降维处理的从数据中提取某些公共部分,然后对这些公共部分进行分析和处理. #主成分分析 是将多指标化为少数几个综合指 ...
- ESXi License过期解决办法
http://blog.sina.com.cn/s/blog_538439270101pqls.html
- asp.net--mvc--异步编程
Using Asynchronous Methods in ASP.NET MVC 4 asp.net mvc中的异步只能增加系统的性能,原来需要500个线程的,现在需要50个就够了,对一些常规的程序 ...
- Unity图片变灰的方式
http://www.tuicool.com/articles/Vruuqme NGUI中的Button差点儿是最经常使用到的控件之中的一个,而且能够组合各种组件(比方UIButtonColor,UI ...