Index: trunk/psLib/src/math/psVectorSmooth.c
===================================================================
--- trunk/psLib/src/math/psVectorSmooth.c	(revision 31660)
+++ trunk/psLib/src/math/psVectorSmooth.c	(revision 42888)
@@ -1,4 +1,4 @@
 #ifdef HAVE_CONFIG_H
-# include "config.h"
+#include "config.h"
 #endif
 
@@ -15,4 +15,5 @@
 #include "psAssert.h"
 #include "psConstants.h"
+#include "psStats.h"
 
 #include "psVectorSmooth.h"
@@ -122,6 +123,6 @@
 psVector *psVectorBoxcar(psVector *output,
                          const psVector *input,
-                         int size
-                        )
+                         int size,
+                         bool robust)
 {
     PS_ASSERT_VECTOR_NON_NULL(input, NULL);
@@ -136,5 +137,4 @@
     output = psVectorRecycle(output, num, input->type.type);
     psVector *nums = psVectorAlloc(num, PS_TYPE_U32); // Number of elements in each bin
-    psU32 *numsData = nums->data.U32;   // Dereferenced version
 
     psVectorInit(output, 0.0);
@@ -147,16 +147,42 @@
       ps##TYPE *outputData = output->data.TYPE; \
       ps##TYPE *inputData = input->data.TYPE; \
-      /* Smooth the vector */ \
-      for (long i = 0; i < num; i++) { \
-          for (long j = PS_MAX(0, i - size); j < PS_MIN(num, i + size + 1); j++) { \
-              outputData[j] += inputData[i]; \
-              numsData[j]++; \
-          } \
-      } \
-      /* Normalisation */ \
-      for (long i = 0; i < num; i++) { \
-          outputData[i] /= numsData[i]; \
-      } \
-      break; \
+      /* Smooth the vector */							 \
+      for (long i = 0; i < num; i++)						 \
+      { 									 \
+          /* sample within the window size */					 \
+          int windowSize = PS_MIN(num, i + size + 1) - PS_MAX(0, i - size);	 \
+          psVector *sample = psVectorAlloc(windowSize, PS_TYPE_##TYPE); 	 \
+          ps##TYPE *sampleData = sample->data.TYPE;				 \
+          /* extract sample from inputData */					 \
+          for (long j = PS_MAX(0, i - size); j < PS_MIN(num, i + size + 1); j++) \
+          {									 \
+              sampleData[j - PS_MAX(0, i - size)] = inputData[j];		 \
+          }									 \
+          /* find the desired statistic from the sample */			 \
+          psStatsOptions statistic;						 \
+          psStats *stats;							 \
+          if (robust)								 \
+          {									 \
+              statistic = PS_STAT_CLIPPED_MEDIAN;				 \
+              stats = psStatsAlloc(statistic);  				 \
+              stats->clipSigma = 2.5;						 \
+          }									 \
+          else  								 \
+          {									 \
+              statistic = PS_STAT_SAMPLE_MEDIAN;				 \
+              stats = psStatsAlloc(statistic);  				 \
+          }									 \
+          if (!psVectorStats(stats, sample, NULL, NULL, 0))			 \
+          {									 \
+              psError(PS_ERR_UNKNOWN, false, "failure to measure stats");	 \
+              psFree(sample);							 \
+              psFree(stats);							 \
+              return NULL;							 \
+          }									 \
+          outputData[i] = psStatsGetValue(stats, statistic);			 \
+          psFree(sample);							 \
+          psFree(stats);							 \
+      } 									 \
+      break;									 \
   }
 
