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