GRASS Programmer's Manual  6.4.2(2012)
c_sig.c
Go to the documentation of this file.
00001 #include <grass/cluster.h>
00002 int I_cluster_signatures(struct Cluster *C)
00003 {
00004     int c, p, band1, band2;
00005     int n;
00006     double m1, m2;
00007     double p1, p2;
00008     double dn;
00009 
00010     /*
00011        fprintf (stderr, "c_sig: 1\n");
00012        fprintf (stderr, "  nclasses %d\n", C->nclasses);
00013        fprintf (stderr, "  npoints  %d\n", C->npoints );
00014        fprintf (stderr, "  nbands   %d\n", C->nbands  );
00015      */
00016     for (n = 0; n < C->nclasses; n++) {
00017         I_new_signature(&C->S);
00018     }
00019 
00020     for (p = 0; p < C->npoints; p++) {
00021         c = C->class[p];
00022         if (c < 0)
00023             continue;
00024         /*
00025            if (c >= C->nclasses)
00026            fprintf (stderr, " class[%d]=%d ** illegal **\n", p, c);
00027          */
00028         dn = n = C->count[c];
00029         if (n < 2)
00030             continue;
00031         for (band1 = 0; band1 < C->nbands; band1++) {
00032             m1 = C->sum[band1][c] / dn;
00033             p1 = C->points[band1][p];
00034             for (band2 = 0; band2 <= band1; band2++) {
00035                 m2 = C->sum[band2][c] / dn;
00036                 p2 = C->points[band2][p];
00037                 C->S.sig[c].var[band1][band2] += (p1 - m1) * (p2 - m2);
00038             }
00039         }
00040     }
00041 
00042     for (c = 0; c < C->nclasses; c++) {
00043         dn = n = C->S.sig[c].npoints = C->count[c];
00044         if (n == 0)
00045             dn = 1.0;
00046         for (band1 = 0; band1 < C->nbands; band1++)
00047             C->S.sig[c].mean[band1] = C->sum[band1][c] / dn;
00048         dn = n = C->count[c] - 1;
00049         if (n < 1)
00050             continue;
00051         for (band1 = 0; band1 < C->nbands; band1++)
00052             for (band2 = 0; band2 <= band1; band2++)
00053                 C->S.sig[c].var[band1][band2] /= dn;
00054         C->S.sig[c].status = 1;
00055     }
00056 
00057     return 0;
00058 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines