I'm trying to model a pulse waveform in my application and I need a way to keep track of the pulses so that I can repeat their sequence. From the figure shown below, what I'd like to do is simulate the first three pulses (pulse 1-3), then simulate pulse 4 immediately after pulse 3, and simulate pulse 5 immediately after 4. Then repeat the whole sequence N times.
As shown in the diagram, I have the interval in seconds, the start time of the first pulse in seconds, and the duration of each pulse also in seconds. My application will be running real time in a run loop where it will be executing at 1 Hz.
My question is, how do I keep track of all the pulses and make sure they are all simulated with respect to each other? What I mean by simulated is, for example I'd like to print a simple statement during the duration of the 1st three pulses, and the same for pulse 4 and 5. Can someone suggest a pseudo algorithm at the very least for this operation?
Assuming I understood the problem correctly, the way I would do it is to use modulo-arithmetic, and characterize each pulse-train as a boolean function with a timestamp as a parameter to the function, e.g.:
The benefit of doing it this way is that you can simulate an infinite series of pulses while using only a small, fixed amount of memory. It also allows you to efficiently query what the expected state of each pulse-train was/will-be at any past/future time (i.e. not just as at the current time), should you need to do so.
Here's a simple demonstration program that prints out a ticker-tape of four pulses over the course of 100 simulated "seconds":
The output looks like this:
Defining the class
sequence
as follows, we can simply check activity of each pulse bysequence::isActive
. DEMO is here.