-- GPL 2.0 -- L Shems -- time filtering of any source local script = {} local P = {} script.input = { { "Source", SOURCE}, -- the value to be filtered { "Window", VALUE, 1, 99, 5 }, -- window in time for running average { "Freq", VALUE, 1, 5, 5 }, -- number of samples / second for running average { "Alpha%", VALUE, 1, 99, 25 }, -- attenuation factor for lowpass filter -- 100 is no attentuation, direct response --see https://en.wikipedia.org/wiki/Low-pass_filter } script.output = { "mean","lowP","raw" } P.time = 0 P.values = {} --fifo buffer for SOURCE P.values[0] = 0 --0 element is used as index function script.run(source,window,frequency,alpha) P.time = P.time or getTime() local dt = math.ceil(100/frequency) local nValues = window*frequency if getTime() >= (P.time + dt) then getValueSource = source --simple running average P.values[0] = P.values[0] % nValues + 1 P.values[P.values[0]] = getValueSource P.mean = 0 for n,v in ipairs(P.values) do P.mean = P.mean + v/#P.values end P.lowPass = (P.lowPass or getValueSource) + alpha/100*(getValueSource - (P.lowPass or getValueSource) ) P.time = nil --reset timer end return P.mean,P.lowPass,getValueSource end return script