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.2 2008/02/12 08:31:45 mauro 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 aos_in = 0;
0062
0063
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
0077
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
0098 if ~isempty(ps)
0099 pl = combine(ps);
0100 else
0101 pl = plist();
0102 end
0103
0104
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
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
0155
0156
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
0162 b = ao(d, h);
0163
0164
0165 b = setnh(b, 'name', sprintf('downsample(%s)', invars{j}));
0166
0167
0168 bo = [bo b];
0169
0170 end
0171
0172
0173 bo = reshape(bo, size(as));
0174
0175
0176 function pl_default = getDefaultPL()
0177
0178 pl_default = plist([param('factor', '')
0179 param('offset', 0)]);
0180
0181
0182