
//==============================================================================
//  Function for calculation of mean and standard deviation:
//  input:
//  nPoints   - number of points for processing
//  pfVext    - array with data
//  output:
//  pfMean	  - pointer to mean value
//  pfStdDev  - pointer to standard deviation
//==============================================================================
#include <math.h>
int CalcMeanStdDev(int nPoints, double* pfVect, double* pfMean,double* pfStdDev)
{
  int i;
  double fSum=0.;
  double fSum2=0.;
  double fPoints = (double) nPoints;
  
  if(nPoints < 2) return 1; // not enough data !
  
  for(i=0; i<nPoints; i++)
  {
    fSum  += pfVect[i];
    fSum2 += pfVect[i] * pfVect[i];
  }
  
  *pfMean   = fSum / fPoints;
  
  *pfStdDev = ((fSum2 / fPoints) - (*pfMean)*(*pfMean)) * fPoints / (fPoints-1.);
  *pfStdDev = sqrt(*pfStdDev);
  
  return 0;
}

//==============================================================================

#include <stdio.h>

//==============================================================================
//  TEST:
//==============================================================================
int main(int nArgs, char** ppArgs)
{
  int n = 9;
  double dat[9] = {1., 2., 3., 4., 5., 6., 7., 8., 9.};
  
  double fMean, fStdDev;
  
  CalcMeanStdDev(n, dat, &fMean, &fStdDev);
  
  printf("mean=%f   stddev=%f\n", fMean, fStdDev);

  return 0;
}
