1-D Interpolation between any number of points

375 Views Asked by At

I'm looking for an algorithm which will allow me to calculate outputs for any given input instead of just the few ones I'm given in advance.

The input is a number from interval $0 - 100$ representing a percentage. I'm given a few of the desired outputs in advance; they correspond to inputs evenly distributed along the whole $0 - 100$ range. For example, if I am given three outputs, A, B and C, they correspond to $100$, $50$ and $0$ respectively. If, instead, I am given five of them, they should correspond to inputs of $100$, $75$, $50$, $25$ and $0$. In between these given values, the output should increase or decrease proportionally. For example, in the first example, the output corresponding to 75 should be right in the middle between A and B (since $75$ is right in the middle between $100$ and $50$ and output $A$ corresponds to input $100$ and output $B$ to input $50$) and the output for $25$ should be right in the middle between $B$ and $C$.

In summary: I'm given an array of values (the "given" outputs) consisting of at least two values. I'm also given an input value in the range $0 - 100$. The algorithm should output a single value corresponding to this input; which satisfies the description above.

Since my background in maths is very limited, I would prefer it if you could explain the mathematical symbols or name the variables something reminiscent of their function.

1

There are 1 best solutions below

2
On BEST ANSWER

Some of your examples don't make sense assuming my interpretation of the problem.

In general, you have a sequence of $n \geq 2$ real numbers $(q_0, q_1,...,\, q_{n-1})$ and your interpolation parameter $t \in [0,1]$. You're only interested in the 2 values whose indices bookend $tn - t$. The interpolated value is: $$ (1 - \alpha) \,q_{\lfloor mt\rfloor}+\alpha \,q_{\lceil mt\rceil} $$ where $m = n - 1$ and $\alpha = mt - \lfloor mt \rfloor$.

Looking at your 2nd example, you have $q = (0.5, 2, 1, 0, 1)$ with $n=5$ and $m=4$. Note that I've flipped your order as traditionally you progress left to right. At $t=0.125$ we have $mt = 4 \times 0.125 = 0.5$ and:

$$ \begin{align} \alpha &= 0.5 - \lfloor 0.5 \rfloor \\ &= 0.5 - 0 \\ &= 0.5 \end{align} $$ which gives us $$ (1-0.5) q_0 + 0.5q_1 = 1.25 $$ Which obviously doesn't equal 1, although it does yield 0 when $t=0.75 $.

EDIT: The question has changed since I provided this answer.