0

Allow me to separate this to increasing difficulty questions:


1.

I have some 1d curve, given as a (n,) point array.

I would like to have it re-sampled k times, and have the results come from a cubic spline that passes through all points.

This can be done with interp1d


2. The curve is given at non-same-interval samples as an array of shape (n, 2) where (:, 0) represents the sample time, and (:, 1) represent the sample values.

I want to re-sample the curve at k same-time-intervals.

How can this be done?

I thought i could do t_sampler = interp1d(np.arange(0,k),arr[:, 0]) for the time, then interp1d(t_sampler(np.arange(0,k)), arr[:, 1])

Am I missing something with this?


3.

How can I re-sample the curve at equal distance intervals? (question 2 was equal time intervals)


4.

What if the curve is 3d given by an array of shape (n, 4), where (:,0) are the (non uniform) sampling times, and the rest are the locations sampled?


Sorry for many-questionsin-single-question, they seemed too similar to open a new question for every one.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • Did **2.** work for you? **3** - Did you try making a poly or interp1d swapping the x an y (time and distance) then do the same as **2.**? – wwii Dec 12 '19 at 23:09
  • Look at splprep for curves and distances along rhe curve – ev-br Dec 13 '19 at 10:15
  • @wwii 2. gives me `"A value in x_new is below the interpolation " "range."` on `interp1d(t_sampler(np.arange(0,k)), arr[:, 1])` and I have no Idea why – Gulzar Dec 15 '19 at 07:10
  • Possibly related: [`ValueError: A value in x_new is above the interpolation range.` - ...?](https://stackoverflow.com/questions/45429831/valueerror-a-value-in-x-new-is-above-the-interpolation-range-what-other-re) ... without an [mcve] (emphasis on minimal, reproducible) it will be hard to help. – wwii Dec 15 '19 at 17:13
  • `Sorry for many-questionsin-single-question,` - one problem with asking multiple questions is that there may be SO Q&A's for one or more of them but there won't be an one that covers them all. You should ask one at a time with a [mcve] and if appropriate some or all may get marked duplicate – wwii Dec 15 '19 at 17:47

1 Answers1

0

Partial answer; for 1 and 2 I would do this:

from scipy.interpolate import interp1d
import numpy as np

# dummy data
x = np.arange(-100,100,10)
y = x**2 + np.random.normal(0,1, len(x))

# interpolate:
f = interp1d(x,y, kind='cubic')

# resample at k intervals, with k = 100:
k = 100
# generate x axis:
xnew = np.linspace(np.min(x), np.max(x), k)

# call f on xnew to sample y values:
ynew = f(xnew)

plt.scatter(x,y)
plt.plot(xnew, ynew)
warped
  • 8,947
  • 3
  • 22
  • 49
  • This solves 1. For 2, I'm not so sure, because `interp1d` wants both x and y to be of the same length, thus can't interpolate sampled y on xnew directly – Gulzar Dec 13 '19 at 09:48
  • I think I misunderstood 2 in that case. Would it be possible for you to share examples of the data? – warped Dec 13 '19 at 09:59
  • It is I who didn't understand. this solves 1 and 2. – Gulzar Dec 15 '19 at 20:31