3.5.3.1.16 Movavg

Description

This function returns the moving average of adjacent ranges [i-nb, i+nf], for a point i. The part of range beyond the input vector is dropped.

The adjacent average at point i with iback backward offset and iforw forward offset is:

 adjavg =\frac{\sum_{i-nb}^{i+nf} x_i}{nb+nf+1}

Syntax

vector movavg(vector vx, int back, int forward)

Parameters

vx

The data vector used to calculate adjacent average.

back

is the offset backward with respect to current row number.

forward

is the offset forward with respect to current row number.

Return

Return the adjacent average vector.

Example

// Col(2) will be filled with adjacent value at each point. Note that 
// col(2)[9] = (col(1)[9]+col(1)[10])/2 and col(1)[10] = col(2)[10]

for(ii=1;ii<=10;ii++) col(1)[ii] = ii;
col(2)=movavg(col(1),0, 2);
// The subrange [10:20] in col(2) will be filled with adjacent average 
// of the given subrange [10:20] in col(1). 

range aa=col(1)[10:20];
range bb=col(2)[10:20];
bb = movavg(aa, 3, 3);