Smoothing data from a sensor

前端 未结 5 541
抹茶落季
抹茶落季 2020-11-29 17:06

I have a 3D sensor which measures v(x,y,z) data. I\'m only using the x and y data. Smoothing only x and y would be enough.

If I use a log to show the data, it shows

5条回答
  •  猫巷女王i
    2020-11-29 17:37

    So I came here looking to solve the same problem (sensor input smoothing in Android) and here's what I came up with:

    /*
     * time smoothing constant for low-pass filter
     * 0 ≤ α ≤ 1 ; a smaller value basically means more smoothing
     * See: http://en.wikipedia.org/wiki/Low-pass_filter#Discrete-time_realization
     */
    static final float ALPHA = 0.2f;
    
    protected float[] accelVals;
    
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            accelVals = lowPass( event.values, accelVals );
    
        // use smoothed accelVals here; see this link for a simple compass example:
        // http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html
    }
    
    /**
     * @see http://en.wikipedia.org/wiki/Low-pass_filter#Algorithmic_implementation
     * @see http://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter
     */
    protected float[] lowPass( float[] input, float[] output ) {
        if ( output == null ) return input;
    
        for ( int i=0; i

    Thank you @slebetman for pointing me toward the Wikipedia link, which after a little reading drew me to the algorithm on the wikipedia Low-pass filter article. I won't swear I have the best algorithm (or even right!) but anecdotal evidence seems to indicate it's doing the trick.

提交回复
热议问题