Highlighted

Community Trekker

Joined:

Jul 10, 2015

## How to resample / interpolate unevenly spaced time series?

I have an dataset consisting of two columns, X and Y, resulting from a measurement (cyclic voltammetry using PAR potentiostat). Unfortunately, the spacing of X is not equal (varies between 0.0005 and 0.0015), and the instrument's software cannot fix this. I would like to resample or interpolate the data to get equal spacing of, say, 0.001 in X. Is there a fast way to do it?

2 REPLIES

Staff

Joined:

Jun 23, 2011

## Re: How to resample / interpolate unevenly spaced time series?

The answer to your question depends on how irregular your x values are, and what, precisely, you mean by 'interpolate'.

So the script below is definitely not a recommendation, just an example of the kind of thing that's possible. If you don't need a script, you should be able to follow similar steps manually.

Probably, though, the important question is what will you use the final values for?

NamesDefaultToHere(1);

// (0) Make some evenly spaced data using a particular function

h = Function( {x}, {Default Local}, 0.5 + 0.4 * Sin( 2 * Pi() * x ) );

N = 100;                                                       // Number of (equally-spaced) x values in [0,1]

xVals = Index( 0, 1, 1 / (N - 1) )`;                           // x values as a column vector

yVals = h( xVals ) + J( N, 1, Random Normal( 0, 0.1 ) );       // y values as a column vector

// (1) Randomly decimate three quarters the data to simulate uneven x values (but still on the original grid)

del = RandomIndex(N, Round(3*N/4, 0));

xVals[del] = [];

yVals[del] = [];

dt = NewTable("Uneven x",

NewColumn("x", Numeric, Continuous, Values(xVals)),

NewColumn("y", Numeric, Continuous, Values(yVals))

);

// (2) Fit a kernel smoother and save the prediction formula

biv = dt << Bivariate( Y( :y ), X( :x ) );

biv << Kernel Smoother( 1, 1, 0.48261, 0, {Save Prediction Formula});

// (3) Get the prerdiction formula

pForm = Column(dt, "Loess Predictor for y") << getProperty("Formula");

// (4) Build a new table to hold equally-spaced x values

CMD = Expr(

dt2 = NewTable("Even x",

NewColumn("x", Numeric, Continuous),

NewColumn("Predicted y", Numeric, Continuous, Formula(TBD))

)

);

SubstituteInto(CMD, Expr(TBD), EvalExpr(pForm));

CMD;

// (5) Get the simulated measured data into two vectors

xVals = Column(dt, "x") << GetAsMatrix;

yVals = Column(dt, "y") << GetAsMatrix;

// (6) Find the smallest x increment in the data

xDel = Min(xVals[2::NRow(xVals)] - xVals[1::(NRow(xVals)-1)]);

// (7) Fill the table with the interpolated data using an equal spacing of, say 3*xDel

xVals2 = Index( 0, 1, 3*xDel )`;

Column(dt2, "x") << SetValues(xVals2);

// (8) Look at the result

dt2 << Graph Builder(

Size( 534, 454 ),

Show Control Panel( 0 ),

Variables( X( :x ), Y( :Predicted y ) ),

Elements( Points( X, Y, Legend( 5 ) ) )

);

Community Trekker

Joined:

Jul 10, 2015

Thank you!