1

The data is a Path in space. I have 3D location data (x,y,z) and the time the location point was recorded.

The x ,y, and z coordinates are the point locations of something traveling through 3D Space. The time values is the time (beginning at 0) that each point was recorded.

x     y    z    time(s)
0.1   2.2  3.3  0
2.4   2.4  4.2  0.3
4.5   2.5  1.8  0.6

I will eventually miss some recording events. (this is known and accepted as true) And the data stream will continue at a different time interval:

x     y    z    time(s)
0.1   2.2  3.3  0
2.4   2.4  4.2  0.3
//missing x,y,z data point at time 0.6
//missing x,y,z data point at time 0.9
4.5   2.5  1.8  1.2
...
...

Please note the data was simplified. My goal is to interpolate the missing 3D points at the known missing times. I have looked into various interpolating techniques but I am not entirely sure which interpolation method is suited for my problem.

1) Can someone succinctly explain the kind of problem this is? My math is extremely rusty and I am not sure how to properly describe it, which leads me to investigating interpolation techniques that may not be properly suited.

2) Update 1 Tricubic Interpolation should not apply here since I am not working with a grid in 3D space. I am working with a trajectory. I have found a Tricubic Interpolation implementation in the Apache math3 commons, however I am not sure if this is what I need. If you look at the arguments it takes, it takes a double[][][] fval matrix that I am not sure about.

3) If not best suited for Java, what is the best tool to interpolate this data?

Update 2 - Question regarding Spectre's solution

In your edit you provide the following tip regarding "matching first derivations of joint points":

let define our t=<-2,+2> and sample the control point like this (does not really matter how it will just affect the coefficients magnitude and including -1,0,1 will ease up the equations a lot):

p(-2) = p0
p(-1) = p1
p( 0) = p2
p( 1) = p3

now let assume we want to interpolate all the points on the interval t=<0,1> so all points between p2 and p3. And we want continuous piecewise curve so first derivations on joint points should match. We got one more control point on the left side so the second derivation can match there too:

p'(0) = 0.5*((p3-p2)+(p2-p1)) = 0.5*(p3-p1)
p'(1) = 0.5*((p4-p3)+(p3-p2)) = 0.5*(p4-p2)
p''(0)= 0.5*(((p2-p1)-(p1-p0))+((p4-p3)-(p3-p2)))
      = 0.5*((p2-2*p1+p0)+(p4-2*p3+p2))
      = 0.5*(p0+p4)-p1+p2-p3

Hope I did not make any silly mistake in the second derivation. Now just substitute p(t) with known control points and form system of equations and compute a0,a1,a2,a3,a4 algebraically from p0,p1,p2,p3,p4.

1) What do you mean by joint points?

2)Where do

p'(0) = 0.5*((p3-p2)+(p2-p1)) = 0.5*(p3-p1)
p'(1) = 0.5*((p4-p3)+(p3-p2)) = 0.5*(p4-p2)

come from? Do they in any way relate to p(0) = p2 and p(1) = p3 ? Can they be anything you chose ?

It can be re written as p'(0) = 0.5*((p(3)-p(0)) + (p(0)-p(-1)) correct? It is not clear to me why this is being done. Or even why it can be done

2b) Similar question for

p''(0)= 0.5*(((p2-p1)-(p1-p0))+((p4-p3)-(p3-p2)))
      = 0.5*((p2-2*p1+p0)+(p4-2*p3+p2))
      = 0.5*(p0+p4)-p1+p2-p3

but I am assuming that clarifying question 2) will alleviate my ambiguity for 2b) because I have no idea where the equation came from either.

What follows is pretty straight forward,then it's just systems of equations

