Home > classes > @ao > downsample.m

downsample

PURPOSE ^

DOWNSAMPLE AOs containing time-series data.

SYNOPSIS ^

function bo = downsample(varargin)

DESCRIPTION ^

 DOWNSAMPLE AOs containing time-series data.

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

 DESCRIPTION: DOWNSAMPLE AOs containing time-series data.

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

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

 VERSION:     $Id: downsample.m,v 1.2 2008/02/12 08:31:45 mauro Exp $

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

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

 The following call returns a string that contains the routine CVS version:

 >> version = downsample(ao,'Version')

 The following call returns a string that contains the routine category:

 >> category = downsample(ao,'Category')

 HISTORY: 14-05-07 M Hewitson
             Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

Generated on Tue 26-Feb-2008 10:52:52 by m2html © 2003