0
votes

I am very new to python(say 1 week) and numpy/scipy but not new to programming at all but wondering how to do the following correctly (preferably with numpy/scipy):

So say I have an 150x200 ndarray with float values. I want to interpolate a line from 20:40 to 100:150 with 500 points in between.

Getting the x:y interpolation I have with:

xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)

But now how i get the values(on that line only) interpolated using numpy/scipy?

PS I use python3

2
So resulting in a 1 dimensional array with 500 values - ovanwijk

2 Answers

0
votes

Check out Scipy.interpolate

import numpy as np
from scipy.interpolate import interp1d
xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)
f = interpolate.interp1d(x, y)

xnew = np.arange(20, 30, 0.1)
ynew = f(xnew)
0
votes

I currently did it like this:

I am wondering if I do this correctly:

def get_interpolated_slice(self, grid_start_x, grid_start_y, grid_end_x, grid_end_y, resolution=100):

    x = np.arange(self.z_data.shape[1])
    y = np.arange(self.z_data.shape[0])
    interpolated_z_ft = interpolate.interp2d(x, y, self.z_data)

    xvalues = np.linspace(grid_start_x, grid_end_x, resolution)
    yvalues = np.linspace(grid_start_y, grid_end_y, resolution)

    interpolated_y_values = interpolated_y_m(xvalues, yvalues)

    z_to_return = [None] * resolution
    for i in range(resolution):
        z_to_return[i] = interpolated_z_values[i][i]

    return z_to_return

So what do I do: I interpolate the 2d array(matrix) completely for a range between 2 points. Since it will be a resolution X resolution grid I can make a diagonal 'walk' through the matrix that will result in the a line.

Can someone confirm if this is correct and/or this is the correct way to do it?