对于几乎相同的数组,插值给出不同的结果

编程入门 行业动态 更新时间:2024-10-23 19:20:41
本文介绍了对于几乎相同的数组,插值给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试对两个二维数组进行插值。

I'm trying to interpolate two 2-dimensional arrays.

原始数组在下图中显示为蓝色。第二个显示为红色的是相同的蓝色排列,但向上移动并稍微向右移动,并且从右上角移出了一些点。

The original array is shown in blue in the image below. The second one shown, in red, is the same array in blue but moved up and slightly to the right, and with a handful of points removed from the top right end.

我使用相同的过程(请参见问题末尾)用于在现有的点之间插入额外的点,我得到以下结果:

I use the same process (see end of question) for interpolating extra points in between the existing ones and I get this result:

第二个数组(红色),现在已插值,

The second array (in red), now interpolated, is clearly wrong in its lower part.

为什么会发生这种情况,如何使这两个插值都正确?

Why is this happening and how can I make it so that both interpolations are correct?

MWE:

import matplotlib.pyplot as plt def interp(x, y): ''' Returns interpolated x, y arrays. ''' N = 1000 t = np.linspace(0, len(x), N) xi = np.interp(t, np.arange(len(x)), x) yi = np.interp(t, np.arange(len(x)), y) intp = np.asarray([xi, yi]) return intp # Data a = [[1.6050000000000004, 1.491999999999999, 1.4189999999999987, 1.3709999999999987, 1.3330000000000002, 1.299999999999999, 1.2650000000000006, 1.229000000000001, 1.186, 1.1319999999999997, 0.9959999999999996, 0.8200000000000003, 0.6580000000000004, 0.30200000000000005, 0.26400000000000023, 0.19399999999999995, 0.125, 0.06899999999999995, 0.03100000000000014, 0.006000000000000005, -0.0020000000000000018, -0.04499999999999993, -0.07400000000000007, -0.08300000000000007, -0.10300000000000004, -0.10799999999999998, -0.121, -0.122, -0.132, -0.13699999999999996, -0.139, -0.14300000000000002, -0.1449999999999999, -0.14700000000000002, -0.14700000000000002, -0.14700000000000002, -0.1439999999999999, -0.14100000000000001, -0.1379999999999999, -0.1379999999999999, -0.137, -0.13599999999999968, -0.13500000000000023, -0.13600000000000012, -0.13999999999999968, -0.14700000000000024, -0.1529999999999998, -0.1589999999999998, -0.1589999999999998, -0.15399999999999991, -0.1469999999999998, -0.14000000000000012, -0.133, -0.125, -0.11800000000000033, -0.1080000000000001, -0.09799999999999986, -0.0950000000000002, -0.08599999999999985, -0.07500000000000018, -0.06099999999999994, -0.040999999999999925, -0.007000000000000117, 0.019000000000000128, 0.05600000000000005, 0.10899999999999999, 0.18599999999999994, 0.28200000000000003, 0.3939999999999997, 0.54, 0.7159999999999997, 0.8460000000000001, 0.9720000000000002, 1.023, 1.101, 1.1420000000000001, 1.178, 1.2109999999999999, 1.243, 1.276, 1.306, 1.3379999999999999, 1.3649999999999998, 1.3309999999999997, 1.297, 1.265, 1.232, 1.1989999999999998, 1.1589999999999998, 1.123, 1.0060000000000002, 0.8420000000000001, 0.7129999999999996, 0.5959999999999996, 0.5219999999999998, 0.45500000000000007, 0.39600000000000035, 0.32099999999999973, 0.2520000000000002, 0.19700000000000006, 0.14700000000000024, 0.10000000000000009, 0.06300000000000017, 0.10400000000000009, 0.15700000000000003, 0.2200000000000002, 0.28600000000000003, 0.3620000000000001, 0.4299999999999997, 0.4979999999999998, 0.5699999999999998, 0.6589999999999998, 0.754, 0.8720000000000003, 1.028, 1.105, 1.1969999999999998, 1.2349999999999999, 1.2679999999999998, 1.3079999999999998, 1.335, 1.3689999999999998, 1.4049999999999998, 1.44, 1.475, 1.5100000000000002, 1.541, 1.573, 1.601, 1.6269999999999998, 1.6509999999999998, 1.6730000000000005, 1.694, 1.6750000000000003, 1.6910000000000003, 1.7069999999999999, 1.706], [12.488, 11.541, 10.944, 10.473, 10.078, 9.647, 9.269, 8.87, 8.439, 8.026, 7.101, 6.145, 5.359, 3.366, 3.077, 2.737, 2.438, 2.197, 2.008, 1.856, 1.807, 1.438, 1.068, 0.92, 0.525, 0.407, 0.053, 0.032, -0.237, -0.448, -0.493, -0.715, -0.92, -1.015, -1.123, -1.31, -1.493, -1.672, -1.846, -1.862, -1.931, -2.007, -2.049, -2.07, -2.063, -2.018, -1.985, -1.963, -1.955, -1.94, -2.007, -2.163, -2.296, -2.425, -2.55, -2.675, -2.802, -2.844, -2.931, -3.02, -3.113, -3.205, -3.286, -3.319, -3.334, -3.331, -3.308, -3.263, -3.191, -3.087, -2.945, -2.79, -2.563, -2.496, -2.576, -2.684, -2.795, -2.905, -3.007, -3.113, -3.218, -3.316, -3.421, -3.328, -3.23, -3.125, -3.024, -2.928, -2.856, -2.858, -3.119, -3.33, -3.457, -3.546, -3.597, -3.641, -3.68, -3.727, -3.761, -3.787, -3.805, -3.819, -3.837, -3.911, -3.942, -3.946, -3.932, -3.908, -3.88, -3.852, -3.815, -3.767, -3.708, -3.615, -3.442, -3.308, -3.19, -3.23, -3.312, -3.416, -3.477, -3.59, -3.69, -3.793, -3.903, -3.995, -4.071, -4.152, -4.213, -4.268, -4.35, -4.4, -4.452, -4.424, -4.469, -4.494, -4.492]] b = [[2.0069999999999997, 2.006, 1.9910000000000003, 1.994, 1.9750000000000003, 1.9730000000000005, 1.9509999999999998, 1.9269999999999998, 1.901, 1.873, 1.841, 1.8100000000000003, 0.5200000000000002, 0.457, 0.5860000000000001, 0.4040000000000001, 0.6620000000000001, 1.7750000000000001, 0.7299999999999998, 0.7979999999999998, 0.36300000000000016, 0.4000000000000001, 0.8699999999999999, 0.44700000000000023, 1.74, 0.49700000000000005, 0.9589999999999999, 0.5520000000000003, 0.6209999999999998, 1.054, 1.7049999999999998, 0.6960000000000004, 0.7550000000000001, 1.1720000000000004, 0.8219999999999998, 1.6689999999999998, 0.8959999999999997, 1.635, 1.0129999999999997, 1.328, 1.6649999999999998, 1.6079999999999999, 0.35600000000000004, 0.409, 1.1420000000000001, 1.6309999999999998, 0.3190000000000001, 1.638, 1.5679999999999998, 0.48599999999999993, 1.405, 0.29299999999999987, 0.5820000000000001, 1.597, 1.535, 1.606, 0.25900000000000006, 0.6939999999999997, 1.4969999999999999, 1.565, 1.3060000000000003, 0.23900000000000005, 1.576, 0.8400000000000001, 1.532, 0.2249999999999998, 1.5430000000000001, 1.0159999999999998, 0.21400000000000013, 1.4989999999999999, 1.511, 1.423, 1.4589999999999999, 0.2049999999999998, 0.20200000000000012, 1.478, 1.1460000000000001, 1.4420000000000002, 0.1919999999999999, 1.401, 1.2720000000000002, 0.18199999999999966, 1.323, 0.175, 0.16699999999999998, 0.15999999999999986, 0.16399999999999987, 0.1600000000000003, 0.16499999999999976, 0.15299999999999975, 0.1640000000000003, 0.1530000000000002, 0.14700000000000019, 0.14100000000000018, 0.14100000000000018, 0.14600000000000007, 0.16299999999999998, 0.1620000000000001, 0.1620000000000001, 0.15899999999999997, 0.15600000000000008, 0.15299999999999997, 0.15299999999999997, 0.15299999999999997, 0.15500000000000008, 0.15699999999999997, 0.16099999999999998, 0.16300000000000003, 0.16799999999999998, 0.178, 0.179, 0.192, 0.19699999999999995, 0.21699999999999992, 0.22599999999999992, 0.25500000000000006, 0.298, 0.306, 0.3310000000000001, 0.36899999999999994, 0.425, 0.49399999999999994, 0.5640000000000003, 0.6020000000000001, 0.9580000000000004, 1.1200000000000003, 1.2959999999999996, 1.4319999999999997], [8.836, 8.838000000000001, 8.861, 8.878, 8.906, 8.93, 8.98, 9.062000000000001, 9.117, 9.178, 9.259, 9.335, 9.384, 9.388, 9.398, 9.419, 9.422, 9.427, 9.45, 9.478, 9.493, 9.511, 9.515, 9.525, 9.536999999999999, 9.543, 9.563, 9.568999999999999, 9.603, 9.622, 9.64, 9.65, 9.689, 9.715, 9.733, 9.74, 9.784, 9.853, 9.873000000000001, 9.888, 9.909, 9.914, 9.996, 9.999, 10.0, 10.002, 10.011, 10.014, 10.018, 10.022, 10.022, 10.044, 10.067, 10.1, 10.1, 10.112, 10.125, 10.139, 10.14, 10.205, 10.211, 10.217, 10.217, 10.243, 10.306000000000001, 10.31, 10.323, 10.385, 10.399000000000001, 10.402000000000001, 10.425, 10.472, 10.474, 10.486, 10.528, 10.535, 10.54, 10.646, 10.655000000000001, 10.754, 10.767, 10.780000000000001, 10.834, 10.905000000000001, 11.034, 11.167, 11.26, 11.267, 11.281, 11.312000000000001, 11.323, 11.323, 11.345, 11.367, 11.375, 11.39, 11.399000000000001, 11.468, 11.484, 11.658, 11.837, 12.02, 12.207, 12.315, 12.41, 12.615, 12.837, 12.882, 13.093, 13.362, 13.383000000000001, 13.737, 13.855, 14.25, 14.398, 14.768, 15.137, 15.186, 15.338000000000001, 15.527000000000001, 15.768, 16.067, 16.407, 16.696, 18.689, 19.475, 20.431, 21.356]] f, (ax1, ax2) = plt.subplots(1, 2) ax1.set_title('Not interpolated') ax1.scatter(a[0], a[1], s=30, c='blue', lw=0.1) ax1.scatter(b[0], b[1], s=30, c='red', lw=0.1) # Get interpolated arrays. a_i = interp(*a) b_i = interp(*b) ax2.set_title('Interpolated') ax2.scatter(a_i[0], a_i[1], s=30, c='blue', lw=0.1) ax2.scatter(b_i[0], b_i[1], s=30, c='red', lw=0.1) plt.show()

推荐答案

代码中的问题不是插值函数,而是数据本身。 b 中的点不按适当顺序排列,即,如果通过变量 t 参数化曲线,那么 b 中的点不是按 t 的顺序递增。

The problem in your code is not the interpolation function, but the data itself. The points in b are not in the "proper" order, i.e. if you parametrize the curve by a variable t, then the points in b are not in the order of increasing t.

如果您将b转换为 a ,并且去除了某些元素,则可以看到以下内容:

You can see this if you take b to be a translated by some vector and with some elements removed:

print np.array(a).shape, np.array(b).shape b = np.array(a) + np.array([[2, 3]]).T b = b[:, [x for x in range(len(b[0])) if x < 100 or (x > 100 and x % 2)]] print np.array(a).shape, np.array(b).shape

更多推荐

对于几乎相同的数组,插值给出不同的结果

本文发布于:2023-11-10 02:16:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   插值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!