turion / rhine

Haskell Functional Reactive Programming framework with type-level clocks
http://hackage.haskell.org/package/rhine
121 stars 21 forks source link

Q: What if a computation is slower than the sample rate? #151

Closed freckletonj closed 3 years ago

freckletonj commented 3 years ago

Ex: a sample rate of 10 Hz, but the intervening computation takes say 3s?

Maybe I missed it in the paper, but I could see 2 solutions:

  1. Realtime solution : a resampling strategy that allows to "ignore clock ticks" if a separate thread is still computing a result from a previous tick. It could interpolate a result for a computation that we didn't have time to actually calculate.

  2. Simulation-time-accurate : A slow computation blocks the entire clock tree. So if you want to simulate 10 x 10Hz samples and each computation takes 3s, the Rhine will take 30s to run, but the result will in-spirit represent 1s of simulation.

turion commented 3 years ago

This is one of the ramifications that I unfortunately couldn't go into in the article, but it's an important question. It depends a bit on whether the computation can be run concurrently. If yes, you can use EventClock http://hackage.haskell.org/package/rhine-0.7.0/docs/FRP-Rhine-Clock-Realtime-Event.html. Also, it depends on whether your computation is slow because it blocks on some device access or because it needs to do heavy calculations. And it depends on what you want to achieve. Is it ok if the computation delays the clock, i.e. do we have to achieve the sample rate?

So maybe you can specify a particular use case, and then I can tell you more precisely what would be the right approach?

In the meanwhile I'm going to throw some ideas around how to achieve roughly what you proposed, maybe it's what you're looking for.

freckletonj commented 3 years ago

Awesome, that doesn't seem too bad! My usecase was initially just to digest the paper properly, but now knowing this isn't too tough, I may use it in robotics simulation and audio generation!

Thank you, I feel lucky to have this documentation to now refer back to :D

turion commented 3 years ago

Great :) feel free to come back for any questions. If your use case is open source, feel free to ping me on a PR if you run into trouble, and I can fix it there.

I'll close now. Reopen for further questions.