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:
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:
float filteringFactor = 0.1; lowPassed = newValue * filteringFactor + lowPassed * (1.0 - filteringFactor);
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:
float filteringFactor = 0.1; lowPassed = newValue * filteringFactor + lowPassed * (1.0 - filteringFactor); highPassed = newValue - lowPassed;
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.
3.142.200.109