Skip to main content
Skip table of contents

Moving Average

Lua Scripting can be used to implement moving averaging algorithms which allow Modbus polling applications to easily oversample analog inputs and achieve the benefits of higher accuracy analog inputs on low cost devices.  This is especially true when the Modbus polling applications are only requesting data once per second.  For customer convenience, we have three examples that perform these calculations. See the descriptions below. The examples can be found in our Kipling repository under the advanced scripts section.

Moving Average: Compute After Sampling

This script reads analog inputs and computes the moving average after each sampling interval.  This script is particularly useful for Modbus polling applications that need to be sure that each "moving average" value that gets read is the most up to date value. The script can be customized by editing the constants:

CODE
local sampleIntervalHZ = 10 -- Sampling interval in HZ
local numSamplesToAverage = 100 -- Number of samples to cached & average
local channels = {0, 2} -- Which analog inputs/registers to read & average

To assist with determining maximum sampling rates and buffer sizes, digital I/O lines FIO4-5 on T4s and FIO0-1 on the T7 are configured to output waveforms that can be read with an oscilloscope or logic analyzer. As a really low-cost solution, LEDs can be installed and looked at to see how bright they are and used to determine how much idle time the script has.

https://github.com/labjack/labjack_kipling/blob/master/ljswitchboard-module_manager/lib/switchboard_modules/lua_script_debugger/premade_scripts/advanced_scripts/moving_average_compute.lua

Moving Average: Interval Compute (Low Speed)

This script reads analog inputs and computes the average value of each read channel on a given interval. This is useful for situations where analog inputs want to be read at high data rates. Keep in mind that while a script is computing the average value analog input values aren't being read. In time-critical applications where this is important to characterize and understand, I/O lines can be toggled and monitored with an oscilloscope to determine if the script is reading data fast enough and with low enough jitter.

This script can be customized by editing the sampling interval, number of samples to average, user-ram update rate, and channel register constants.

https://github.com/labjack/labjack_kipling/blob/master/ljswitchboard-module_manager/lib/switchboard_modules/lua_script_debugger/premade_scripts/advanced_scripts/moving_average_interval_compute_LS.lua

Moving Average: Interval Compute (High Speed)

Warning: This script calculates the moving average by maintaining a "sum" which is (in memory) a float32 data type. While the value will remain close to the true value, the precision may slowly drift.

This script reads analog inputs and computes the average value of each read channel on a given interval. This is useful for situations where analog inputs want to be read at high data rates. Keep in mind that while a script is computing the average value analog input values aren't being read. In time-critical applications where this is important to characterize and understand, I/O lines can be toggled and monitored with an oscilloscope to determine if the script is reading data fast enough and with low enough jitter.

This script can be customized by editing the sampling interval, number of samples to average, user-ram update rate, and channel register constants.

https://github.com/labjack/labjack_kipling/blob/master/ljswitchboard-module_manager/lib/switchboard_modules/lua_script_debugger/premade_scripts/advanced_scripts/moving_average_interval_compute_HS.lua

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.