• # How to implement a filter algorithm

how can i implement a filtering algorithm to give me the median value from a set of sensor readings. i need this for some digital sensors which sometime give false readings - basically want to put all readings of say the last one minute in an array, sort it and then pick the median (middle value).

is there an easy way to do this, any pointers will be appreciated...

• The best way to do this is with a meta point. Write the code necessary to get all your points into an array, here's some code I stole:

``````var myArr = new Array();
values.push(sens1.value)
...
values.push(sensN.value)

values.sort( function(a,b) {return a - b;} ); //works for our numeric values

var half = Math.floor(values.length/2);

if(values.length % 2)
return values[half];
else
return (values[half-1] + values[half]) / 2.0;
``````

Hope that helps.

• @phildunlap said:

The best way to do this is with a meta point. Write the code necessary to get all your points into an array, here's some code I stole:

``````
values.push(sens1.value)
...
values.push(sensN.value)

``````

Hope that helps.

Thanks for the quick response, a clarification please :-

``````
sens1.value
....
sensN.value

``````

wont this be value of n different points, i need to get the last n values of the same point into the array, how can i do that ?

• Oh, I apologize, I misunderstood your question.

So 'sens' will be our point that we want the history of.

values.push(sens.prev(MINUTE, 1).firstValue)
values.push(sens.prev(MINUTE, 2).firstValue)
values.push(sens.prev(MINUTE, 3).firstValue)
...
values.push(sens.prev(MINUTE, n).firstValue)

• @phildunlap said:

values.push(sens.prev(MINUTE, 1).firstValue)

...
values.push(sens.prev(MINUTE, n).firstValue)

thanks, this seems exactly what i need.

one other request, where do i find documentation on what you have described above e.g. prev(MINUTE,1).firstvalue - what other functions etc are there which could be used ?

• Most of the available data is documented in the help question mark on the page. I went to the source code for the 'firstValue' attribute, which is not listed in the help document. The public repo can be seen at www.github.com/infiniteautomation

Posts 7Views 2038