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

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

问题描述

仍然在BigNerdRanch iOS开发书上。

Still on the BigNerdRanch iOS Development book.

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

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天全站免登陆