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.
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).
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).
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:
This is clearly unwanted as well.
I hope this is of help to anyone. For those interested in the code (python), here it is.