reference/ocr-simple/histogram.cc
changeset 0 6b8091ca909a
equal deleted inserted replaced
-1:000000000000 0:6b8091ca909a
       
     1 #include <math.h>
       
     2 #include "histogram.h"
       
     3 #include "tcl_interface.h"
       
     4 
       
     5 void Histogram::initialize_mean()
       
     6 {
       
     7   int total_weight = 0;
       
     8   int i;
       
     9   for(i = 0; i < num_rows; i++)
       
    10     {
       
    11       total_weight = total_weight + row_weights[i];
       
    12     }
       
    13   mean = (double) total_weight / (double) num_rows;
       
    14 }
       
    15 
       
    16 inline double sqr(double x) {return(x*x);}
       
    17 
       
    18 void Histogram::initialize_variance()
       
    19 { 
       
    20   double sum = 0;
       
    21   for(int i = 0; i < num_rows; i++)
       
    22       {
       
    23 	sum += sqr(row_weights[i] - mean);
       
    24       }
       
    25   variance = (double) sum / (double) num_rows;
       
    26 }
       
    27 
       
    28 void Histogram::initialize_standard_dev()
       
    29 {
       
    30   standard_dev = sqrt(variance);
       
    31 }
       
    32 #define SKIP 5
       
    33 /* should get this skip from project.cc (!!) */
       
    34 Histogram::display()
       
    35 {
       
    36 #if 0
       
    37   docommand(".histogram.c delete hist");
       
    38   docommand(".histogram.c create text 275 300 -font -adobe-helvetica-medium-o-normal--34-240-100-100-p-176-iso8859-1 -text \"%.3lf degrees, SD = %.3lf\" -tags hist", cut_angle, standard_dev);
       
    39   for(int i = 0; i < num_rows; i++)
       
    40       {
       
    41 	docommand(".histogram.c create line 0 %d %d %d -fill blue -tags {hist} -width %d", i*SKIP, row_weights[i], i*SKIP, SKIP);
       
    42       }
       
    43   docommand("update");
       
    44 #endif
       
    45 }
       
    46 
       
    47 Histogram::Histogram(int n_rows, int* r_weights, double c_angle)
       
    48 {
       
    49   num_rows = n_rows;
       
    50   row_weights = r_weights;
       
    51   cut_angle = c_angle;
       
    52   initialize_mean();
       
    53   initialize_variance();
       
    54   initialize_standard_dev();
       
    55 }
       
    56 
       
    57 
       
    58 
       
    59 
       
    60 
       
    61 
       
    62 
       
    63 
       
    64 
       
    65 
       
    66 double find_int_sd(int* values, int num_values)
       
    67 /* find the standard deviation of an array of ints */
       
    68 {
       
    69   int total_weight = 0;
       
    70   int i;
       
    71   for(i = 0; i < num_values; i++)
       
    72     {
       
    73       total_weight = total_weight + values[i];
       
    74     }
       
    75   double mean_weight = (double) total_weight / (double) num_values;
       
    76   double standard_sum = 0;
       
    77   for(i = 0; i < num_values; i++)
       
    78     {
       
    79       standard_sum += sqr(values[i] - mean_weight);
       
    80     }
       
    81   standard_sum = standard_sum / (double) num_values;
       
    82   return sqrt(standard_sum);
       
    83 }
       
    84