00001 00030 #ifndef HISTOGRAM_H 00031 #define HISTOGRAM_H 00032 00033 #include <itpp/base/mat.h> 00034 00035 00036 namespace itpp { 00037 00040 00074 template<typename Num_T> 00075 class Histogram { 00076 public: 00079 Histogram(Num_T from = Num_T(0), Num_T to = Num_T(99), int n_bins = 100); 00081 ~Histogram() {}; 00082 00084 void setup(Num_T from, Num_T to, int n_bins); 00085 00087 void update(Num_T value); 00089 void update(Vec<Num_T> values); 00091 void update(Mat<Num_T> values); 00092 00094 void reset() { trials_cnt = 0; bins.zeros(); }; 00096 int get_bin(int ix) const { return bins(ix); }; 00098 ivec get_bins() const { return bins; }; 00100 Vec<Num_T> get_bin_centers() const { return center_vals; }; 00102 Num_T get_bin_center(int ix) const { return center_vals(ix); }; 00104 Vec<Num_T> get_bin_lefts() const { return lo_vals; }; 00106 Num_T get_bin_left(int ix) const { return lo_vals(ix); }; 00108 Vec<Num_T> get_bin_rights() const { return hi_vals; }; 00110 Num_T get_bin_right(int ix) const { return hi_vals(ix); }; 00111 00113 vec get_pdf() const; 00115 vec get_cdf() const; 00116 00118 int bins_num() const { return num_bins; }; 00120 int trials_num() const {return trials_cnt;}; 00121 00122 private: 00124 int num_bins; 00126 Num_T step; 00128 Vec<Num_T> lo_vals; 00130 Vec<Num_T> hi_vals; 00132 Vec<Num_T> center_vals; 00134 ivec bins; 00136 int trials_cnt; 00137 }; 00138 00139 template<class Num_T> 00140 inline Histogram<Num_T>::Histogram(Num_T from, Num_T to, int n_bins) 00141 00142 { 00143 setup(from, to, n_bins); 00144 } 00145 00146 template<class Num_T> 00147 inline void Histogram<Num_T>::setup(Num_T from, Num_T to, int n_bins) 00148 { 00149 num_bins = n_bins; 00150 lo_vals.set_size(n_bins); 00151 hi_vals.set_size(n_bins); 00152 center_vals.set_size(n_bins); 00153 bins.set_size(n_bins); 00154 trials_cnt = 0; 00155 step = (to - from) / (num_bins - 1); 00156 center_vals = linspace(from, to, num_bins); 00157 lo_vals = center_vals - step/2; 00158 hi_vals = center_vals + step/2; 00159 reset(); 00160 } 00161 00162 template<class Num_T> 00163 inline void Histogram<Num_T>::update(Num_T value) 00164 { 00165 // search for the corresponding bin using dichotomy approach 00166 int start = 0; 00167 int end = num_bins - 1; 00168 int test = (start + end) / 2; 00169 00170 while (start < end) { 00171 if (value < lo_vals(test)) 00172 end = test - 1; 00173 else if (value >= hi_vals(test)) 00174 start = test + 1; 00175 else 00176 break; 00177 test = (start + end) / 2; 00178 }; 00179 00180 bins(test)++; 00181 trials_cnt++; 00182 } 00183 00184 template<class Num_T> 00185 inline void Histogram<Num_T>::update(Vec<Num_T> values) 00186 { 00187 for (int i = 0; i < values.length(); i++) 00188 update(values(i)); 00189 } 00190 00191 template<class Num_T> 00192 inline void Histogram<Num_T>::update(Mat<Num_T> values) 00193 { 00194 for (int i = 0; i < values.rows(); i++) 00195 for (int j = 0; j < values.cols(); j++) 00196 update(values(i,j)); 00197 } 00198 00199 template<class Num_T> 00200 inline vec Histogram<Num_T>::get_pdf() const 00201 { 00202 vec pdf(num_bins); 00203 for (int j = 0; j < num_bins; j++) 00204 pdf(j) = static_cast<double>(bins(j)) / trials_cnt; 00205 return pdf; 00206 } 00207 00208 template<class Num_T> 00209 inline vec Histogram<Num_T>::get_cdf() const 00210 { 00211 ivec tmp = cumsum(bins); 00212 vec cdf(num_bins); 00213 for (int j = 0; j < num_bins; j++) 00214 cdf(j) = static_cast<double>(tmp(j)) / trials_cnt; 00215 return cdf; 00216 } 00217 00219 00220 } // namespace itpp 00221 00222 #endif // #ifndef HISTOGRAM_H
Generated on Sun Sep 14 18:57:06 2008 for IT++ by Doxygen 1.5.6