mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [
git repo
]
Loading...
Searching...
No Matches
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>
43
class
histogramUniform
44
{
45
public
:
46
realT
m_min
{ 0 };
///< The mininum bin location
47
realT
m_max
{ 0 };
///< The maximum bin location
48
realT
m_width
{ 0 };
///< The bin width
49
50
std::vector<realT>
_freqs
;
///< The frequencies, one for each bin.
51
52
/// Default c'tor, does not allocate.
53
/** Must call setup before use */
54
histogramUniform
()
55
{
56
}
57
58
/// Setup the histogram, performing allocations.
59
histogramUniform
( realT
mn
,
///< [in] the new minimum bin location
60
realT
mx
,
///< [in] the new maximum bin location
61
realT w
///< [in] the bin width
62
)
63
:
m_min
(
mn
),
m_max
(
mx
),
m_width
( w )
64
{
65
reset
();
66
}
67
68
/// Setup the histogram, performing allocations.
69
void
setup
( realT
mn
,
///< [in] the new minimum bin location
70
realT
mx
,
///< [in] the new maximum bin location
71
realT w
///< [in] the bin width
72
)
73
{
74
m_min
=
mn
;
75
m_max
=
mx
;
76
m_width
= w;
77
78
reset
();
79
}
80
81
/// Resize and 0 the frequency vector. Assumes m_min, m_max, and m_width are set.
82
void
reset
()
83
{
84
_freqs
.resize( (
m_max
-
m_min
) /
m_width
+ 1, 0 );
85
}
86
87
/// Accumulate a value in the appropriate bin.
88
void
accum
(
const
realT &
val
/**< [in] The value to accumulate */
)
89
{
90
int
i
= (
val
-
m_min
) /
m_width
;
91
if
(
i
< 0 )
92
i
= 0;
93
if
(
i
>=
_freqs
.size() )
94
i
=
_freqs
.size() - 1;
95
96
++
_freqs
[
i
];
97
}
98
99
/// Accumulate a vector of values.
100
void
accum
(
const
std::vector<realT> &
vals
/**< [in] The vector of values to accumulate */
)
101
{
102
for
(
int
i
= 0;
i
<
vals
.size(); ++
i
)
103
accum
(
vals
[
i
] );
104
}
105
106
/// Get the frequency in the i-th bin.
107
realT
freq
(
int
i
/**< [in] the bin number */
)
108
{
109
return
_freqs
[
i
];
///\returns the current value of _freqs[i].
110
}
111
112
/// Get the number of bins
113
int
bins
()
114
{
115
return
_freqs
.size();
///\returns the size of the frequency vector.
116
}
117
118
/// Get the value of the left-edge of the i-th bin.
119
realT
binLeft
(
int
i
/**< [in] the bin number */
)
120
{
121
return
m_min
+
i
*
m_width
;
///\returns the value of the left-edge of the i-th bin.
122
}
123
124
/// Get the value of the middle of the i-th bin.
125
realT
binMid
(
int
i
/**< [in] the bin number */
)
126
{
127
return
m_min
+
i
*
m_width
+ 0.5 *
m_width
;
///\returns the value of the middle of the i-th bin.
128
}
129
130
/// Get the value of the right edge of the i-th bin.
131
realT
binRight
(
int
i
/**< [in] the bin number */
)
132
{
133
return
m_min
+
i
*
m_width
+ 1.0 *
m_width
;
///\returns the value of the right edge of the i-th bin.
134
}
135
136
/// Normalize the current frequencies so that the integral over all bins is 1.
137
/** This normalizes the histogram so that it is a probability distribution, such that the sum
138
* \f$ \sum_i P_i \Delta x = 1 \f$ where \f$ \Delta x \f$ is the bin width.
139
*/
140
void
normalize
(
141
int
excludeTop
=
142
0
/**< [in] [optional] specifies a number of bins at the top of the range to exclude from the sum */
)
143
{
144
realT sum = 0;
145
146
for
(
int
i
= 0;
i
<
_freqs
.size() -
excludeTop
; ++
i
)
147
{
148
sum +=
_freqs
[
i
];
149
}
150
151
for
(
int
i
= 0;
i
<
_freqs
.size(); ++
i
)
152
{
153
_freqs
[
i
] /= ( sum *
m_width
);
154
}
155
}
156
};
157
158
}
// namespace math
159
}
// namespace mx
160
161
#endif
// math_histogramUniform_hpp
mx::math::histogramUniform
A histogram with uniform bin spacing.
Definition
histogramUniform.hpp:44
mx::math::histogramUniform::histogramUniform
histogramUniform(realT mn, realT mx, realT w)
Setup the histogram, performing allocations.
Definition
histogramUniform.hpp:59
mx::math::histogramUniform::binRight
realT binRight(int i)
Get the value of the right edge of the i-th bin.
Definition
histogramUniform.hpp:131
mx::math::histogramUniform::normalize
void normalize(int excludeTop=0)
Normalize the current frequencies so that the integral over all bins is 1.
Definition
histogramUniform.hpp:140
mx::math::histogramUniform::accum
void accum(const realT &val)
Accumulate a value in the appropriate bin.
Definition
histogramUniform.hpp:88
mx::math::histogramUniform::freq
realT freq(int i)
Get the frequency in the i-th bin.
Definition
histogramUniform.hpp:107
mx::math::histogramUniform::setup
void setup(realT mn, realT mx, realT w)
Setup the histogram, performing allocations.
Definition
histogramUniform.hpp:69
mx::math::histogramUniform::m_width
realT m_width
The bin width.
Definition
histogramUniform.hpp:48
mx::math::histogramUniform::accum
void accum(const std::vector< realT > &vals)
Accumulate a vector of values.
Definition
histogramUniform.hpp:100
mx::math::histogramUniform::m_min
realT m_min
The mininum bin location.
Definition
histogramUniform.hpp:46
mx::math::histogramUniform::binMid
realT binMid(int i)
Get the value of the middle of the i-th bin.
Definition
histogramUniform.hpp:125
mx::math::histogramUniform::bins
int bins()
Get the number of bins.
Definition
histogramUniform.hpp:113
mx::math::histogramUniform::m_max
realT m_max
The maximum bin location.
Definition
histogramUniform.hpp:47
mx::math::histogramUniform::binLeft
realT binLeft(int i)
Get the value of the left-edge of the i-th bin.
Definition
histogramUniform.hpp:119
mx::math::histogramUniform::_freqs
std::vector< realT > _freqs
The frequencies, one for each bin.
Definition
histogramUniform.hpp:50
mx::math::histogramUniform::histogramUniform
histogramUniform()
Default c'tor, does not allocate.
Definition
histogramUniform.hpp:54
mx::math::histogramUniform::reset
void reset()
Resize and 0 the frequency vector. Assumes m_min, m_max, and m_width are set.
Definition
histogramUniform.hpp:82
mx::math::six_fifths
constexpr floatT six_fifths()
Return 6/5 in the specified precision.
Definition
constants.hpp:404
mx
The mxlib c++ namespace.
Definition
mxError.hpp:106
math
histogramUniform.hpp
Generated on Wed Mar 5 2025 10:09:16 for mxlib by
1.9.8