Edwin Diaz
  • 1,693
  • 1
  • 12
  • 16
  • It depends on how you want to interpolate. Do you want a linear interpolation? Cubic spline? Linear in x but spline in y and z? You have to be the one who decides. – FredK Jul 06 '17 at 18:19
  • @FredK Given the problem which method would you recommend? I would like to avoid linear interpolation because the data is not very linear in nature. I think I would like to go for the more advanced techniques. – Edwin Diaz Jul 06 '17 at 18:22
  • Are your values x y and z dependant from each other? Or do you want three interpolations, each for every variable (p1(s)=x, p2(s)=y, p3(s)=z)? – F. Fritz Jul 06 '17 at 18:28
  • @F.Fritz I am not sure how to answer your first question. If you imagine taking your index finger in the air and drawing a downward spiral, each data point is on the line of that spiral. I am not sure if the values are dependent or not. They are for sure not scattered. I hadn't considered 3 interpolations for each value. What is that called? – Edwin Diaz Jul 06 '17 at 18:36
  • I don't think one interpolation for each value has a special name. I don't know if you can interpolate one function: p: R^1 -> R^3. But i guess one function would be better. If you want three interpolation, I would suggest Aitken-Neville-interpolation or Newton-interpolation. But I don't know if java has native methods for this. – F. Fritz Jul 06 '17 at 18:48
  • @F.Fritz Ok, if I wanted to do 3 interpolation methods, would I treat each variable, x, y, and z as being separate from each other? In other words, would I then have 3 tables (matrices) with the columns being [x , time][y, time][z time]? Then would I apply Neville or Newton to each of those matrices, interpolating at the missing time interval? – Edwin Diaz Jul 06 '17 at 19:03
  • yeah, my idea was to calculate each function and then solve the function with the missing time slots. But i only find liner or interpolations with three grid points and not a interpolation with n grid points. I will search a little bit on the weekend. – F. Fritz Jul 06 '17 at 22:05

1 Answers1

2

As your data are most likely just some smooth curve sampled points I would use cubic interpolation polynomial like this:

Curve properties are such that it goes through all control points (t={-1,0,+1,+2}) and the direction (1st derivation) at inner control points is average of booth sides to join smoothly (similar to Bezier cubics).

The algo is like this:

  1. take 2 point before missing point and 2 after

    lets call them p0,p1,p2,p3 they should be ideally time equidistant ones ... and ordered by time.

  2. compute 4 coefficients for each axis

    d1=0.5*(p2.x-p0.x);
    d2=0.5*(p3.x-p1.x);
    ax0=p1.x;
    ax1=d1;
    ax2=(3.0*(p2.x-p1.x))-(2.0*d1)-d2;
    ax3=d1+d2+(2.0*(-p2.x+p1.x));
    
    d1=0.5*(p2.y-p0.y);
    d2=0.5*(p3.y-p1.y);
    ay0=p1.y;
    ay1=d1;
    ay2=(3.0*(p2.y-p1.y))-(2.0*d1)-d2;
    ay3=d1+d2+(2.0*(-p2.y+p1.y));
    
    d1=0.5*(p2.z-p0.z);
    d2=0.5*(p3.z-p1.z);
    az0=p1.z;
    az1=d1;
    az2=(3.0*(p2.z-p1.z))-(2.0*d1)-d2;
    az3=d1+d2+(2.0*(-p2.z+p1.z));
    
  3. set parameter t=<0,1> to value corresponding to missing time

    so if you chose points p0,p1,p2,p3 with times t0,t1,t2,t3 then the missing time tm corresponds to parameter:

    t = (tm-t1)/(t2-t1);
    
  4. compute missing point.

    x=ax0+ax1*t+ax2*t*t+ax3*t*t*t
    y=ay0+ay1*t+ay2*t*t+ay3*t*t*t
    z=az0+az1*t+az2*t*t+az3*t*t*t
    

You can use polynomials of higher order if this is not enough by deriving similar equations or fitting. Also take a look at this:

[Edit1] constructing own polynomial

The answer to your comment is in [edit2] of Impact of cubic and catmull splines on image which is also linked in previous link above. To make 4th degree interpolation polynomial in similar manner you will have 5 points (p0,p1,p2,p3,p4) and equations:

p(t)= a0 + a1*t + a2*t*t + a3*t*t*t + a4*t*t*t*t
p'(t) =    a1 + 2*a2*t + 3*a3*t*t + 4*a4*t*t*t
p''(t) =        2*a2   + 6*a3*t   +12*a4*t*t

let define our t=<-2,+2> and sample the control point like this (does not really matter how it will just affect the coefficients magnitude and including -1,0,1 will ease up the equations a lot):

p(-2) = p0
p(-1) = p1
p( 0) = p2
p( 1) = p3
p( 2) = p4

now let assume we want to interpolate all the points on the interval t=<0,1> so all points between p2 and p3. And we want continuous piecewise curve so first derivations on joint points should match. We got one more control point on the left side so the second derivation can match there too:

