MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

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

Moderators: roger, gloomyandy, skoehler

MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby Programus » Sun May 18, 2014 8:49 am

I built a robot with EV3 ultrasonic sensor. And I copied the code below from wiki page: http://sourceforge.net/p/lejos/wiki/Sensor%20Framework/
Code: Select all
// get a port instance
Port port = LocalEV3.get().getPort("S2");

// Get an instance of the Ultrasonic EV3 sensor
SensorModes sensor = new EV3UltrasonicSensor(port);

// get an instance of this sensor in measurement mode
SampleProvider distance= sensor.getModeName("Distance");

// stack a filter on the sensor that gives the running average of the last 5 samples
SampleProvider average = new MeanFilter(distance, 5);

// initialise an array of floats for fetching samples
float[] sample = new float[average.sampleSize()];

// fetch a sample
average.fetchSample(sample, 0);


I found that once I got a Infinity value, I would always get Infinity or NaN instead of a meaningful number.
I investigated the source of MeanFIlter, and found the problem might be in SumFilter where the result is calculated by substracting from or add to sum. Once the sum became Infinity or NaN, the result would be the same forever.
Code: Select all
    // substract oldest sample from sum
    getOldest(buf, 0);
    for (int i = 0; i < sampleSize; i++) {
      sum[i] -= buf[i];
    }
    super.fetchSample(buf, 0);
    // add the newest sample to sum
    for (int i = 0; i < sampleSize; i++) {
      sum[i] += buf[i];
      sample[i + off] = sum[i];
    }


I am not sure whether this problem had been solved in 0.8.1 beta. If not, hope this could be solved in the future version.
User avatar
Programus
Novice
 
Posts: 34
Joined: Fri Sep 18, 2009 1:43 pm
Location: China

Re: MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby gloomyandy » Sun May 18, 2014 8:58 am

You can fix the problem yourself by creating another filter to either filter out the NaN, or to convert the NaN into some other value. If you do that you could provide some input on the best way to handle this situation (and we might add the NaN filter to the set of filters). Perhaps some sort of limit filter that allows you to replace any values >= to a limit (or <= to a limit), with the limit might be the way to go (with an option to simply ignore the value if outside the range).
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4083
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby Aswin » Mon May 19, 2014 6:22 am

This is a bug. The filter should start returning valid numbers once the NaN's are dropped from the buffer.
I'll take care of this.

On a side note. The US-sensor uses the value of 255 as an out-of-range indication. LeJOS does not, it uses NaN to indicate out-of-range. The reason for this is that other range sensors might have a bigger range, for those sensors 255 will be a valid measurement.
When using the statistical filters one should be aware of the way these filters treat the NaN value. The sum and mean statistics will return a NaN when one of the samples in the buffer is a NaN. The min, max and median filters will ignore the NaN values in the buffer and still return a valid value when there is at least one number in the buffer.

Aswin
My NXT blog: http://nxttime.wordpress.com/
Aswin
leJOS Team Member
 
Posts: 203
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands

Re: MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby Programus » Mon May 19, 2014 12:37 pm

Aswin wrote:This is a bug. The filter should start returning valid numbers once the NaN's are dropped from the buffer.
I'll take care of this.

On a side note. The US-sensor uses the value of 255 as an out-of-range indication. LeJOS does not, it uses NaN to indicate out-of-range. The reason for this is that other range sensors might have a bigger range, for those sensors 255 will be a valid measurement.
When using the statistical filters one should be aware of the way these filters treat the NaN value. The sum and mean statistics will return a NaN when one of the samples in the buffer is a NaN. The min, max and median filters will ignore the NaN values in the buffer and still return a valid value when there is at least one number in the buffer.

Aswin


I agree with this spec. It makes sense. :D
User avatar
Programus
Novice
 
Posts: 34
Joined: Fri Sep 18, 2009 1:43 pm
Location: China

Re: MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby Programus » Mon May 19, 2014 12:40 pm

gloomyandy wrote:You can fix the problem yourself by creating another filter to either filter out the NaN, or to convert the NaN into some other value. If you do that you could provide some input on the best way to handle this situation (and we might add the NaN filter to the set of filters). Perhaps some sort of limit filter that allows you to replace any values >= to a limit (or <= to a limit), with the limit might be the way to go (with an option to simply ignore the value if outside the range).


Thank you for your advice.
Making my own filter is an alternative. I didn't do so because it is not really matter whether I use a filter or use the raw value, actually. And I am lazy. :P
User avatar
Programus
Novice
 
Posts: 34
Joined: Fri Sep 18, 2009 1:43 pm
Location: China

Re: MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby Aswin » Mon May 19, 2014 9:56 pm

Bug Fixed.

If you are lazy you'll have to wait till the next release. If not you can already safely use the Median filter as an alternative.
My NXT blog: http://nxttime.wordpress.com/
Aswin
leJOS Team Member
 
Posts: 203
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands

Re: MeanFilter returns NaN once get a Infinity - 0.8.0 alpha

Postby Programus » Tue May 20, 2014 1:40 pm

Aswin wrote:Bug Fixed.

If you are lazy you'll have to wait till the next release. If not you can already safely use the Median filter as an alternative.


Thank you for fixing it. I believe it would not be too long for the next release, so I would like to wait. You know, I am lazy. :P
User avatar
Programus
Novice
 
Posts: 34
Joined: Fri Sep 18, 2009 1:43 pm
Location: China


Return to EV3 Software

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests

more stuff