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.4 2008/03/02 20:55:29 hewitson 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.4 2008/03/02 20:55:29 hewitson 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.4 2008/03/02 20:55:29 hewitson 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 
0062 % Collect input ao's, plist's and ao variable names
0063 in_names = {};
0064 for ii = 1:nargin
0065   in_names{end+1} = inputname(ii);
0066 end
0067 [as, pl, invars] = collect_inputs(varargin, in_names);
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(pl)
0076   pl = combine(pl);
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 % Look for a factor (and offset) in varargin if no plist has them
0086 if isempty(factor)
0087   if isnumeric(varargin{end}) && isnumeric(varargin{end-1})
0088     factor = varargin{end-1};
0089     offset = varargin{end};
0090   elseif isnumeric(varargin{end})
0091     factor = varargin{end};
0092   end
0093   if rem(factor, floor(factor)) ~= 0
0094     warning('!!! Downsample factor should be an integer. Rounding. !!!');
0095     factor = round(factor);
0096   end  
0097 end
0098 
0099 if isempty(factor)
0100   error('### Please specify a decimation factor either directly or in a plist.');
0101 end
0102 if isempty(offset)
0103   warning('!!! No offset specified; using default of 0 samples !!!');
0104   offset = 0;
0105 end
0106 
0107 %% Loop over input AOs
0108 bo = [];
0109 for j=1:Na
0110 
0111   a  = as(j);
0112   d  = a.data;
0113   x  = d.x;
0114   y  = d.y;
0115   fs = d.fs;
0116   if isa(d, 'tsdata')
0117     ss = 1+offset;
0118     samples = ss:factor:len(a);
0119     d = set(d, 'x', x(samples));
0120     d = set(d, 'y', y(samples));
0121     d = set(d, 'fs', fs/factor);
0122   else
0123     error('### I can only downsample time-series AOs.');
0124   end
0125 
0126   %------- Make output AO
0127 
0128   % create new output history
0129   plo  = plist([param('factor', factor) param('offset', offset)]);
0130   h = history(ALGONAME, VERSION, plo, a.hist);
0131   h = set(h, 'invars', invars);
0132 
0133   % make output analysis object
0134   b = ao(d, h);
0135 
0136   % set name
0137   b = setnh(b, 'name', sprintf('downsample(%s)', invars{j}));
0138 
0139   % Add to output array
0140   bo = [bo b];
0141 
0142 end
0143 
0144 % Reshape the ouput to the same size of the input
0145 bo = reshape(bo, size(as));
0146 
0147 %% Get default params
0148 function pl_default = getDefaultPL()
0149 
0150   pl_default = plist([param('factor',  '')
0151                       param('offset',  0)]);
0152 
0153 
0154 % END

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003