Search code examples
javascriptcalculus

Writing a function for smoothing center values in specified range


I'm not sure this is the right place to ask this kind of question (maybe mathematica would be better ?).

I'm writing a program that reads input values from an analog joystick.

The values range from -100 to 100 with 0 being the center value.

My problem is, the stick values aren't very precise in the center area. For example, with the stick released, I should get an input value equal to 0 but I often get values like 2, 7, -5, etc (almost never exceeding 10 or -10).

What I'd like to do is applying a "smoothing" function to reduce the values around the 0 area.

I'm not experienced with mathematics and the best function I could find so far is:

function smooth(x)  {
    return Math.pow(x,3)/10000;
}

This gives me this kind of values:

As you can see, this function is too "aggressive" because even f(20) ~= 0 !

Any idea of which better function I could use ?


Solution

  • I ended up mixing my first solution with the one provided by Nina.

    var deadzone = 15;
    
    function smooth(x) {
        return Math.abs(x) <= deadzone ? Math.pow(x,3)/Math.pow(deadzone,2) : x;
    }
    

    See this graph