There are some answers on how to get a smooth squarewave function. But I would like to have a smooth boxcar function or rectangle function with 2 different widths.: wup
, and wdown
One solution is the Fourier Transform, but I prefer having an approximation with a smoothness factor.
Thank you very much.
Here is the non-smooth version. I would like to smooth it. Note that: this is not a square wave function (the smooth version is known the solution. This is the boxcar function with 2 widths: up width is 2, and down width is 3.
pulse[h_, wup_, wdown_, t_] :=
With[{period = wup + wdown, duty = wup/(wup + wdown)},
h*UnitBox[Mod[t/period, 1.]/(2. duty)]]
Plot[{pulse[1, 2, 3, t]}, {t, 0, 10}, Exclusions -> None]
Square Wave Pulse with Uniform Width:
I can do a smooth version only for a square wave function, with a uniform width, but not the rectangle function.
smoothPulse[ePulse_, wup_, wdown_, smoothness_, t_] :=
ePulse/2 + ePulse/\[Pi]*ArcTan[Sin[\[Pi] t/wup]*smoothness]
Rectangle Pulse with 2 differents widths:
I don't know the smooth version. I tried to build a smooth UnitBox version.
smoothunitbox[t_, sharpness_] :=
Piecewise[{{Tanh[sharpness*(2 t + 1)] + 1,
t < 0}, {1 - Tanh[sharpness*(2 t - 1)], t >= 0}}]/2
smoothRectangle[h_, wup_, wdown_, smoothness_, t_] :=
With[{period = wup + wdown, duty = wup/(wup + wdown)},
h*smoothunitbox[Mod[t/period, 1.]/(2. duty), smoothness]]
Plot[smoothRectangle[1, 2, 4, 20, t], {t, 0, 10}, Exclusions -> None]
But the smooth is not good. How to improve?
UnitBox
like in my now deleted answer.smoothbox[t_, sharpness_] := Piecewise[{ {Tanh[sharpness*(2 t + 1)] + 1, t < 0}, {1 - Tanh[sharpness*(2 t - 1)], t >= 0}}]/2
and plot with:Plot[{UnitBox[t], smoothbox[t, 15]}, {t, -2, 2}, Exclusions -> None]
however you would have to convert your pulse train to centers and widths first. $\endgroup$Mod[t/period, 1]
in fort
intobox[..]
. $\endgroup$