5
$\begingroup$

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.

enter image description here

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]

enter image description here


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?

enter image description here

$\endgroup$
11
  • $\begingroup$ There's some good examples for triangle, sawtooth, and square waves here already: mathematica.stackexchange.com/a/38295/72682 $\endgroup$
    – flinty
    Nov 4, 2020 at 12:26
  • $\begingroup$ yes, I've read it. But the solution is only for square wave, not the rectangle function. $\endgroup$
    – Nam Nguyen
    Nov 4, 2020 at 12:28
  • $\begingroup$ Right, I mis-read it. You could still try a $\tanh(x)$ compression to get a smooth alternative to 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$
    – flinty
    Nov 4, 2020 at 13:03
  • $\begingroup$ yes, it seems that is the only solution right now. We don't have a solution for the whole range. :) Post your answer and I will accept it. @flinty $\endgroup$
    – Nam Nguyen
    Nov 4, 2020 at 13:07
  • 1
    $\begingroup$ Plug Mod[t/period, 1] in for t into box[..]. $\endgroup$
    – Michael E2
    Nov 4, 2020 at 15:25

2 Answers 2

4
$\begingroup$

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)}]

enter image description here

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}]

enter image description here

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}]

enter image description here

$\endgroup$
4
  • $\begingroup$ can this method (NIntegrate) work with a rectangle? Have you tried? $\endgroup$
    – Nam Nguyen
    Nov 4, 2020 at 13:34
  • $\begingroup$ Your update is a square wave function (with a width of 2). Can you plot a smooth version of a rectangle function with 2 widths: a, b. $\endgroup$
    – Nam Nguyen
    Nov 4, 2020 at 14:56
  • 1
    $\begingroup$ @NamNguyen It is OK now? $\endgroup$
    – cvgmt
    Nov 4, 2020 at 15:18
  • $\begingroup$ Yes, your method is great. Thank you. $\endgroup$
    – Nam Nguyen
    Nov 4, 2020 at 15:21
3
$\begingroup$

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}]

piecewise linear plot 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]

one period with 20% duty cycle. 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]

periodic smooth square wave

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]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.