How to generate equispaced interpolating values

前端 未结 5 536
既然无缘
既然无缘 2020-12-25 08:01

I have a list of (x,y) values that are not uniformly spaced. Here is the archive used in this question.

I am able to interpolate between the values but what I get ar

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-25 08:31

    Let's first consider a simple case. Suppose your data looked like the blue line, below.

    enter image description here

    If you wanted to select equidistant points that were r distance apart, then there would be some critical value for r where the cusp at (1,2) is the first equidistant point.

    If you wanted points that were greater than this critical distance apart, then the first equidistant point would jump from (1,2) to some place very different -- depicted by the intersection of the green arc with the blue line. The change is not gradual.

    This toy case suggests that a tiny change in the parameter r can have a radical, discontinuous affect on the solution.

    It also suggests that you must know the location of the ith equidistant point before you can determine the location of the (i+1)-th equidistant point.

    So it appears an iterative solution is required:

    import numpy as np
    import matplotlib.pyplot as plt
    import math
    
    x, y = np.genfromtxt('data', unpack=True, skip_header=1)
    # find lots of points on the piecewise linear curve defined by x and y
    M = 1000
    t = np.linspace(0, len(x), M)
    x = np.interp(t, np.arange(len(x)), x)
    y = np.interp(t, np.arange(len(y)), y)
    tol = 1.5
    i, idx = 0, [0]
    while i < len(x):
        total_dist = 0
        for j in range(i+1, len(x)):
            total_dist += math.sqrt((x[j]-x[j-1])**2 + (y[j]-y[j-1])**2)
            if total_dist > tol:
                idx.append(j)
                break
        i = j+1
    
    xn = x[idx]
    yn = y[idx]
    fig, ax = plt.subplots()
    ax.plot(x, y, '-')
    ax.scatter(xn, yn, s=50)
    ax.set_aspect('equal')
    plt.show()
    

    enter image description here

    Note: I set the aspect ratio to 'equal' to make it more apparent that the points are equidistant.

提交回复
热议问题