• No se han encontrado resultados

Noise is everywhere. This world is made of noise. This is what everybody related to sensors

will constantly repeat. But what is noise? It's unexpected data generated by sensors or signal

sources. It can completely ruin the behavior of an autonomous system if not treated properly. Here, we will use the implementation from the Measuring distance – infrared and ultrasonic recipe. The Sharp IR is known for its interference with basically anything, and here we will

Getting ready

The following are the ingredients needed for this recipe: f An Arduino board connected to a computer via USB

f A Sharp infrared proximity sensor such as the GP2Y0A21YK or the GP2Y0A02YK0F

How to do it…

This recipe uses the implementation from the Measuring distance – infrared and ultrasonic recipe. Please implement the same circuit as you did there.

Code

The following code will read the output of the Sharp IR sensor and will use two filtering methods to filter it and then print it over the serial connection:

int sensorPin = A0; // Declare the used sensor pin

// Function that reads a sensor with specified number of samples // Returns the mean filtered value

int readMean(int pin, int samples){ // Variable to store the sum of readings int sum = 0;

// Read the samples and add them all for (int i = 0; i < samples; i++){

sum = sum + analogRead(pin);

}

// Divide the sum by the number of samples sum = sum/samples;

// Return the sum return sum; }

// Function that reads a sensor with specified number of samples // Returns the median filtered value

int readMedian (int pin, int samples){

// Variable to store the readings int raw[samples];

// Read the samples each as a value in the vector for (int i = 0; i < samples; i++){

raw[i] = analogRead(pin);

}

// Sort the values // Lazy bubble sort

int temp = 0; // temp value

for (int i = 0; i < samples; i++){

for (int j = i; j < samples - 1; j++){

// Check if values out of order if (raw[j] > raw[j + 1]){

// If so, swap them temp = raw[j]; raw[j] = raw[j + 1]; raw[j + 1] = temp; } } }

// Return the middle value return raw[samples/2]; }

void setup(){

// Start the Serial connection Serial.begin(9600);

}

void loop(){

// Print the normal value and then a space Serial.print(analogRead(sensorPin));

Serial.print(" ");

// Print the mean filtered value and then a space Serial.print(readMean(sensorPin, 15));

Serial.print(" ");

// Print the median filtered value

Serial.println(readMedian(sensorPin, 15));

// Short delay for the Serial delay(100);

How it works…

There are hundreds of noise reduction filters, each with their own advantages and disadvantages. Here, we explore two very common and useful ones: the mean filter and the median filter. A filter takes some values and uses the relation between them to figure

out which one is closer to reality.

Noise for a Sharp IR, for example, can be a random reading of 80 cm when the object is at 25 cm. The sensor might continuously output 25 cm and then suddenly, just for one reading, output a glitch of 80 cm. This can cause catastrophic effects on any autonomous system that is critically dependent on the distance.

Let's look at each of the two algorithms individually.

Mean filter

The mean filter takes a few readings and then averages them. This generally reduces noise,

but at the expense of a lower response rate. Because we have to read multiple samples and average them, we increase the required time and decrease the overall response frequency. Most of the time, this is not a luxury and it is required. Good and slow values are much better than bad values.

1. We declare a function with two parameters: one will be the pin to which the sensor is connected, and the second one will be the number of samples:

int readMean(int pin, int samples){

2. Then we continuously read the analog input using a for loop while adding the values to a sum variable:

int sum = 0;

for (int i = 0; i < samples; i++){

sum = sum + analogRead(pin);

}

3. And here comes the averaging. We divide the sum of all samples by the number of samples, thus obtaining the average value:

sum = sum/samples;

In the end, we just return this averaged sum value using return sum. Median filter

The median filter is a bit more complicated but very powerful. It is much more responsive than the mean filter, as it doesn't manipulate the values in any way. It works on the assumption that noise will be both overshooting and undershooting. Overshooting means that the returned value is greater than the actual value while undershooting is the opposite.

The filter works by taking a number of samples, sorting them in ascending order, and then returning the central value. Usually, if the noise is roughly equal in both directions, the filtered

value will be the expected value.

1. As with the mean filter, we declare a function with a pin and a sample parameter:

int readMedian (int pin, int samples){

2. Then we declare an array to hold all the values that we also read in a for loop:

int raw[samples];

for (int i = 0; i < samples; i++){

raw[i] = analogRead(pin);

}

Here comes the important part—sorting the array. Here we use a bubble sort, the laziest of all sorting algorithms, but also the easiest to understand and implement. Take a look at the See also section for more information about it. The algorithm will return a sorted vector with the smallest samples at the beginning and the largest ones at the end of it.

Lastly, we return the value in the center, which should be very close to the expected value:

return raw[samples/2];

Main loop()

In the loop() function we just print the normal value, the mean filtered value, and the median filtered value, all using a serial connection:

Serial.print(analogRead(sensorPin)); Serial.print(" "); Serial.print(readMean(sensorPin, 15)); Serial.print(" "); Serial.println(readMedian(sensorPin, 15)); delay(100);

To test, set a sample size of 15 and run the code. Move an object in front of the sensor and

then copy the data and put it in chart-generating software such as Microsoft Office Excel. Vary the sample size until it fits your application.

See also

For more about the bubble sort algorithm, visit http://en.wikipedia.org/wiki/ Bubble_sort.