Home > classes > @ao > consolidate.m

consolidate

PURPOSE ^

CONSOLIDATE resamples all input AOs onto the same time grid.

SYNOPSIS ^

function varargout = consolidate(varargin)

DESCRIPTION ^

 CONSOLIDATE resamples all input AOs onto the same time grid.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 CONSOLIDATE resamples all input AOs onto the same time grid and truncates all
             time-series to start at the maximum start time of the inputs and end
             at the minimum stop time of the inputs.

 CALL:       >> bs = consolidate(as)

 INPUTS:     as  - array of analysis objects
             pl  - parameter list (see below)

 OUTPUTS:    bs  - array of analysis objects, one for each input

 PARAMETER LIST:
             'fs'    - specify a new sample rate for the resample grid.


 M-FILE INFO: Get information about this methods by calling
              >> ao.getInfo('consolidate')

              Get information about a specified set-plist by calling:
              >> ao.getInfo('consolidate', 'None')

 VERSION:     $Id: consolidate.m,v 1.12 2008/08/08 12:29:16 hewitson Exp $

 HISTORY:     21-05-2008 M Hewitson
                 Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % CONSOLIDATE resamples all input AOs onto the same time grid.
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % CONSOLIDATE resamples all input AOs onto the same time grid and truncates all
0005 %             time-series to start at the maximum start time of the inputs and end
0006 %             at the minimum stop time of the inputs.
0007 %
0008 % CALL:       >> bs = consolidate(as)
0009 %
0010 % INPUTS:     as  - array of analysis objects
0011 %             pl  - parameter list (see below)
0012 %
0013 % OUTPUTS:    bs  - array of analysis objects, one for each input
0014 %
0015 % PARAMETER LIST:
0016 %             'fs'    - specify a new sample rate for the resample grid.
0017 %
0018 %
0019 % M-FILE INFO: Get information about this methods by calling
0020 %              >> ao.getInfo('consolidate')
0021 %
0022 %              Get information about a specified set-plist by calling:
0023 %              >> ao.getInfo('consolidate', 'None')
0024 %
0025 % VERSION:     $Id: consolidate.m,v 1.12 2008/08/08 12:29:16 hewitson Exp $
0026 %
0027 % HISTORY:     21-05-2008 M Hewitson
0028 %                 Creation
0029 %
0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 %           't'     - specify a new time vector to resample on to. This
0033 %                     will be truncated to fit within the maximum start
0034 %                     time and minimum stop time of the inputs.
0035 %      or
0036 
0037 function varargout = consolidate(varargin)
0038 
0039   % Check if this is a call for parameters
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   % Collect input variable names
0049   in_names = cell(size(varargin));
0050   for ii = 1:nargin,in_names{ii} = inputname(ii);end
0051 
0052   % Collect all AOs and plists
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   % Decide on a deep copy or a modify
0057   bs = copy(as, nargout);
0058   na = numel(bs);
0059 
0060   % Combine plists
0061   pl = combine(pl, getDefaultPlist);
0062 
0063   % Get only tsdata AOs
0064   inhists = [];
0065   for j=1:na
0066     if ~isa(bs(j).data, 'tsdata')
0067       bs(j) = [];
0068       warning('!!! Skipping AO %s - it''s not a time-series AO.', bs(j).name);
0069     else
0070       % gather the input history objects
0071       inhists = [inhists bs(j).hist];
0072     end
0073   end
0074 
0075   %----------------- Drop all repeated samples
0076   utils.helper.msg(msg.PROC1, 'drop duplicates');
0077   for j=1:na
0078     utils.helper.msg(msg.PROC2, 'processing %s', bs(j).name);
0079     dropduplicates(bs(j));
0080   end
0081 
0082   %----------------- Interpolate all missing samples
0083   utils.helper.msg(msg.PROC1, 'interpolate missing samples');
0084   for j=1:na
0085     utils.helper.msg(msg.PROC2, 'processing %s', bs(j).name);
0086     interpmissing(bs(j));
0087   end
0088 
0089   %----------------- Fix uneven sampling
0090   utils.helper.msg(msg.PROC1, 'fixing uneven sample rates');
0091   for j=1:na
0092     utils.helper.msg(msg.PROC2, 'processing %s', bs(j).name);
0093     fixfs(bs(j));
0094   end
0095   %----------------- Resample all vectors to same fs
0096   utils.helper.msg(msg.PROC1, 'resample to same fs');
0097   % If fs is specified, use it. Otherwise, use max of all
0098   % input AOs.
0099   fs = find(pl, 'fs');
0100   if isempty(fs)
0101     % compute max fs
0102     fs = 0;
0103     for j=1:na
0104       if bs(j).data.fs > fs
0105         fs = bs(j).data.fs;
0106       end
0107     end
0108   end
0109   utils.helper.msg(msg.PROC2, 'resampling all time-series to an fs of %f', fs);
0110 
0111   for j=1:na
0112     % Check the resampling factor
0113     [P,Q] = utils.math.intfact(fs,bs(j).data.fs);
0114     if P > 100 || Q > 100
0115       utils.helper.msg(msg.PROC2, 'resampling factor too high [%g/%g]. Trying interpolation', P, Q);
0116       N  = length(bs(j).data.getX);
0117       t0 = bs(j).data.getX(1);
0118       t  = linspace(t0, t0+(P*N/Q-1)/fs, P*N/Q);
0119       interp(bs(j), plist('vertices', t));
0120     else
0121       resample(bs(j), plist('fsout', fs));
0122     end
0123   end
0124 
0125 
0126   %----------------- Resample all vectors on to the same grid
0127   utils.helper.msg(msg.PROC1, 'resample to same grid');
0128   % compute new time grid
0129   for j=1:na
0130     N = length(bs(j).data.getX);
0131     t = linspace(0, (N-1)/fs, N);
0132     interp(bs(j), plist('vertices', t));
0133   end
0134 
0135   %---------------- Time properties of AOs
0136   % Find max start time
0137   start = 0;
0138   for j=1:na
0139     dstart = bs(j).data.t0.utc_epoch_milli/1000 + bs(j).data.getX(1);
0140     if dstart > start
0141       start = dstart;
0142     end
0143   end
0144 
0145   % Find min stop time
0146   stop = 1e20;
0147   for j=1:na
0148     dstop = floor(bs(j).data.t0.utc_epoch_milli/1000 + bs(j).data.getX(end));
0149     if dstop < stop
0150       stop = dstop;
0151     end
0152   end
0153 
0154 
0155   %----------------- Truncate all vectors
0156   utils.helper.msg(msg.PROC1, 'truncate all vectors');
0157   utils.helper.msg(msg.PROC2, 'truncating vectors on interval [%g,%g]', start, stop);
0158 
0159   % split each ao
0160   bs = split(bs, plist('split_type', 'times', 'times', [start stop]));
0161 
0162   nsecs = [];
0163   for j=1:na
0164     if isempty(nsecs)
0165       nsecs = bs(j).data.nsecs;
0166     end
0167     if nsecs ~= bs(j).data.nsecs
0168       error('### Something went wrong with the truncation. Vectors don''t span the same time period.');
0169     end
0170   end
0171 
0172   %----------------- Set history on output AOs
0173 
0174   for j=1:na
0175     bs(j).addHistory(getInfo, pl, ao_invars(j), inhists(j));
0176     bs(j).setName(sprintf('%s(%s)', mfilename, ao_invars{j}), 'internal');
0177   end
0178 
0179   if nargout > 0
0180     varargout{1} = bs;
0181   end
0182 end
0183 
0184 %--------------------------------------------------------------------------
0185 % Get Info Object
0186 %--------------------------------------------------------------------------
0187 function ii = getInfo(varargin)
0188   if nargin == 1 && strcmpi(varargin{1}, 'None')
0189     sets = {};
0190     pl   = [];
0191   else
0192     sets = {'Default'};
0193     pl   = getDefaultPlist;
0194   end
0195   % Build info object
0196   ii = minfo(mfilename, 'ao', '', 'Signal Processing', '$Id: consolidate.m,v 1.12 2008/08/08 12:29:16 hewitson Exp $', sets, pl);
0197 end
0198 
0199 %--------------------------------------------------------------------------
0200 % Get Default Plist
0201 %--------------------------------------------------------------------------
0202 function pl_default = getDefaultPlist()
0203   pl_default = plist('fs', []);
0204 end
0205

Generated on Mon 25-Aug-2008 22:39:29 by m2html © 2003