MRPT  1.9.9
CHistogram.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "math-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
13 #include <mrpt/math/CHistogram.h>
14 #include <mrpt/math/utils.h> // linspace()
15 
16 using namespace mrpt;
17 using namespace mrpt::math;
18 
19 /*---------------------------------------------------------------
20  Constructor
21  ---------------------------------------------------------------*/
22 CHistogram::CHistogram(const double min, const double max, const size_t nBins)
23  : m_min(min), m_max(max), m_bins(nBins, 0), m_count(0)
24 {
25  ASSERT_(nBins > 0);
26  ASSERT_(max > min);
27  m_binSizeInv = nBins / (m_max - m_min);
28 }
29 
30 /*---------------------------------------------------------------
31  clear
32  ---------------------------------------------------------------*/
34 {
35  m_bins.assign(m_bins.size(), 0);
36  m_count = 0;
37 }
38 
39 /*---------------------------------------------------------------
40  add
41  ---------------------------------------------------------------*/
42 void CHistogram::add(const double x)
43 {
44  ASSERT_(!m_bins.empty());
45  if (x < m_min || x > m_max) return;
46 
47  auto ind = static_cast<size_t>(m_binSizeInv * (x - m_min));
48  if (ind >= m_bins.size()) ind = m_bins.size() - 1;
49 
50  m_bins[ind]++;
51  m_count++;
52 }
53 
54 /*---------------------------------------------------------------
55  getBinCount
56  ---------------------------------------------------------------*/
57 size_t CHistogram::getBinCount(const size_t index) const
58 {
59  if (index >= m_bins.size()) THROW_EXCEPTION("Index out of bounds");
60 
61  return m_bins[index];
62 }
63 
64 /*---------------------------------------------------------------
65  getBinRatio
66  ---------------------------------------------------------------*/
67 double CHistogram::getBinRatio(const size_t index) const
68 {
69  if (index >= m_bins.size()) THROW_EXCEPTION("Index out of bounds");
70 
71  if (m_count)
72  return m_bins[index] / double(m_count);
73  else
74  return 0;
75 }
76 
78  std::vector<double>& x, std::vector<double>& hits) const
79 {
80  linspace(m_min, m_max, m_bins.size(), x);
81  const size_t N = m_bins.size();
82  hits.resize(N);
83  for (size_t i = 0; i < N; i++) hits[i] = static_cast<double>(m_bins[i]);
84 }
85 
87  std::vector<double>& x, std::vector<double>& hits) const
88 {
89  const size_t N = m_bins.size();
90  linspace(m_min, m_max, N, x);
91 
92  hits.resize(N);
93  const double K = m_binSizeInv / m_count;
94  for (size_t i = 0; i < N; i++) hits[i] = K * m_bins[i];
95 }
96 
98  double min, double max, double binWidth)
99 {
100  ASSERT_(max > min);
101  ASSERT_(binWidth > 0);
102  return CHistogram(
103  min, max, static_cast<size_t>(ceil((max - min) / binWidth)));
104 }
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:33
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:41
CHistogram(const double min, const double max, const size_t nBins)
Constructor.
Definition: CHistogram.cpp:22
double m_binSizeInv
((max-min)/nBins)^-1
Definition: CHistogram.h:39
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
void add(const double x)
Add an element to the histogram.
Definition: CHistogram.cpp:42
double m_min
The histogram limits.
Definition: CHistogram.h:37
void linspace(T first, T last, size_t count, VECTOR &out_vector)
Generates an equidistant sequence of numbers given the first one, the last one and the desired number...
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
Definition: CHistogram.cpp:77
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t m_count
The total elements count.
Definition: CHistogram.h:43
size_t getBinCount(const size_t index) const
Retuns the elements count into the selected bin index, where first one is 0.
Definition: CHistogram.cpp:57
double getBinRatio(const size_t index) const
Retuns the ratio in [0,1] range for the selected bin index, where first one is 0. ...
Definition: CHistogram.cpp:67
void clear()
Clear the histogram:
Definition: CHistogram.cpp:33
CHistogram createWithFixedWidth(double min, double max, double binWidth)
Constructor with a fixed bin width.
Definition: CHistogram.cpp:97
void getHistogramNormalized(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts, normalized such as the integral of the histogram...
Definition: CHistogram.cpp:86



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020