从查找表数据插值 [英] Interpolating data from a look up table
问题描述
LUT = np.genfromtxt('test.out', delimiter=',', dtype=float)
LUT:
12, 25, 136, 6743
13, 26, 139, 6786
14, 27, 142, 6791
15, 28, 145, 6789
待从LUT读
值如下:
Values to be read from the LUT are as follows:
x1, x2, x3 = 12.5, 25.5, 137
在LUT每个给定的值(3列)读取相邻的两个值,我必须线性插入的结果(LUT第4列)。
Reading the neighboring two values in the LUT for each of the given values (3 columns), I have to linearly interpolate the results (4th column in LUT).
的给定的值(X1,X2,X3)属于第1和LUT的第二排之间。在此基础上如何阅读第一和第二排的结果?
The given values (x1, x2, x3) belongs to between 1st and 2nd row of the LUT. Based on this how to read the results between 1st and 2nd row?
推荐答案
定坐标列表 COORDS
要插值,可以使用 scipy.spatial.cKDTree
来获得你的表所必需的线性插值的2最接近的条目。下面的code给出了一个使用示例,已经量化。
Given a list of coordinates coords
where you want to interpolate, you can use scipy.spatial.cKDTree
to obtain the 2 closest entries of your table that are necessary for the linear interpolation. The code below shows an usage example, already vectorized.
import numpy as np
from scipy.spatial import cKDTree
# inputs
LTU = np.genfromtxt('test.txt', delimiter=',')
coords = ((12.5, 25.5, 137),
(13.5, 26.5, 141),
(14.5, 25.5, 144))
# querying and interpolating
xyz = LTU[:, :3]
val = LTU[:, 3]
del LTU # attempt to clean up memory
tree = cKDTree(xyz)
dist, ind = tree.query(coords, k=2)
d1, d2 = dist.T
v1, v2 = val[ind].T
v = (d1)/(d1 + d2)*(v2 - v1) + v1
print(v)
#[ 6758.73909236 6789.16987298 6790.03575996]
这篇关于从查找表数据插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!