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 at least two time-series 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.14 2008/09/05 11:05:28 ingo 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 at least two time-series 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.14 2008/09/05 11:05:28 ingo 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   if numel(as) < 2
0057     error('### Consolidate requires at least two time-series AOs to work.');
0058   end
0059   
0060   % Decide on a deep copy or a modify
0061   bs = copy(as, nargout);
0062   na = numel(bs);
0063 
0064   % Combine plists
0065   pl = combine(pl, getDefaultPlist);
0066 
0067   % Get only tsdata AOs
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       % gather the input history objects
0075       inhists = [inhists bs(j).hist];
0076     end
0077   end
0078 
0079   %----------------- Drop all repeated samples
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   %----------------- Interpolate all missing samples
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   %----------------- Fix uneven sampling
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   %----------------- Resample all vectors to same fs
0100   utils.helper.msg(msg.PROC1, 'resample to same fs');
0101   % If fs is specified, use it. Otherwise, use max of all
0102   % input AOs.
0103   fs = find(pl, 'fs');
0104   if isempty(fs)
0105     % compute max fs
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     % Check the resampling factor
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   %----------------- Resample all vectors on to the same grid
0131   utils.helper.msg(msg.PROC1, 'resample to same grid');
0132   % compute new time grid
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   %---------------- Time properties of AOs
0140   % Find max start time
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   % Find min stop time
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   %----------------- Truncate all vectors
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   % split each ao
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   %----------------- Set history on output AOs
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 % Get Info Object
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   % Build info object
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 % Get Default Plist
0205 %--------------------------------------------------------------------------
0206 function pl_default = getDefaultPlist()
0207   pl_default = plist('fs', []);
0208 end
0209

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003