如何使用Python在非矩形坐标中插值数值?

huangapple go评论79阅读模式
英文:

How to interpolate values in non-rectangular coordinates using Python?

问题

我需要使用Python来对我的光学系统中的值进行补偿。我已经测量了我的补偿参数在桌子的角落的依赖关系,并且我想在那里进行线性插值,但地图不是一个矩形。
示例:

角落坐标:

a_real = (45, 45)
a_coeff = (333, 223)

b_real = (-45, -45)
b_coeff = (325, 243)

c_real = (-45, 45)
c_coeff = (339, 244)

d_real = (45, -45)
d_coeff = (319, 228)

假设我想要知道点(40, 40)或(0, 0)处的补偿系数。
如何使用Python在非矩形坐标中插值数值?

  1. 如何实现这个目标?我正在看scipy.interpolate.interp2d,但我不确定它是否适用于我的情况。
  2. 如果我想要添加更多点,来定义我的网格,会怎么样?
英文:

I need to make compensation of values in my optical system using Python. I've measured the dependency of my compensation params in the corners of my table and I want to interpolate such value there linearly, but a map is not a rectangle.
Example:

# Corners coordinates:
a_real = (45, 45) 
a_coeff = (333, 223)

b_real = (-45, -45)
b_coeff = (325, 243)

c_real = (-45, 45)
c_coeff = (339, 244)

d_real = (45, -45)
d_coeff = (319, 228)

Let's say, I want to know compensation coefficients in points (40, 40), or (0, 0).
如何使用Python在非矩形坐标中插值数值?

  1. How this can be done? I'm looking at scipy.interpolate.interp2d but I'm not sure that it is my case
  2. What if I want to add more points, defining my grid?

答案1

得分: 2

你正在处理2D的非结构化点,所以你可以使用例如SciPy的interpolate.griddata函数。

我建议以下解决方案。我已经重新排列了您的数据,使之更方便使用。您可以将所有要添加的点都放入points NumPy数组中。

import numpy as np
from scipy.interpolate import griddata

points = np.array([[333, 223], [325, 243], [339, 244], [319, 228]])

values_a = np.array([45, -45, -45, 45])
values_b = np.array([45, -45, 45, -45])
new_value_a = griddata(points, values_a, (325, 232), method="nearest")
new_value_b = griddata(points, values_b, (325, 232), method="nearest")

该代码允许您计算具有坐标(325, 232)的新点的两个值。如果新点位于由您的点定义的凸包之外,那么您需要设置fill_value参数的值(除非您使用文档中解释的nearest方法)。

英文:

You are dealing with 2D unstructured points, so you could use, for instance, the SciPy's interpolate.griddata function.

I would suggest the following solution. I have rearranged your data in a more convenient way. You can add all the points you want to the points NumPy array.

import numpy as np
from scipy.interpolate import griddata

points = np.array([[333, 223], [325, 243], [339, 244], [319, 228]])

values_a = np.array([45, -45, -45, 45])
values_b = np.array([45, -45, 45, -45])
new_value_a = griddata(points, values_a, (325, 232), method="nearest")
new_value_b = griddata(points, values_b, (325, 232), method="nearest")

The code allows you to compute the two values for a new point having coordinates (325, 232). If the new point lies outside the convex hull defined by your points, then you need to set the value of the fill_value parameter (unless you are using the nearest method as explained in the documentation).

huangapple
  • 本文由 发表于 2023年1月9日 01:28:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定