我需要低通滤波器Sensor.TYPE_ROTATION_VECTOR吗?

时间:2016-06-12 23:43:16

标签: android sensor android-sensors lowpass-filter

如此Readme所述,建议对Android传感器进行低通滤波。这也推荐用于TYPE_ROTATION_VECTOR传感器,它是一个软件传感器吗?

1 个答案:

答案 0 :(得分:0)

是。你需要这样做。在某些设备上,低通滤波器不适用于软件传感器:

public class LowPassFilter {
    // Time constant in seconds
    static final float timeConstant = 0.297f;
    private float alpha = 0.15f;
    private float dt = 0;
    private float timestamp = System.nanoTime();
    private float timestampOld = System.nanoTime();
    private float output[] = new float[]{ 0, 0, 0 };

    private long count = 0;

    public float[] lowPass(float[] input)
    {
        timestamp = System.nanoTime();

        // Find the sample period (between updates).
        // Convert from nanoseconds to seconds
        dt = 1 / (count / ((timestamp - timestampOld) / 1000000000.0f));

        count++;

        // Calculate alpha
        alpha = timeConstant / (timeConstant + dt);

        output[0] = calculate(input[0], output[0]);
        output[1] = calculate(input[1], output[1]);
        output[2] = calculate(input[2], output[2]);

        return output;
    }


    float calculate(float input, float output){
        float out = alpha * output  + (1 - alpha) * input;
        return out;
    }


}