Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Sensor "de-noising" (http://www.chiefdelphi.com/forums/showthread.php?t=114539)

Kevin Sevcik 04-03-2013 15:30

Re: Sensor "de-noising"
 
Quote:

Originally Posted by mrklempae (Post 1243484)
This year we're using a moving average to filter out noise from our sensors. It's about seven lines of code total, and works by feeding a stream of data into an array and calculating the average continuously. It works really well for rangefinders, but I'm sure it's applicable to almost anything. All you need to do to adjust the sensitivity is change the sample size.

Applicable to anything except a digital signal. Unless 2/3rds on has some sort of meaning I'm unaware of.

EricS-Team180 05-03-2013 13:26

Re: Sensor "de-noising"
 
While Kevin's approach is far more elegant, this might work for you in a pinch:
Code:


/* some set-up */
#include <stdio.h>
#define true 1
#define false !true
typedef char boolean;

/* a program to run the stuff */
int main (void)
{
static int array[5] = {0,0,0,0,0};
static int counter = 0;
boolean persistence;
boolean digitalInput;
int i;

/* run the persistance checker in a loop */
 for (i=0; i<10; i++)
 { 

  /* put in some false then put in some true */
  if(i>4)
    digitalInput = true;
  else
    digitalInput = false;

  /* check on the input and fill the
  * persistence array with the result
  */ 
  if( digitalInput)
    array[counter] = 1;
  else
    array[counter] = 0;

  /* keep tabs on the array index */
  counter++;
  if(counter > 4)
        counter = 0;

/* now AND the entire array and show the result */
  persistence = array[0] && array[1] && array[2] && array[3] && array[4];
  printf(" perist = %d \n",persistence);

 }

}

When the array fills and remains filled, you get true, otherwise it's false
...ugly but it could work

Eric

Ether 05-03-2013 17:08

Re: Sensor "de-noising"
 

Here's a simple IIR filter which does essentially the same thing with 2 lines of code, no loops, and no conditional logic:

Code:

ave = 0.5*(input+ave);
output = truncate(ave+0.03);

It does use floating point (for ave), but the cRIO has an FPU.

input and output are 0 or 1.



otherguy 06-03-2013 02:24

Re: Sensor "de-noising"
 
Last year we created a class to handle averaging out our noise. we were using this for sensors that gave us analog data, but you could send in 1.0 and 0.0 on the output side for a digital signal.

What ended up being nice about this class last year was that we could very easily change how many samples were being included in the average. As we started to speed things up throughout the season, we needed the average to be more responsive (fewer samples)

Here's the Java code: https://gist.github.com/jcorcoran/5097376

Ether 06-03-2013 08:47

Re: Sensor "de-noising"
 
Quote:

Originally Posted by otherguy (Post 1244328)
What ended up being nice about this class last year was that we could very easily change how many samples were being included in the average.

The 0.03 number can be adjusted to accomplish the same thing.



Kevin Sevcik 06-03-2013 09:01

Re: Sensor "de-noising"
 
Quote:

Originally Posted by Ether (Post 1244089)

Here's a simple IIR filter which does essentially the same thing with 2 lines of code, no loops, and no conditional logic:

Code:

ave = 0.5*(input+ave);
output = truncate(ave+0.03);

It does use floating point (for ave), but the cRIO has an FPU.

input and output are 0 or 1.

It's been a while since I've done IIR filters and such, so my math on them is rusty. Is that actually exactly equivalent to checking than the signal has been 1 for the last X ms? It would seem that it would tolerate some blips low. And it definitely switches 1->0 quicker than 0->1.

Mind you, it's probably a perfectly cromulent and simple solution to the OP's actual problem, it just seems different than actual debouncing.

Ether 06-03-2013 09:25

Re: Sensor "de-noising"
 
Quote:

Originally Posted by Kevin Sevcik (Post 1244366)
It's been a while since I've done IIR filters and such, so my math on them is rusty. Is that actually exactly equivalent to checking than the signal has been 1 for the last X ms? It would seem that it would tolerate some blips low. And it definitely switches 1->0 quicker than 0->1.

Mind you, it's probably a perfectly cromulent and simple solution to the OP's actual problem, it just seems different than actual debouncing.

As shown, it debounces only in one direction: it requires 5 ones in a row to switch the output to 1, but only one zero to switch back to zero (and stay zero until 5 more consecutive ones are seen).

If you want to debounce in both directions, change the second line to

Code:


if(output==0) output=truncate(ave+0.03); else output=truncate(ave+1-0.03);


efoote868 06-03-2013 22:58

Re: Sensor "de-noising"
 
Quote:

Originally Posted by Ether (Post 1244381)
As shown, it debounces only in one direction: it requires 5 ones in a row to switch the output to 1, but only one zero to switch back to zero (and stay zero until 5 more consecutive ones are seen).

If you want to debounce in both directions, change the second line to

Code:


if(output==0) output=truncate(ave+0.03); else output=truncate(ave+1-0.03);


If I understand what you're doing, .03 is a nice way to represent 2 ^ -5, and if you wanted more or less consecutive bits (allowing longer debouncing) you'd adjust the exponent accordingly.

Ether 06-03-2013 23:21

Re: Sensor "de-noising"
 
Quote:

Originally Posted by efoote868 (Post 1244713)
...if you wanted more or less consecutive bits (allowing longer debouncing) you'd adjust the exponent accordingly.

Yes.




All times are GMT -5. The time now is 04:48.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi