I'm resampling a signal (which takes values [0,1]) of N samples (blu points) to one with N/5 samples, where (for each group of 5 samples) I store in two arrays the max and the min values of the grouped block (i.e. peaks); you can see the max values are yellows and min are reds, for each step-group:
Now, what I need to do is to "join" those points, getting the same signal and keeping (that's the important aspect) min/max value for each peak. My idea is:
- start from index t=0.
- When the next max is increasing, use current max point (yellow one)
- When the next max is decreasing, use current min point (red one)
It seems to works very well (you can see the white line is taking the points it should take). But this don't always works. In fact at the 4° change of slope, this is not true. It should take in order red-red-red-yellow-yellow. But with my method, its red-red-red-red-yellow (because between the 3° and 4° yellow there is a decreasing):
Visually, it's easy: the point that "more" hover/is near to the blu at that step is the one that need to be taken.
Since when I iterate Red[] and Yellow[] I've not the Blu[] one, how would you find it mathematically?


Here is my illustration of the situation at hand:
The problem is to "resample" the signal using only 5-sample maxima and minima, keeping local extrema intact. (As I understand it, this precludes standard Fourier transform-based resampling methods. We accept a bit more errors in the frequency domain, to be able to keep track of instant signal extrema.)
In essence, we wish output sample $y_n$ to be between the 5-sample limits, $min_n \le y_n \le max_n$ (where $n$ is the index for the output sample). So, let's define a coefficient $0 \le c_n \le 1$, so that $$y_n = min_n + c_n ( max_n - min_n )$$ This simplifies the problem into choosing $0 \le c_n \le 1$ for each output sample.
Let's define some additional terminology:
$n$ is a local maximum when $max_n \gt max_{n-1}$ and $max_n \gt max_{n+1}$
$n$ is a local minimum when $min_n \lt min_{n-1}$ and $min_n \lt min_{n+1}$
We can use the above terminology to define two cases that will let us retain all local maxima and minima (unless they both occur in the same five-input-sample part):
When $n$ is a local maximum and not a local minimum, $c_n = 1$ ($y_n = max_n$).
When $n$ is a local minimum and not a local maximum, $c_n = 0$ ($y_n = min_n$).
The case when $n$ is both a local maximum and a local minimum occurs when this five-sample part of the signal contains more noise or high-frequency components than the preceding or following part, and is not decided by the above.
The case when $n$ is not a local maximum or a local minimum is also undecided.
The most straightforward option is to just use $c_n = 1/2$ for the undecided cases -- all except the two cases named above -- so that $y_n = (min_n + max_n)/2$, i.e. midway between the five-sample limits. In some ways, this is the best option, because we do not make any additional assumptions on the signal.
Pseudocode to do this (even on a microcontroller or similar) with minimal buffering (three minimum and maximum values) and one input sample group (5 original samples) latency:
Applying this to the above signal yields![Filtered signal]](https://srv-file2.gofile.io/download/Sd2vBD/18e0acbd870a5b55a526b2202b59efbc/filter-minmax5.svg)