Menu

histogram functionality

Andy Panov
2006-03-16
2012-09-15
  • Andy Panov

    Andy Panov - 2006-03-16

    Hi ediap,

    I have noticed, it++ lacks histogram functionality in stat.h. I have got a template class implementing histogram computations. Please let me know if you are intersted in integrating it into the library.

    /*!
    \file
    \brief Class for Histogram computation
    \author Andy Panov

    $Revision: $

    $Date: $
    */

    ifndef __histogram_h

    define __histogram_h

    include "base/matfunc.h"

    include "base/vec.h"

    namespace itpp{
    //! \addtogroup statistics
    //!@{

    /*! 
    \brief A class for the histogram computation
    */
    
    template<class T> class Histogram{
    public:
        //! Constructor constructs histogram with 100 bins by default spanning values from 0 to 99
        Histogram(T from = 0, T to = 99, int n_bins = 100);
        //! Virtual destructor
        virtual ~Histogram(){};
        //! Histogram update
        void update(T value);
        //! Bins reset, so accumulation can be restarted
        void reset(){trials_cnt = 0;bins.zeros();};
        //! Access to single bin
        int get_bin(int ix) const {return bins[ix];};
        //! Access to histogram as a vector
        ivec get_bins() const {ivec ret = bins; return ret;};
        //! Access to bin centers
        Vec<T> get_bin_vals()const {Vec<T> ret = center_vals; return ret;};
        T get_bin_val(int ix)const {return center_vals[ix];};
        //! Experimental Probability Density Function (PDF) computation
        vec get_pdf() const;
        //! Experimental Cumulative Density Function (CDF) computation  
        vec get_cdf() const;
        //! Current number of bins
        int bins_num() const {return num_bins;};
    
    private:
        //! Number of processed samples
        int trials_cnt;
        //! Number of bins
        int num_bins;
        //! Step between bins
        T step;
        //! Low boundaries of histogram bins
        Vec<T> low_vals;
        //! Upper boundaries of histogram bins
        Vec<T> hi_vals;
        //! Bin centers
        Vec<T> center_vals; 
        //! Bins storage
        ivec bins;
    };
    
    template<class T>
    inline Histogram<T>::Histogram(T from, T to, int n_bins): num_bins(n_bins), low_vals(n_bins),
                                hi_vals(n_bins),center_vals(n_bins),bins(n_bins),trials_cnt(0){
                step = (to - from)/(num_bins-1);
                center_vals = linspace(from,to,num_bins);
                low_vals = center_vals - step/2; 
                hi_vals = center_vals + step/2; 
                reset();
    }
    
    //
    //Histogram methods. Inlining is used to speed things up.
    //
    template<class T>
    inline void Histogram<T>::update(T value){        
            //search for the corresponding bin
            //using dichotomy approach
            int start = 0;
            int end = num_bins - 1;
            int test = (start + end)/2;;
            while(start < end){
                if(value < low_vals[test])
                    end = test-1;
                else if(value >= hi_vals[test])
                    start = test+1;
                else
                    break;
                test = (start + end)/2;
            };
    
    
            bins[test] +=1;
            trials_cnt++;
    }
    
    template<class T>
    inline vec Histogram<T>::get_pdf() const {
            vec pdf(num_bins);
            for (int j =0; j < num_bins; j++)
                pdf[j] = static_cast<double>(bins[j])/trials_cnt;
            return pdf;
    }
    
    template<class T>
    inline vec Histogram<T>::get_cdf() const {
            ivec tmp = cumsum(bins);            
            vec cdf(num_bins);
            for (int j =0; j < num_bins; j++)
                cdf[j] = static_cast<double>(tmp[j])/trials_cnt;      
            return cdf; 
    }
    

    }

    endif //#ifndef __histogram_h

    Best regards.
    Andy.

     
    • Andy Panov

      Andy Panov - 2006-03-16

      Hi Adam

      I gonna put the source to the Feature Requests tracker tomorrow.

      Best Regards.
      Andy.

       
    • Adam Piątyszek

      Adam Piątyszek - 2006-03-16

      Hi Andy,

      Thanks for your contribution. Of couse, we are open to include new functions and classes to the IT++ library. But first we need to check out if everything new fits IT++ and works as expected.

      To help us, could you please post a patch or a new file to the Feature Requests tracker, so your contribution will not get lost in the forum threads.

      Thanks in advance!

      BR,

      /ediap

       

Log in to post a comment.