ilovenetruinos / ev3g-odometry

A set of Custom EV3 Blocks for Mobile Wheeled robots designed with Lego Mindstorms EV3
MIT License
8 stars 2 forks source link

Create Block to Calculate the median of the sensor value with N measures #31

Open jabrena opened 7 years ago

jabrena commented 7 years ago

It is very common to receive values from sensors with weird values in some cases. In order to avoid this case, it is better if the developer read the sensor some times and later calculate the median of the value.

https://en.wikipedia.org/wiki/Median

function median(values) {
    values.sort( function(a,b) {return a - b;} );
    var half = Math.floor(values.length/2);
    if(values.length % 2)
        return values[half];
    else
        return (values[half-1] + values[half]) / 2.0;
}

var list1 = [3, 8, 9, 1, 5, 7, 9, 21];
median(list1);