MATLAB: Interpolation over NaN in Time Series

Question: How to locally interpolate NaN over short lengths?

I have a time series ("x" data is selectively equal in time "t") that have NaN blocks. For instance:

x = [ 1 2 4 2 3 15 10 NaN NaN NaN NaN 2 4 NaN 19 25] t = [0.1 0.2 0.3 ...etc..] 

I want to interpolate over NaN.

The most basic approach is to simply linearly interpolate from the leftmost data point to the rightmost data point. For instance. a string from x = 10 to x = 2 and 4 NaN values โ€‹โ€‹will be assigned the values โ€‹โ€‹from the string.

The length of the time series is ~ 1.5 million with ~ 10000 NaN, so I do not want to include data (in interpolation) that are far from the places of NaN. Some of the NaNs have a length of 1000-2000.

 X(isnan(X)) = interp1(find(~isnan(X)), X(~isnan(X)), find(isnan(X)), 'linear'); 

will linearly interpolate over NaN using the entire time series.

How would I interpolate locally? Linear should be enough. Perhaps linear interpolation includes several points to the left and to the right of NaN blocks (possibly 100-200 points). Perhaps the natural neighbor or spline algorithm (?) Is more suitable; I have to be careful not to add abnormal behavior to the time series (for example, interpolation adding dummy โ€œpowerโ€ to the frequency).

UPDATE: A time series is a record of the minute temperature for a year. Linear interpolation is sufficient; I just need to fill in the blanks with a length of ~ 6-7 hours of NaN (I have data before the NaN faults and after the NaN spaces).

+6
source share
2 answers

I think this (at least in part) is what you are looking for:

 % example data x = [ 1 2 4 2 3 15 10 NaN NaN NaN NaN 2 4 NaN 19 25]; t = linspace(0.1, 10, numel(x)); % indices to NaN values in x % (assumes there are no NaNs in t) nans = isnan(x); % replace all NaNs in x with linearly interpolated values x(nans) = interp1(t(~nans), x(~nans), t(nans)); 

Please note that here you can easily switch the interpolation method:

 % cubic splines x(nans) = interp1(t(~nans), x(~nans), t(nans), 'spline'); % nearest neighbor x(nans) = interp1(t(~nans), x(~nans), t(nans), 'nearest'); 
+4
source

Consider using inpaint_nans , a very nice tool designed to interpolate NaN elements in an array of 1 or 2-dimensional type using elements other than NaN.It can also extrapolate because it does not use data triangulation. It also allows for different approaches to interpolation.

+3
source

All Articles