Smoothness Example

This Script will generate the the same smoothness value that the smoothness event detector. You can use the to calculate what your low limit should be on the smoothness detector. Note that generate history my not give the same results with this as it uses the real time updates for the boxcar. Give your point variable the name “signal” and set to context update.

var boxcarLength = 60;
var boxcar = signal.last(boxcarLength);
//Note that as in the detector if there isn't at least 3 values it is considered smooth
if(boxcar.size() <3)
return 1;
var prev = Number.NaN;
var lastAngle = Number.NaN;
var sumErr = 0;
var count = 0;
//Walk backwards across the list to perform calc in time order
for(var i=boxcar.size()-1; i>=0; i--){
var value = boxcar.get(i).value;
var opp = value - prev;
var hyp = Math.sqrt(0.1 + opp * opp);
var angle = Math.asin(opp / hyp);
var diff = angle - lastAngle;
var norm = diff / Math.PI;
sumErr += norm <0 ? -norm : norm;
lastAngle = angle;
prev = value;
var err = sumErr / count;
return (1.0 - err);