MATLAB: How to resample non-linear breath-by-breath data (so inconsistant time)

inconsistent time seriesresampletimeseries

Hi all,
I have a question regarding my data (example random numbers). If the subject had two breaths within a second there are two datapoints at the same time value. I think the biggest problem is the non-linearity of the data?
x=[0 1 2 5 7 8 9 15 19 20 21 22 23 24 27 30 31 32 33 34 35 36 36 38]
y=[1 2 5 3 4 8 6 5 8 5 8 6 85 85 66 44 55 66 88 77 99 88 55 66]
However, I need to resample to a 5 second time scale with it's resampled y data
x2=[0 5 10 15 20]
I wanted to use timeseries but this gave me the following error:
Warning: Cannot extrapolate
> In tsinterp>linearinter (line 145)
In tsinterp>localInterpolate (line 240)
In tsinterp (line 89)
In tsarrayFcn (line 26)
In tsdata.interpolation/interpolate (line 72)
In timeseries/resample (line 106)

Best Answer

For your sample data, this works:
x=[0 1 2 3 4 4 5 6 7 7 8 9 10 11 12 13 14 14 15 16 17 18 19 20];
y=[1 2 5 3 4 8 6 5 8 5 8 6 85 85 66 44 55 66 88 77 99 88 55 66];
xi=[0 5 10 15 20];
dup = find(diff([eps x]) == 0); % Find Duplicated ‘x’ Values
x(dup) = x(dup)+1E-8; % Add ‘sqrt(eps)’ To Duplicated Values
yi = interp1(x, y, xi, 'linear', 'extrap'); % Interploate
yi =
1 6 85 88 66
If you have more than one repeated consecutive ‘x’ value, this gets a bit more involved, but the same approach applies.