Best overlap between curves

Introduction

Given two curves $y1(x)$ and $y2(x)$ how can we find the best overlap of those two curves ?

The notion of "best overlap" refers to the minimisation of a distance between the curves. Definin the distance is thus the first step.

In the following examples, we will take the distance between two curves as the quadratic difference.

Provided that $y1$ and $y2$ have the same number of points $N$, we define the distance as follows:

$$d(y1, y2) = \sum_{i=0}^{N-1} \left[y_1(x_i) - y_2(x_i) \right]^2 $$

Now, we supposed that the the curves are considered at the same locations $x_i$, so we will just use the index $i$ to tag the values of y in a array of length $N$:

$$d(y1, y2) = \sum_{i=0}^{N-1} \left(y_{1,i} - y_{2,i} \right)^2 $$

Intuitively, finding the best overlap would just consist in making a curve "glide" horizontally while keeping the other fixed. Then, for each increment of translation, we would compute the distance and try to get the minimum among all the computed distances. This would give us the "best" overlap.

curve_swipe.png

Here I would like to mention two things:

  1. We may be tempted to go for a convolution product because it seems to be exactly what we are trying to do. However, as I will show later it doesn't work.

  2. The problem that will come quickly is how to compare the distance between two sets of curves, where both sets are of different lengths (number of points). We need to renormalize it, otherwise, a bad overlap between two short pieces may yield a better distance than a good yet non perfect overlap between two long pieces of curve.

We will call $d_{m}$ the normalized distance and define it as follows:

$$d_{m}(y1, y2) = \frac{d(y1,y2)}{N} = \frac{1}{N} \sum_{i=0}^{N-1} \left(y_{1,i} - y_{2,i} \right)^2 $$

Imports

Initial Problem

We want to retreive the horizontal shift between $y1$ and $y2$ in order to find the best overlap between the two curves.

We will call this shift, the offset $o$.

Convolution

Conclusion: We see that the convolution doesn't yield the right answer.

Swiping curves

Let's now write a function overlap1 that will compute $d$ for all the possible horizontal translation of $y2$ while keeping $y1$ fixed.

Observation: Interestingly, it seems that our naive approach led to a result. However, the overlap function shows a shape that could lead to several local minima. Is it out of luck that we managed to get the good response ?

Let's try on two other functions and let's use another algorithm.

Observation: we see that our algotithm seems to work well on functions that show some variation but fail on a flat line. To be honest, the flat line does't show any offset and the answer should have been 0. Here, it means that any position give the same distance. However, it seems that the first absolute minima alway provides the right answer. Strangeley, numpy.argmin is supposed to return the first argmin found.

Conclusion: the algorithm works for pieces of the same lengths and the special case of flat lines has to be trated seperately.

Now let's change the algorithm to improve this first work.

Final Solution

Here, we reuse the work done previously, but we change the distance from $d$ to $d_{m}$. It graphically improves the interpretation of the shape of the overlap funtion.

Conclusion: we managed to come up with an algorith to find the best overlap between two curves given a quadratic average distance. The fact of averaging renormalizes the contribution of each overlapping interval and provides a clearer overlap graph to interpret.