0

I am trying to interpolate my time series data so that it is on an evenly spaced time line and I am using the following code

% interpolate the series
t = 0 : 3 : 400;  
Series 1 = interp1(series 1(:,1),series 1(:,2),t,'linear');

But this error message keeps showing up and I am unsure why

Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.

Error in interp1 (line 183)
F = griddedInterpolant(X,V,method);

This is what part of the time series looks like

series = [  3.585,  0.21
            5.135,  0.08    
            7.4,    0.19
            11.125, -0.15
            13.175, -0.27
            16.045, -0.26   
            20.37,  -0.12
            25.24,  0.02
            27.58,  0.05
            30.38,  0.02
            33.515  0.1];
Community
  • 1
  • 1
Tina
  • 23
  • 6
  • Your `interp1` call isn't valid MATLAB syntax. Did you try `interp1(series(:,1), series(:,2), t ,'linear');`? – Suever Apr 15 '16 at 18:50

1 Answers1

0

You are getting this error because you apparently have duplicate times in your input data. You will need to handle these duplicates in some way. One way to do this, would be to only use the first of the duplicate values for each time. You can easily do this with unique.

[uniqueTimes, ind] = unique(series(:,1));
vals = series(ind, 2);

%// Values at which we want to interpolate
tt = linspace(min(uniqueTimes), max(uniqueTimes), 100);

%// Perform interpolation
newval = interp1(uniqueTimes, vals, tt, 'linear');

If instead you want all values at the same sampled time to be averaged together, you can use unique combined with accumarray to do this for you.

%// Average all values that have the same time
[uniqueTimes, ~, inds] = unique(series(:,1));
vals = accumarray(inds, series(:,2), [], @mean);

%// Values at which we want to inteprolate
tt = linspace(min(uniqueTimes), max(uniqueTimes), 100);

%// Perform interpolation
newval = interp1(uniqueTimes, vals, tt, 'linear');
Suever
  • 64,497
  • 14
  • 82
  • 101