0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 function varargout = consolidate(varargin)
0038
0039
0040 if utils.helper.isinfocall(varargin{:})
0041 varargout{1} = getInfo(varargin{3});
0042 return
0043 end
0044
0045 import utils.const.*
0046 utils.helper.msg(msg.MNAME, 'running %s/%s', mfilename('class'), mfilename);
0047
0048
0049 in_names = cell(size(varargin));
0050 for ii = 1:nargin,in_names{ii} = inputname(ii);end
0051
0052
0053 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
0054 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
0055
0056 if numel(as) < 2
0057 error('### Consolidate requires at least two time-series AOs to work.');
0058 end
0059
0060
0061 bs = copy(as, nargout);
0062 na = numel(bs);
0063
0064
0065 pl = combine(pl, getDefaultPlist);
0066
0067
0068 inhists = [];
0069 for j=1:na
0070 if ~isa(bs(j).data, 'tsdata')
0071 bs(j) = [];
0072 warning('!!! Skipping AO %s - it''s not a time-series AO.', bs(j).name);
0073 else
0074
0075 inhists = [inhists bs(j).hist];
0076 end
0077 end
0078
0079
0080 utils.helper.msg(msg.PROC1, 'drop duplicates');
0081 for j=1:na
0082 utils.helper.msg(msg.PROC2, 'processing %s', bs(j).name);
0083 dropduplicates(bs(j));
0084 end
0085
0086
0087 utils.helper.msg(msg.PROC1, 'interpolate missing samples');
0088 for j=1:na
0089 utils.helper.msg(msg.PROC2, 'processing %s', bs(j).name);
0090 interpmissing(bs(j));
0091 end
0092
0093
0094 utils.helper.msg(msg.PROC1, 'fixing uneven sample rates');
0095 for j=1:na
0096 utils.helper.msg(msg.PROC2, 'processing %s', bs(j).name);
0097 fixfs(bs(j));
0098 end
0099
0100 utils.helper.msg(msg.PROC1, 'resample to same fs');
0101
0102
0103 fs = find(pl, 'fs');
0104 if isempty(fs)
0105
0106 fs = 0;
0107 for j=1:na
0108 if bs(j).data.fs > fs
0109 fs = bs(j).data.fs;
0110 end
0111 end
0112 end
0113 utils.helper.msg(msg.PROC2, 'resampling all time-series to an fs of %f', fs);
0114
0115 for j=1:na
0116
0117 [P,Q] = utils.math.intfact(fs,bs(j).data.fs);
0118 if P > 100 || Q > 100
0119 utils.helper.msg(msg.PROC2, 'resampling factor too high [%g/%g]. Trying interpolation', P, Q);
0120 N = length(bs(j).data.getX);
0121 t0 = bs(j).data.getX(1);
0122 t = linspace(t0, t0+(P*N/Q-1)/fs, P*N/Q);
0123 interp(bs(j), plist('vertices', t));
0124 else
0125 resample(bs(j), plist('fsout', fs));
0126 end
0127 end
0128
0129
0130
0131 utils.helper.msg(msg.PROC1, 'resample to same grid');
0132
0133 for j=1:na
0134 N = length(bs(j).data.getX);
0135 t = linspace(0, (N-1)/fs, N);
0136 interp(bs(j), plist('vertices', t));
0137 end
0138
0139
0140
0141 start = 0;
0142 for j=1:na
0143 dstart = bs(j).data.t0.utc_epoch_milli/1000 + bs(j).data.getX(1);
0144 if dstart > start
0145 start = dstart;
0146 end
0147 end
0148
0149
0150 stop = 1e20;
0151 for j=1:na
0152 dstop = floor(bs(j).data.t0.utc_epoch_milli/1000 + bs(j).data.getX(end));
0153 if dstop < stop
0154 stop = dstop;
0155 end
0156 end
0157
0158
0159
0160 utils.helper.msg(msg.PROC1, 'truncate all vectors');
0161 utils.helper.msg(msg.PROC2, 'truncating vectors on interval [%g,%g]', start, stop);
0162
0163
0164 bs = split(bs, plist('split_type', 'times', 'times', [start stop]));
0165
0166 nsecs = [];
0167 for j=1:na
0168 if isempty(nsecs)
0169 nsecs = bs(j).data.nsecs;
0170 end
0171 if nsecs ~= bs(j).data.nsecs
0172 error('### Something went wrong with the truncation. Vectors don''t span the same time period.');
0173 end
0174 end
0175
0176
0177
0178 for j=1:na
0179 bs(j).addHistory(getInfo, pl, ao_invars(j), inhists(j));
0180 bs(j).setName(sprintf('%s(%s)', mfilename, ao_invars{j}), 'internal');
0181 end
0182
0183 if nargout > 0
0184 varargout{1} = bs;
0185 end
0186 end
0187
0188
0189
0190
0191 function ii = getInfo(varargin)
0192 if nargin == 1 && strcmpi(varargin{1}, 'None')
0193 sets = {};
0194 pl = [];
0195 else
0196 sets = {'Default'};
0197 pl = getDefaultPlist;
0198 end
0199
0200 ii = minfo(mfilename, 'ao', '', utils.const.categories.sigproc, '$Id: consolidate.m,v 1.14 2008/09/05 11:05:28 ingo Exp $', sets, pl);
0201 end
0202
0203
0204
0205
0206 function pl_default = getDefaultPlist()
0207 pl_default = plist('fs', []);
0208 end
0209