SoDaRadio-5.0.3-master:8901fb5
Histogram.hxx
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017 Matthew H. Reilly (kb1vc)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef HISTOGRAM_HDR
30 #define HISTOGRAM_HDR
31 #include <iostream>
32 #include <fstream>
33 
34 namespace SoDa {
39  class Histogram {
40  public:
51  Histogram(unsigned int _num_buckets,
52  double _min, double _max) {
53  num_buckets = _num_buckets;
54  min = _min;
55  max = _max;
56  table = new unsigned int [num_buckets];
57  recip_bucket_interval = ((double) num_buckets) / (max - min);
58  for(unsigned int i = 0; i < num_buckets; i++) table[i] = 0;
59  }
60 
61  void updateTable(double v) {
62  int idx = lround((v - min) * recip_bucket_interval);
63  if(v > max) table[num_buckets - 1]++;
64  else if(v < min) table[0]++;
65  else table[idx]++;
66  }
67 
68  void writeTable(const std::string & fname) {
69  std::ofstream of(fname.c_str());
70 
71  double bucket_interval = 1.0 / recip_bucket_interval;
72  double half_int = 0.5 * bucket_interval;
73 
74  for(unsigned int i = 0; i < num_buckets; i++) {
75  double mid = min + half_int + ((double) i) * bucket_interval;
76  if(table[i] != 0) of << boost::format("%d %g %d\n") % i % mid % table[i];
77  }
78 
79  of.close();
80  }
81 
82  protected:
83  unsigned int * table;
85  unsigned int num_buckets;
86  double min, max;
87  };
88 
89 }
90 
91 
92 #endif
void updateTable(double v)
Definition: Histogram.hxx:61
unsigned int * table
Definition: Histogram.hxx:83
double recip_bucket_interval
Definition: Histogram.hxx:84
Histogram(unsigned int _num_buckets, double _min, double _max)
Constructor for the histogram.
Definition: Histogram.hxx:51
A generic histogram widget.
Definition: Histogram.hxx:39
unsigned int num_buckets
Definition: Histogram.hxx:85
void writeTable(const std::string &fname)
Definition: Histogram.hxx:68