Page 1 of 1

Median or mode filter

Posted: Tue Sep 26, 2017 2:45 pm
by stefan_FVT
Hi,
I'm sampling the ultrasonic sensor but the values are a bit noisy and I would like to calculate the median or mode value for 10 measurements with each measurement taken with half a second apart.

I couldn't find any function for this in the library and I've tried importing Arduino libraries with errors. If someone have a filter or a code snippet that works I would be thankful!

/Stefan

Re: Median or mode filter

Posted: Wed Sep 27, 2017 10:30 am
by libelium-dev
Hi Stefan,

You can calculate the average for 10 values developing a code like this

Code: Select all

  int sum = 0;
  for ( int i=0; i<10; i++){    
    // get distance     
    dist = Agriculture.getDistance();  // this function will be different depending on your sensor board
  
    sum = sum + dist;
    delay(1000);
  }
  
  // calculate average
  float average = sum/10;
Regards

Re: Median or mode filter

Posted: Wed Sep 27, 2017 11:41 am
by stefan_FVT
I don't want to use an average value since it will include the noise.

I rather use a median or mode to exclude abnormal readings.

Is there a way of doing this?

Re: Median or mode filter

Posted: Thu Sep 28, 2017 8:39 am
by libelium-dev
Hi,

What formula do you want to implement?

Regards