home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / lib2 / gaussian.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-21  |  3.0 KB  |  91 lines

  1. /* Gaussian . C
  2. %
  3. %    It creates an one-dimensional gaussian mask with standard deviation
  4. %    "sigma" with mask size = "window.
  5. %    The values of the mask are scaled to ensure the sum(maskarr[i]) = 1.0;
  6. %    and the scaling factor is returned by function.
  7. %    The "precision" argument (positive integer) determines
  8. %    on how many intervals each entry of the mask is computed.
  9. %    If precision=1, each mask-entry is computed by considering
  10. %    only its mid-point; else "precision" equally spaced points
  11. %    are computed for each array-entry.
  12. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  13.  
  14. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  15. Permission is granted to reproduce this software for non-commercial
  16. purposes provided that this notice is left intact.
  17.  
  18. It is acknowledged that the U.S. Government has rights to this software
  19. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  20. and the University of California.
  21.  
  22. This software is provided as a professional and academic contribution
  23. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  24. with no warranties of any kind whatsoever, no support, no promise of
  25. updates, or printed documentation. By using this software, you
  26. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  27. University of California shall have no liability with respect to the
  28. infringement of other copyrights by any part of this software.
  29.  
  30. For further information about this notice, contact William Johnston,
  31. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  32. (wejohnston@lbl.gov)
  33.  
  34. For further information about this software, contact:
  35.     Jin Guojun
  36.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  37.     g_jin@lbl.gov
  38.  
  39. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  40. %
  41. % AUTHOR:    Jin Guojun - LBL    10/1/90
  42. */
  43.  
  44. #include <math.h>
  45. #include "stdef.h"
  46. #ifndef    Float
  47. #define    Float    float
  48. #endif
  49.  
  50. double
  51. gauss_mask(sigma, window, maskarr, precision, prt_chan)
  52. double    sigma;
  53. Float    *maskarr;
  54. FILE    *prt_chan;
  55. {
  56. register int    i=1;
  57. int    j, il,
  58.     nh = il = window >> i;
  59. bool    even = !(window & 1);
  60. double    Const, divisor, sum,
  61.     estimate, deltax;
  62. register double    x=1.;
  63. #define    scale    sum
  64.  
  65. if (precision < i)
  66.     message("gauss_mask:\n\
  67.     The precision must be a positive integer, now is %d\n", precision=i);
  68. if (!sigma)    prgmerr(0, "gauss_mask: zero sigma? is %f now\n", sigma=x);
  69.  
  70. Const    = x / (sigma * sqrt(2.0 * M_PI));
  71. divisor    = -.5/ (sigma * sigma);
  72. deltax    = x / precision;
  73. x    = (deltax - x + even) / 2.;    /* aways <= 0    */
  74.  
  75. for(i=nh, il-=even; i < window; i++,il--)    {
  76.     for(estimate=0, j=precision; j--; x+=deltax)
  77.         estimate += (Const * exp(x * x * divisor));
  78.     maskarr[i] = maskarr[il] = estimate / precision;
  79. }
  80.  
  81. if (prt_chan)    {
  82.     fprintf(prt_chan, "   sigma=%lf, size=%d, precision=%d \n",
  83.         sigma, window, precision);
  84.     for(i=window; i--;)    fprintf(prt_chan, "    %f\n", maskarr[i]);
  85. }
  86. for (sum=0, i=window; i--;)    sum += maskarr[i];
  87. scale = 1. / sum;
  88. for(i=window; i--;)    maskarr[i] *= scale;
  89. return    scale;
  90. }
  91.