Hi Frank,
you are quite right.
$Mod$ also seems to cause problems. As you say:
DutyCycle[d_, Ts_, t_] := Piecewise[{{1, 0 <= Mod[t, Ts] <= d Ts}}];
does not seem to work. And
DutyCycle[d_, Ts_, t_] := Ceiling[-(Mod[t, Ts] - Ts d)]
does not work either. So there is a problem with
$Mod$. But
DutyCycle[d_, Ts_, t_] := HeavisideTheta[Sin[2 Pi/Ts*t + Pi*d] - Sin[Pi*d]];
and
DutyCycle[d_, Ts_, t_] := If[Sin[2 Pi/Ts*t + Pi*d] - Sin[Pi*d] > 0, 1, 0];
do work; in spite of the latter having the "If". Curiously enough,
DutyCycle[d_, Ts_, t_] := HeavisideTheta[SawtoothWave[t/Ts - d] - (1 - d)];
does not work.
In the following case, however, there is no
$Mod$ and things still go wrong:
DutyCycle[d_, Ts_, t_] := If[IntervalMemberQ[IntervalUnion @@ Table[Interval[{k Ts, Ts d + k Ts}], {k, 0, 4}], t], 1, 0];
This last one, I couldn't even make work with Piecewise, i.e.
DutyCycle[d_, Ts_, t_] := Piecewise[{{1, IntervalMemberQ[IntervalUnion @@ Table[Interval[{k Ts, Ts d + k Ts}], {k, 0, 4}], t]}}];
does not work either.
DutyCycle[d_, Ts_, t_] := Evaluate[Piecewise[Flatten[Table[{{1, (k - 1) Ts < t < (k + Td) Ts}, {0, (k + Td) Ts < t < (k + 1) Ts}}, {k, -2,3} ], 1]]];
does work, but
DutyCycle[d_, Ts_, t_] := Piecewise[Flatten[Table[{{If[Evaluate[0 < Mod[t, Ts] < d Ts], 1, 0], (k - 1) Ts < t < (k + d) Ts}, { If[Evaluate[0 < Mod[t, Ts] < d Ts], 1, 0], (k + d) Ts < t < (k + 1) Ts}}, {k, -2, 3} ], 1]];
does not - making the case against
$Mod$ even stronger.
If I have not made a mistake, all of these functions should be identical in the interval of interest, say from
$0$ to
$nc Ts$, which you can test by running
Plot[{DutyCycle[Td, Ts, t]}, {t, -nc Ts, 2 nc Ts}]
--- some work some don't.
Cheers,
Marco
PS: I had some other examples of writing this function, but unfortunately, the session died on me when I wanted to post.