Tesseract  3.02
tesseract-ocr/wordrec/measure.h
Go to the documentation of this file.
00001 /* -*-C-*-
00002  ********************************************************************************
00003  *
00004  * File:        measure.h  (Formerly measure.h)
00005  * Description:  Statistics for a group of single measurements
00006  * Author:       Mark Seaman, SW Productivity
00007  * Created:      Fri Oct 16 14:37:00 1987
00008  * Modified:     Mon Apr  8 09:42:28 1991 (Mark Seaman) marks@hpgrlt
00009  * Language:     C
00010  * Package:      N/A
00011  * Status:       Reusable Software Component
00012  *
00013  * (c) Copyright 1987, Hewlett-Packard Company.
00014  ** Licensed under the Apache License, Version 2.0 (the "License");
00015  ** you may not use this file except in compliance with the License.
00016  ** You may obtain a copy of the License at
00017  ** http://www.apache.org/licenses/LICENSE-2.0
00018  ** Unless required by applicable law or agreed to in writing, software
00019  ** distributed under the License is distributed on an "AS IS" BASIS,
00020  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  ** See the License for the specific language governing permissions and
00022  ** limitations under the License.
00023  *
00024  ********************************************************************************
00025  */
00026 
00027 #ifndef MEASURE_H
00028 #define MEASURE_H
00029 
00030 /*
00031 ----------------------------------------------------------------------
00032               I n c l u d e s
00033 ----------------------------------------------------------------------
00034 */
00035 
00036 #include <math.h>
00037 
00038 /*
00039 ----------------------------------------------------------------------
00040               T y p e s
00041 ----------------------------------------------------------------------
00042 */
00043 
00044 typedef struct
00045 {
00046   long num_samples;
00047   float sum_of_samples;
00048   float sum_of_squares;
00049 } MEASUREMENT;
00050 
00051 /*
00052 ----------------------------------------------------------------------
00053               M a c r o s
00054 ----------------------------------------------------------------------
00055 */
00056 
00057 /**********************************************************************
00058  * add_sample
00059  *
00060  * Add one more sample to a measurement.
00061  **********************************************************************/
00062 
00063 #define ADD_SAMPLE(m,s)                           \
00064 (m.sum_of_samples += (float) (s),               \
00065         m.sum_of_squares += (float) (s) * (float) (s), \
00066         ++m.num_samples)
00067 
00068 /**********************************************************************
00069  * mean
00070  *
00071  * Return the mean value of the measurement.
00072  **********************************************************************/
00073 
00074 #define MEAN(m)                                       \
00075 ((m).num_samples ?                                  \
00076         ((float) ((m).sum_of_samples / (m).num_samples)) : \
00077         0)
00078 
00079 /**********************************************************************
00080  * new_measurement
00081  *
00082  * Initalize a record to hold a measurement of a group of individual
00083  * samples.
00084  **********************************************************************/
00085 
00086 #define new_measurement(m)   \
00087 ((m).num_samples    = 0, \
00088         (m).sum_of_samples = 0, \
00089         (m).sum_of_squares = 0)
00090 
00091 /**********************************************************************
00092  * number_of_samples
00093  *
00094  * Return the number of samples in a measurement.
00095  **********************************************************************/
00096 
00097 #define number_of_samples(m)  \
00098 ((m).num_samples)
00099 
00100 /**********************************************************************
00101  * standard_deviation
00102  *
00103  * Return the standard deviation of the measurement.
00104  **********************************************************************/
00105 
00106 #define standard_deviation(m)                                \
00107 ((float) sqrt (VARIANCE (m)))
00108 
00109 /**********************************************************************
00110  * variance
00111  *
00112  * Return the variance of the measurement.
00113  **********************************************************************/
00114 
00115 #define VARIANCE(m)                                   \
00116 (((m).num_samples > 1) ?                            \
00117         ((float)                                           \
00118         (((m).num_samples * (m).sum_of_squares -          \
00119                 (m).sum_of_samples * (m).sum_of_samples) /      \
00120         (((m).num_samples - 1) * (m).num_samples)))   :  \
00121         0)
00122 
00123 /**********************************************************************
00124  * print_summary
00125  *
00126  * Summarize a MEASUREMENT record.
00127  **********************************************************************/
00128 
00129 #define print_summary(string,measure)                       \
00130 cprintf ("\t%-20s \tn = %d, \tm = %4.2f, \ts = %4.2f\n ",  \
00131                         string,                                          \
00132                         number_of_samples  (measure),                    \
00133                         MEAN           (measure),                    \
00134                         standard_deviation (measure))
00135 #endif