Inspiration

For some tremor therapies, when a stimulus lands matters as much as whether it lands. Research on phase-dependent stimulation suggests a pulse at the right point of the tremor cycle can damp the tremor, and at the wrong point can make it worse. The timing budget is tight:

$$\Delta\phi = 360^\circ \times f \times \Delta t$$

At a tremor frequency \(f = 5\) Hz, being late by \(\Delta t = 1\) ms already costs 1.8° of phase. So the computer driving such a device must never be the reason a pulse lands at the wrong moment, even when it is busy. That is exactly what a real-time OS promises, and we wanted to measure it instead of assuming it.

What it does

qnx-phaselock is a three-thread controller on QNX 8.0:

Thread Priority Job
sensor 60 samples the tremor every 1 ms
estimator 50 predicts when the tremor next reaches its peak (90°)
stim 55 sleeps until that moment and fires

Because the reference phase is known, every stimulus's error splits exactly into two parts:

  • estimator error: the prediction was wrong
  • timing error: the OS woke up late

It runs on synthetic tremor and on six real Parkinson's recordings from PhysioNet. Nothing is ever connected to a person: "firing" records a timestamp.

How we built it

  • QNX SDP 8.0, C compiled with qcc, running the QNX x86_64 QEMU image on WSL2.
  • QEMU icount mode, where virtual time counts executed instructions, so every measurement repeats and the laptop's own load can't leak in.
  • QNX instrumented kernel tracing (tracelogger, traceprinter) to see what each thread was actually doing.
  • A QNX 8 high-resolution timer (timer_create with TIMER_TOLERANCE) for the stimulus thread.
  • Pre-registered tests: every experiment had its pass/fail rule written in a public engineering log before it ran, including the ones that failed.

Results

Question Answer
Are the measurements repeatable? Runs agree within 0.44%, separate boots within 6 ns; host load moves them by 0.35%.
Do priorities matter under CPU load? With real-time priorities, 0 stimuli missed. Left at default priorities, 15–20% missed (80 in the demo run).
Why are stimuli missed? The kernel trace shows the stimulus thread sitting ready for a median 2.0 ms, up to one 4 ms round-robin time slice, behind a CPU hog. With real-time priority it waits 1.7 µs.
How late is QNX itself? Ordinary sleeps: up to about 2°. The high-resolution timer: 0.009°, about 4 µs.
What's left? The phase estimator, at 9°–113° (p95) on real tremor. The OS is no longer the limit.

Challenges we ran into

  • Our first repeatability test failed. Run-to-run offsets jumped between 73 µs and 926 µs. The cause was QNX aligning ordinary sleeps to its 1 ms clock period; once deadlines were aligned to it, a newly pre-registered retest passed. The failed test stays in the log.
  • Emulated time behaves strangely. In icount mode an idle QNX's clock races ahead (it counted 1,475 s in 10 real seconds), so SSH logins timed out mid-password. All file transfers had to happen in the normal VM.
  • QNX 8 is tickless. We tried to shrink the clock period with ClockPeriod() and got "Not supported". QNX 8 removed run-time period changes, which led us to its high-resolution timers instead, and to the biggest improvement in the project.
  • Two emulated CPUs added lateness that vanished on one CPU. The trace placed it before the thread becomes ready, which fits QEMU taking turns between virtual CPUs.
  • Better estimators did worse. A calibrated endpoint-corrected Hilbert transform, a band-pass zero-crossing method and a phase-locked loop were all tuned on half the recordings, and all lost to our simple estimator. We recorded that as a negative result, and the held-out half is still untouched.

What we learned

On this workload, priorities decide whether stimuli are missed, the timer type decides how late they are, and on real patient data the estimator, not the OS, sets the accuracy. Writing the pass rule down before each test kept us honest when the data disagreed with us.

What's next

  • Run on real hardware (Raspberry Pi 5) with an output pin and an oscilloscope.
  • Build an estimator that beats the simple one, tested once on the held-out recordings.

Built with: QNX SDP 8.0 · QNX OS 8.0.0 · C · qcc · QEMU · WSL2 · Python · NumPy · SciPy · PhysioNet tremordb (ODC-By 1.0)

Repo: https://github.com/Watcher-ux/qnx-phaselock

Built With

Share this project:

Updates

Submission history