sampling rate problem

Discussion in 'Java' started by wanwan, Nov 10, 2006.

  1. wanwan

    wanwan Guest

    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?
     
    wanwan, Nov 10, 2006
    #1
    1. Advertising

  2. wanwan

    Daniel Pitts Guest

    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.
     
    Daniel Pitts, Nov 10, 2006
    #2
    1. Advertising

  3. 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.
     
    Timothy Bendfelt, Nov 10, 2006
    #3
  4. wanwan

    wanwan Guest


    > 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
     
    wanwan, Nov 10, 2006
    #4
  5. wanwan

    Karl Uppiano Guest

    "wanwan" <> wrote in message
    news:...
    >
    >> 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
     
    Karl Uppiano, Nov 10, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    Over-Sampling

    ALuPin, Mar 11, 2005, in forum: VHDL
    Replies:
    5
    Views:
    1,935
  2. Albretch
    Replies:
    0
    Views:
    312
    Albretch
    Nov 29, 2004
  3. Marco

    sampling rate

    Marco, Aug 21, 2006, in forum: VHDL
    Replies:
    3
    Views:
    776
    Thomas Fischer
    Aug 24, 2006
  4. Sevana Oy
    Replies:
    0
    Views:
    1,144
    Sevana Oy
    May 6, 2011
  5. Syed Adnan Ahmed

    WEb Services that gets the Interest Rate USA (Morgage rate)

    Syed Adnan Ahmed, Sep 4, 2003, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    386
    Syed Adnan ahmed
    Sep 8, 2003
Loading...

Share This Page