how to approximate time-series data
<p>I'm not sure if this is the right term but I think I want to s̶m̶o̶o̶t̶h̶ ̶a̶n̶d̶/̶o̶r̶ approximate a data set. I have 30 data points as it is presented in the chart below (the red line with dots)
I want to approximate the dataset so it can be described with fewer data points. The black line represents what I want to achieve.<a href="https://i.stack.imgur.com/OgkQy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OgkQy.png" alt="chart1"></a></p>

<p>I want to be able to define an approximation level which will control how much the result data set will differ from the original one.
The approximated data set should contain a set of data points which I can connect together using straight lines.</p>

<p>What is the right algorithm or a math function to solve this problem? I don't expect an implementation here, but rather some suggestions where to start.</p>

<p>I wrote my implementation of the approximation algorithm. It works in most of the cases, but there are certain situations in which it returns non-optimal data.
The example below shows three dotted lines. Thin red line is the original dataset, a thick red-black dotted line is generated by my algorithm, the green line is what I'd like to achieve. </p>

<p><a href="https://i.stack.imgur.com/dn3x3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dn3x3.png" alt="my implementation"></a> </p>

<pre><code>        var previousValue;
        return array.map(function (dataPoint, index, fullArray) {
            var approximation = dataPoint;

            if (index &gt; 0) {
                if (Math.abs(previousValue - value) &lt; tolerance) {
                    approximation = previousValue;
                } else {
                    previousValue = dataPoint;
                }

            } else {
                previousValue = dataPoint;
            }

            return approximation;
        });
</code></pre>