cartographer源码分析(5)-common-fixed_ratio_sampler.h
来源:互联网 发布:java布局管理器 编辑:程序博客网 时间:2024/06/06 00:51
源码可在https://github.com/learnmoreonce/SLAM 下载
文件:common/fixed_ratio_sampler.h#ifndef CARTOGRAPHER_COMMON_FIXED_RATIO_SAMPLER_H_#define CARTOGRAPHER_COMMON_FIXED_RATIO_SAMPLER_H_#include <string>#include "cartographer/common/port.h"namespace cartographer {namespace common {/*FixedRatioSampler是频率固定的采样器类,目的是从数据流中均匀的按照固定频率采样数据FixedRatioSampler不可拷贝,不可赋值.成员函数提供2种操作:Pulse()产生一个事件pulses,并且:如果计入采样samples,返回trueDebugString():以string形式输出采样率*/// Signals when a sample should be taken from a stream of data to select a// uniformly distributed fraction of the data.class FixedRatioSampler { public: explicit FixedRatioSampler(double ratio); ~FixedRatioSampler(); FixedRatioSampler(const FixedRatioSampler&) = delete; FixedRatioSampler& operator=(const FixedRatioSampler&) = delete; // Returns true if this pulse should result in an sample. bool Pulse(); // Returns a debug string describing the current ratio of samples to pulses. string DebugString(); private: // Sampling occurs if the proportion of samples to pulses drops below this // number. const double ratio_; int64 num_pulses_ = 0; //产生的脉冲次数 int64 num_samples_ = 0;//记录的采样次数};} // namespace common} // namespace cartographer#endif // CARTOGRAPHER_COMMON_FIXED_RATIO_SAMPLER_H_
.
文件:fixed_ratio_sampler.cc:#include "cartographer/common/fixed_ratio_sampler.h"namespace cartographer {namespace common {FixedRatioSampler::FixedRatioSampler(const double ratio) : ratio_(ratio) {}FixedRatioSampler::~FixedRatioSampler() {}bool FixedRatioSampler::Pulse() { //如果当前采样率 samples/ pulses低于固定采样率,此次事件计入采样. ++num_pulses_; if (static_cast<double>(num_samples_) / num_pulses_ < ratio_) { ++num_samples_; return true; } return false;}string FixedRatioSampler::DebugString() { return std::to_string(num_samples_) + " (" + std::to_string(100. * num_samples_ / num_pulses_) + "%)";}} // namespace common} // namespace cartographer
…
测试代码:#include "cartographer/common/fixed_ratio_sampler.h"#include "gtest/gtest.h"namespace cartographer {namespace common {namespace {TEST(FixedRatioSamplerTest, AlwaysTrue) { FixedRatioSampler fixed_ratio_sampler(1.);//固定采样率是1hz,每次Pulse都采集samples for (int i = 0; i < 100; ++i) { EXPECT_TRUE(fixed_ratio_sampler.Pulse()); }}TEST(FixedRatioSamplerTest, AlwaysFalse) { FixedRatioSampler fixed_ratio_sampler(0.);//0 hz,不采集samples for (int i = 0; i < 100; ++i) { EXPECT_FALSE(fixed_ratio_sampler.Pulse()); }}TEST(FixedRatioSamplerTest, SometimesTrue) { FixedRatioSampler fixed_ratio_sampler(0.5); //0.5hz for (int i = 0; i < 100; ++i) { EXPECT_EQ(i % 2 == 0, fixed_ratio_sampler.Pulse());//每2次Pulse采集一次samples }}TEST(FixedRatioSamplerTest, FirstPulseIsTrue) { // Choose a very very small positive number for the ratio. FixedRatioSampler fixed_ratio_sampler(1e-20); //0.000000...001hz EXPECT_TRUE(fixed_ratio_sampler.Pulse()); for (int i = 0; i < 100; ++i) { EXPECT_FALSE(fixed_ratio_sampler.Pulse()); //每100000000次Pulse采集一次samples }}} // namespace} // namespace common} // namespace cartographer
本文发于:
* http://www.jianshu.com/u/9e38d2febec1
* https://zhuanlan.zhihu.com/learnmoreonce
* http://blog.csdn.net/learnmoreonce
* slam源码分析微信公众号:slamcode
阅读全文
0 0
- cartographer源码分析(5)-common-fixed_ratio_sampler.h
- cartographer源码分析(5)-common-rate_time.h
- cartographer源码分析(3)-common-port.h
- cartographer源码分析(4)-common-time.h
- cartographer源码分析(6)-common-histogram.h
- cartographer源码分析(7)-common-math.h
- cartographer源码分析(8)-common-make_unique.h
- cartographer源码分析(9)-common-mutex.h
- cartographer源码分析(10)-common-thread_pool.h
- cartographer源码分析(11)-common-blocking_queue.h
- cartographer源码分析(13)-transform-rigid_transform.h
- cartographer源码分析(14)-transform-transform.h
- cartographer源码分析(15)-transform-transform_interpolation_buffer.h
- cartographer源码分析(17)-sensor-point_cloud.h
- cartographer源码分析(19)-sensor-range_data.h
- cartographer源码分析(20)-sensor-data.h
- cartographer源码分析(21)-sensor-ordered_multi_queue.h
- cartographer源码分析(22)-sensor-collator.h
- 全选 反选
- LeetCode | 48. Rotate Image
- PMODAD1 实现模拟数据的采集 时序篇
- Tensorflow教程--新手入门笔记--安装
- linux 怎么完全卸载mysql数据库
- cartographer源码分析(5)-common-fixed_ratio_sampler.h
- react native RadioButton(单选按钮)
- 后台使用imagemagick的convert命令来处理图片真是太方便了。
- YouTube开发指南
- 开发工具心得:如何 10 倍提高你的 Webpack 构建效率
- linux tomcat日志中文变成???
- 概率论基本概念
- 站点访问异常弹窗
- innosetup if else 语句中 end 之后; 讲解