Hysteresis Round用于解决由于噪声引起的“闪烁”值

时间:2012-05-01 21:32:29

标签: c# math rounding

背景 我们有一个嵌入式系统,使用10位模数转换器将线性位置(0 mm - 40 mm)从电位计电压转换为数字值。

    ------------------------
0mm |                      | 40 mm
    ------------------------

我们向用户显示1 mm增量的线性位置。防爆。 1毫米,2毫米,3毫米等

问题: 我们的系统可用于电磁噪声环境,这可能导致线性位置由于噪声进入ADC而“闪烁”。例如,当电位计为39 mm时,我们会看到如下值:39,40,39,40,39,38,40等。

由于我们每1毫米四舍五入,如果值在1.4和1.6毫米之间切换,我们将看到1到2之间的闪烁。

建议的软件解决方案: 假设我们无法更改硬件,我想在舍入值时添加一些滞后以避免这种闪烁。这样:

如果当前值为1mm,如果原始值为1.8或更高,则只能达到2mm。 同样,如果当前值为1mm,如果原始值为0.2或更低,则只能达到0mm。

我编写了以下简单应用来测试我的解决方案。如果我走在正确的轨道上,或者您有任何建议,请告诉我。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDFSHysteresis
{
    class Program
    {
        static void Main(string[] args)
        {
            double test = 0;
            int curr = 0;
            Random random = new Random();

            for (double i = 0; i < 100; i++)
            {
                test = test + random.Next(-1, 2) + Math.Round((random.NextDouble()), 3);
                curr = HystRound(test, curr, 0.2);

                Console.WriteLine("{0:00.000} - {1}", test, curr);
            }

            Console.ReadLine();
        }

        static int HystRound(double test, int curr, double margin)
        {

            if (test > curr + 1 - margin && test < curr + 2 - margin)
            {
                return curr + 1;
            }
            else if (test < curr - 1 + margin && test > curr - 2 + margin)
            {
                return curr - 1;
            }
            else if (test >= curr - 1 + margin && test <= curr + 1 - margin)
            {
                return curr;
            }
            else
            {
                return HystRound(test, (int)Math.Floor(test), margin);
            }
        }
    }
}

示例输出:

Raw      HystRound
======   =========
00.847   1
00.406   1
01.865   2
01.521   2
02.802   3
02.909   3
02.720   3
04.505   4
06.373   6
06.672   6
08.444   8
09.129   9
10.870   11
10.539   11
12.125   12
13.622   13
13.598   13
14.141   14
16.023   16
16.613   16

3 个答案:

答案 0 :(得分:1)

如何使用最后N秒的读数平均值,其中N可能相当小/亚秒,具体取决于您的采样率?

根据您的需要,您可以使用简单的线性平均值或更复杂的线性平均值。维基百科上详细介绍了几种移动平均算法:

http://en.wikipedia.org/wiki/Moving_average

根据您的敏感度/响应能力需求,如果新读数超过运行平均值X%,则可以重置平均值。

答案 1 :(得分:0)

我不得不处理类似的事情,我不得不从电路读取电压输出并在计算机屏幕上显示图表。最重要的是,这实际上取决于您的系统要求。如果要求是“1mm”精度那么你就没有什么可以做的。否则,如上所述,您可以使用多种方法来帮助您减轻闪烁。你可以:

  • 计算用户可以配置的特定时间段内这些值的平均值。
  • 允许用户设置“灵敏度阈值”。该阈值可用于决定天气以将新值视为有效。在您的示例中,阈值可以设置为2毫米,在这种情况下,39, 40, 39, 38等值将显示为39mm
  • 另外,您是否考虑在应用程序和硬件本身之间放置一个外部稳定器?

答案 2 :(得分:0)

我认为Gareth Rees对一个非常相似的问题给出了很好的答案: how to prevent series of integers to have the same value to often