Example Code

Calculating Moving Average

Code and Documents

Attachment

This VI calculates and displays the moving average, using a preselected number.

First, the VI initializes two shift registers. The top shift register is initialized with one element, then continuously adds the previous value with the new value. This shift register keeps the total of the last x measurements. After dividing the results of the add function with the preselected value, the VI calculates the moving average value. The bottom shift register contains an array with the dimension "Average". This shift register keeps all values of the measurement. The replacement function replaces the new value after every loop.

This VI is very efficient and fast because it uses the replace element function inside the while loop, and it initializes the array before it enters the loop.

This VI was created in LabVIEW 6.1.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors