1

I am trying to do spline interpolation between two arrays in Python. My data set looks like this:

           |      5        15
    -------+--------------------
        1    32.68      29.16
        2    32.73      27.20
        3    32.78      28.24
        4    32.83      27.27
        5    32.88      25.27
        6    32.93      31.35
        7    32.98      27.39
        8    33.03      26.42
        9    33.08      27.46
       10    33.13      30.50
       11    33.18      27.53
       12    33.23      29.57
       13    33.23      27.99
       14    33.23      28.64
       15    33.23      26.68
       16    33.23      29.72

And I am trying to do a spline interpolation between the two points and produce the values for 10, something that will eventually look like this (but spline interpolated):

           |   10
      -----+--------
        1       30.92
        2       29.965
        3       30.51
        4       30.05
        5       29.075
        6       32.14
        7       30.185
        8       29.725
        9       30.27
        10      31.815
        11      30.355
        12      31.4
        13      30.61
        14      30.935
        15      29.955
        16      31.475

I have been looking at examples of using scipy.interpolate.InterpolatedUnivariateSpline, but it seems to take only one array for x and one for y, and I can't figure out how to make it interpolate these two arrays.

Can someone please help point me in the right direction?

ali_m
  • 71,714
  • 23
  • 223
  • 298
user252652
  • 179
  • 1
  • 1
  • 9
  • 4
    Hello, there seem to be 2 problems with what you are doing. 1. you want to interpolate a value between 2 values but you only have those 2 values, so a spline interpolation does not make any sense. 2. Your data seems to be Bivariate and not Univariate: one variable represented by your columns and one variable by your lines. So maybe you need to try a Bivariate interpolation. – Ben K. Aug 25 '15 at 09:22
  • @user1901493 It's not obvious from the question whether the data should be considered univariate or bivariate. That depends on whether or not each row is considered to be an independent data series to be interpolated along. – ali_m Aug 25 '15 at 16:48
  • OK, let's consider it to be independent. So for each line you need to interpolate from values 5,15 to the one at 10. Are there additional values for 0, 20 ,25 etc? One more question: can you post your code, I don't see how you organised your arrays. – Ben K. Aug 25 '15 at 20:09
  • Did you find a solution? – Delosari Jun 19 '17 at 22:17

1 Answers1

1

With the amount of data you have, only two points for each x value, piecewise linear interpolation is the most practical tool. Taking your two arrays to be v5 and v15 (values along y=5 line and y=15 line), and the x-values to be 1,2, ..., 16, we can create a piecewise linear interpolant like this:

from scipy.interpolate import interp2d    
f = interp2d(np.arange(1, 17), [5, 15], np.stack((v5, v15)), kind='linear')

This can be evaluated in the usual way: for example, f(np.arange(1, 17), 10) returns precisely the numbers you expected.

[ 30.92 ,  29.965,  30.51 ,  30.05 ,  29.075,  32.14 ,  30.185,
    29.725,  30.27 ,  31.815,  30.355,  31.4  ,  30.61 ,  30.935,
    29.955,  31.475]

interp2d can also create a cubic bivariate spline, but not from this data: your grid is too small in the y-direction.