# Resampling from variable to fixed-rate [closed]

I have a gyroscope which reports x,y,z values to my embedded device. The rate that one sample appears is variable and has a jitter of around 10ms.

However I later want to have a log file with a fixed sample rate (higher than the input sample rate). I found this post What is an algorithm to re-sample from a variable rate to a fixed rate?

Unfortunately it seems way too complicated compared to what I want to do. For me it would be fine simply interpolating linearly between the samples, but I'm missing the correct algorithm to do so. Is there any common way on how to interpolate a variable sample rate to a fixed sample rate?

## closed as unclear what you're asking by Stanley Pawlukiewicz, lennon310, MBaz, A_A, jojek♦Oct 18 '18 at 12:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• practical means a solution given a set of trade offs. you really have not identified what made those solutions in the post you refer to impractical or much about your particular situation. I didn’t see anything that would preclude python in any of those answers. I didn’t see anyone propose a Kalman Filter so you can add that to the candidate list – Stanley Pawlukiewicz Oct 16 '18 at 13:23
• Yeah I was unsure on how to correctly phrase my question. I edited my original post maybe it is a bit clearer now. Thanks for the Kalman suggestion in any way though. – binloan Oct 24 '18 at 8:02

If you want to do it yourself, programming a linear interpolation is really simple. You interpolate between sample 1 and sample 2 by calculating the slope to generate a line between the samples (y are your measurements and x the times)

\begin{align} m &= \frac{y_1 - y_2}{x_2 - x_1} \\ y_1(x) &= m (x - x_1) + y_1 \end{align}

this is the linear interpolation between sample 1 and 2, it allows you to calculate the interpolated values for any point in time between sample 1 and 2. After that you just have to repeat this for sample 2 and 3 and so on. It's always the same, that means it comes down to writing one function and a loop. Just google linear interpolation and you can probably find some nice animations and youtube videos that visualize the process.

If you don't want to write the code yourself Scipy offers you multiple ways of interpolating data you can find a nice documentation here:

https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

the first way is probably the simplest (i just stole the examples from the page there):

from scipy.interpolate import interp1d

x = % array with the timestamps of your measurements

f1 = interp1d(x, y) % default is linear
f2 = interp1d(x, y, kind='cubic')

% xnew contains all points at which you want to sample the interpolated function
% linspace should go from your start time to your end time
xnew = np.linspace(0, 10, num=1001, endpoint=True)
ynew1 = f1(xnew) % generate the y values for all x values in xnew
ynew2 = f2(xnew)


now you can just plot(xnew, ynew) and it should look interpolated.

spline interpolation is another popular choice you can find it a bit further down with another example you could try that too if you'd like to. I personally like spline the most but none of them will magically generate data that means there really isn't a magical solution that is best, it depends on what you want to do with the result.

• Thanks for your detailed answer. However I think you misread the part that I have a variable timing window. I can't just interpolate with the same value between to samples since they might once be 20ms apart and other times be 5ms apart. – binloan Oct 22 '18 at 8:14
• no, that's why you have to input X and Y of your data in the functions, x and y being pairs where x is the time you sampled at and y the value you measured at time x. – user38202 Oct 23 '18 at 5:56
• True - I misread myself. That's a very good example thanks for you clarification! – binloan Oct 24 '18 at 7:51