Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Kirl on March 14, 2012

Title: interpolating values
Post by: Kirl on March 14, 2012
I need to interpolate 4 values, am I right that interpolation is basically calculating an average?

Code: [Select]
(value1 + value2 + value3 + value4) /4;
I never had any kind of formal math training so I'm mostly clueless when it comes to specific terms.
Title: Re: interpolating values
Post by: hellfire on March 14, 2012
That's one possible method.
If your values have a spatial relationship you might want to consider gauss filter (http://en.wikipedia.org/wiki/Gaussian_filter), median cut (http://en.wikipedia.org/wiki/Median_filter) or linear regression (http://en.wikipedia.org/wiki/Linear_regression), too.
Title: Re: interpolating values
Post by: Kirl on March 15, 2012
Thanks! Wikipedia is normally my fist stop for finding anything, but the mathematical notation is near impossible to decipher. Theory is often sufficient, but I find most of wikipedia's math articles are lacking in clarity as well.

I should probably just find some math training.  :whisper:
Title: Re: interpolating values
Post by: hellfire on March 15, 2012
Wikipedia is normally my fist stop for finding anything, but the mathematical notation is near impossible to decipher.
Maybe I can omit the math with a simple example.
Imagine a set of values "v" over the x axis:
Code: [Select]
int v[]= { 1, 3, 6, 5, 8,12,11,14 };
int x[]= { 0, 1, 2, 3, 4, 5, 6, 7 };

Let's take an averaging radius of 2 values, so for eg x=3 you'd weight the values 1..5.
The unweighted approach:
Code: [Select]
average= ( v[1] + v[2] + v[3] + v[4] + v[5] ) / 5;
       = ( 3 + 6 + 5 + 8 + 12 ) / 5 = 6.8

The gauss filter calculates a weighted average by taking the distance into account (the bigger the distance, the smaller the weight):
Code: [Select]
float maximum= 0.4;
float medium= 0.25;
float small= 0.05;
average= v[1]*small + v[2]*medium +  v[3]*maximum  + v[4]*medium + v[5]*small;
       = 6.25
Notice that the sum of all weights is 1.0.

For median cut you'd sort all values in the averaging range and pick the center.
Anomalies are expected to gather at the upper and lower end (so the center element is usually pretty average):
Code: [Select]
v[1..5]: 3, 6, 5, 8,12
sorted:  3, 5, 6, 8,12
center:  6
Title: Re: interpolating values
Post by: ttemper on March 15, 2012
@hellfire, great explanation on the different techniques. pixels will never look the same! :P K++
Title: Re: interpolating values
Post by: Kirl on March 15, 2012
Yes, an excellent explanation of the various forms, thanks a lot!

Proably best to replace the wikipedia articles with your post!  ;D