Math: Interpolate between 1D set of numbers
Hi everyone,
Would anyone be able to help me with the following problem ?? I'd like to do it with basic math and not with Max objects that do the work for me.
This is due to me building a system in GLSL and codebox.
So lets say I have a list of numbers and I want to interpolate between them using one number how would I do it ?
I'd also like to do this with lists of varying sizes with 4 elements , 6 elements etc.. But hey help with this one example would go a long way to helping this math noob !
Weight is the value between 0. and 1. that I want to use to smoothly go between the lists.
f1 = 1.0
f2 = 0.5
f3 = 0.75
f4 = 0.
f5 = 0.9
If weight is 0.0 =1.0
If weight is 0.125 = 0.75
If weight is 0.25 =0.5
If weight is 0.5 =0.75
If weight is 0.75 =0.0
If weight is 1.0 =0.9
I'd appreciate any help with this as I've been banging at it all day but don't think I'm even using the correct terms in google to find it.
One thing you would want to read about is "linear interpolation".
Taking off from your example:
You have an array of 5 values. Let's call the length of the array L, and let's number the indices of the locations in the array starting from zero (0 to L-1). And for brevity let's call your 0-to-1 "weight" value w. To find the location in the array that corresponds to any given w, just multiply w times (L-1). Then interpolate between the array values above and below that location.
For example, given your array (but numbering the indices 0-4 instead of 1-5), what would be the value that corresponds to w=0.6? Multiply w (0.6) times L-1 (4) and you get 2.4. Truncate that value, meaning take just the integer part of it (2), to find the index below that location. Add one to that to get the index above your location (2+1=3). Take the decimal part of your location (0.4, let's call it d), and find the value v that is 0.4 of the way from the value at index 2 (0.75, let's call it a) and the value at index 3 (0.0, let's call it b).
The value you want can be found by
v = a + d(b-a) 0.75 + 0.4(0.0-0.75) = 0.75 - 0.3 = 0.45
or
v = (1-d)a + db (1.-0.4)0.75 + 0.4(0.0) = 0.45 + 0.0 = 0.45
There are more elaborate types of interpolation than straight linear that are preferable for some purposes, but this will get you started (and corresponds to the example you gave).
Thank you so much Christopher ! I'll be honest I've looked at that page for hours today and I am just so bad at converting algebra into code. Practice makes perfect !
The formulas at the bottom of my previous post are the ones that are relevant to the linear interpolation issue, and they're pretty much translated into code for you in the "Programming language support" section of that Wikipedia page.
If you find text-based programming easier than Max programming, you could implement this task pretty easily with JavaScript in the Max js object. The object could accept a 'list' message to set the array, and a 'float' message (with its range clipped or wrapped between 0 and 1) to specify your "weight" value and output the corresponding value.
There is no array access in Gen but if the list is in param form, you can use a function as the second arg of swiz.
swiz(v, trunc(w*(n-1)) );
Where v is a parameterized vector of length n indexed by weighting factor w.