Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > sampling rate problem

Reply
Thread Tools

sampling rate problem

 
 
wanwan
Guest
Posts: n/a
 
      11-10-2006
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?

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      11-10-2006

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

 
Reply With Quote
 
 
 
 
Timothy Bendfelt
Guest
Posts: n/a
 
      11-10-2006



On Fri, 10 Nov 2006, wanwan wrote:

> 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.
 
Reply With Quote
 
wanwan
Guest
Posts: n/a
 
      11-10-2006

> 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

 
Reply With Quote
 
Karl Uppiano
Guest
Posts: n/a
 
      11-10-2006

"wanwan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
>
>> 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


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Audio Compression Advisor online recommendations on bit rate and sampling rate selection Sevana Oy VHDL 0 05-06-2011 12:43 PM
Adobe Audition 1.5 allows WMA monoaural audio at 44.1 KHz sample-rate with a CBR bit-rate of 20 kbps Radium Computer Support 1 07-18-2007 08:51 PM
question- recording analog stereo music signal onto DVDR HDD- what's the resolution/sampling rate/bit rate ? musicman DVD Video 0 12-29-2006 12:42 PM
sampling rate Marco VHDL 3 08-24-2006 11:04 AM
I have a series of CDs that I have ripper to MP3 - there speach - so I used a low sampling rate... Marc Computer Support 22 01-22-2004 04:13 PM



Advertisments