Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accelerometer Low Pass Filtering

Still on the BigNerdRanch iOS Development book.

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];
}

to this:

- (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.

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.

like image 723
Joris Ooms Avatar asked Aug 04 '11 13:08

Joris Ooms


People also ask

Why low-pass filter is used in accelerometer?

The total noise power at the output of a system directly depends on the system bandwidth. That's why we need a low-pass filter to limit the bandwidth to the maximum frequency needed by the application. This maximizes the resolution and dynamic range of the accelerometer.

What is high pass filter in accelerometer?

High-pass filters are generally included in the analog circuits to prevent drift in piezoelectric accelerometer signals. Analog high-pass filters remove low frequency information, but also corrupt the amplitude and phase of the signal near the filter corner frequency.

What does low-pass filtering do?

A lowpass filter is one which suppresses or attenuates the high frequency components of a spectrum while 'passing' the low frequencies within a specified range.

How does accelerometer remove drift data?

In order to remove the drift, based in my experiments experience, I sugesst you to use a detrend operation (implemented in matlab 'detrend') and then a high pass filter with a specific and tested cuttoff frequency. I recomend to use Butterworth or Chebychev high pass filters.


1 Answers

accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));

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.

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.

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.

like image 111
Mike Khan Avatar answered Oct 19 '22 03:10

Mike Khan