MeanFilter does'nt work as expected

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

peter14
New User
Posts: 3
Joined: Wed Feb 28, 2018 6:39 pm

MeanFilter does'nt work as expected

Postby peter14 » Wed Mar 07, 2018 6:49 am

Hi,
I want to get the median result from 5 samples.
The following code does not work on EV3 with the standard ultrasonic sensor:

EV3UltrasonicSensor usSe = new EV3UltrasonicSensor(SensorPort.S2);

SampleProvider usSp = usSe.getDistanceMode();
SampleProvider medSp = new MedianFilter(usSp, 5);

float[] usBuffer = new float[usSp.sampleSize()];
usSp.fetchSample(usBuffer, 0);
for(float val:usBuffer ) System.out.print(val + " ");
System.out.println();

float[] medBuffer = new float[medSp.sampleSize()];
medSp.fetchSample(medBuffer, 0);
for(float val:usBuffer ) System.out.print(val + " ");

The output is:
0.582
0.582

The first one is what expected. The second one not when i turn the robot around while running.
When I trace into the sources I see, that the sample size is allways (Median, Mean, ...) 1. Thus only the first element of the buffer is written.

Can you help / explain?
Last edited by peter14 on Wed Mar 07, 2018 8:06 am, edited 1 time in total.

User avatar
gloomyandy
leJOS Team Member
Posts: 6048
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: MeanFilter does'nt work as expected

Postby gloomyandy » Wed Mar 07, 2018 9:43 am

I don't think the median filter works at all (I'm not sure it is actually possible to have a general "rolling" median filter, but please feel free to prove me wrong), but the mean filter should be fine.
leJOS news https://lejosnews.wordpress.com/

peter14
New User
Posts: 3
Joined: Wed Feb 28, 2018 6:39 pm

Re: MeanFilter does'nt work as expected

Postby peter14 » Thu Mar 08, 2018 7:57 pm

Thanks a lot for your response.
I tried the mean one to check ...
In fact I replaced the median filter for the mean one:

EV3UltrasonicSensor usSe = new EV3UltrasonicSensor(SensorPort.S2);

SampleProvider usSp = usSe.getDistanceMode();
SampleProvider meanSp = new MeanFilter(usSp, 5);

float[] usBuffer = new float[usSp.sampleSize()];
usSp.fetchSample(usBuffer, 0);
for(float val:usBuffer ) System.out.print(val + " ");
System.out.println();

float[] meanBuffer = new float[meanSp.sampleSize()];
meanSp.fetchSample(meanBuffer, 0);
for(float val:meanBuffer ) System.out.print(val + " ");

Result (as reminded, but I was not sure, so I better checked):

0.47100002
0.47100002


The different values result from different locations of the robot

Observation: same issue
Guess: Either I do'nt understand your filtering concept, or something is wrong with it.

User avatar
gloomyandy
leJOS Team Member
Posts: 6048
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: MeanFilter does'nt work as expected

Postby gloomyandy » Thu Mar 08, 2018 8:20 pm

You are only fetching a single sample, so the mean will be the same as the actual value. You need to fetch more samples for the mean to kick in, once you have more than the specified "window size" in your case I think it is is 5 samples then the mean will giving you a moving average over the last n samples. Up until that pint you will get a mean of 1, 2, 3, 4, 5 values.

Please see:
https://lejosnews.wordpress.com/2014/01 ... framework/
https://lejosnews.wordpress.com/2015/01 ... erometers/
https://sourceforge.net/p/lejos/wiki/Se ... Framework/

If you look at the source then things become pretty obvious...
https://sourceforge.net/p/lejos/ev3/cod ... ilter.java
https://sourceforge.net/p/lejos/ev3/cod ... ilter.java
leJOS news https://lejosnews.wordpress.com/

peter14
New User
Posts: 3
Joined: Wed Feb 28, 2018 6:39 pm

Re: MeanFilter does'nt work as expected

Postby peter14 » Sat Mar 10, 2018 7:52 am

Ahh, I understand, but was not able to retrieve it from code or doc at time of posting .
I will try it asap and see ...


Return to “EV3 Software”

Who is online

Users browsing this forum: gloomyandy and 1 guest