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;
00024 bins.start(i, start );
00025 bins.end(i, end );
00026 start = end;
00027 end = end + bin_size;
00028 }
00029
00030
00031
00032
00033
00034
00035 bins.end(num_bins-1, bins.end(num_bins-1) + (bin_size / 10.0) );
00036 }
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;
00083 more_values = pool.get_val( val );
00084 }
00085 else {
00086 bin_ix++;
00087 end = bins.end(bin_ix);
00088 }
00089 }
00090
00091 delete [] sort_data;
00092 }
00093