Finn's LabVIEW Goldmine:

Moving Average (MA) Filter

LabVIEW file (sub-VI):

SubVI_moving_average_filter.vi

What is it?

It is a recursive (or online, or real-time) moving average filter implemented as a sub-VI.

The filter output is calculated with

yf_k = (1 - a)*yf_km1 + a*y_k

where

a = 1/N

where

N = ts/ts + 1

Above:

  • y_k is filter input at time index k (“now”).

  • yf_k is filter output at time index k (“now”).

  • yf_km1 is filter output at time index k-1 (“km1 = k minus 1”, i.e. previous time index).

  • ts is the time step (sampling time).

  • tf [s] is the filter time constant, or filter time window (i.e. the time interval over which the filter inputs are averaged). The smallest value of tf is 0 implying the filter does not actually filter.

  • N is the filter length (number of samples averaged).

  • a is a filter parameter.

This MA filter is identical to a time constant filter if the filter time window (t_ma) is identical to the filter time constant, and the time steps of both filters are equal.

Inputs and outputs of this subVI

Inputs:

  • y_k

  • tf [s]

  • ts [s]

Outputs:

  • yf_k

  • N

  • a

How to include the filter in your block-diagram?

  1. Save the subVI file to any folder.

  2. Add the subVI to the block diagram of your VI via Functions Palette / Select a VI etc. Then connect its inputs and outputs to the proper signals (wires) in your block diagram.

Filter tuning

To increase the filtering (smoothing), increase tf.

With tf = 0 there is no filtering, i.e. the input signal just pass through the filter.


[Finn 's LabVIEW Page]


Updated 20 October 2024 by Finn Aakre Haugen