c_means.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*!
  2. \file cluster/c_means.c
  3. \brief Cluster library - Means value
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <math.h>
  10. #include <grass/cluster.h>
  11. /*!
  12. \brief Calculate means value
  13. \param C pointer to Cluster structure
  14. \return 0
  15. */
  16. int I_cluster_means(struct Cluster *C)
  17. {
  18. int band;
  19. int class;
  20. double m, v; /* m=mean, v=variance then std dev */
  21. double s;
  22. G_debug(3, "I_cluster_means(nbands=%d,nclasses=%d)",
  23. C->nbands, C->nclasses);
  24. for (band = 0; band < C->nbands; band++) {
  25. s = C->band_sum[band];
  26. m = s / C->npoints;
  27. v = C->band_sum2[band] - s * m;
  28. v = sqrt(v / (C->npoints - 1));
  29. for (class = 0; class < C->nclasses; class++)
  30. C->mean[band][class] = m;
  31. if (C->nclasses > 1)
  32. for (class = 0; class < C->nclasses; class++)
  33. C->mean[band][class] +=
  34. ((2.0 * class) / (C->nclasses - 1) - 1.0) * v;
  35. }
  36. return 0;
  37. }