pythoninterpolationencoder

interpolate data at specific index


i'm working with a rotary encoder sensor witch only give back data when is rotates. So when i start a measurment it gives me the a column of timestaps like this (example):

0 1 2 20 21 22

it means in the first two second the sensor rotates but after the first two second it does not move till 20 seconds. i want to fill up the gap between 2 and 20 with 3 to 19 with steps of 1. But i also need te fill it up like this in mijn distance data at these specific indexes. Does anyone know how to do this?


Solution

  • My understanding is that you have data the form of

    times = [0, 1, 2, 20, 21, 22]
    rotations = [2.9, 3.1, 3.4, 2.5, 1, 0.7]
    

    and want the following output:

    times_interpolated = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
    rotations_interpolated = [2.9, 3.1, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 2.5, 1, 0.7]
    

    The solution below is not very elegant, but should be easy to understand. Note that this only works reliably if the times are integers.

    times_interpolated, rotations_interpolated = [], []
    i = 0
    for t in range(times[0], times[-1] + 1):
        if t == times[i + 1]:
            i += 1
        times_interpolated.append(t)
        rotations_interpolated.append(rotations[i])
    

    I hope this helps.