For the More Curious: Filtering and Frequency

In general, there are two ways of altering the accelerometer data to suit your needs: change the frequency of accelerometer data updates and apply a filter to the data. When you’re writing an application that relies on accelerometer data, you should determine the update interval and filtering algorithm that gives the user the best experience.

In terms of update intervals, here are some recommendations:

Orientation Applications
If your application relies on the current orientation of the device (for example, to rotate an arrow to point in a certain direction), the accelerometer can update infrequently. A value of 1/20 to 1/10 seconds for the updateInterval is sufficient.
Game Applications
An application that uses accelerometer data as input for controlling a visual object in real-time needs a slightly faster update interval. For applications like this, the updateInterval should be between 1/30 to 1/60 seconds.
High-Frequency Applications
Applications that need to squeeze every little update out of the accelerometer should set the updateInterval between 1/70 and 1/100 seconds (the smallest possible interval). An application that detects shakes is updating at a high frequency.

Once you have chosen the right update interval, you need to choose what type of filter is best. Typically, you’ll choose either a low-pass filter or a high-pass filter.

Using a low-pass filter, as you did in the exercise, isolates the gravity component of the acceleration data and reduces the effect of sudden changes in the device’s orientation. In most situations, it gives you just the orientation of the device. A basic low-pass filter equation looks like this:

f​l​o​a​t​ ​f​i​l​t​e​r​i​n​g​F​a​c​t​o​r​ ​=​ ​0​.​1​;​
l​o​w​P​a​s​s​e​d​ ​=​ ​n​e​w​V​a​l​u​e​ ​*​ ​f​i​l​t​e​r​i​n​g​F​a​c​t​o​r​ ​+​ ​l​o​w​P​a​s​s​e​d​ ​*​ ​(​1​.​0​ ​-​ ​f​i​l​t​e​r​i​n​g​F​a​c​t​o​r​)​;​

where lowPassed is the output. Notice that the previous output is used the next time the equation is solved and that the new value produced by the accelerometer is blended with all of the previous values. The output of a low-pass filter is essentially a weighted average of previous inputs, and sudden movements will not affect the output as much as they would with unfiltered data.

On the other hand, sometimes you want to ignore orientation and focus on sudden movements, like a shake. For this, you would use a high-pass filter. Now that you know the low-pass filter, the high-pass signal is what’s left if you subtract out the low-pass signal:

f​l​o​a​t​ ​f​i​l​t​e​r​i​n​g​F​a​c​t​o​r​ ​=​ ​0​.​1​;​
l​o​w​P​a​s​s​e​d​ ​=​ ​n​e​w​V​a​l​u​e​ ​*​ ​f​i​l​t​e​r​i​n​g​F​a​c​t​o​r​ ​+​ ​l​o​w​P​a​s​s​e​d​ ​*​ ​(​1​.​0​ ​-​ ​f​i​l​t​e​r​i​n​g​F​a​c​t​o​r​)​;​
h​i​g​h​P​a​s​s​e​d​ ​=​ ​n​e​w​V​a​l​u​e​ ​-​ ​l​o​w​P​a​s​s​e​d​;​

There are other algorithms for high-pass filtering, but this one is especially easy to understand.

Figure 8.3 is a graph over time of low-pass and high-pass filtering on a device that is being shaken.

Figure 8.3  Low- and high-pass filter graphs

Low- and high-pass filter graphs

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.200.109