上下文:我正在尝试改进 iPhone CLLocationManager 返回的值,尽管这是一个更普遍适用的问题。关键是 CLLocationManger 在感觉合适时返回当前速度的数据,而不是以固定的采样率。
我想使用反馈方程来提高精度
v=(k*v)+(1-k)*currentVelocity
其中 currentVelocity 是 didUpdateToLocation:fromLocation: 返回的速度,v 是输出速度(也用于反馈元件)。
由于 didUpdateToLocation:fromLocation: 的“as and when”性质,我可以计算自上次调用以来的时间间隔,并执行类似
for (i=0;i
可以工作,但浪费周期。特别是因为我可能希望将 timeintervalsincelastcalls 测量为十分之一秒。
有没有办法在没有循环的情况下解决这个问题?即重写(积分?)公式,以便我在方程中放入一个区间,并得到与迭代相同的答案?
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 ?
发布评论
评论(2)
如果您编写原始方程,则
可以应用另一个SO问题。
If you write your original equation as
you can apply the answer from another SO question.
您可以根据间隔的大小选择 k 的值,而不是迭代。例如,如果间隔长度是一小时 - 您可能希望 k 为 0。
可以很容易地预先计算各种间隔大小的 k,以给出与迭代给出的相同答案。只需通过迭代计算变化(您已经有相应的代码),然后计算 k 的值即可以代数方式得出该值。
使用查找值表来代替昂贵的计算是一个常见的程序员绝地技巧。 (现在我的答案与代码有关!)
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!)