20 #ifndef OST_STAT_ACCUMULATOR_HH
21 #define OST_STAT_ACCUMULATOR_HH
24 #include <boost/math/special_functions/binomial.hpp>
29 namespace ost {
namespace img {
namespace alg {
35 static bool less_cmp_(
const D& v1,
const D& v2){
return v1<v2;}
39 static bool less_cmp_(
const Complex& v1,
const Complex& v2){
return abs(v1)<abs(v2);}
44 template<
unsigned int MAX_MOMENT=4,
typename DATATYPE=Real>
51 max_(-std::numeric_limits<DATATYPE>::max()),
52 min_(std::numeric_limits<DATATYPE>::max()),
57 for(
unsigned int i=0;i<MAX_MOMENT;++i){
74 for(
unsigned int i=1;i<MAX_MOMENT;++i){
106 max_=std::max<DATATYPE>(max_,acc.max_,MinMax<DATATYPE>::less_cmp_);
107 min_=std::min<DATATYPE>(min_,acc.min_,MinMax<DATATYPE>::less_cmp_);
109 DATATYPE delta=acc.m_[0]-m_[0];
110 DATATYPE delta_w=delta/wn;
112 DATATYPE w1w2_delta_w=w_*acc.w_*delta_w;
113 DATATYPE w1w2_delta_wp=w1w2_delta_w*w1w2_delta_w;
118 DATATYPE mn[MAX_MOMENT];
119 for(
unsigned int p=3;p<=MAX_MOMENT;++p){
120 w1w2_delta_wp*=w1w2_delta_w;
123 DATATYPE delta_wk=1.0;
127 for(
unsigned int k=1;k<=p-2;++k){
131 s+=boost::math::binomial_coefficient<Real>(p, k)*(mw2k*m_[p-k-1]+w1k*acc.m_[p-k-1])*delta_wk;
133 mn[p-3]=acc.m_[p-1]+s+w1w2_delta_wp*(iw2pm1-miw1pm1);
135 for(
unsigned int p=3;p<=MAX_MOMENT;++p){
140 m_[1]+=acc.m_[1]+delta_w*delta*acc.w_*w_;
142 m_[0]+=delta_w*acc.w_;
171 throw Error(
"Invalid moment.");
174 throw Error(
"Moment was not calculated.");
182 throw Error(
"Mean was not calculated.");
195 throw Error(
"Variance was not calculated.");
213 return sqrt(sum2_/(w_));
219 throw Error(
"Skewness was not calculated.");
224 return m_[2]/sqrt(m_[1]*m_[1]*m_[1]);
231 throw Error(
"Kurtosis was not calculated.");
236 return w_*m_[3] / (m_[1]*m_[1]);
245 DATATYPE m_[MAX_MOMENT];
251 #endif // OST_STAT_ACCUMULATOR_HH
DATATYPE GetRootMeanSquare() const
DATATYPE GetMoment(unsigned int i) const
std::complex< Real > Complex
StatAccumulator< MAX_MOMENT, DATATYPE > operator+(const StatAccumulator< MAX_MOMENT, DATATYPE > &acc2) const
DATATYPE GetStandardDeviation() const
StatAccumulator< MAX_MOMENT, DATATYPE > & operator+=(const StatAccumulator< MAX_MOMENT, DATATYPE > &acc)
Real GetNumSamples() const
StatAccumulator(DATATYPE val, Real w=1.0)
void operator()(DATATYPE val, Real w=1.0)
DATATYPE GetKurtosis() const
DATATYPE GetVariance() const
DATATYPE GetMaximum() const
DATATYPE GetMinimum() const
DATATYPE GetSkewness() const