00001 00029 #ifndef HISTOGRAM_H 00030 #define HISTOGRAM_H 00031 00032 #include <itpp/base/mat.h> 00033 00034 00035 namespace itpp 00036 { 00037 00040 00074 template<typename Num_T> 00075 class Histogram 00076 { 00077 public: 00080 Histogram(Num_T from = Num_T(0), Num_T to = Num_T(99), int n_bins = 100); 00082 ~Histogram() {}; 00083 00085 void setup(Num_T from, Num_T to, int n_bins); 00086 00088 void update(Num_T value); 00090 void update(Vec<Num_T> values); 00092 void update(Mat<Num_T> values); 00093 00095 void reset() { trials_cnt = 0; bins.zeros(); }; 00097 int get_bin(int ix) const { return bins(ix); }; 00099 ivec get_bins() const { return bins; }; 00101 Vec<Num_T> get_bin_centers() const { return center_vals; }; 00103 Num_T get_bin_center(int ix) const { return center_vals(ix); }; 00105 Vec<Num_T> get_bin_lefts() const { return lo_vals; }; 00107 Num_T get_bin_left(int ix) const { return lo_vals(ix); }; 00109 Vec<Num_T> get_bin_rights() const { return hi_vals; }; 00111 Num_T get_bin_right(int ix) const { return hi_vals(ix); }; 00112 00114 vec get_pdf() const; 00116 vec get_cdf() const; 00117 00119 int bins_num() const { return num_bins; }; 00121 int trials_num() const {return trials_cnt;}; 00122 00123 private: 00125 int num_bins; 00127 Num_T step; 00129 Vec<Num_T> lo_vals; 00131 Vec<Num_T> hi_vals; 00133 Vec<Num_T> center_vals; 00135 ivec bins; 00137 int trials_cnt; 00138 }; 00139 00140 template<class Num_T> 00141 inline Histogram<Num_T>::Histogram(Num_T from, Num_T to, int n_bins) 00142 00143 { 00144 setup(from, to, n_bins); 00145 } 00146 00147 template<class Num_T> 00148 inline void Histogram<Num_T>::setup(Num_T from, Num_T to, int n_bins) 00149 { 00150 num_bins = n_bins; 00151 lo_vals.set_size(n_bins); 00152 hi_vals.set_size(n_bins); 00153 center_vals.set_size(n_bins); 00154 bins.set_size(n_bins); 00155 trials_cnt = 0; 00156 step = (to - from) / (num_bins - 1); 00157 center_vals = linspace(from, to, num_bins); 00158 lo_vals = center_vals - step / 2; 00159 hi_vals = center_vals + step / 2; 00160 reset(); 00161 } 00162 00163 template<class Num_T> 00164 inline void Histogram<Num_T>::update(Num_T value) 00165 { 00166 // search for the corresponding bin using dichotomy approach 00167 int start = 0; 00168 int end = num_bins - 1; 00169 int test = (start + end) / 2; 00170 00171 while (start < end) { 00172 if (value < lo_vals(test)) 00173 end = test - 1; 00174 else if (value >= hi_vals(test)) 00175 start = test + 1; 00176 else 00177 break; 00178 test = (start + end) / 2; 00179 }; 00180 00181 bins(test)++; 00182 trials_cnt++; 00183 } 00184 00185 template<class Num_T> 00186 inline void Histogram<Num_T>::update(Vec<Num_T> values) 00187 { 00188 for (int i = 0; i < values.length(); i++) 00189 update(values(i)); 00190 } 00191 00192 template<class Num_T> 00193 inline void Histogram<Num_T>::update(Mat<Num_T> values) 00194 { 00195 for (int i = 0; i < values.rows(); i++) 00196 for (int j = 0; j < values.cols(); j++) 00197 update(values(i, j)); 00198 } 00199 00200 template<class Num_T> 00201 inline vec Histogram<Num_T>::get_pdf() const 00202 { 00203 vec pdf(num_bins); 00204 for (int j = 0; j < num_bins; j++) 00205 pdf(j) = static_cast<double>(bins(j)) / trials_cnt; 00206 return pdf; 00207 } 00208 00209 template<class Num_T> 00210 inline vec Histogram<Num_T>::get_cdf() const 00211 { 00212 ivec tmp = cumsum(bins); 00213 vec cdf(num_bins); 00214 for (int j = 0; j < num_bins; j++) 00215 cdf(j) = static_cast<double>(tmp(j)) / trials_cnt; 00216 return cdf; 00217 } 00218 00220 00221 } // namespace itpp 00222 00223 #endif // #ifndef HISTOGRAM_H
Generated on Wed Jul 27 2011 16:27:05 for IT++ by Doxygen 1.7.4