Interpolation giving different results for almost identical arrays

不羁岁月 提交于 2019-12-11 10:13:30

问题


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()

回答1:


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.

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


来源:https://stackoverflow.com/questions/26131383/interpolation-giving-different-results-for-almost-identical-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!