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, pl)      - use plist to get parameters
              b = downsample(a1, a2, pl) - downsample both a1 and a2; b is then a 2x1
                                   vector.

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

 VERSION:     $Id: downsample.m,v 1.5 2008/03/17 06:29:31 mauro Exp $

 EXAMPLES:
 
 1) downsample x4; offset is set to default of 0

   p = plist('factor',4);
   b = downsample(a, p); 

 2) downsample x2 with 1 sample offset

   p = plist('factor',2,'offset',1);
   b = downsample(a,p);

 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, pl)      - use plist to get parameters
0009 %              b = downsample(a1, a2, pl) - downsample both a1 and a2; b is then a 2x1
0010 %                                   vector.
0011 %
0012 % PARAMETERS: 'factor'  - decimation factor [default: 1]
0013 %             'offset'  - sample offset for decimation [default: 0]
0014 %
0015 % VERSION:     $Id: downsample.m,v 1.5 2008/03/17 06:29:31 mauro Exp $
0016 %
0017 % EXAMPLES:
0018 %
0019 % 1) downsample x4; offset is set to default of 0
0020 %
0021 %   p = plist('factor',4);
0022 %   b = downsample(a, p);
0023 %
0024 % 2) downsample x2 with 1 sample offset
0025 %
0026 %   p = plist('factor',2,'offset',1);
0027 %   b = downsample(a,p);
0028 %
0029 % The following call returns a parameter list object that contains the
0030 % default parameter values:
0031 %
0032 % >> pl = downsample(ao, 'Params')
0033 %
0034 % The following call returns a string that contains the routine CVS version:
0035 %
0036 % >> version = downsample(ao,'Version')
0037 %
0038 % The following call returns a string that contains the routine category:
0039 %
0040 % >> category = downsample(ao,'Category')
0041 %
0042 % HISTORY: 14-05-07 M Hewitson
0043 %             Creation
0044 %
0045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0046 
0047 ALGONAME = mfilename;
0048 CATEGORY = 'Signal Processing';
0049 VERSION  = '$Id: downsample.m,v 1.5 2008/03/17 06:29:31 mauro Exp $';
0050 
0051 %% Check if this is a call for parameters
0052 if nargin == 2
0053   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0054     in = char(varargin{2});
0055     if strcmp(in, 'Params')
0056       bo = getDefaultPL();
0057       return
0058     elseif strcmp(in, 'Version')
0059       bo = VERSION;
0060       return
0061     elseif strcmp(in, 'Category')
0062       bo = CATEGORY;
0063       return
0064     end
0065   end
0066 end
0067 
0068 invars = {};
0069 as     = [];
0070 ps     = [];
0071 
0072 % Collect input ao's, plist's and ao variable names
0073 in_names = {};
0074 for ii = 1:nargin
0075   in_names{end+1} = inputname(ii);
0076 end
0077 [as, pl, invars] = collect_inputs(varargin, in_names);
0078 
0079 Na = numel(as);
0080 if isempty(as)
0081   error('### Please input at least one AO.');
0082 end
0083 
0084 %% Check for input plist
0085 if isempty(pl)
0086   error('### Please give a plist with a parameter ''Factor''.');
0087 end
0088 
0089 
0090 %% Get parameters from plist
0091 offset = find(pl, 'offset');
0092 factor = find(pl, 'factor');
0093 
0094 % Checking downsampling value is valid
0095 if isempty(factor)
0096   error('### Please give a plist with a parameter ''Factor''.');
0097 end
0098 
0099 % Checking downsampling value is integer
0100 if rem(factor, floor(factor)) ~= 0
0101   warning('!!! Downsample factor should be an integer. Rounding. !!!');
0102   factor = round(factor);
0103 end
0104 
0105 % Checking sample offset value
0106 if isempty(offset)
0107   warning('!!! No offset specified; using default of 0 samples !!!');
0108   offset = 0;
0109 end
0110 
0111 
0112 %% Loop over input AOs
0113 bo = [];
0114 for j=1:Na
0115 
0116   a  = as(j);
0117   d  = a.data;
0118   x  = d.x;
0119   y  = d.y;
0120   fs = d.fs;
0121   if isa(d, 'tsdata')
0122     ss = 1+offset;
0123     samples = ss:factor:len(a);
0124     d = set(d, 'x', x(samples));
0125     d = set(d, 'y', y(samples));
0126     d = set(d, 'fs', fs/factor);
0127   else
0128     error('### I can only downsample time-series AOs.');
0129   end
0130 
0131   %------- Make output AO
0132 
0133   % create new output history
0134   plo  = plist('factor', factor, '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   b = setnh(b, 'name', sprintf('downsample(%s)', invars{j}));
0143 
0144   % Add to output array
0145   bo = [bo b];
0146 
0147 end
0148 
0149 % Reshape the ouput to the same size of the input
0150 bo = reshape(bo, size(as));
0151 
0152 %% Get default params
0153 function pl_default = getDefaultPL()
0154 
0155   pl_default = plist('factor',  1, ...
0156                       'offset',  0);
0157 
0158 
0159 % END

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003