Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

extract/lib/filter.c

Go to the documentation of this file.
00001 /* === Doxygen Comment ======================================= */
00013 /* =========================================================== */
00014 
00015 #include <IFU_io.h>
00016 #include <IFU_math.h>
00017 
00018 /* === Doxygen Comment ======================================= */
00033 /* =========================================================== */
00034 
00035 void spatial_filter(int nrow, float X[], float Y[], float Z[], float rad,
00036                     char method, int minNnb, int Nnb[])
00037 {
00038   int *work;
00039   int l,n,i, nneighbors;
00040   
00041   float *Zout;
00042   float dx, dy, r2, rad2;
00043   double *Znb;
00044   
00045   rad2 = SQ(rad);
00046 
00047   Zout =  (float *)malloc(nrow*sizeof(float));
00048   Znb  = (double *)malloc(nrow*sizeof(double));
00049   work =    (int *)malloc(nrow*sizeof(int));    /* work array */
00050 
00051   if (DEBUG) 
00052     print_msg("Spatial filtering: Min. neighbors: %d, Exclude center: %s",
00053               ABS(minNnb),(minNnb>0)? "No":"Yes");
00054 
00055   for (l=0; l<nrow; l++) { 
00056     /* looking for neighbors */
00057     for (nneighbors=n=0; n<nrow; n++) {
00058       if (minNnb < 0 && n == l) continue;
00059       dx = X[n] - X[l];
00060       dy = Y[n] - Y[l];
00061       r2 = dx*dx + dy*dy;
00062       if (r2 > rad2) continue;
00063       Znb[nneighbors++] = Z[n];
00064     }
00065 
00066     /* filtering */
00067     if (nneighbors < ABS(minNnb)) {
00068       Zout[l] = Z[l];
00069       if (Nnb != NULL) Nnb[l] = 0;
00070     } 
00071     else {
00072       switch(method) {
00073       case 'M':                              /* Median filter */
00074         Zout[l] = median(Znb,nneighbors,work);
00075         break;
00076       case 'A':                              /* Average filter */
00077         for (i=1; i<nneighbors; i++) Znb[0] += Znb[i]; /* build-up sum */
00078         Zout[l] = Znb[0]/nneighbors;
00079         break;
00080       }
00081       if (Nnb != NULL) Nnb[l] = nneighbors;
00082     }
00083   }
00084   /* storage */
00085   memcpy(Z, Zout, nrow*sizeof(float));      /* Copy in input array */
00086 
00087   free(Znb); free(Zout); free(work);
00088 }
00089 

Generated on Wed Oct 26 23:59:39 2005 for Snifs by doxygen 1.3.5