03-03-2008 09:42 AM
03-03-2008 10:23 AM
03-03-2008 10:29 AM
03-03-2008 10:55 AM
03-03-2008 11:12 AM
Here's a running average of doubles, with discard of outliers. It is not efficient.
/*==========================================================================*/
/* Function: RunningAverage */
/* Purpose : Computes 10-point running average of input data array */
/* : stores average into old array locations, overwriting raw data */
/* Inputs : int nMaxIndex max number of data values in the input array */
/* : double rawData pointer to the raw data array to be smoothed */
/* : double MIN_DATA_VALUE valid data value low limit */
/* : double MAX_DATA_VALUE valid data value high limit */
/* Outputs : none */
/* Return : none */
/*==========================================================================*/
void RunningAverage (int nMaxIndex, double rawData[], double MIN_DATA_VALUE, double MAX_DATA_VALUE) {
int i;
int j;
int nValidReadings;
double fSmoothArray[4200];
// -------------------------------------------------------------------
// smooth raw data with a 5-second running mean
// This is numerically inefficient code, because it sums the same values
// many times. No need for efficiency here, we're in-between segments...
// -------------------------------------------------------------------
// compute a 10-point running average (there are two data points per second)
for (i = 10; i < nMaxIndex; i++) {
fSmoothArray[i] = 0.0;
nValidReadings = 0;
for (j = i - 10; j < i; j++) {
// Disregard 'spurious' data points
if ((rawData[j] > MIN_DATA_VALUE) && (rawData[j] < MAX_DATA_VALUE)) {
fSmoothArray[i] += rawData[j];
nValidReadings++;
}
}
if (nValidReadings > 0) {
fSmoothArray[i] = fSmoothArray[i] / nValidReadings;
}
else {
fSmoothArray[i] = INVALID_DATA_VALUE;
}
}
for (i = 10; i < nMaxIndex; i++) {
rawData[i] = fSmoothArray[i];
}
return;
} // end, RunningAverage
03-03-2008 11:21 AM - edited 03-03-2008 11:22 AM
03-03-2008 01:23 PM
Colin.
AnalysisLibErrType Mean (const double Input_Array[] ,int Number_of_Elements ,double *Mean );
03-03-2008 05:24 PM - edited 03-03-2008 05:30 PM
03-04-2008 06:16 AM
Ok, thanks to all for your opinions.
I've made a function to do the moving (running) average, each point of the array is averaged with itself and the values close to it, exept the first and the last points, these point are ony averaged upwards (first) or backwards (last)
The function can be optimized but it works fine.
definition: int MovingAverage( int averaging, double data[] , int size, double retval[] );
averaging= the number of times a value is averaged with his "neighbours" upward and backwards
data= array of raw data
size= the size of the data array
retval = the output array
int MovingAverage( int averaging, double data[] , int size, double retval[] )
{
int i,ii;
size= size-1;
Set1D (retval, size, 0.0); // Set the output array to 0
for( i=0; i<= averaging ; i++){ //loop to average the first points of the array
for ( ii=0; ii<=averaging; ii++ ){
retval[i]+= data[i+ii];
}
retval[i] = retval[i] / (averaging+1);
}
for( i=0; i<= averaging ; i++){ //loop to average the last points of the array
for ( ii=0; ii<=averaging-1; ii++ ){
retval[i+size-averaging] += data[i-ii+size-averaging];
}
retval[i+size-averaging] = retval[i+size-averaging] / (averaging+1);
}
for (i=averaging+1; i<= (size-averaging-1); i++) //loop to average the middle points of the array
{
for ( ii=0; ii<=averaging; ii++ ){
retval[i]+= data[i-ii];
}
for ( ii=1; ii<=averaging; ii++ ){
retval[i]+= data[i+ii];
}
retval[i] = retval[i] / ((2*averaging)+1);
}
return 0;
}
09-28-2009 04:03 PM