Mathematica Asked on April 28, 2021
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?
use the mollifier
in mathematics. It also work for the Piecewise
function.
Reply the comment
a = 2;
b = 3;
S[x_ /; 0 <= x <= a] := 1;
S[x_ /; a <= x <= a + b] := 0;
S[x_ /; x >= a + b] := S[x - (a + b)];
S[x_ /; x <= a + b] := S[x + a + b];
ρ = 1/NIntegrate[Exp[-1/(1 - x^2)], {x, -1, 1}];
φ[x_, ϵ_] = (ρ/ϵ)* Piecewise[{{Exp[-ϵ^2/(ϵ^2 - x^2)], -ϵ < x < ϵ}}];
Plot[S[x], {x, -2 (a + b), 2 (a + b)}]
Plot[NIntegrate[φ[t - x, .3]*S[x], {x, -2 (a + b),
2 (a + b)}], {t, -2 (a + b), 2 (a + b)}]
Updated
f[x_] = Piecewise[{{1, 0 <= x <= 2}}];
s[x_] = f[Mod[x, 4, -2]];
ρ = 1/NIntegrate[Exp[-1/(1 - x^2)], {x, -1, 1}];
φ[x_, ϵ_] = (ρ/ϵ)*Piecewise[{{Exp[-ϵ^2/(ϵ^2 - x^2)], -ϵ < x < ϵ}}];
Plot[s[x], {x, -4, 4}]
Plot[NIntegrate[φ[t - x, .3]*s[x], {x, -4, 4}], {t, -4, 4}]
Original
But I don't know how to smooth the rectangle, I only try to smooth the Abs
function.
g[x_] = Piecewise[{{x, 0 <= x <= 1}, {-x, -1 <= x <= 0}}];
h[x_] = g[Mod[x, 2, -1]];
Plot[h[x], {x, -4, 4}]
ρ = 1/NIntegrate[Exp[-1/(1 - x^2)], {x, -1, 1}];
φ[x_, ϵ_] = (ρ/ϵ)*Piecewise[{{Exp[-ϵ^2/(ϵ^2 - x^2)], -ϵ < x < ϵ}}];
Plot[NIntegrate[φ[t - x, 1/8]*h[x], {x, -4, 4}], {t, -4,4}]
Correct answer by cvgmt on April 28, 2021
Suppose you want the square wave high 20% of the time. The following helps.
DutyCycle = 0.2; Plot[Piecewise[{{x/(2 DutyCycle),
x < DutyCycle}, {(1 - 2 DutyCycle + x)/(2 - 2 DutyCycle),
DutyCycle < 1}}], {x, 0, 1}]
Based on that we can make one period of our smooth square-wave by doing this.
singlePeriod[t_, Smoothness_, DutyCycle_] := Piecewise[{
{ArcTan[Sin[2 [Pi] t/(2 DutyCycle)]*Smoothness]/
ArcTan[Smoothness], t < DutyCycle},
{ArcTan[Sin[[Pi] (1 - 2 DutyCycle + t)/(1 - DutyCycle)]*Smoothness]/
ArcTan[Smoothness], DutyCycle < 1}
}]; Plot[singlePeriod[t, 12, 0.2], {t, 0, 1}, PlotRange -> All,Exclusions -> None]
Make the above periodic using this:
smoothPulse[t_,Smoothness_,DutyCycle_]:=singlePeriod[Mod[t,1],Smoothness,DutyCycle]; Plot[smoothPulse[t,12,0.2],{t,0,4},PlotRange->All,Exclusions->None]
With appropriate use smoothPulse above, we can change the high-value, low-value, period, and phase of a smoothPulse. Here is an example:
Plot[2.5+2.5*smoothPulse[12t+0.2,12,0.2],{t,0,0.33333},PlotRange->All,Exclusions->None]
Answered by Ted Ersek on April 28, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP