Search Unity

Accelerometer and Low Pass Filtering

Discussion in 'iOS and tvOS' started by nvarcha, Jul 26, 2010.

  1. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    Hi.

    I know this has been asked a few times, but after a couple of days of researching, I still don't get it.

    I have an object that moves accordingly to the accelerometer input. This generates, as expected, a lot of jittering since the accelerometer reads small vibrations even when standing on my desk.

    I've researched about low pass filtering, which would remove that small jittering.

    However, the Low Pass Filtering example in the Unity docs (despite the coding error it has), it's just a simple Lerp (smoothing over time). I don't know if Low Pass Filtering is actually that, but from what I've been reading, it doesn't seem right.

    Also, I'm not getting the desired result, since the jittering is either still there, or the smoothing is too big and the reaction of the object is way too slow (I rotate the iPhone and the object follows from behind). Again, a typical effect of Lerp.

    I've read Apple's implementation of a Low Pass Filter. I've tried that, it also felt just like a smooth (Lerp).

    I've seen apps that do a great job filtering the accelerometer jittering, but they hardly stay behind following an object.

    Is there an algorithm I should be searching? or is it more like a hand-made code?

    Thanks in advance for any tips.
     
  2. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    734
    I had to filter accelerator input myself just yesterday. After a couple of searches on the forum i used the following code and got great results, i even combined it with the Lerp-with-previous-frame-accelvalues-method you mentioned.
    Var-names should be selfspeaking:

    _acceleration.x, _acceleration.y are the raw accel-input data (mapped to my coordinatenames already)






    private const float AccelSmoothing=0.5f;
    private const float AccelLowPass=0.1f;
    .
    .
    .
    somewhere later in the class code....
    .
    .
    .

    _acceleration.x = ( _acceleration.x * AccelLowPass) + (_accelerationPrevious.x * (1.0f - AccelLowPass));
    _acceleration.y = ( _acceleration.y * AccelLowPass) + (_accelerationPrevious.y * (1.0f - AccelLowPass));

    _accelerationSmooth = Vector3.Lerp( _acceleration,_accelerationPrevious , AccelSmoothing);
    _accelerationPrevious = _acceleration;
     
  3. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    Hi, spacefrog.

    Thank you very much for sharing.

    Unfortunately, it didn't help. Smoothing is nice but it's still a bit behind on "reaction".

    Your code is actually doing two "lerps". Your first portion of the code:

    Code (csharp):
    1. _acceleration.x = ( _acceleration.x * AccelLowPass) + (_accelerationPrevious.x * (1.0f - AccelLowPass));
    is actually a Lerp.

    Maybe a Low pass filter is just that, a smoothing or interpolation of values. It certainly looks more complicated than that in the docs around the web.

    I've come with a balance between jittering and "reaction" that, sort of, suits my needs.

    I'll keep searching, though. I'm sure there's a way, I'm just not that expert in signals or math.

    Again, thanks.
     
  4. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    734
    ha - thanks for the hint

    actually yesterday my brain was not working at all. Had too much beer the day before - and this was the result ... ;-)
     
  5. luisanton

    luisanton

    Joined:
    Aug 25, 2009
    Posts:
    325
    You could use a median filter, computed from the last n measures.

    Median filter is usually slow, because it requires samples to be sorted. But you could keep the vector of previous measured sorted by inserting new values at the right place, and make it a little bit faster by looking just on the right or second half (because you know the previous median value)
     
  6. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    Thanks for the recommendation.

    It is definitely an option.

    However, I think because of processing and the result I need, I'll stick with a simple "smoothing" technique.

    If I need something more, I'll program an anti-jitter code myself, ad-hoc for the accelerometer.

    So far I'm getting results that are acceptable.

    Again, thanks.
     
  7. luisanton

    luisanton

    Joined:
    Aug 25, 2009
    Posts:
    325
    Well, I used accelerometers for yaw pitch control and I needed no smoothing at all...
     
  8. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    I used the accelerometer to control camera rotation and movement based on an example script that came with Unity. Not had a problem with jitter.

    Have you tried the accelerometer control examples that come with Unity?
     
  9. TriplePAF

    TriplePAF

    Joined:
    Aug 19, 2009
    Posts:
    246
    I posted some code in this topic. It's contains a simple algorithm what works pretty good with fast camera rotations:

    http://forum.unity3d.com/viewtopic.php?t=41641&start=0&postdays=0&postorder=asc&highlight=

    Just drop the script into your C# project and create an acceleration lowpass Vector like this:

    Code (csharp):
    1.  
    2. Vector3 lowPassAcc = AccelerationHelper.LowPassFilterAdaptive (AccelerationHelper.LastAcceleration, AccelerationHelper.Acceleration, 60f, 5f, true);
    3.  
    Peter.
     
  10. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Thanks, it's working great! I was using Lerp to cut off the jittering, but it didn't work as well as your code. My game is working great now.
     
  11. Mrkanghoo

    Mrkanghoo

    Joined:
    Dec 22, 2012
    Posts:
    8
    hi,i just count step when i shake.when they shake one time the step=step+1.but i can't control on acceleration.how to do it?
     
  12. thinkingonpause

    thinkingonpause

    Joined:
    Nov 23, 2012
    Posts:
    2
    Just over 3 years later and this was THE SOLUTION I was looking for. The links from other people's solutions was great for study as well, but people like you guys are what makes this community so strong!!!!
    If you're interested this is the game I'm going to be applying your help in:
    http://paultproductions.com/projects/deliverance/deliverance.html

    Go TriplePAF!!! I hope you guys are subscribed/get to see my gratitude.
     
  13. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
  14. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Anyone?
     
  15. CYS

    CYS

    Joined:
    Aug 25, 2013
    Posts:
    11