00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CHISTOGRAM_H 00029 #define CHISTOGRAM_H 00030 00031 #include <mrpt/utils/utils_defs.h> 00032 00033 /*--------------------------------------------------------------- 00034 Class 00035 ---------------------------------------------------------------*/ 00036 namespace mrpt 00037 { 00038 namespace math 00039 { 00040 /** This class provides an easy way of computing histograms for unidimensional real valued variables. 00041 * Call "getHistogram" or "getHistogramNormalized" to retrieve the full list of bin positions & hit counts. 00042 * 00043 * Example: 00044 \code 00045 CHistogram hist(0,100,10); 00046 hist.add(86); 00047 hist.add(7); 00048 hist.add(45); 00049 00050 std::cout << hist.getBinCount(0) << std::endl; // Result: "1" 00051 std::cout << hist.getBinRatio(0) << std::endl; // Result: "0.33" 00052 \endcode 00053 */ 00054 class MRPTDLLIMPEXP CHistogram 00055 { 00056 private: 00057 double m_min,m_max; //!< The histogram limits 00058 double m_binSizeInv; //!< ((max-min)/nBins)^-1 00059 std::vector<size_t> m_bins; //!< The bins counter 00060 size_t m_count; //!< The total elements count 00061 00062 public: 00063 /** Constructor 00064 * \exception std::exception On nBins<=0 or max<=min 00065 */ 00066 CHistogram(const double min, const double max, const size_t nBins); 00067 00068 /** Clear the histogram: 00069 */ 00070 void clear(); 00071 00072 /** Add an element to the histogram. If element is out of [min,max] it is ignored. */ 00073 void add(const double x); 00074 00075 /** Add a vector of elements to the histogram. If an element is out of [min,max] it is ignored. */ 00076 template <typename T> 00077 void add(const std::vector<T> &x) { 00078 for (size_t i=0;i<x.size();i++) 00079 add(x[i]); 00080 } 00081 00082 /** Retuns the elements count into the selected bin index, where first one is 0. 00083 * \exception std::exception On invalid index 00084 */ 00085 int getBinCount(const size_t index) const; 00086 00087 /** Retuns the ratio in [0,1] range for the selected bin index, where first one is 0. 00088 * It returns 0 if no elements have been added. 00089 * \exception std::exception On invalid index. 00090 */ 00091 double getBinRatio(const size_t index) const; 00092 00093 /** Returns the list of bin centers & hit counts 00094 * \sa getHistogramNormalized 00095 */ 00096 void getHistogram( vector_double &x, vector_double &hits ) const; 00097 00098 /** Returns the list of bin centers & hit counts, normalized such as the integral of the histogram, interpreted as a density PDF, amounts to 1. 00099 * \sa getHistogram 00100 */ 00101 void getHistogramNormalized( vector_double &x, vector_double &hits ) const; 00102 00103 00104 }; // End of class def. 00105 00106 } // End of namespace 00107 } // End of namespace 00108 #endif
Page generated by Doxygen 1.6.1 for MRPT 0.7.1 SVN: at Tue Dec 22 08:29:35 CET 2009 |