【问题标题】:filter/interpolate x,y points to specific # of points, with a list of specific points to include过滤/插值 x,y 点到特定的点数,并包含要包含的特定点的列表
【发布时间】:2019-07-28 04:28:04
【问题描述】:

我需要一些帮助来过滤/插入python中的一些数据。

我有一个 x,y 点列表(大约 1500 对),我想将其插值到 500 个点,同时保留尽可能多的细节(不仅仅是删除 2/3 的点)。

我还想包含结果数据集必须包含的 x 点列表。

有人知道我该如何处理吗?

【问题讨论】:

  • 我认为你需要的是下采样,而不是插值。使用 numpy 索引:arr2 = arr[1::3]
  • @Masoud,此操作将导致 2/3 的分数下降
  • @Derek Eden,你能判断 x-es 是否均匀分布吗?如果是这样,那么您可以简单地取每 3 个连续点的平均值,例如 arr.reshape(-1,3).mean(axis=-1)
  • @tstanisl 不幸的是,它们不是……有些靠得很近,有些距离更远,但我很欣赏这个建议。我在我的 OP 中添加了一个数字,以更好地描述我想要做的事情do..可能与本地最小值/最大值有关?
  • 这取决于您认为“细节”以及您实际想要删除的内容。我建议首先通过生成您认为最好的 500 x 坐标的函数运行您的数据,然后使用 scipy interpolate 函数之一,例如 interp1d

标签: python numpy scipy filtering interpolation


【解决方案1】:

您可以尝试过滤与相邻顶点共线的顶点。 您可以使用向量叉积进行此检查。

设 p0,p1,p2 为arr 中的连续点。令 A 为从 p1 指向 p0 的向量,令 B 为从 p1 指向 p2 的向量。如果这些向量的叉积为零,则这些向量位于同一条线上,并且可以删除中间点。

代码:

import numpy as np
import matplotlib.pyplot as plt

# First generate dataset similar to yours
# Generate joint points
x0, y0 =np.random.uniform(size=(2,10))
x0.sort() # sort if so it can be used by np.interp()

# Next generate sampling points, add joint points to the set
x = np.hstack((x0, np.random.uniform(size=50)));
x.sort() # sort to have meaningful neghbours

# interpolate at xes
y = np.interp(x, x0, y0)

# compute vector A and B
xa = x[:-2]-x[1:-1]
xb=x[2:]-x[1:-1]
ya=y[:-2]-y[1:-1]
yb=y[2:]-y[1:-1]

# compute cross product
cross = xa*yb-xb*ya

# accept only point which cross-product is above threshold, add the first and the last sample, which were omitted in cross-product calculations
mask = np.hstack([True, abs(cross)>1e-9, True])
# uncomment line below in order to spare additional vertices
# mask[indices_to_keep] = True

# select vertices
xs, ys = x[mask], y[mask]

# draw result
plt.plot(x, y, '-o')
plt.plot(xs, ys, 'x')
plt.show()

结果是:

【讨论】:

  • 抱歉,我的电脑上装有数据集。我会在可能的情况下在上面测试这段代码并提供数据集……不过这看起来很有希望
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2011-12-29
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
相关资源
最近更新 更多