-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistogram.h
72 lines (59 loc) · 1.31 KB
/
histogram.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#define NCAN 100
class Histogram {
public:
long nc; // Número de canales
long nd; // Número de datos introducidos
double x0, x1; // Extremos del intervalo cubierto
double dx; // Tamaño de la celda
double sx; // Suma de todos los valores tomados
double sx2; // Suma de cuadrados de todos los
// valores tomados
long H0; // Valor del intervalo (-infinito, x0)
long H1; // Valor del intervalo (x1, +infinito)
long H[NCAN]; // Valores de las nc celdas
// Métodos
//
void clean() {
nd = 0;
H0 = 0;
H1 = 0;
sx = 0.;
sx2 = 0.;
for (int i=0; i<NCAN; i++) H[i] = 0;
}
void init(double X0, double X1) {
nc = NCAN;
clean();
x0 = X0;
x1 = X1;
dx = (x1-x0)/NCAN;
}
Histogram() { init(0.,1.); }
~Histogram() { }
void count(double x) {
int i = (int) floor((x-x0)/dx);
if (i < 0) H0++;
else if (i<NCAN) H[i]++;
else H1++;
sx += x;
sx2 += x*x;
nd++;
}
void acumulate(const Histogram* h) {
nd += h->nd;
H0 += h->H0;
H1 += h->H1;
sx += h->sx;
sx2 += h->sx2;
for (int i=0; i<NCAN; i++) H[i] += h->H[i];
}
double average() {
if (nd>0) return sx/nd; else return 0.;
}
double sigma2() {
if (nd>0) return (sx2-sx*sx/nd)/nd; else return -1.;
}
};
#endif