2

### Author Topic: Filtering bogus readings  (Read 3587 times)

0 Members and 1 Guest are viewing this topic.

#### Brandon121233

• Contest Winner
• Supreme Robot
• Posts: 320
• "Genius is %1 inspiration and %99 perspiration"
##### Filtering bogus readings
« on: April 22, 2007, 08:41:07 PM »
Hey everyone
If no one has noticed yet- my weakpoint in robotics is software (I only took a half year course in C++, and tried to reach myself the rest). My question regards making an Ultrasonic sensor more reliable. The sensor takes readings about 22 times a second, and since its going so fast ever so often it has a bogus reading. Right now to kinda smooth out the readings, I take and average the first 10 readings then convert that into my usable inches foramt. But sometimes for instance- it might take 9- 14inch readings and 1- 230inch reading, and that obviously throws off the average by a good amount, causing a false trigger, or no trigger when it should have.
I am wondering if there is an efficient way to analyze the first 10 values, then allow only like the ones that fall within say 80%-90% of the mean. I think I could do this on my own with a few hundred lines of repetitive- extraneous code, but does anyone know of a function that already exists that could do this, or a way to do it with as little effort as possible?
I am using the Arduino language, so if anyone could help in that format or a similar C++ format I can understand that would be terrific.
Thanks in advance- Brandon
Hell, there are no rules here—we're
trying to accomplish something.

—Thomas Edison

#### Kohanbash

• Supreme Robot
• Posts: 430
##### Re: Filtering bogus readings
« Reply #1 on: April 23, 2007, 03:31:38 AM »
Hi
I am not sure what arduino is. but this should work assuming initial values are correct.

count=1
mean = (NewValue+OldValue) / count
if (NewValue >mean * .80) and (NewValue <mean * 1.20) then
.....
.....
count++

else
.....
.....

Robots for Roboticists Blog - http://robotsforroboticists.com/

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Filtering bogus readings
« Reply #2 on: April 23, 2007, 09:54:13 AM »
Maybe try calculating the mode or the median of the values instead?

Median - middle value when they are listed in order of size

Mode - most common value
Imperial College Robotics Society
www.icrobotics.co.uk

#### nanob0t

• Robot Overlord
• Posts: 145
• Trust Your Technolust
##### Re: Filtering bogus readings
« Reply #3 on: April 23, 2007, 02:37:19 PM »
Apply a bit more math, if you took a statistics class, you learned about outliers:

An outlier equals the means the third quartile minus the first quartile x 1.5 added to the first/third quartile.

A quartile is the 4ths of a data set.  The median would be the middle.  For example:

1 3 5 7 9 11 13 15 17

Median = 10
1st Quartile = 6
2nd Quartile = 14
Outlier # = (14 - 6) x 1.5 = 12
Outliers = x > -6  x > 26

If you can represent that in code, then find the mean after finding outliers, you'll have a very precise code, but this will make it much larger.

Just a thought.  If you don't have any idea on how to represent this, just ask.

#### Brandon121233

• Contest Winner
• Supreme Robot
• Posts: 320
• "Genius is %1 inspiration and %99 perspiration"
##### Re: Filtering bogus readings
« Reply #4 on: April 23, 2007, 04:33:15 PM »
Are there any functions like somehting that would arrange everything in numerical order, and a function to find the mean, mode, and median?
Hell, there are no rules here—we're
trying to accomplish something.

—Thomas Edison

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Filtering bogus readings
« Reply #5 on: April 23, 2007, 04:48:14 PM »
Heres a sorting function you probably could use.

http://www.devx.com/tips/Tip/12940
Imperial College Robotics Society
www.icrobotics.co.uk

#### Brandon121233

• Contest Winner
• Supreme Robot
• Posts: 320
• "Genius is %1 inspiration and %99 perspiration"
##### Re: Filtering bogus readings
« Reply #6 on: April 23, 2007, 05:45:31 PM »
I don't think I can import the necessary library into Arduino, if someone who has used the Arduino program could tell me how to be able to import and use the sort() function that would be spectacular.
Hell, there are no rules here—we're
trying to accomplish something.

—Thomas Edison

#### nanob0t

• Robot Overlord
• Posts: 145
• Trust Your Technolust
##### Re: Filtering bogus readings
« Reply #7 on: April 23, 2007, 06:03:57 PM »
Is there a summation function?  Represented by the weird Eish symbol.

#### Brandon121233

• Contest Winner
• Supreme Robot
• Posts: 320
• "Genius is %1 inspiration and %99 perspiration"
##### Re: Filtering bogus readings
« Reply #8 on: April 23, 2007, 06:56:31 PM »
I think I can use some of this code http://www.tigoe.net/pcomp/code/archives/arduino/000711.shtml and then from there I know how to do the rest
Hell, there are no rules here—we're
trying to accomplish something.

—Thomas Edison

• Supreme Robot
• Posts: 11,680