histogram.cpp

Go to the documentation of this file.
00001 //
00002 // C++ Interface: histogram
00003 //
00004 // Description: 
00005 //
00006 //
00007 // Author: Nicholas Phillips <Nicholas.G.Phillips@nasa.gov>, (C) 2008
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 
00013 #include <iostream>
00014 #include <iomanip>
00015 #include <math.h>
00016 #include "histogram.h"
00017 
00018 using namespace std;
00019 
00020 /* ------------------------------------------------------------------------------------
00021 'setup' 
00022         - determine the min and max values in the data
00023         - calculate some stats of the data
00024         
00025 Arguments:
00026         x: the vector of values to histogram
00027 Returned:
00028         Nothing
00029 
00030 Written by Nicholas Phillips, UMCP, 6 August 2008.
00031 ------------------------------------------------------------------------------------ */
00032 void Histogram::setup(vector<float> &x)
00033 {
00034         minv = maxv = x[0];
00035         amaxv = fabs(x[0]);
00036         double ttl = 0;
00037         double ttlsqr = 0;
00038         for(unsigned long i = 0; i < x.size(); i++) {
00039                 if( x[i] > maxv ) maxv = x[i];
00040                 if( x[i] < minv ) minv = x[i];
00041                 if( fabs(x[i]) > amaxv ) amaxv = fabs(x[i]);
00042                 ttl += x[i];
00043                 ttlsqr += x[i]*x[i];
00044         }
00045         meanv = ttl/x.size();
00046         stddevv = ttlsqr/x.size() - meanv*meanv;
00047         stddevv = stddevv > 0 ? sqrt(stddevv) : 0;
00048 
00049         return;
00050 } 
00051 /* ------------------------------------------------------------------------------------
00052 'set' 
00053         Using a fixed bin count, compute the histogram
00054         
00055 Arguments:
00056         x:      the vector of values to histogram
00057         minr:   The bottom value for  the histogram
00058         maxr:   The top value for  the histogram
00059 Returned:
00060         Nothing
00061 
00062 Written by Nicholas Phillips, UMCP, 6 August 2008.
00063 ------------------------------------------------------------------------------------ */
00064 void Histogram::build(vector<float> &x, const float minr, const float maxr)
00065 {
00066         nbin=2048;
00067         h.clear();
00068         h.resize(nbin);
00069         long n = x.size();
00070 
00071         hmax=0;
00072         for(long i = 0; i < n; i++) {
00073                 long bin = (long)(nbin*(x[i]-minr)/(maxr-minr));
00074                 if( (bin < 0) || (bin >= nbin) ) continue; // out of range
00075                 h[bin]++;
00076                 if( h[bin] > hmax ) hmax = h[bin];
00077         }
00078 
00079         return;
00080 } 
00081 
00082 /* ------------------------------------------------------------------------------------
00083 'operator()' 
00084         
00085 Arguments:
00086         x: bin value request, assumed 0 <= x < 1
00087 Returned:
00088         bin value, as a value between 0 and 1
00089 
00090 Written by Nicholas Phillips, UMCP, 6 August 2008.
00091 ------------------------------------------------------------------------------------ */
00092 float Histogram::operator()(const float x) const
00093 {
00094         long bin = (long)(nbin*x);
00095         if( (bin < 0) || (bin >= nbin) )
00096                 return 0;
00097         return ((float)h[bin])/hmax;
00098 }
00099 
00100 /* ------------------------------------------------------------------------------------
00101 'operator()' 
00102         
00103 Arguments:
00104         x0: Lower limit of bin request range
00105         x1: Upper limit of bin request range
00106                 both are assumed 0 <= x < 1
00107 Returned:
00108         bin value, as a value between 0 and 1.
00109         If more than one bin, average of bin values in the range
00110 
00111 Written by Nicholas Phillips, UMCP, 6 August 2008.
00112 ------------------------------------------------------------------------------------ */
00113 float Histogram::operator()(const float x0, const float x1) const
00114 {
00115         long bin0 = (long)(nbin*x0);
00116         long bin1 = (long)(nbin*x1);
00117 
00118         if( (bin0 < 0) || (bin1 >= nbin) )
00119                 return 0;
00120 
00121         if( bin0 == bin1 ) return ((float)h[bin0])/hmax;
00122 
00123         float y = 0;
00124         for(long i = bin0; i <= bin1; i++)
00125                 y += h[i];
00126         y /= bin1-bin0+1;
00127         return y/hmax;
00128 }

Generated on Fri Feb 6 15:32:42 2009 for Skyviewer by  doxygen 1.4.7