-
Notifications
You must be signed in to change notification settings - Fork 0
/
repnan_documentation.m
executable file
·97 lines (82 loc) · 3.08 KB
/
repnan_documentation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%% |repnan| documentation
% This function replaces |NaN| values in a 1D array by replacing them with
% interpolated or neighboring values. Interpolation schemes assume data are
% evenly spaced. This function does not extrapolate.
%
%% Syntax
%
% x = repnan(x);
% x = repnan(x,method);
%
%% Description
%
% |x = repnan(x)| returns |x| sans NaNs.
%
% |x = repnan(x,method)| specifies a method for replacing the original |x|'s
% NaNs. Methods can be
%
% * |'linear'| (default) performs linear interpolation over missing values
% in |x|,
% * |'nearest'| performs nearest-neighbor interpolation,
% * |'spline'| performs spline interpolation using not-a-knot end conditions,
% * |'pchip'| is shape-preserving piecewise cubic interpolation,
% * |'cubic'| same as |'pchip'|,
% * |'v5cubic'| cubic convolution via Matlab v5 algorithm
% * |'next'| replaces |NaN| values in |x| with the next non-NaN value in
% |x|,
% * |'previous'| replaces |NaN| values in |x| with the previous non-NaN value in
% |x|,
%
%% Examples
% You measured some data in the lab, but the ground connection on your
% sensor's cable was spotty, so you've got some missing data entries.
% Here's the data you have:
t = 1:.5:10;
x = round(10*sin(t));
x([3 7 8 9 16:18]) = NaN
plot(t,x,'o-','linewidth',2,'color',[0.8039 0.6902 0.3294])
box off
axis([0 10 -10 10.5])
xlabel('time (s)')
ylabel('signal amplitude (V)')
text(0,0,' measured data','color',[0.8039 0.6902 0.3294],'fontweight','bold')
hold on
%%
% There are a number of ways to take care of those |NaN| values. One of the
% simplest workarounds is to linearly interpolate between valid |x| values.
% If no interpolation method is specified, |repnan| interpolates linearly:
xlin = repnan(x);
plot(t,xlin,'-','color',[0.6902 0.4039 0.7569])
text(0,-1,' linear interpolation','color',[0.6902 0.4039 0.7569])
%%
% Cubic interpolation might be a better choice for this application:
xcubic = repnan(x,'pchip');
plot(t,xcubic,'-','color',[0.5922 0.7333 0.7490])
text(0,-2,' cubic interpolation','color',[0.5922 0.7333 0.7490])
%%
% Or better yet, spline:
xspline = repnan(x,'spline');
plot(t,xspline,'color',[0.7529 0.3216 0.2863])
text(0,-3, ' spline interpolation','color',[0.7529 0.3216 0.2863])
%%
% For some applications, you might want the nearest neighbor:
xnear = repnan(x,'nearest');
plot(t,xnear,'color',[0.3765 0.2588 0.3725])
text(0,-4, ' nearest neighbor','color',[0.3765 0.2588 0.3725])
%%
% Sometimes you may want to replace missing data with the most recent
% or previous valid value in |x|:
xprev = repnan(x,'previous');
plot(t,xprev,'color',[0.3373 0.3804 0.2353])
text(0,-5,' previous neighbor','color',[0.3373 0.3804 0.2353])
%%
% And other times you may want to replace missing data with the next valid
% value in |x|:
xnext = repnan(x,'next');
plot(t,xnext,'color',[0.4941 0.8078 0.3686])
text(0,-6,' next neighbor','color',[0.4941 0.8078 0.3686])
%% Author Info
%
% Written by <http://www.chadagreene.com Chad A. Greene> of the
% University of Texas Institute for Geophysics
% (<http://www.ig.utexas.edu/people/students/cgreene/ UTIG>), October 31, 2014.