加速度计低通滤波 [英] Accelerometer Low Pass Filtering

查看:48
本文介绍了加速度计低通滤波的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在 BigNerdRanch iOS 开发书籍上.

Still on the BigNerdRanch iOS Development book.

在加速度计一章中,他们首先实现了加速度计跟踪,但它相当跳跃.然后他们建议通过更改原始代码对其应用低通滤波器:

In the Accelerometer chapter, they first implement accelerometer tracking but it's fairly jumpy. They then suggest to apply a low pass filter to it by changing the original code:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    HypnosisView *hv = (HypnosisView *)[self view];

    [hv setXShift:10.0 * [acceleration x]];
    [hv setYShift:10.0 * [acceleration y]];

    [hv setNeedsDisplay];
}

为此:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    HypnosisView *hv = (HypnosisView *)[self view];

    float xShift = [hv xShift] * 0.8 + [accel x] * 2.0;
    float yShift = [hv yShift] * 0.8 + [accel y] * 2.0;

    [hv setXShift:xShift];
    [hv setYShift:yShift];

    [hv setNeedsDisplay];
}

相当简单的问题:他们从哪里获得这些值?我一直在查看文档,发现了一些关于低通滤波器的内容,其中建议使用以下代码:

Fairly simple question: where do they get these values from? I've been looking through the documentation and I found something about low pass filters, which suggests the following code:

   #define kFilteringFactor 0.1

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    // Use a basic low-pass filter to keep only the gravity component of each axis.
    accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
    accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
    accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));

   // Use the acceleration data.
}

但是,我首先尝试使用该代码,但我得到了一个错误(通过分析我的应用程序),说'*' 的左值是一个垃圾值".我的加速度计跟踪也不起作用.

However, I first tried with that code and I got an error (by analyzing my app) saying 'the left value of '*' is a garbage value'. My accelerometer tracking didn't work either.

我对这些值的含义感到相当困惑.例如,在代码的第一部分,他们为什么要将加速度值乘以 10?为了获得更大"的运动?我可以从中得到一些道理,但是带有低通滤波器的第二个代码对我来说完全没有意义.

I'm fairly confused as to what these values mean. For example, in the first part of the code, why do they multiply the acceleration values by 10? To get a 'bigger' movement? I could make some sense out of that, but the second code with the low pass filter makes absolutely no sense to me.

推荐答案

accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));

这段代码中发生了什么,您将此刻的加速度乘以过滤因子 0.1,然后将其添加到上次调用更新时的过滤加速度 0.9.

Whats happening in this code you are multiplying the acceleration at the moment by the Filtering factor 0.1 and then adding it to the filtered acceleration of the last time an update was called by 0.9.

这几乎是获取新值并将其添加为总 accelX 的 10%,其他 90% 由前一个值组成,这取决于之前的值,这取决于之前的值等等.这会删除高频值,因为只允许 10% 的任何更改通过新的 accelX 值.

This is pretty much getting the new value and adding it as 10% of the total accelX the other 90% is made up of the previous value which depends on the value before that, which depends on the value before that and so on. This cuts out high frequency values as only allows 10% of any change to go through to the new accelX value.

0.1 的 KFilteringFactor 使此滤波器滤除所有高频.您肯定希望通过更改此值来进行试验以适合您的特定应用程序.

The KFilteringFactor of 0.1 makes this filter cut out all high frequencies. You will definitely want to experiment by changing this value to suit your particular application.

这篇关于加速度计低通滤波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