tesseract  5.0.0-alpha-619-ge9db
measure.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * File: measure.h
4  * Description: Statistics for a group of single measurements
5  * Author: Mark Seaman, SW Productivity
6  *
7  * (c) Copyright 1987, Hewlett-Packard Company.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  *****************************************************************************/
19 
20 #ifndef MEASURE_H
21 #define MEASURE_H
22 
23 /*
24 ----------------------------------------------------------------------
25  I n c l u d e s
26 ----------------------------------------------------------------------
27 */
28 
29 #include <cmath>
30 
31 /*
32 ----------------------------------------------------------------------
33  T y p e s
34 ----------------------------------------------------------------------
35 */
36 
37 typedef struct
38 {
39  long num_samples;
40  float sum_of_samples;
41  float sum_of_squares;
42 } MEASUREMENT;
43 
44 /*
45 ----------------------------------------------------------------------
46  M a c r o s
47 ----------------------------------------------------------------------
48 */
49 
50 /**********************************************************************
51  * add_sample
52  *
53  * Add one more sample to a measurement.
54  **********************************************************************/
55 
56 #define ADD_SAMPLE(m, s) \
57  (m.sum_of_samples += (float)(s), \
58  m.sum_of_squares += (float)(s) * (float)(s), ++m.num_samples)
59 
60 /**********************************************************************
61  * mean
62  *
63  * Return the mean value of the measurement.
64  **********************************************************************/
65 
66 #define MEAN(m) \
67  ((m).num_samples ? ((float)((m).sum_of_samples / (m).num_samples)) : 0)
68 
69 /**********************************************************************
70  * new_measurement
71  *
72  * Initialize a record to hold a measurement of a group of individual
73  * samples.
74  **********************************************************************/
75 
76 #define new_measurement(m) \
77  ((m).num_samples = 0, (m).sum_of_samples = 0, (m).sum_of_squares = 0)
78 
79 /**********************************************************************
80  * number_of_samples
81  *
82  * Return the number of samples in a measurement.
83  **********************************************************************/
84 
85 #define number_of_samples(m) \
86 ((m).num_samples)
87 
88 /**********************************************************************
89  * standard_deviation
90  *
91  * Return the standard deviation of the measurement.
92  **********************************************************************/
93 
94 #define standard_deviation(m) \
95 ((float) sqrt (VARIANCE (m)))
96 
97 /**********************************************************************
98  * variance
99  *
100  * Return the variance of the measurement.
101  **********************************************************************/
102 
103 #define VARIANCE(m) \
104  (((m).num_samples > 1) \
105  ? ((float)(((m).num_samples * (m).sum_of_squares - \
106  (m).sum_of_samples * (m).sum_of_samples) / \
107  (((m).num_samples - 1) * (m).num_samples))) \
108  : 0)
109 
110 /**********************************************************************
111  * print_summary
112  *
113  * Summarize a MEASUREMENT record.
114  **********************************************************************/
115 
116 #define print_summary(string, measure) \
117  cprintf("\t%-20s \tn = %d, \tm = %4.2f, \ts = %4.2f\n ", string, \
118  number_of_samples(measure), MEAN(measure), \
119  standard_deviation(measure))
120 #endif
MEASUREMENT
Definition: measure.h:36