Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

sampling rate problem

Thread view: 
wanwan - 10 Nov 2006 19:07 GMT
I have a thread that records mouse positions at 100Hz, but I can't get
the desired sampling rate. The simplified code is:

static int mousepos_x, mousepos_y; // updated by another class
int[][] samples = new int [15000][2];
int count = 0;
....
public class SampleMouse implements Runnable {
....
   public void run () {
       while (running) {
          samples[count][0] = mousepos_x;
          samples[count++][1] = mousepos_y;
          Thread.sleep(10);
       }
   }
}

I also tried in the run method with another algorithm:

long starttime; // (class scope variable) in ns

while (running) {
   if (System.nanoTime() > starttime + count*10*1000000) {
       samples[count][0] = mousepos_x;
       samples[count++][1] = mousepos_y;
   }
}

I also tried setting the thread priority to max.

With any of the approaches, I get a discrepancy of over 20%

Does anyone have a good way to get the desired sampling rate?
Daniel Pitts - 10 Nov 2006 19:20 GMT
> I have a thread that records mouse positions at 100Hz, but I can't get
> the desired sampling rate. The simplified code is:
[quoted text clipped - 30 lines]
>
> Does anyone have a good way to get the desired sampling rate?

First, this is a threading nightmare.
You need to syncronize so that your reading of mousepos_x and
mousepos_y is atomic. Also, there is no guaranty that your other
thread's data will be accessible to this thread unless you syncronize.

It would ALSO probably be better for you to use the Timer class,
instead of busy-waiting.

The other suggestion I have for you is to instead use temporal event
based updates (calculating the change in time from the last position),
rather than polling every n milliseconds.

The real question I have for you is, why do you want to sample mouse
movement at all? Is it for some sort of macro? Just an excercise?
Something else that could be done using standard libraries instead of
writing your own sampling method?

Well, hope the suggestions help, good luck finding the right way.
wanwan - 10 Nov 2006 20:41 GMT
> First, this is a threading nightmare.
> You need to syncronize so that your reading of mousepos_x and
> mousepos_y is atomic. Also, there is no guaranty that your other
> thread's data will be accessible to this thread unless you syncronize.

I don't need the mouse positions to be precise to the pixel. I wrote
this class strictly for the purpose of data collection. Data analysis
is not done in real time.

> It would ALSO probably be better for you to use the Timer class,
> instead of busy-waiting.

Since I made the mouse sampling class a thread, it is doing something
independent of the rest of the software (I am writing a GUI). So I
thought it'd be same idea as Timer class

> The other suggestion I have for you is to instead use temporal event
> based updates (calculating the change in time from the last position),
> rather than polling every n milliseconds.

What is a temporal event? It sounds like interrupt vs polling in asm.
But what in Java can achieve the interrupt effect?

> The real question I have for you is, why do you want to sample mouse
> movement at all? Is it for some sort of macro? Just an excercise?
> Something else that could be done using standard libraries instead of
> writing your own sampling method?

It is a software used for engineering research purposes

> Well, hope the suggestions help, good luck finding the right way.
Thanks
Karl Uppiano - 10 Nov 2006 21:37 GMT
>> First, this is a threading nightmare.
>> You need to syncronize so that your reading of mousepos_x and
[quoted text clipped - 28 lines]
>> Well, hope the suggestions help, good luck finding the right way.
> Thanks

Since it is unlikely that you will ever get exact timing from sleep or timer
facilities in Java (they make disclaimers about precision and accuracy), it
is better to turn the problem on its head. You get mouse move events from
the system. Check the system time when you get the event, and calculate the
time between events.

I had a similar problem getting accurate timer delays while measuring wind
speed in a weather application that I wrote. I finally realized that I could
use the timer to provide reasonably periodic wake-up events, but I needed to
determine the actual interval since the last event and calculate the wind
velocity using the measured time interval. This works even when huge delays
due to other applications bogging down the system (which can cause delays of
several seconds in some cases). I wrote an article about it - want to read
it? Here it goes: http://mysite.verizon.net/Karl_Uppiano/winddata.html
Timothy Bendfelt - 10 Nov 2006 20:29 GMT
> I have a thread that records mouse positions at 100Hz, but I can't get
> the desired sampling rate. The simplified code is:
[quoted text clipped - 30 lines]
>
> Does anyone have a good way to get the desired sampling rate?

You have a tough problem on you hands.

Short of using the java realtime spec and a realtime OS this is not
something you can do reliably in java. It is also going to be OS and
harware dependent due to various thread scheduling policies.

I have had some success (~60Hz) on non-realtime OS by interfacing with
native timers via JNI.  On windows look at the multimedia timer, on linux
you might try the rtc timer. You are still going to have to contend with
scheduling jitter and GC whenever you cross the jni boundry.

If you are not concerned about jitter and want a high sampling rate
consider using a native solution (multimedia timer, rtc, hpet) to collect
the data at (near) the desired rate.  Deliver this data to the java
side of the application (via jni or some IPC like a socket) at a lower
frequency.  If ever your sampling thread crosses jni into the java side of
things you can kiss 100HZ behind.

If you are using linux and your sampling code is *very* tight you can get
decent scheduling stability with the SCHED_FIFO policy and the rtc timer.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.