SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
Histogram.hxx
Go to the documentation of this file.
1
/*
2
Copyright (c) 2017, 2025 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
#pragma once
29
30
#include <iostream>
31
#include <fstream>
32
33
namespace
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
;
86
double
recip_bucket_interval
;
87
unsigned
int
num_buckets
;
88
double
min
,
max
;
89
};
90
91
}
92
93
SoDa::Histogram::recip_bucket_interval
double recip_bucket_interval
Definition
Histogram.hxx:86
SoDa::Histogram::table
unsigned int * table
Definition
Histogram.hxx:85
SoDa::Histogram::updateTable
void updateTable(double v)
Definition
Histogram.hxx:60
SoDa::Histogram::Histogram
Histogram(unsigned int _num_buckets, double _min, double _max)
Constructor for the histogram.
Definition
Histogram.hxx:50
SoDa::Histogram::num_buckets
unsigned int num_buckets
Definition
Histogram.hxx:87
SoDa::Histogram::max
double max
Definition
Histogram.hxx:88
SoDa::Histogram::min
double min
Definition
Histogram.hxx:88
SoDa::Histogram::writeTable
void writeTable(const std::string &fname)
Definition
Histogram.hxx:67
SoDa
Definition
AudioIfc.hxx:34
src
Histogram.hxx
Generated on
for SoDaRadio-12.2.0-cut_dependencies:6c82803 by
1.14.0