reference/ocr-simple/histogram.cc
author viric@llimona
Thu, 18 May 2006 23:12:51 +0200
changeset 0 6b8091ca909a
permissions -rw-r--r--
Init from working directory of svn repository.

#include <math.h>
#include "histogram.h"
#include "tcl_interface.h"

void Histogram::initialize_mean()
{
  int total_weight = 0;
  int i;
  for(i = 0; i < num_rows; i++)
    {
      total_weight = total_weight + row_weights[i];
    }
  mean = (double) total_weight / (double) num_rows;
}

inline double sqr(double x) {return(x*x);}

void Histogram::initialize_variance()
{ 
  double sum = 0;
  for(int i = 0; i < num_rows; i++)
      {
	sum += sqr(row_weights[i] - mean);
      }
  variance = (double) sum / (double) num_rows;
}

void Histogram::initialize_standard_dev()
{
  standard_dev = sqrt(variance);
}
#define SKIP 5
/* should get this skip from project.cc (!!) */
Histogram::display()
{
#if 0
  docommand(".histogram.c delete hist");
  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);
  for(int i = 0; i < num_rows; i++)
      {
	docommand(".histogram.c create line 0 %d %d %d -fill blue -tags {hist} -width %d", i*SKIP, row_weights[i], i*SKIP, SKIP);
      }
  docommand("update");
#endif
}

Histogram::Histogram(int n_rows, int* r_weights, double c_angle)
{
  num_rows = n_rows;
  row_weights = r_weights;
  cut_angle = c_angle;
  initialize_mean();
  initialize_variance();
  initialize_standard_dev();
}










double find_int_sd(int* values, int num_values)
/* find the standard deviation of an array of ints */
{
  int total_weight = 0;
  int i;
  for(i = 0; i < num_values; i++)
    {
      total_weight = total_weight + values[i];
    }
  double mean_weight = (double) total_weight / (double) num_values;
  double standard_sum = 0;
  for(i = 0; i < num_values; i++)
    {
      standard_sum += sqr(values[i] - mean_weight);
    }
  standard_sum = standard_sum / (double) num_values;
  return sqrt(standard_sum);
}