local script = {} local P = {} script.input = { { "LStelem", SOURCE}, -- a switch detecting telemetry OK { "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 { "Misses%", VALUE, 1, 99, 50 }, -- max percentage of misses over Window, --below which value is kept at previous -- when #misses reached, value is set to ZERO { "Misses#", VALUE, 1, 99, 25 }, -- max number of misses, below which value is kept at previous -- when #misses reached, value is set to ZERO } script.output = { "mean","lowP","mis%","mis#","#mis","raw" } P.time = 0 P.values = {} --fifo buffer for SOURCE P.values[0] = 0 --0 element is used as index P.nMissed = 0 --initialisation P.missing = {} --fifo buffer P.missing[0] = 0 --0 element is used as index function script.run(telemSwitch,window,frequency,alpha,pMissing,nMissed) P.time = P.time or getTime() local dt = math.ceil(100/frequency) local nValues = window*frequency if getTime() >= (P.time + dt) then getValueSource = (telemSwitch==-1024) and 0 or getValue("RSSI")*1024/100 --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 --see https://en.wikipedia.org/wiki/Low-pass_filter P.lowPass = (P.lowPass or getValueSource) + alpha/100*(getValueSource - (P.lowPass or getValueSource) ) --hold on miss until treshold misses reached P.nMissed = (getValueSource==0) and (P.nMissed +1) or 0 -- (treshold NOT reached) and ((NO VALUES) then HOLD) else SOURCE (is 0 if No Values!!) P.nMisses = (P.nMissed < nMissed) and ((getValueSource==0) and P.nMisses) or getValueSource --HOLD on missing SOURCE until treshold missing% reached, nValues from average --simple running average P.missing[0] = P.missing[0] % nValues + 1 P.missing[P.missing[0] ] = (getValueSource==0) and 100 or 0 --set 100% when missing P.pMissing = 0 for n,v in ipairs(P.missing) do P.pMissing = P.pMissing + v/#P.missing end -- (treshold NOT reached ) and ((NO VALUES) then HOLD) else SOURCE (is 0 if No Values!!) P.pMisses = (P.pMissing < pMissing) and ((getValueSource==0) and P.pMisses) or getValueSource P.time = nil --reset timer end return P.mean,P.lowPass,P.pMisses,P.nMisses,P.nMissed*1024/100,getValueSource end return script