FIR
- Finite Impulse Response filter

Moving averages, summation
(AmiBroker 5.40)


SYNTAX FIR( array, coefficients, size )
RETURNS ARRAY
FUNCTION This function implements general-purpose Finite Impulse Response filter (FIR) http://en.wikipedia.org/wiki/Finite_impulse_response

The output is convolution of input aray with coefficents table (impulse response table).

The function performs automatic normalization of coefficient table if necessary (if its sum is not 1)

INPUTS:

  • array - input array to be smoothed
  • coefficients - array of FIR coefficients
  • size - number of coefficients ( filter_order + 1 )

It is functional (but 2+ times faster) equivalent of following AFL:

function FIR_AFL( input, coeff, size )
{
 result =
0;
 sumcoeff =
0;
 for( i = 0; i < Min( size, BarCount ); i++ )
 {
   sumcoeff += coeff[ i ];
   result += coeff[ i ] *
Ref( input, - size + i + 1 );
 }

 return result/sumcoeff;
}

EXAMPLE // for example 5 bar weighted moving average can be written:
coeff[
0 ] = 1;
coeff[
1 ] = 2;
coeff[
2 ] = 3;
coeff[
3 ] = 4;
coeff[
4 ] = 5;

FIR( Close, coeff, 5 );

// Another example: ALMA which is FIR Filter with
// shifted Gaussian coefficents can be implemented as follows:

function ALMA_AFL( input, range, Offset, sigma )
{
 local m, im, s, Coeff;

 m =
floor( Offset * (range - 1) );
 s = range / sigma;

 for( i = 0; i < Min( range, BarCount ); i++ )
 {
   im = i - m;
   Coeff[ i ] =
exp( - ( im * im )/ ( 2 * s * s ) );
 }

 return FIR( input, Coeff, range );
}

SEE ALSO

References:

The FIR function is used in the following formulas in AFL on-line library:

More information:

See updated/extended version on-line.