FillGaps_ez

Version 1.0.2 (6.05 KB) by wfH
FILLGAPS_EZ fills gaps (missing values) in an array using recursive interpolation (1D) in forward or backward direction.
80 Downloads
Updated 18 Jan 2019

View License

FillGaps_ez fills gaps (missing values) in an array using recursive interpolation (1D) in forward or backward direction.
This function can return a logical array corresponding to the entries of input data that were filled.
It also returns a log about how ugly the data is.
User can specify a maximum gap to determine the gap to be filled or not.
Additionally, user can specify a minimum number of valid samples when interpolation.
NOTE. Smoothing techniques may be necessary when the interpolated data is too ugly (I use a median filter for example. Modify it as you need).

Syntax:
[newdata] = FillGaps(data)
[newdata] = FillGaps(data, dim)
[newdata, rmse, event, nanmask] = FillGaps(..., fc)

Inputs:
data - (double | single ), any dimension
Input data to be interpolated

dim - integer scalar
Dimension to operate along. If no value is specified, then the default is the first array dimension whose size does not equal 1.
If 'dim' is a negative value, recursive interpolation is performed in BACKWARD direction.
NOTE. The forward-interpolated data might be quite different from the backward-interpolated data when appling smoothing techniques (e.q. medfilt1).

fc - struct
FillCode that specifies the behaviour of interpolation.
A FillCode must contains fields namely 'maxgap', 'minseg', 'show', 'method'
Or FillCode would be set by default.
'maxgap' specifies maximum number of gap. Any gap exceeding this value will not be filled.
'minseg' specifies minimum number of samples. Any segment shorter than this value is UNSTABLE and would be regarded as a gap.
'show' specifies ploting or not. Default is false.
Default interpolation method is 'spline'. Change as needed.

Outputs:
dataf - the same size and type as the input data

eventflag - same size as the input data
event1 for the 'Left-truncated data'
event2 for the 'Right-truncated data'
event3 for the 'too-short segments data'
event9 for the 'Enormous gaps found'
event0 for All values are zero or NaN in the data

nanmask - logical, same size as the input data
It corresponds to the entries of input data that were filled.

Examples:
% 1. Create data with gaps
x = linspace(-4,8, 800);
y1 = sin(x);
y1([1:3, 25:32, 228:240, 410:425, 615:620, 624:628, 700:711, 720:725, 730:740, 775:790, 799]) = nan;
y2 = cos(x);
y2([228:240, 624:628, 720:725, 730:740, 795:799]) = nan;
y2([2:4, 100:105, 334:344, 501:505]) = rand(25,1);
y3 = (.5+rand(1,800)/2).*sin(x);
y4 = (.5+rand(1,800)/2).*cos(x);
y3([1:3, 25:32, 228:240, 410:425, 615:620, 624:628, 700:711, 720:725, 730:740, 775:790, 799]) = nan;
y4([228:240, 624:628, 720:725, 730:740, 795:799]) = nan;
YY = cat(4,cat(3,y1,y2), cat(3,y3,y4));
% 2. Set FillCode
fc = struct('maxgap', 15, 'minseg', 5, 'show', true, 'method', 'cubic');
% 3. Fill gaps easily !
y1f = FillGaps_ez(y1,[],fc); % forward interpolation along the first nonsingleton dimension
y3fp = FillGaps_ez(y3,2,fc); % forward interpolation
y3fn = FillGaps_ez(y3,-2,fc); % backward interpolation
isequal(y3fn(~isnan(y3fn)), y3fp(~isnan(y3fp))) %
YYf = FillGaps_ez(YY,2,fc); % multidimensional data with quick visual inspection

Cite As

wfH (2024). FillGaps_ez (https://www.mathworks.com/matlabcentral/fileexchange/70028-fillgaps_ez), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Descriptive Statistics in Help Center and MATLAB Answers
Acknowledgements

Inspired by: RunLength, subtightplot

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.2

typo

1.0.1

typo

1.0.0