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] 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); M-FILE INFO: Get information about this methods by calling >> ao.getInfo('downsample') Get information about a specified set-plist by calling: >> ao.getInfo('downsample', 'None') VERSION: $Id: downsample.m,v 1.15 2008/09/05 11:05:29 ingo Exp $ HISTORY: 14-05-07 M Hewitson Creation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 % DOWNSAMPLE AOs containing time-series data. 0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0003 % 0004 % DESCRIPTION: DOWNSAMPLE AOs containing time-series data. 0005 % 0006 % CALL: b = downsample(a, pl) - use plist to get parameters 0007 % b = downsample(a1, a2, pl) - downsample both a1 and a2; b is then a 2x1 0008 % vector. 0009 % 0010 % PARAMETERS: 'factor' - decimation factor [default: 1] 0011 % 'offset' - sample offset for decimation [default: 0] 0012 % 0013 % EXAMPLES: 1) downsample x4; offset is set to default of 0 0014 % 0015 % >> p = plist('factor',4); 0016 % >> b = downsample(a, p); 0017 % 0018 % 2) downsample x2 with 1 sample offset 0019 % 0020 % >> p = plist('factor',2,'offset',1); 0021 % >> b = downsample(a,p); 0022 % 0023 % M-FILE INFO: Get information about this methods by calling 0024 % >> ao.getInfo('downsample') 0025 % 0026 % Get information about a specified set-plist by calling: 0027 % >> ao.getInfo('downsample', 'None') 0028 % 0029 % VERSION: $Id: downsample.m,v 1.15 2008/09/05 11:05:29 ingo Exp $ 0030 % 0031 % HISTORY: 14-05-07 M Hewitson 0032 % Creation 0033 % 0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0035 0036 function varargout = downsample(varargin) 0037 0038 % Check if this is a call for parameters 0039 if utils.helper.isinfocall(varargin{:}) 0040 varargout{1} = getInfo(varargin{3}); 0041 return 0042 end 0043 0044 import utils.const.* 0045 utils.helper.msg(msg.MNAME, 'running %s/%s', mfilename('class'), mfilename); 0046 0047 % Collect input variable names 0048 in_names = cell(size(varargin)); 0049 for ii = 1:nargin,in_names{ii} = inputname(ii);end 0050 0051 % Collect all AOs 0052 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); 0053 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names); 0054 0055 % Decide on a deep copy or a modify 0056 bs = copy(as, nargout); 0057 0058 % Combine plists 0059 pl = combine(pl, getDefaultPlist); 0060 0061 % Check for input plist 0062 if isempty(pl) 0063 error('### Please give a plist with a parameter ''Factor''.'); 0064 end 0065 0066 % Get parameters from plist 0067 offset = find(pl, 'offset'); 0068 factor = find(pl, 'factor'); 0069 0070 % Checking downsampling value is valid 0071 if isempty(factor) 0072 error('### Please give a plist with a parameter ''Factor''.'); 0073 end 0074 0075 % Checking downsampling value is integer 0076 if rem(factor, floor(factor)) ~= 0 0077 warning('!!! Downsample factor should be an integer. Rounding. !!!'); 0078 factor = round(factor); 0079 end 0080 0081 % Checking sample offset value 0082 if isempty(offset) 0083 warning('!!! No offset specified; using default of 0 samples !!!'); 0084 offset = 0; 0085 end 0086 0087 % Loop over input AOs 0088 for j=1:numel(bs) 0089 if isa(bs(j).data, 'tsdata') 0090 % get samples 0091 ss = 1+offset; 0092 samples = ss:factor:length(bs(j).data.y); 0093 % select samples 0094 bs(j).setXY(bs(j).data.getX(samples), bs(j).data.getY(samples), 'internal'); 0095 % Set new sample rate 0096 bs(j).setFs(bs(j).data.fs/factor, 'internal'); 0097 % drop X vector again if we can 0098 bs(j).data.collapseX; 0099 else 0100 error('### I can only downsample time-series AOs.'); 0101 end 0102 0103 % Add history 0104 bs(j).addHistory(getInfo, pl, ao_invars(j), bs(j).hist); 0105 % set name 0106 bs(j).setName(sprintf('downsample(%s)', ao_invars{j}), 'internal'); 0107 end 0108 0109 % Set output 0110 if nargout > 0 0111 varargout{1} = bs; 0112 end 0113 end 0114 0115 %-------------------------------------------------------------------------- 0116 % Get Info Object 0117 %-------------------------------------------------------------------------- 0118 function ii = getInfo(varargin) 0119 if nargin == 1 && strcmpi(varargin{1}, 'None') 0120 sets = {}; 0121 pl = []; 0122 else 0123 sets = {'Default'}; 0124 pl = getDefaultPlist; 0125 end 0126 % Build info object 0127 ii = minfo(mfilename, 'ao', '', utils.const.categories.sigproc, '$Id: downsample.m,v 1.15 2008/09/05 11:05:29 ingo Exp $', sets, pl); 0128 end 0129 0130 %-------------------------------------------------------------------------- 0131 % Get Default Plist 0132 %-------------------------------------------------------------------------- 0133 function pl = getDefaultPlist() 0134 pl = plist('factor', 1, 'offset', 0); 0135 end 0136 0137