# Interpolation to create equally spaced value using Python

I am working on a project in which I am required to use Interpolation to create equally spaced position values as the position samples are not equally spaced due to varying train speed.

So, the total length of my data is 93636218. After interpolation, the length has reduced to 5506829. This is creating problem for me as I am unable to insert the interpolated values into my original data. I have used the following code for interpolation:

x = np.array(data['Position [m]'])
y = np.array(sensor data)
spacing = 0.01
position_equally_spaced_x = np.arange(x[0], x[-1], spacing)
sensor data_new = interp1d(x, y)(position_equally_spaced_x)

• Why must the interpolated values be inserted back into the original data? There seems to be something missing in the problem statement. Does the code do the expected interpolation?
– Peter K.
Oct 21, 2022 at 12:53
• I need to insert the column back to the original data because there are other columns of my original data which I need to use for my classification problem Oct 21, 2022 at 13:17
• @PeterK. I believe that she wants to convert an evenly spaced in time sequence into an evenly spaced in position sequence, and that the time samples don't always line up with the desired positions. Oct 21, 2022 at 14:26
• Is there a reason that simple linear interpolation doesn't work? Oct 21, 2022 at 14:27
• Yes exactly @TimWescott. Interpolation is necessary to remove noise in position space. But then it creates problem because the output of filtered position space data is not similar to my original data. And even some values are also not similar when compared to the original data Oct 21, 2022 at 14:36

The length of your interpolation vector is $$L = \frac{x[N-1]-x[0]}{\Delta}$$ where $${\Delta}$$ is the spacing $$N$$ the length of your $$x$$ vector. If you want the length of the interpolation vector to be the same size you need to choose the spacing accordingly, i.e. $$L=N$$. We get
$$\Delta = \frac{x[N-1]-x[0]}{N}$$