I have a function $f(t)$ that is piecewise constant: $$ f(t) = a_i \forall t\in[t_i,t_{i+1}) $$ with $n$ values $a_0, a_1, ..., a_{n-1}$, and $n+1$ values $t_0, t_1, ..., t_n$.

I want to approximate this function with a function $g(t)$ that is continuous, with the condition that the average of $g(t)$ equals that of $f(t)$ in each piece: $$ \int_{t_i}^{t_{i+1}}g(t) dt = a_i\cdot(t_{i+1}-t_i) $$

In addition, I'd like $g$ to have no structure (oscillations) at a timescale below that of the individual pieces. For example: if $a_{i-1} < a_i < a_{i+1}$, I'd like $g$ to have no extremes in $t \in[t_i,t_{i+1})$.

The function $g(t)$ may be a piecewise function (i.e., existing as a different equation in each piece, and not necessarily differentiable in the $t$-values $t_0, t_i, ...$).

I can imagine several ways of doing this, but before I reinvent the wheel, I thought I'd ask here if there is a 'common' way to do this. I find many ways to do the reverse, but maybe I'm not using the correct search terms, as I don't know what the mathematical term for this process is.


I've added some initial implementations as an answer to this question.

However, what I think would be the best way of getting a good function $g$, is using a physical analogue; but I've started a seperate question for that here.


I think is difficult, or perhaps impossible, to avoid extrema of the built function.

Take $$ y_0=a_0,\quad y_i=\frac{a_{i-1}+a_i}{2},\quad y_n=a_{n-1} $$ then take a quadratic that pass through the required ends, $$ f(x)=y_i\frac{x-x_i}{x_{i+1}-x_i}+y_{i+1}\frac{x-x_{i+1}}{x_i-x_{i+1}}+A_i(x-x_i)(x-x_{i+1}),\quad x_i\leq x\leq x_{i+1} $$ with $A_i$ such that the area is the right one $$ \int_{x_i}^{x_{i+1}}f(x)dx=\frac{1}{6}[3(y_i+y_{i+1})-A_i(x_{i+1}-x_i)^2]=a_i $$ Example:

enter image description here


Alright, I couldn't help myself and implemented various ways of constructing such a function $g$.

Each of these fulfills the condition, that the piecewise integral equals that of the original function $f$. Also, except the very final one, all functions $g$ are piecewise calculated for one interval at a time, though the values of $f$ in preceding or following intervals might be used as well.

First, the top row:

(A) The graph on the left shows polynomials where the function value at the boundary between to intervals is fixed to the average of the two $f$-values that meet there. In the case shown here, with 4th-degree polynomials, 2 more degrees of freedom are available, so the integral of $g$ is made to equal that of $f$ in the surrounding 2 intervals as well.

(B) The graph in the middle also show polynomials, but here the midpoint condition is relaxed, which causes the curves to be discontinuous. The 4 degrees of freedom that are left, are used to make the integral of $g$ equal that of $f$ in the 4 surrounding intervals as well.

(C) The graph on the right shows a fourier series, in this case with 5 components. The coefficients are calculated from the value of $f$ in the interval under consideration, and also the 4 surrounding it. Because there is no condition on the value at the boundary, this curve is also discontinuous.

The second row contains the same functions, but here a 4th-degree polynomial was added to each interval, such that the curves are continuous and differentiable also at the boundaries without changing their average. The added polynomial has left function value, left first derivative, right function value, right first derivative such that the intervals meeting at a boundary have equal limits for their value and derivative. (Well, in principle at least - I'm doing the calculations with discrete time series). This is the row that I'd use a function of in any real-life application.

Finally, in the bottom left corner, I've added a single fourier series fitted to 100 intervals at once. This looks great, but has some drawbacks that I'll go into.

But first some images and close-ups. I'm only comparing the graphs in the second row.

enter image description here Lots of up and down. The additional (unwanted) extrema in (A) are visible; less of these are present in (B) and (C), but they also occur there (the first three values of $f$ that are shown are in increasing order). The tops of (C) seem to be excessively high. Out of these three, I'd pick (B).

enter image description here Here (B) is clearly best, as it does not have the local maxima on the left side that (A) and (C) do. Still, not perfect, as has the same unwanted wobbling on the right as (A) and (C).

enter image description here Mainly down. Especially (C) has some additional wobble at the beginning and end of the descending part. Here, too, I'd pick (B).

Then, the single Fourier series. This is quite good as well. However, because there are only constraints on the piecewise integral, it is possible that the amplitudes of the highest-frequency components are very high, which is not what we want. Here is an example: enter image description here This is clearly unwanted as well.

I hope this is of help to anyone. For those interested in the code (python), here it is.


Your Answer

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

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