我从android加速度计获得了传感器值,但值不一致。我认为,如果我们可以添加一些过滤器,那么我们可以轻松地检查数据可靠性。假设如果我建立了一个低通滤波器,如修复两个值高低

if value less than low ---- device is still(not moving)

if values more than low less than high -- device is staring to move

if values more than high --- we attenuate the value( device moving)

现在,我无法确定什么是值以及如何实现这样的事情。或者,如果我的想法是正确的,我的意思是说这种类型的过滤器是否可以工作。

最佳答案

您的想法或多或少都可以。但是需要一些补充。我们可以使用低通滤波器。您提供的逻辑应用于确定数据Alpha(而非数据)的变化率。我可以给您一个简单的课程,为您做些工作

public class minPassFilter {

private static final float A_DEFAULT = 0.333f;
private static final float A_STEADY       = 0.001f;
private static final float A_START_MOVING = 0.6f;
private static final float A_MOVING       = 0.9f;

private minPassFilter() { }

public static float[] filter(float min, float max, float[] present, float[] former) {
    if (present==null || former==null)
        throw new NullPointerException("Input and former arrays can't be NULL");
    if (present.length!=former.length)
        throw new IllegalArgumentException("Input and former arrays must have the same length");

    float A = computeA(min,max,present,former);

    for ( int i=0; i<present.length; i++ ) {
        former[i] = former[i] + A * (present[i] - former[i]);
    }
    return former;
}

private static final float computeA(float min, float max, float[] present, float[] former) {
    if(former.length != 3 || present.length != 3) return A_DEFAULT;

    float x1 = present[0],
          y1 = present[1],
          z1 = present[2];

    float x2 = former[0],
          y2 = former[1],
          z2 = former[2];

    float distance = (float)(Math.sqrt( Math.pow((double)(x2 - x1), 2d) +
                                        Math.pow((double)(y2 - y1), 2d) +
                                        Math.pow((double)(z2 - z1), 2d))
    );

    if(distance < min) {
        return A_STEADY;
    } else if(distance >= min || distance < max) {
        return A_START_MOVING;
    }
    return A_MOVING;
}
}


您可以将传感器数据传递给此类,并获取过滤后的值。

供参考,github链接为this

10-07 13:01