0001 function bo = downsample(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
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
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
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
0075 if ~isempty(pl)
0076 pl = combine(pl);
0077 else
0078 pl = plist();
0079 end
0080
0081
0082 offset = find(pl, 'offset');
0083 factor = find(pl, 'factor');
0084
0085
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
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
0127
0128
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
0134 b = ao(d, h);
0135
0136
0137 b = setnh(b, 'name', sprintf('downsample(%s)', invars{j}));
0138
0139
0140 bo = [bo b];
0141
0142 end
0143
0144
0145 bo = reshape(bo, size(as));
0146
0147
0148 function pl_default = getDefaultPL()
0149
0150 pl_default = plist([param('factor', '')
0151 param('offset', 0)]);
0152
0153
0154