p'(0) = 0.5*((p3-p2)+(p2-p1)) = 0.5*(p3-p1)
p'(1) = 0.5*((p4-p3)+(p3-p2)) = 0.5*(p4-p2)
p''(0)= 0.5*(((p2-p1)-(p1-p0))+((p4-p3)-(p3-p2)))
      = 0.5*((p2-2*p1+p0)+(p4-2*p3+p2))
      = 0.5*(p0+p4)-p1+p2-p3

Hope I did not make any silly mistake in the second derivation. Now just substitute p(t) with known control points and form system of equations and compute a0,a1,a2,a3,a4 algebraically from p0,p1,p2,p3,p4. Hint use t=0,t=+1 and t=-1 so you will get linear equations for those. For example:

p( 0) = p2 = a0 + a1*0 + a2*0*0 + a3*0*0*0 + a4*0*0*0*0
        p2 = a0

As you can see a0 is computed really simply the same can be used for derivations:

p'(0) = 0.5*(p3-p1)          = a1 + 2*a2*0 + 3*a3*0*0 + 4*a4*0*0*0
p''(0)= 0.5*(p0+p4)-p1+p2-p3 =      2*a2   + 6*a3*0   +12*a4*0*0
-------------------------------------------------------------------
        0.5*(p3-p1)          = a1  
        0.5*(p0+p4)-p1+p2-p3 =      2*a2
-------------------------------------------------------------------
        0.5*(p3-p1)                 = a1  
        0.25*(p0+p4)-0.5*(p1+p2-p3) = a2
-------------------------------------------------------------------

