How do I approximate a staircase function (rounding) with triangle wave?

3k Views Asked by At

enter image description here

The rounding function may look like this. I think I might be able to simulate this function with $\text{trianglewave}(x)+x$, some thing like that, but I don't know how to do it.

My final goal is to come up with any functions that can approximate this rounding function while it is differentiable (a smooth approximation).

3

There are 3 best solutions below

2
On BEST ANSWER

Here's a suggestion. Set $n$ to a large constant value, and let \begin{align} h(t,c) &= \frac{n}{\sqrt\pi} e^{-n^2 (t-c)^2}, \\ g(t) &= \sum_{k=-\infty}^\infty h\left(t,k+\tfrac12\right), \\ f(x) &= \int_0^x g(t)\,dt \\ &= \frac12 \sum_{k=0}^\infty \left(\mathrm{erf}\left(n \left(x-k-\tfrac12 \right)\right) + \mathrm{erf}\left(n \left(x+k+\tfrac12 \right)\right)\right). \end{align} For $n=100$, the function $f$ has a graph like the one this link produces, except that it continues stepping up or down indefinitely in each direction. (In the linked graph, only the first few terms of $f(t)$ are counted.)

This graph has none of the extra little "bumps" that the Fourier series has, and it is differentiable.

3
On

As pointed out in the comment, you can not approximate a discontinuous function with a continuous one; unless an infinite series is used. The triangle wave in some sense can be considered as a "sharpened" sine function. You can plot $\sin(2\pi x)$ and TriangleWave[x] (referred as $T(x)$ hereafter) in Mathematica to see what I mean. And $T(n x)$ form an orthogonal basis just like $\sin(2 \pi n x)$ over interval $[0, 1]$.

Then you can expand any periodic function in terms of $T(n x)$. In your case the function has to be "decomposed" first. I will illustrate steps with the standard floor function.

  1. Remove the linear component and subtract the mean value, so the transformed function is periodic and oscillation around $x$ axis with equal amplitude. This is called "de-trend" and "de-mean" in signal processing.

$$f(x) = floor(x)-x+1/2$$

  1. Expand f(x) as: $$f(x) = \sum_{n=1}^{\infty} a_n T(n x)$$

  2. Find $a_n$: $$ a_n = \frac{\int_0^1 f(x) T(n x) dx}{\int_0^1 T(n x)^2} = \frac{3}{8 n} $$

  3. Transform back (add mean and linear component): $$floor(x) = \sum_{n=1}^{\infty} \frac{3}{8 n} T(n x) + x - 1/2$$

    • Figure using 10 triangle waves:

Triangle wave approximation of floor function

2
On

In addition to the answer of Taozi, approximates with Fourier series are shown below. Of course, the accuracy depends on the number $N$ of terms :

enter image description here

Note : A typo corrected.