How to implement a LowPass Filter?

允我心安 提交于 2019-12-03 07:49:05

A 1st order IIR low-pass filter can be of the form:

output_value = rate * input_value + (1.0 - rate) * previous_output_value;

which is pretty much what's inside Apple's AccelerometerGraph example. You select the rate parameter depending on what frequency (very very roughly shakes per second) you want to roll-off or start to attenuate to get a smoother resulting output, and the sample rate of the input data.

A low pass filter is simply smoothing of the results to remove the high frequencies. The simplest low pass filter is a box filter which is done by averaging n samples together.

For averaging 2 samples together this is as simple as doing:

sample[n] (sample[n] + sample[n + 1]) / 2;

If Apple's AccelerometerGraph example is too complex for you to understand, I created a simpler accelerometer example for my class which you can download here. This implements a simple low-pass and high-pass filter for raw accelerometer values, then logs the results to the screen.

As hotpaw2 and Goz describe, this uses a very simple weighted rolling average for the filter calculation:

UIAccelerationValue lowPassFilteredXAcceleration = (currentXAcceleration * kLowPassFilteringFactor) + (previousLowPassFilteredXAcceleration * (1.0 - kLowPassFilteringFactor));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!