- Home /
Help me out with a simple smoothing function for accelerometer data?
I am saving the last 30 data from Input.accelerationEvents in a List to catch tilt gestures.
One problem I'm getting is that the raw data often has "stairsteps". two or three samples that are the same or almost the same, followed by a jump to two or three higher or lower values that are almost the same, in the middle of an otherwise smooth slope. for example:
... 1.35, 1.40, 1.51, 1.51, 1.67, 1.67, 1.73, 1.81, 1.89, ...
In this case, I'd want to bump the second 1.51 upward a bit and the 1.67 down a bit to give me a sequence more like
... 1.35, 1.40, 1.51, 1.56, 1.61, 1.67, 1.73, 1.81, 1.89, ...
which would be a lot more useful if I'm looking for directional trends - after smoothing that's an upward trend of 9, instead of three upward trends of 3, 2, and 3.
So I asked Wikipedia and I am now baffled and overwhelmed by the amount and complexity of all the different kinds of smoothing functions there are.
Maybe 'smoothing' isn't even really what I want to do, because something like this coming from the accelerometer:
... 0.23, 0.25, 0.87, 0.48, 0,41, ...
is significant- I don't want to flatten any spikes, just cut the corners off all the stairsteps.
Answer by whydoidoit · Jun 09, 2012 at 04:57 PM
Not sure what language you are using but you could use Distinct from Linq to get only the unique values without the stairsteps:
using System.Linq;
....
public List<float> myInputs = new List<float>();
...
var inputsToUser = myInputs.Distinct(); //Returns a list of the unique values
Removing all the duplicates was my first thought- but it doesn't really work for two reasons- first, the jaggies are usually not exactly the same value; just very close. Second, discarding values would shorten the list for one axis more than the others in an unpredictable way which would mess up slopes and relative ti$$anonymous$$gs and not let me loop through all 3 at once without getting ArrayIndexOutOfBounds exceptions.
Sure I see your point - but also most smoothing functions are going to bias towards the values that are repeated I guess. You could effectively draw a spline between the points and/or normalize the points as if they were a curve and interpolate through them therefore removing the problem of there being an unequal number.
Not sure how you are consu$$anonymous$$g this data? Have you tried a simple smooth using 2/3 the value 1/6 the value before and 1/6 the value after for instance? A lot of this will depend on how it actually feels rather than some correct formula. I guess the period of smoothing will depend on the data you are seeing - so perhaps you should smooth over a longer period?
You could also take the values and clamp to the interquartile range or maybe the 5th to the 95th percentiles for more accuracy (sort the data, take the 5% and 95% of sample values). I guess I can't really work out if you need to remove outliers or just smooth the curve.