Smoothing Out Values

Must Read

7 Critical Things You Must Know Before Refinancing Your Home!

Refinancing your home - everyone's doing it! Everywhere you turn, there's someone talking about refinancing; when they refinanced their...

Canon PowerShot G12, Stop Right There!

Shutter speed is the main tool in the photographer’s arsenal for capturing great action shots. The ability to freeze...

The Marketing Mix

There are four important components of marketing. They are called the 4 Ps – product, price, place and promotion. Marketing...

Accelerometer sensors are prone to spurious results. Accelerometer values are not always coming across at a regular pace, and users can make erratic movements. If you want to avoid a jittery effect, smooth out the values. You can do this by combining previous readings with the current reading and leaving out a range of values according to a defined filter.

There are two approaches to accomplishing this. A high-pass filter is used for an application that is looking for rapid changes by weighting the current reading more than the previous reading. A good use case for a high-pass filter is detecting a shake motion, as in our previous example. A low-pass filter is used for an application that is looking for fine-tuned, stable values. This type of filter would be used in a game of precision, such as carefully pushing an object toward a target.

When testing these filters, the values start adjusting as expected after a few seconds.

Using a high-pass filter

The high-pass filter works with a fixed filter. Try different filter values until you achieve the effect you are looking for:

const FILTER:Number = 0.10;
var high:Number = 0.0;
var oldHigh:Number = 0.0;
function onUpdate(event:AccelerometerEvent):void {
var ax:Number = event.accelerationX;
high = ax – (ax*FILTER) + oldHigh * (1.0 – FILTER);
oldHigh = high;

Let’s go over the formula. ax is the new accelerometer value on the x-axis. high is the adjusted value after applying the filter formula, and oldHigh is the cumulated value over time.

ax is reduced by one-tenth of its value. For instance, if ax is equal to 3:

high = ax – (ax*FILTER)
3 – (3*0.10)
3 – 0.30

Let’s say that initially oldHigh is equal to zero, but over time, it is equal to 2. The following code reduces oldHigh by one-tenth:

oldHigh = oldHigh * (1.0 – FILTER));
2 * (1.0 – 0.10);
2 * 0.90

The new high value is 4.5:

2.70 + 1.80

Let’s examine these values over time. Returning to our shake example, to test the code with the high-pass filter, the threshold value is increased:

const THRESHOLD:Number = 5.0;
var isMeasuring:Boolean = false;
var isShaking:Boolean = false;
const FILTER:Number = 0.10;
var high:Number = 0.0;
var oldHigh:Number = 0.0;
function onUpdate(e:AccelerometerEvent):void {
if (isMeasuring) {
isMeasuring = true;
var ax:Number = event.accelerationX;
high = ax – (ax*FILTER) + oldHigh * (1.0 – FILTER);
oldHigh = high;
if (Math.abs(high) > THRESHOLD) {
isShaking = true;
if (isShaking) {
// we have a shake
isMeasuring = false;

Using a low-pass filter

The low-pass filter has the effect of changing slowly and stabilizing the input. It is handy for rotating art for a flight simulation or a rowing boat.

The low-pass filter looks as follows:

const FILTER:Number = 0.10;
var low:Number = 0.0;
var oldLow:Number = 0.0;
function onUpdate(event:AccelerometerEvent):void {
var ax:Number = accelerationX;
low = ax * FILTER + oldLow * (1.0 – FILTER);
oldLow = low;

Keep in mind that the more you filter the value, the more lag you will get in response because the average converses more slowly to the current value of the sensor.


Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This