sampling rate problem

W

wanwan

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?
 
D

Daniel Pitts

wanwan said:
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?

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.
 
T

Timothy Bendfelt

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?

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.
 
W

wanwan

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
 
K

Karl Uppiano

wanwan said:
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.

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

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

It is a software used for engineering research purposes

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top