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.html,v 1.7 2008/01/22 20:43:15 hewitson 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.html,v 1.7 2008/01/22 20:43:15 hewitson 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.html,v 1.7 2008/01/22 20:43:15 hewitson 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 invars = {};
0047 as     = [];
0048 ps     = [];
0049 aos_in = 0;
0050 
0051 %% collect input ao's, plist's and ao variable names
0052 
0053 for j=1:nargin
0054 
0055   if isa(varargin{j}, 'ao')
0056     as = [as varargin{j}];
0057     aos_in = aos_in + 1;
0058 
0059     ao_name = inputname(j);
0060     if isempty(ao_name)
0061       ao_name = 'no_ao_name';
0062     end
0063 
0064     % Memorise the variable name of the corresponding analysis object.
0065     % If the ao is an array or vector add the index to the variable name
0066     if numel(varargin{j}) == 1
0067       invars{end+1} = ao_name;
0068     else
0069       for ii=1:numel(varargin{j})
0070         [I,J] = ind2sub(size(varargin{j}),ii);
0071         invars{end+1} = sprintf('%s(%d,%d)', ao_name, I, J);
0072       end
0073     end
0074   end
0075   if isa(varargin{j}, 'plist')
0076     ps = [ps varargin{j}];
0077   end
0078 end
0079 
0080 Na = numel(as);
0081 if isempty(as)
0082   error('### Please input at least one AO.');
0083 end
0084 
0085 %% Combine plists
0086 if ~isempty(ps)
0087   pl = combine(ps);
0088 else
0089   pl = plist();
0090 end
0091 
0092 %% Get parameters from plist
0093 offset = find(pl, 'offset');
0094 factor = find(pl, 'factor');
0095 
0096 if rem(factor, floor(factor)) ~= 0
0097   warning('!!! Downsample factor should be an integer. Rounding. !!!');
0098   factor = round(factor);
0099 end
0100 
0101 if nargin-aos_in == 1
0102   if isnumeric(varargin{end})
0103     factor = varargin{end};
0104   end
0105 end
0106 if nargin-aos_in == 2
0107   if isnumeric(varargin{end-1})
0108     factor = varargin{end-1};
0109   end
0110   if isnumeric(varargin{end})
0111     offset = varargin{end};
0112   end
0113 end
0114 
0115 if isempty(factor)
0116   error('### Please specify a decimation factor either directly or in a plist.');
0117 end
0118 if isempty(offset)
0119   warning('!!! No offset specified; using default of 0 samples !!!');
0120   offset = 0;
0121 end
0122 
0123 %% Loop over input AOs
0124 bo = [];
0125 for j=1:Na
0126 
0127   a  = as(j);
0128   d  = a.data;
0129   x  = d.x;
0130   y  = d.y;
0131   fs = d.fs;
0132   if isa(d, 'tsdata')
0133     ss = 1+offset;
0134     samples = ss:factor:len(a);
0135     d = set(d, 'x', x(samples));
0136     d = set(d, 'y', y(samples));
0137     d = set(d, 'fs', fs/factor);
0138   else
0139     error('### I can only decimate time-series AOs.');
0140   end
0141 
0142   %------- Make output AO
0143 
0144   % create new output history
0145   plo  = plist([param('factor', factor) param('offset', offset)]);
0146   h = history(ALGONAME, VERSION, plo, a.hist);
0147   h = set(h, 'invars', invars);
0148 
0149   % make output analysis object
0150   b = ao(d, h);
0151 
0152   % set name
0153   b = setnh(b, 'name', sprintf('decimate(%s)', invars{j}));
0154 
0155   % Add to output array
0156   bo = [bo b];
0157 
0158 end
0159 
0160 % Reshape the ouput to the same size of the input
0161 bo = reshape(bo, size(as));
0162 
0163 %% Get default params
0164 function pl_default = getDefaultPL()
0165 
0166   pl_default = plist([param('factor',  '')
0167                       param('offset',  0)]);
0168 
0169 
0170 % END

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003