how to resample non-linear breath-by-breath data (so inconsistant time)
7 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Star Strider
on 12 Feb 2016
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.
2 Comments
Star Strider
on 12 Feb 2016
It can interpolate with NaN values. The trick is to create a continuous ‘xq’ (query) vector, then delete the entire row (or column) that contains the NaN values, and interpolate (and extrapolate if the NaN is at the end).
Example code:
M = [1:15; 2 8 7 6 NaN 8 4 9 7 2 NaN 7 1 3 5]'; % Original Data
Mn = M(~isnan(M(:,2)),:); % Delete Rows With ‘NaN’
xq = 1:max(M(:,1)); % Query Vector
yq = interp1(Mn(:,1), Mn(:,2), xq, 'linear', 'extrap');
figure(1)
plot(M(:,1), M(:,2), '-bp', 'LineWidth',1.5)
hold on
plot(xq, yq, '--+r', 'LineWidth',1)
hold off
grid
legend('Original Data', 'Interpolated Data')
More Answers (1)
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!