Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/interpmissing.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % INTERPMISSING interpolate missing samples in a time-series. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % INTERPMISSING interpolate missing samples in a time-series. Missing samples | |
5 % are identified as being those where the time-span between one | |
6 % sample and the next is larger than d/fs where d is a | |
7 % tolerance value. Missing data is then placed in the gap in | |
8 % steps of 1/fs. Obviously this is only really correct for | |
9 % evenly sampled time-series. | |
10 % | |
11 % CALL: bs = interpmissing(as) | |
12 % | |
13 % INPUTS: as - array of analysis objects | |
14 % pl - parameter list (see below) | |
15 % | |
16 % OUTPUTS: bs - array of analysis objects, one for each input | |
17 % | |
18 % | |
19 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'interpmissing')">Parameters Description</a> | |
20 % | |
21 % VERSION: $Id: interpmissing.m,v 1.30 2011/04/08 08:56:16 hewitson Exp $ | |
22 % | |
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
24 | |
25 function varargout = interpmissing(varargin) | |
26 | |
27 % Check if this is a call for parameters | |
28 if utils.helper.isinfocall(varargin{:}) | |
29 varargout{1} = getInfo(varargin{3}); | |
30 return | |
31 end | |
32 | |
33 import utils.const.* | |
34 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
35 | |
36 % Collect input variable names | |
37 in_names = cell(size(varargin)); | |
38 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
39 | |
40 % Collect all AOs and plists | |
41 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
42 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
43 | |
44 % Decide on a deep copy or a modify | |
45 bs = copy(as, nargout); | |
46 | |
47 % Combine plists | |
48 pl = parse(pl, getDefaultPlist); | |
49 | |
50 | |
51 % Get tolerance | |
52 dtol = find(pl, 'd'); | |
53 | |
54 % Get only tsdata AOs | |
55 for j=1:numel(bs) | |
56 if isa(bs(j).data, 'tsdata') | |
57 | |
58 % capture input history | |
59 ih = bs(j).hist; | |
60 | |
61 % find missing samples | |
62 t = []; | |
63 d = diff(bs(j).data.getX); | |
64 idxs = find(d>dtol/bs(j).data.fs); | |
65 utils.helper.msg(msg.PROC1, 'found %d data gaps', numel(idxs)); | |
66 | |
67 % create new time grid | |
68 count = 0; | |
69 fs = bs(j).data.fs; | |
70 for k=1:numel(idxs) | |
71 idx = idxs(k); | |
72 if isempty(t) | |
73 t = bs(j).data.getX(1:idxs(1)); | |
74 end | |
75 % now add samples at 1/fs until we are within 1/fs of the next sample | |
76 gap = bs(j).data.getX(idx+1) - bs(j).data.getX(idx) - 1/fs; | |
77 tfill = [[1/fs:1/fs:gap] + bs(j).data.getX(idx)].'; | |
78 count = count + numel(tfill); | |
79 | |
80 if k==numel(idxs) | |
81 t = [t; tfill; bs(j).data.getX(idx+1:end)]; | |
82 else | |
83 t = [t; tfill; bs(j).data.getX(idx+1:idxs(k+1))]; | |
84 end | |
85 end | |
86 utils.helper.msg(msg.PROC1, 'filled with %d samples', count); | |
87 | |
88 % now interpolate onto this new time-grid | |
89 if ~isempty(t) | |
90 bs(j).interp(plist('vertices', t, 'method', find(pl, 'method'))); | |
91 bs(j).name = sprintf('interpmissing(%s)', ao_invars{j}); | |
92 % Add history | |
93 bs(j).addHistory(getInfo('None'), pl, ao_invars(j), ih); | |
94 % clear errors | |
95 bs(j).clearErrors; | |
96 else | |
97 utils.helper.msg(msg.PROC1, 'no missing samples found in %s - no action performed.', ao_invars{j}); | |
98 end | |
99 else | |
100 utils.helper.msg(msg.PROC1, 'skipping AO %s - it''s not a time-series AO.', ao_invars{j}); | |
101 end | |
102 end | |
103 | |
104 % Set output | |
105 if nargout == numel(bs) | |
106 % List of outputs | |
107 for ii = 1:numel(bs) | |
108 varargout{ii} = bs(ii); | |
109 end | |
110 else | |
111 % Single output | |
112 varargout{1} = bs; | |
113 end | |
114 end | |
115 | |
116 %-------------------------------------------------------------------------- | |
117 % Get Info Object | |
118 %-------------------------------------------------------------------------- | |
119 function ii = getInfo(varargin) | |
120 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
121 sets = {}; | |
122 pl = []; | |
123 else | |
124 sets = {'Default'}; | |
125 pl = getDefaultPlist; | |
126 end | |
127 % Build info object | |
128 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: interpmissing.m,v 1.30 2011/04/08 08:56:16 hewitson Exp $', sets, pl); | |
129 end | |
130 | |
131 %-------------------------------------------------------------------------- | |
132 % Get Default Plist | |
133 %-------------------------------------------------------------------------- | |
134 function plout = getDefaultPlist() | |
135 persistent pl; | |
136 if exist('pl', 'var')==0 || isempty(pl) | |
137 pl = buildplist(); | |
138 end | |
139 plout = pl; | |
140 end | |
141 | |
142 function pl = buildplist() | |
143 pl = plist(); | |
144 | |
145 % d | |
146 p = param({'d','The time interval tolerance for finding missing samples.'}, {1, {1.5}, paramValue.OPTIONAL}); | |
147 pl.append(p); | |
148 | |
149 % Interpolation method | |
150 pli = ao.getInfo('interp').plists; | |
151 p = pli.params(pli.getIndexForKey('method')); | |
152 pl.append(p); | |
153 | |
154 end |