home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_6.3 / XSGT / HIST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  1.6 KB  |  104 lines

  1. /* 
  2.  * hist.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * HIST(ogram evaluation)
  11.  *
  12.  * routines to evaluate statistics and histogram of input data set
  13.  *
  14.  */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include "vora.h"
  20.  
  21.  
  22. /*
  23.  * determine mean of input data set
  24.  */
  25. double
  26. find_mean (data, n)
  27.      float *data;
  28.      int n;
  29. {
  30.   int i;
  31.   double mean = 0.0;
  32.  
  33.   for (i = 0; i < n; i++)
  34.     mean += *(data + i);
  35.  
  36.   return (mean / (double) n);
  37. }
  38.  
  39. /*
  40.  * determine standard deviation of input data set
  41.  */
  42. double
  43. find_sigma (data, n, mean)
  44.      float *data;
  45.      int n;
  46.      double mean;
  47. {
  48.   int i;
  49.   double xi, sigma = 0.0;
  50.  
  51.   for (i = 0; i < n; i++) {
  52.     xi = (double) (*(data + i));
  53.     sigma += (xi - mean) * (xi - mean);
  54.   }
  55.   sigma /= (double) (n - 1);
  56.  
  57.   return (sqrt (sigma));
  58. }
  59.  
  60.  
  61.  
  62. /*
  63.  * construct histogram of input data
  64.  */
  65. void
  66. construct_hist (n, data, hist, bw, data_base)
  67.      int n;
  68.      float *data, *hist;
  69.      double bw, data_base;
  70. {
  71.   int i, ibin;
  72.  
  73.   for (i = 0; i < n; i++) {
  74.     if (*(data + i) != 0) {
  75.       ibin = 0;
  76.       while (*(data + i) >= data_base + ibin * bw)
  77.         ibin++;
  78.       *(hist + ibin - 1) += 1;
  79.     }
  80.   }
  81. }
  82.  
  83.  
  84. /*
  85.  * construct histogram of sorted(!) input data
  86.  */
  87. void
  88. construct_shist (n, data, hist, bw, data_base)
  89.      int n;
  90.      float *data, *hist;
  91.      double bw, data_base;
  92. {
  93.   int i, ibin;
  94.  
  95.   ibin = 0;
  96.   for (i = 0; i < n; i++) {
  97.     if (*(data + i) != 0) {
  98.       while (*(data + i) >= data_base + ibin * bw)
  99.         ibin++;
  100.       *(hist + ibin - 1) += 1;
  101.     }
  102.   }
  103. }
  104.