mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
histogramUniform.hpp
Go to the documentation of this file.
1 /** \file histogramUniform.hpp
2  * \author Jared R. Males
3  * \brief Header for the std::vector utilities
4  * \ingroup gen_math_files
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef math_histogramUniform_hpp
28 #define math_histogramUniform_hpp
29 
30 namespace mx
31 {
32 namespace math
33 {
34 
35 ///A histogram with uniform bin spacing
36 /** Calculates the frequency in bins with uniform spacing.
37  *
38  * \tparam realT the real data type
39  *
40  * \ingroup gen_math
41  */
42 template<typename realT>
44 {
45 public:
46 
47  realT m_min {0}; ///<The mininum bin location
48  realT m_max {0}; ///<The maximum bin location
49  realT m_width {0}; ///<The bin width
50 
51  std::vector<realT> _freqs; ///<The frequencies, one for each bin.
52 
53  /// Default c'tor, does not allocate.
54  /** Must call setup before use */
56 
57  ///Setup the histogram, performing allocations.
58  histogramUniform( realT mn, ///< [in] the new minimum bin location
59  realT mx, ///< [in] the new maximum bin location
60  realT w ///< [in] the bin width
61  ) : m_min(mn), m_max(mx), m_width(w)
62  {
63  reset();
64  }
65 
66  ///Setup the histogram, performing allocations.
67  void setup( realT mn, ///< [in] the new minimum bin location
68  realT mx, ///< [in] the new maximum bin location
69  realT w ///< [in] the bin width
70  )
71  {
72  m_min = mn;
73  m_max = mx;
74  m_width = w;
75 
76  reset();
77  }
78 
79  ///Resize and 0 the frequency vector. Assumes m_min, m_max, and m_width are set.
80  void reset()
81  {
82  _freqs.resize( (m_max - m_min)/m_width + 1, 0);
83  }
84 
85  ///Accumulate a value in the appropriate bin.
86  void accum( const realT & val /**< [in] The value to accumulate */)
87  {
88  int i = (val - m_min) / m_width;
89  if( i < 0) i = 0;
90  if( i >= _freqs.size()) i = _freqs.size()-1;
91 
92 
93  ++_freqs[i];
94  }
95 
96  ///Accumulate a vector of values.
97  void accum( const std::vector<realT> & vals /**< [in] The vector of values to accumulate */)
98  {
99  for(int i=0; i< vals.size(); ++i) accum(vals[i]);
100  }
101 
102  ///Get the frequency in the i-th bin.
103  realT freq(int i /**< [in] the bin number */)
104  {
105  return _freqs[i]; ///\returns the current value of _freqs[i].
106  }
107 
108  ///Get the number of bins
109  int bins()
110  {
111  return _freqs.size(); ///\returns the size of the frequency vector.
112  }
113 
114  ///Get the value of the left-edge of the i-th bin.
115  realT binLeft(int i /**< [in] the bin number */)
116  {
117  return m_min + i*m_width; ///\returns the value of the left-edge of the i-th bin.
118  }
119 
120  ///Get the value of the middle of the i-th bin.
121  realT binMid(int i /**< [in] the bin number */)
122  {
123  return m_min + i*m_width + 0.5*m_width; ///\returns the value of the middle of the i-th bin.
124  }
125 
126  ///Get the value of the right edge of the i-th bin.
127  realT binRight(int i /**< [in] the bin number */)
128  {
129  return m_min + i*m_width + 1.0*m_width; ///\returns the value of the right edge of the i-th bin.
130  }
131 
132  ///Normalize the current frequencies so that the integral over all bins is 1.
133  /** This normalizes the histogram so that it is a probability distribution, such that the sum
134  * \f$ \sum_i P_i \Delta x = 1 \f$ where \f$ \Delta x \f$ is the bin width.
135  */
136  void normalize( int excludeTop = 0 /**< [in] [optional] specifies a number of bins at the top of the range to exclude from the sum */)
137  {
138  realT sum = 0;
139 
140  for(int i=0; i< _freqs.size()-excludeTop; ++i)
141  {
142  sum += _freqs[i];
143  }
144 
145  for(int i=0; i< _freqs.size(); ++i)
146  {
147  _freqs[i] /= (sum*m_width);
148  }
149  }
150 
151 };
152 
153 }//namespace math
154 }//namespace mx
155 
156 #endif //math_histogramUniform_hpp
A histogram with uniform bin spacing.
histogramUniform(realT mn, realT mx, realT w)
Setup the histogram, performing allocations.
realT binRight(int i)
Get the value of the right edge of the i-th bin.
void normalize(int excludeTop=0)
Normalize the current frequencies so that the integral over all bins is 1.
void accum(const realT &val)
Accumulate a value in the appropriate bin.
realT freq(int i)
Get the frequency in the i-th bin.
void setup(realT mn, realT mx, realT w)
Setup the histogram, performing allocations.
realT m_width
The bin width.
void accum(const std::vector< realT > &vals)
Accumulate a vector of values.
realT m_min
The mininum bin location.
realT binMid(int i)
Get the value of the middle of the i-th bin.
int bins()
Get the number of bins.
realT m_max
The maximum bin location.
realT binLeft(int i)
Get the value of the left-edge of the i-th bin.
std::vector< realT > _freqs
The frequencies, one for each bin.
histogramUniform()
Default c'tor, does not allocate.
void reset()
Resize and 0 the frequency vector. Assumes m_min, m_max, and m_width are set.
The mxlib c++ namespace.
Definition: mxError.hpp:107