Line smoothing algorithm that perserve data uniformity
Here are two suggestions for you to try.

Suggestion 1: Use a linear filter. Instead of computing the average $x_{2n},x_{2n+1} \mapsto (x_{2n}+x_{2n+1})/2$, try averaging over a larger sequence, e.g. $y_n = (x_{2n-1}+2x_{2n}+2x_{2n+1}+x_{2n+2})/6$.

Suggestion 2: Use a conditional filter: if $x_{2n} &lt; x_{2n+1} &gt; x_{2n+2}$ or $x_{2n-1} &lt; x_{2n} &gt; x_{2n+1}$, then let $y = x_{2n+1}$ or $y = x_{2n}$, respectively, and similarly for local minima; otherwise use the average (or a linear filter) as before.