Equation for a smooth staircase function

$\begingroup$

I am looking for a smooth staircase equation $f(h,w,x)$ that is a function of the step height $h$, step width $w$ in the range $x$.

I cannot use the unit step or other similar functions since they are just one step. I have been experimenting with various sigmoid curves and while I can get a single smooth step I cannot get to realize the staircase shape. The closest staircase function I have found is given in this paper in equation (18) and depicted in Fig. 4 and it is a close example of what I want (i.e generate a staircase in the range $x$ for arbitrary step heights and widths) but it is not smooth at all.

Regarding smooth steps, a likely starting point I found is here but it gives a smooth function of just a single step. I have been unable to modify the equation to make it into a staircase. I would like to specify arbitrary step heights and widths and generate a smooth staircase in the range $x$ specified.

Edit (Extra info): The smooth function I mention above has the problem that the upper, horizontal line is not equal in length to the lower, horizontal line which is why I have been unable to adapt it into a staircase function

Edit 2 Including some pictures What I'm gettingDesired function

Edit 2 Plot of $s$ with a steep slope showing a different width on the first horizontal lineenter image description here

$\endgroup$ 1

6 Answers

$\begingroup$

We can start with a simple soft staircase function:

$$ f(x) = x - sin \space x $$

and then feed it into itself:

$$ y(x) = f(f(x)) $$

then again:

$$ y(x) = f(f(f(x))) $$

and again:

$$ y(x) = f^4(x) $$

As you can see, each iteration makes the "flat" part of the step longer, and the rise steeper.

enter image description hereenter image description hereenter image description hereenter image description here

The period and the height of each step is $ 2 \pi $, so multiply $x$ by $2 \pi / w$ and $y$ by $h / 2 \pi$ to reach your desired scale.

In reality, the curve is only truly flat (zero derivative) at the centre of each step — at every $ 2 \pi k $ — and only close to flat on either side of that point.

Configurability is limited: The softness of the step can only be specified in integer amounts (the number of times we reapply $f$ to itself), and it requires many/infinite applications to make the step really sharp.

$\endgroup$ 1 $\begingroup$

Here is an example based on Math536's answer: Wolfram link

$$f(h,w,a,x) = h \left[\frac{\tanh \left( \frac{ax}{w}-a\left\lfloor \frac{x}{w} \right\rfloor-\frac{a}{2}\right)}{2\tanh\left(\frac{a}{2}\right) } + \frac{1}{2} + \left\lfloor \frac{x}{w} \right\rfloor\right]$$

Where h is the step height, w is the period, and a is the smoothness

$\endgroup$ $\begingroup$

Let $s : [0,1] \to [0,1]$ be a smooth function representing a single step. Assume that there exists some $\epsilon > 0$ such that $s(x) = 0$ for all $x < \epsilon$ and $s(x) = 1$ for all $x > 1 - \epsilon$. Setting $$ f(x) = s(x - \lfloor x \rfloor) + \lfloor x \rfloor$$ then gives us a smooth staircase with steps of height and width $1$. By rescaling $f$, we can get steps of arbitrary width $w$ and height $h$: $$f(h,w,x) = h f(x/w) = h(s(x/w - \lfloor x/w \rfloor) + \lfloor x/w \rfloor).$$

$\endgroup$ 9 $\begingroup$

I used the following function, where the width of the flat part of each step is b and the width of the curved part is c, and each step is 1 unit tall:

$$ f(x) = \lfloor \frac{x}{b+c} \rfloor + \begin{cases} 0, & \text{if } x-(b+c) \lfloor \frac{x}{b+c} \rfloor, \\ s(\frac{x - (b+c) \lfloor \frac{x}{b+c} \rfloor - b}{c}) , & \text{otherwise}. \end{cases} $$

I used $\cos$ for my stepping function:

$$s(u) = \frac{1}{2} - \frac{1}{2} \cos (\large{\pi} \small{u})$$

It might look a bit simpler in Javascript code, using some temporary variables and modulus (%):

function stair (x, b, c) { const width = b + c; const base = Math.floor(x / width); // base of this step const o = x % width; // offset, between 0 and width return base + (o < b ? 0 : step((o - b) / c));
}
function step (u) { return 0.5 - 0.5 * Math.cos(Math.PI * u);
}

although Javascript's % restricts us: x must be non-negative, and b and c must be integers .

Those restrictions can be lifted if we change how o is calculated:

 const o = x - width * base;

In Gnuplot you can experiment using:

gnuplot> f=4; c=1; plot [x=0:20] floor(x/(f+c)) + ((floor(x)%(f+c))<f ? 0 : 0.5-0.5*cos(((x - (f+c)*floor(x/(f+c))) - f) * pi/c))

enter image description here

$\endgroup$ $\begingroup$

You can create a smooth staircase function $f(h, w, x, t)$, which starts of as a straight line at time $t=0$, and gets progressively more pronounced steps as times passes, by solving the ordinary differential equation

$$\frac{\operatorname{d}f(h, w, x, t)}{\operatorname{d}t} = -\sin\left(2\pi \frac{f(h, w, x, t)}{h}\right)h$$

with the boundary condition

$$f(h, w, x, 0) = \frac{h\,x}{w}.$$

The solution to this ODE is

$$ f(h, w, x, t) = \begin{cases}%{ll} f(h, w, x, 0), && \text{if } \displaystyle\frac{x}{w} + 0.5 \in \mathbb{Z}\\ \displaystyle h\left(\frac{\displaystyle \tan^{-1}\left[s\,\tan\left(\frac{\pi\,x}{w}\right)\right]}{\pi} + \left\lfloor \frac{x}{w} + \frac{1}{2} \right\rfloor\right), && \text{otherwise} \end{cases}, $$

where $s=e^{-t}$, which can be interpreted as a smoothness parameter, so we can make $f$ a function of $s$ instead of a function of $t$, i.e., $f(h, w, x, s)$. $s=0$ corresponds to $t=\infty$ and means no smoothness at all (perfectly sharp steps) and $s=1$ corresponds to $t=0$ and means maximum smoothness (a straight line). Values of $s$ greater than one corresponds to negative values of $t$, and will create a basically the same type of staircase but shifted half a step. (The first case in the bracket has been added because the second case is undefined in the middle of a step since $\tan$ becomes singular at those points.)

Plots from Wolfram Alpha:

enter image description here

$\endgroup$ $\begingroup$

Stair cases of Height=H and Width =W may be represented by:

$$S\left(x\right)=H\left(\frac{x}{W}+\frac{1}{2}-\frac{\arctan\left(\tan\left(\pi\left(\frac{x}{W}+\frac{1}{2}\right)\right)\right)}{\pi}\right)$$

Here you can observe

$\endgroup$ 2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like