Home > classes > @ao > decimate.m

decimate

PURPOSE ^

DECIMATE AOs containing time-series data.

SYNOPSIS ^

function bo = decimate(varargin)

DESCRIPTION ^

 DECIMATE AOs containing time-series data.

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

 DESCRIPTION: DECIMATE AOs containing time-series data.

 CALL:        b = decimate(a, 4)     - decimate x4; offset is set to default of 0
              b = decimate(a, 2, 1)  - decimate x2 with 1 sample offset
              b = decimate(a, pl)    - use plist to get paramters
              b = decimate(a1, a2, pl) - decimate both a1 and a2; b is then a 2x1
                                   vector.

 PARAMETERS: 'factor'  - decimation factor
             'offset'  - sample offset for decimation [default: 0]

 VERSION:     $Id: decimate.m,v 1.7 2007/11/02 12:26:24 ingo Exp $

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = decimate(ao, 'Params')

 HISTORY: 14-05-07 M Hewitson
             Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function bo = decimate(varargin)
0002 % DECIMATE AOs containing time-series data.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: DECIMATE AOs containing time-series data.
0007 %
0008 % CALL:        b = decimate(a, 4)     - decimate x4; offset is set to default of 0
0009 %              b = decimate(a, 2, 1)  - decimate x2 with 1 sample offset
0010 %              b = decimate(a, pl)    - use plist to get paramters
0011 %              b = decimate(a1, a2, pl) - decimate both a1 and a2; b is then a 2x1
0012 %                                   vector.
0013 %
0014 % PARAMETERS: 'factor'  - decimation factor
0015 %             'offset'  - sample offset for decimation [default: 0]
0016 %
0017 % VERSION:     $Id: decimate.m,v 1.7 2007/11/02 12:26:24 ingo Exp $
0018 %
0019 % The following call returns a parameter list object that contains the
0020 % default parameter values:
0021 %
0022 % >> pl = decimate(ao, 'Params')
0023 %
0024 % HISTORY: 14-05-07 M Hewitson
0025 %             Creation
0026 %
0027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0028 
0029 ALGONAME = mfilename;
0030 VERSION  = '$Id: decimate.m,v 1.7 2007/11/02 12:26:24 ingo Exp $';
0031 
0032 %% Check if this is a call for parameters
0033 if nargin == 2
0034   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0035     in = char(varargin{2});
0036     if strcmp(in, 'Params')
0037       bo = getDefaultPL();
0038       return
0039     elseif strcmp(in, 'Version')
0040       bo = VERSION;
0041       return
0042     end
0043   end
0044 end
0045 
0046 %% capture input variable names
0047 invars = {};
0048 for j=1:nargin
0049   if isa(varargin{j}, 'ao')
0050     invars = [invars cellstr(inputname(j))];
0051   end
0052 end
0053 
0054 %% Get inputs
0055 as      = [];
0056 ps      = [];
0057 queries = [];
0058 aos_in  = 0;
0059 for j=1:nargin
0060   if isa(varargin{j}, 'ao')
0061     as = [as varargin{j}];
0062     aos_in = aos_in + 1;
0063   end
0064   if isa(varargin{j}, 'plist')
0065     ps = [ps varargin{j}];
0066   end
0067 end
0068 
0069 Na = numel(as);
0070 if isempty(as)
0071   error('### Please input at least one AO.');
0072 end
0073 
0074 %% Combine plists
0075 if ~isempty(ps)
0076   pl = combine(ps);
0077 else
0078   pl = plist();
0079 end
0080 
0081 %% Get parameters from plist
0082 offset = find(pl, 'offset');
0083 factor = find(pl, 'factor');
0084 
0085 if rem(factor, floor(factor)) ~= 0
0086   warning('!!! Downsample factor should be an integer. Rounding. !!!');
0087   factor = round(factor);
0088 end
0089 
0090 if nargin-aos_in == 1
0091   if isnumeric(varargin{end})
0092     factor = varargin{end};
0093   end
0094 end
0095 if nargin-aos_in == 2
0096   if isnumeric(varargin{end-1})
0097     factor = varargin{end-1};
0098   end
0099   if isnumeric(varargin{end})
0100     offset = varargin{end};
0101   end
0102 end
0103 
0104 if isempty(factor)
0105   error('### Please specify a decimation factor either directly or in a plist.');
0106 end
0107 if isempty(offset)
0108   warning('!!! No offset specified; using default of 0 samples !!!');
0109   offset = 0;
0110 end
0111 
0112 %% Loop over input AOs
0113 bo = [];
0114 for j=1:Na
0115 
0116   a  = as(j);
0117   d  = a.data;
0118   t  = d.t;
0119   x  = d.x;
0120   fs = d.fs;
0121   if isa(d, 'tsdata')
0122     ss = 1+offset;
0123     samples = ss:factor:len(a);
0124     d = set(d, 't', t(samples));
0125     d = set(d, 'x', x(samples));
0126     d = set(d, 'fs', fs/factor);
0127   else
0128     error('### I can only decimate time-series AOs.');
0129   end
0130 
0131   %------- Make output AO
0132 
0133   % create new output history
0134   plo  = plist([param('factor', factor) param('offset', offset)]);
0135   h = history(ALGONAME, VERSION, plo, a.hist);
0136   h = set(h, 'invars', invars);
0137 
0138   % make output analysis object
0139   b = ao(d, h);
0140 
0141   % set name
0142   % name for this object
0143   if j > length(invars)
0144     n1 = a.name;
0145   else
0146     n1 = invars{j};
0147   end
0148   b = setnh(b, 'name', sprintf('decimate(%s)', n1));
0149 
0150   % Add to output array
0151   bo = [bo b];
0152 
0153 end
0154 
0155 % Reshape the ouput to the same size of the input
0156 bo = reshape(bo, size(as));
0157 
0158 %% Get default params
0159 function pl_default = getDefaultPL()
0160 
0161   pl_default = plist([param('factor',  '')
0162                       param('offset',  0)]);
0163 
0164 
0165 % END

Generated on Fri 02-Nov-2007 19:39:27 by m2html © 2003