views:

30

answers:

2

Context: I'm trying to improve the values returned by the iPhone CLLocationManager, although this is a more generally applicable problem. The key is that CLLocationManger returns data on current velocity as and when it feels like it, rather than at a fixed sample rate.

I'd like to use a feedback equation to improve accuracy

v=(k*v)+(1-k)*currentVelocity

where currentVelocity is the speed returned by didUpdateToLocation:fromLocation: and v is the output velocity (and also used for the feedback element).

Because of the "as and when" nature of didUpdateToLocation:fromLocation: I could calculate the time interval since it was last called, and do something like

for (i=0;i<timeintervalsincelastcalled;i++) v=(k*v)+(1-k)*currentVelocity

which would work, but is wasteful of cycles. Especially as I probably want timeintervalsincelastcalled to be measured as 10ths of a second.

Is there a way to solve this without the loop ? i.e. rework (integrate?) the formula so I put an interval into the equation and get the same answer as I would have by iteration ?

+1  A: 

Instead of iterating, you could just choose the value of k based on the size of the interval. For example, if the interval length is an hour - you'd probably want k to be 0.

It would be easy to precompute k for a variety of interval sizes to give the same answer as the iteration would give. Just compute the change by iterating (you already have code for that), and then compute the value of k that would give you that algebraicly.

It's a common programmer jedi trick to have a table of lookup values in place of expensive calculations. (there, now my answer has something to do with code!)

David B
sensible, but mtrw's answer wins :-) You wait a week for an answer then two come at once!
Andiih
+1  A: 

If you write your original equation as

v = k*vCurrent + (1-k)*v 

you can apply the answer from another SO question.

mtrw
superb! Thanks mtrw
Andiih