Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

histogram.cpp

Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 
00004 #include "dbl_sort.h"
00005 #include "histogram.h"
00006 
00007 
00011 void histogram::init_bins( bin_vec &bins,
00012                            const double min,
00013                            const double max )
00014 {
00015   size_t num_bins = bins.length();
00016   double range = max - min;
00017   double bin_size = range / static_cast<double>( num_bins );
00018 
00019   double start = min;
00020   double end = min + bin_size;
00021 
00022   for (size_t i = 0; i < num_bins; i++) {
00023     bins[i] = 0;           // initialize frequency to zero
00024     bins.start(i, start ); // initialize the i'th bin start value
00025     bins.end(i, end );     // initialize the i'th bin end value
00026     start = end;
00027     end = end + bin_size;
00028   }
00029   // The frequency in a bin is incremented if a value v is
00030   // in the range start <= v < end.  This is fine until 
00031   // we reach the last bin, which should also get values
00032   // which are in the range start <= v <= end.  So add a
00033   // small amount to the end value to assure that the
00034   // end value of the last bin is beyond the value range.
00035   bins.end(num_bins-1, bins.end(num_bins-1) + (bin_size / 10.0) );
00036 } // init_bins
00037 
00038 
00039 
00052 void histogram::calculate( const double *raw_data,
00053                            const size_t N,
00054                            bin_vec &bins )
00055 {
00056   double *sort_data = new double[ N ];
00057 
00058   for (size_t i = 0; i < N; i++) {
00059     sort_data[i] = raw_data[i];
00060   }
00061     
00062   dbl_sort s;
00063 
00064   s.sort( sort_data, N );
00065   double min = sort_data[0];
00066   double max = sort_data[N-1];
00067 
00068   size_t num_bins = bins.length();
00069 
00070   init_bins( bins, min, max );
00071 
00072   value_pool pool( sort_data, N );
00073 
00074   size_t bin_ix = 0;
00075 
00076   double val;
00077   bool more_values = pool.get_val( val );
00078   double end = bins.end(bin_ix);
00079 
00080   while (bin_ix < num_bins && more_values) {
00081     if (val < end) {
00082       bins[bin_ix] = bins[bin_ix] + 1; // increment the frequency
00083       more_values = pool.get_val( val );
00084     }
00085     else {
00086       bin_ix++;
00087       end = bins.end(bin_ix);
00088     }
00089   } // while
00090 
00091   delete [] sort_data;
00092 } // calculate
00093 

Generated at Wed May 21 21:19:26 2003 for Basic Statistics Functions by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001