I actually need two things:
- A square wave function that allows me to control its duty cycle, period and phase.
- It must work fast enough to be evaluated at least a million times per second.
For the first problem, I found a number of solutions. Some of them may be found here: How to create a rectangle wave (duty cycle $\ne$ 50 %)?
The best one that I tried yet is as follows:
squareWave[period_, duty_, phase_, x_] := Sign[TriangleWave[x/period - phase] - 1 + 2 duty]
Unfortunately, on my current machine it takes about 34 seconds to evaluate it 10^6 times. To compare with,
Sin[x] evaluates 10^6 times in 0.09 seconds.
Is it even possible to achieve my goal? And how?