Android:获取平均传感器值

发布于 2024-12-17 11:43:12 字数 268 浏览 4 评论 0原文

我试图存储过去几秒钟的平均光照水平(使用光传感器),以便在当前光照水平约为平均值的 25% 时调用另一个函数进行比较。

我的问题是我不完全确定如何存储平均值。

我假设你会做一些类似于

while(sensorUpdateTime + sampleTime < CurrentTime)
   average += currentValue / updatesSampleRate;

我只是不知道如何存储平均值的事情。

I'm trying to store the average light level (using the light sensor) from the past few seconds to use in a comparison to call another function if the current light level is about 25% of the average.

My problem is I'm not entirely sure how to go about storing the average.

I'm assuming you would do something along the lines of

while(sensorUpdateTime + sampleTime < CurrentTime)
   average += currentValue / updatesSampleRate;

I'm just kind of at a loss on how to store the average.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-12-24 11:43:12

如果采样率相当恒定,您可以将值存储在数组中并进行“循环”更新(使用 array[sampleNumber % array.length] 查找要删除的最旧值。

然后您只需从保留的总和中减去删除的值,然后添加新值。

这样您就可以获得过去 array.length 传感器读数的平均值,并且您还可以获得方差 - 只需保留平方传感器值的总和,然后:

sigma = sqr( MX2 - MX )

If you sample rate is quite constant, you can store values in an array and do "circular" updates ( use array[sampleNumber % array.length] to find oldest value to expunge.

Then you just substract expunged value out of kept sum, and add new value.

This way you have average over past array.length sensor readings. And you can also get variance - just keep sum of squarer sensor values, and then:

sigma = sqr( MX2 - MX )

夏见 2024-12-24 11:43:12

平均值只是低通滤波器的一个特例。它会带来延迟,但如果没有问题,您可以使用它。 这里有一个低通滤波器实现的伪代码。您只需修改 alfa 参数即可。您可以将此应用于最后 N 个值。

The average is just a particular case of the low pass filter. It will introduce a delay but if there is no problem you can use it. You have here a pseudo-code for a low pass filter implementation. You just modify the alfa parameter. You can apply this for the last N values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文