我正在测量pwm信号。有两种状态。测量结果非常接近1000或2000。我不想取平均值来消除测量噪声。我只是摆脱一个或两个非常不精确的值。
举一个例子,这是预期的(准确的)数据图:
这是真实的测量数据图:
我想消除仅由3或4次测量引起的中间峰值。我想忽略那些度量。
我实际上以为我会创建一个包含10个元素的队列并将其推入其中。如果新的度量值比队列中的值的平均值小500或多于500,则不会将其添加到实际数据数组中。每当队列平均值与新度量值之间的差小于500(在10个元素的队列中为5个度量值)时,我就会开始添加到实际数据数组中并重置队列。
但这似乎不是一种有效的方法。我是数学新手。所以我不知道如何更有效地编写它,并且我需要效率,因为代码将在Arduino上运行。
谢谢
编辑:
我尝试按照建议使用中值过滤
这是中值过滤器应用的图形:
As you can see, it worked perfectly. However, I had to use a filter with a length of 20. That is a lot of data to cache and push to the queue. Especially in Ardunio,c++, which has just 16 megahertz of processing power. Is there a more efficient way for my case?
As @mcdowella suggested. I converted all of my input to ones and zeros.
I am not using median filter since the I will be using the elimination in realtime.
I just take the average of last 10 values that my servo generate and round it to collect my final value. So when 0 are dominant in the last 10 values, I use 0.
This method has a downside of delay. When my sensor starts to measure 0 values, my algorithm detects it after 5 measurement cyles. 25 ms for each. There is a 250 ms of delay.
我知道这是一个懒惰的解决方案,但它似乎对我有用。但是,@ mcdowella的答案非常有用。我希望未来的读者能从中受益更多
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句