Now use t=+1 and t=-1 and compute the a3,a4. You can set the joint point derivations to meet your specific needs (not just average of derivation from left and right) but to form continuous curve like yours is this the best (from my experience).

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thank you for your provided solution. I have a questino regarding your last statement regarding using higher order polynomials if this is not enough. – Edwin Diaz Jul 10 '17 at 16:13
  • @diaz so what is the question? – Spektre Jul 10 '17 at 16:24
  • I pressed enter too early! oops! I manually computed some sample points using your proposed solution, and it worked great for few missing points. But if we assume that I have 300 data points, then 50 missing, then 700 more data points, I would compute 1 missing point to represent the entire 50, correct? If so, this is not too accurate. I was thinking that I could somehow reapply this technique repeatedly, using the newly interpolated datapoint. If not, would I have to use a 1000th degree polynomial for an accurate solution? – Edwin Diaz Jul 10 '17 at 16:28
  • Typing @ Spektre doesn't show up in my comments (without the space) – Edwin Diaz Jul 10 '17 at 16:37
  • @diaz well if you got continuous areas of missing data then you can still interpolate the missing points (not just one) but yes the accuracy is worse then sparse missing points. The order of polynomial to use depends on your data properties ... try to plot is as function of time (3x 2D graph) the more inflex points on/near the missing areas thi higher order of polynomial you need. You can construct your own polynomials or use Catmul-ROM or any interpolation polynomials but beware too high order polynomials usaly oscillate. PS you are commenting my post(Answer) so I am notified automatically – Spektre Jul 10 '17 at 16:46
  • @diaz you can select more distant points to increase precision ... for example with cubics: select `250th, 300th, 350th 400th` and interpolate the missing `50` ... that should be better then selecting `299,300,350,351` unless too big change is present within the skipped points ... all depends on your data properties ... You can also select significant points instead (local min/max) – Spektre Jul 10 '17 at 16:50
  • [Screenshot of some sample data](https://imgur.com/a/oBmBr). The graph on the left is the various coordinates in 3D space. The graph on the right is lines drawn between each point to represent the path in space. I think big changes can be present within the skipped points. Can you point me in the direction of a source where I can learn how to construct my own polynomials? Also, I do not understand when you say If I select "250th 300th 350th 400th" I can interpolate 50 new points. I thought your equations above only interpolated 1 new point. – Edwin Diaz Jul 10 '17 at 17:13
  • @diaz the cubic equations can get you any number of points between the inner 2 points .... you just set `t=0.02, 0.04, 0.06, 0.08, 0.10 , 0.12 ...` the plot suggest there is not any correlation between parts of the trajectory (at least I do not see any) so if your missing part contains complex shapes they will not be recovered. The selection part I wrote about is just that you do not need to use consequent points as control points ... usually good results are obtained when the distance between control points is the same hence the 50 point skip around 50 missing points... – Spektre Jul 10 '17 at 17:39
  • @diaz see [Impact of cubic and catmull splines on image](https://stackoverflow.com/a/23627081/2521214) for an example of constructing own polynomials... you just form equations describing desired shape of polynomial and solve the coefficients from known input control points. You can also use known interpolations like https://en.wikipedia.org/wiki/Lagrange_polynomial ... or use fitting like [How approximation search works](https://stackoverflow.com/a/36163847/2521214) – Spektre Jul 10 '17 at 17:42
  • My silence is me trying to learn more about your suggestions. – Edwin Diaz Jul 11 '17 at 17:18
  • @diaz :) keep going the stuff is super useful for many tasks... just do not get too crazy about degree of polynomial ... I would not go beond `t^6` Here an example very similar task to yours ... [RGB values of visible spectrum](https://stackoverflow.com/a/22681410/2521214) – Spektre Jul 11 '17 at 17:56
  • I am keeping at it :-) ... Can I ask how you arrived at the equations for the coefficients of the cubic equation? d1=0.5*(p2.x-p0.x); d2=0.5*(p3.x-p1.x); ax0=p1.x; ax1=d1; ax2=(3.0*(p2.x-p1.x))-(2.0*d1)-d2; ax3=d1+d2+(2.0*(-p2.x+p1.x));.... I wanted to extend to a 4th degree polynomial but first I need to know how you arrived for the equation for the 3rd degree.... In addition, I am failing to see the similarities between the RGB problem you linked and this problem. :/ – Edwin Diaz Jul 11 '17 at 20:35
  • @diaz added **edit1** with description... for the **RGB** it is really similar you got **3D** path `(x,y,z)` I got **3D** color `(R,G,B) `. You got known and missing points I got `n` known points (obtained from spectral image those bar like graphs where only one value for the whole band is known) and **many times** more unknown points (to fill the whole band). The only difference is that mine polynomial fit is hard-coded as the input data is not changing – Spektre Jul 12 '17 at 08:30
  • Thank you so much for your amazing efforts. I will accept your answer after I submit this. I feel like I am closer to the solution than ever. In order to understand your edits, I have a question regarding your statement "And we want continuous piecewise curve so first derivations on joint points should match. We got one more control point on the left side so the second derivation can match there too: p'(0) = 0.5*((p3-p2)+(p2-p1)) = 0.5*(p3-p1) ... p''(0)= 0.5*(((p2-p1)-(p1-p0))+((p4-p3)-(p3-p2))) = 0.5*((p2-2*p1+p0)+(p4-2*p3+p2)) = 0.5*(p0+p4)-p1+p2-p3 – Edwin Diaz Jul 12 '17 at 21:24
  • I understand that you are using the average as a metric to compare equality between the two. But I don't understand the concept behind matching first derivatives, nor do I know why it is neccessary. What other equations could be used here? And how did you come up with the equations I linked in the previous message? I will update my post with an edit like yours for better visibility. Also, for future readers, I stumbled upon a series of online .pdfs from a class lecture that seems to be related to my topic. [https://www.rose-hulman.edu/~finn/CCLI/Notes/day17.pdf](Check them out here) – Edwin Diaz Jul 12 '17 at 21:38
  • My link sharing failed [Here is the correct link to the Class notes](https://www.rose-hulman.edu/~finn/CCLI/Notes/day17.pdf) **Please note** you can change the day in the url "Notes/dayN.pdf" to go to previous days, 15 is missing, but if you go all the way back to 4 it has good notes on the topic. [Here is the parent directory](https://www.rose-hulman.edu/~finn/CCLI/Notes/) It is quite math heavy so depending on your familiarity it may or not be of much help – Edwin Diaz Jul 12 '17 at 22:12
  • @diaz `p(t)` is your actual position, first derivation `p'(t)` means actual velocity (and also geometrical tangent) and second derivation `p''(t)` means `acceleration` the equations are just derivations of `p(t)`. If the derivations would not match on the joints then the curve will not join smoothly with neighboring patches. You can use any equation you want for example in some cases you want your trajectory to have specific heading , or magnitude of speed etc. all depends on what you want to achieve and what the data is for. The more control points you got the higher derivation you can match – Spektre Jul 13 '17 at 06:32