FILTER overides the filter function for analysis objects. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DESCRIPTION: FILTER overides the filter function for analysis objects. Applies the input digital IIR/FIR filter to the input analysis object. If the input analysis object contains a time-series (tsdata) then the filter is applied using the normal recursion algorithm. The output analysis object contains a tsdata object. If the input analysis object contains a frequency-series (fsdata) then the response of the filter is computed and then multiplied with the input frequency series. The output analysis object contains a frequency series. CALL: >> [b, filt] = filter(a,pl) >> [b, filt] = filter(a,filt,pl) >> b = filter(a,pl) INPUTS: pl - a parameter list a - input analysis object OUTPUTS: filt - a copy of the input filter object with the history values filled in. b - output analysis object containing the filtered data. PARAMETERS: filter - the filter object to use to filter the data VERSION: $Id: filter.m,v 1.18 2007/07/12 15:56:44 ingo Exp $ The following call returns a parameter list object that contains the default parameter values: >> pl = filter(ao, 'Params') HISTORY: 11-02-07 M Hewitson Creation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function varargout = filter(varargin) 0002 % FILTER overides the filter function for analysis objects. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: FILTER overides the filter function for analysis objects. 0007 % Applies the input digital IIR/FIR filter to the input analysis 0008 % object. If the input analysis object contains a 0009 % time-series (tsdata) then the filter is applied using the normal 0010 % recursion algorithm. The output analysis object contains a tsdata 0011 % object. 0012 % 0013 % If the input analysis object contains a frequency-series (fsdata) 0014 % then the response of the filter is computed and then multiplied 0015 % with the input frequency series. The output analysis object 0016 % contains a frequency series. 0017 % 0018 % CALL: >> [b, filt] = filter(a,pl) 0019 % >> [b, filt] = filter(a,filt,pl) 0020 % >> b = filter(a,pl) 0021 % 0022 % INPUTS: pl - a parameter list 0023 % a - input analysis object 0024 % 0025 % OUTPUTS: filt - a copy of the input filter object with the 0026 % history values filled in. 0027 % b - output analysis object containing the filtered data. 0028 % 0029 % PARAMETERS: filter - the filter object to use to filter the data 0030 % 0031 % VERSION: $Id: filter.m,v 1.18 2007/07/12 15:56:44 ingo Exp $ 0032 % 0033 % The following call returns a parameter list object that contains the 0034 % default parameter values: 0035 % 0036 % >> pl = filter(ao, 'Params') 0037 % 0038 % HISTORY: 11-02-07 M Hewitson 0039 % Creation 0040 % 0041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0042 0043 %% Check if this is a call for parameters 0044 if nargin == 2 0045 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0046 in = char(varargin{2}); 0047 if strcmp(in, 'Params') 0048 varargout{1} = getDefaultPL(); 0049 return 0050 end 0051 end 0052 end 0053 0054 %% capture input variable names 0055 invars = {}; 0056 as = []; 0057 ps = []; 0058 fobj = []; 0059 for j=1:nargin 0060 if isa(varargin{j}, 'ao') 0061 invars = [invars cellstr(inputname(j))]; 0062 end 0063 if isa(varargin{j}, 'ao') 0064 as = [as varargin{j}]; 0065 end 0066 if isa(varargin{j}, 'plist') 0067 ps = [ps varargin{j}]; 0068 end 0069 if isa(varargin{j}, 'miir') || isa(varargin{j}, 'mfir') 0070 fobj = varargin{j}; 0071 end 0072 end 0073 % a = varargin{1}; 0074 % pl = plist(); 0075 % if isa(varargin{2}, 'plist') 0076 % pl = varargin{2}; 0077 % % get the filter out 0078 % filt = find(pl, 'filter'); 0079 % elseif isa(varargin{2}, 'miir') || isa(varargin{2}, 'mfir') 0080 % filt = varargin{2}; 0081 % else 0082 % error('### Second argument should be a parameter list or a filter object.'); 0083 % end 0084 0085 if isa(ps, 'plist') 0086 pl = combine(ps); 0087 else 0088 pl = plist(); 0089 end 0090 if isempty(fobj) 0091 fobj = find(pl, 'filter'); 0092 end 0093 0094 % Standard history variable 0095 ALGONAME = mfilename; 0096 VERSION = '$Id: filter.m,v 1.18 2007/07/12 15:56:44 ingo Exp $'; 0097 0098 % Initialise output 0099 bs = []; 0100 0101 % check inputs 0102 if ~isa(fobj, 'miir') && ~isa(fobj, 'mfir') 0103 error('### the filter input should be an miir/mfir object.'); 0104 end 0105 0106 for j=1:length(as) 0107 0108 % get input data 0109 a = as(j); 0110 d = a.data; 0111 0112 %--------- Time-series filter 0113 % 0114 if isa(d, 'tsdata') 0115 0116 % get input data 0117 x = d.x; 0118 fs = d.fs; 0119 0120 if isa(fobj, 'mfir') 0121 0122 % apply filter 0123 disp('* filtering with FIR filter...'); 0124 G = get(fobj, 'g'); 0125 0126 % [fstruct, y] = ltpda_firfilter(struct(filt), d.x); 0127 coeffs = get(fobj, 'a'); 0128 Zi = get(fobj, 'histout'); 0129 [y, Zf] = filter(coeffs, 1, G.*d.x, Zi); 0130 0131 % remove group delay 0132 if isempty(find(pl, 'gdoff')) 0133 gd = floor(get(fobj, 'gd')); 0134 t = d.t(1:end-gd); 0135 y = y(1+gd:end); 0136 end 0137 0138 % consolodate this structure with mfir class before converting. 0139 fobj = set(fobj, 'histout', Zf.'); 0140 0141 else 0142 0143 if fs ~= get(fobj, 'fs') 0144 warning('!!! Filter is designed for a different sample rate of data.'); 0145 % Adjust/redesign if this is a standard filter 0146 fobj = redesign(fobj, fs); 0147 end 0148 0149 % apply filter 0150 % [fstruct, y] = ltpda_iirfilter(struct(fobj), d.x, length(d.x)); 0151 0152 acoeffs = get(fobj, 'a'); 0153 bcoeffs = get(fobj, 'b'); 0154 Zi = get(fobj, 'histout'); 0155 [y, Zf] = filter(acoeffs, bcoeffs, d.x, Zi); 0156 t = d.t; 0157 % consolodate this structure with miir class before converting. 0158 % fobj = set(fobj, 'histin', fstruct.histin); 0159 % fobj = set(fobj, 'histout', fstruct.histout); 0160 fobj = set(fobj, 'histout', Zf); 0161 end 0162 0163 %----- Create output analysis object 0164 % make a new tsdata object 0165 ts = tsdata(t, y); 0166 ts = set(ts, 'name', sprintf('filter %s with %s', d.name, get(fobj,'name'))); 0167 ts = set(ts, 'xunits', d.xunits); 0168 ts = set(ts, 'yunits', d.yunits); 0169 ts = set(ts, 't0', d.t0); 0170 0171 % make a new history object 0172 pl = plist(); 0173 pl = append(pl, param('filter', fobj)); 0174 h = history(ALGONAME, VERSION, pl, a.hist); 0175 h = set(h, 'invars', invars); 0176 0177 % make output analysis object 0178 b = ao(ts, h); 0179 % name for this object 0180 if isempty(invars{1}) 0181 n1 = a.name; 0182 else 0183 n1 = invars{1}; 0184 end 0185 b = set(b, 'name', sprintf('%s(%s)', get(fobj,'name'), n1)); 0186 bs = [bs b]; 0187 0188 0189 0190 %--------- Frequency-series filter 0191 % 0192 elseif isa(d, 'fsdata') 0193 error('### I don''t work yet. Please code me up.'); 0194 0195 else 0196 error('### unknown data type.'); 0197 end 0198 end 0199 0200 0201 if nargout == 1 0202 varargout{1} = bs; 0203 elseif nargout == 2 0204 varargout{1} = bs; 0205 varargout{2} = fobj; 0206 else 0207 error('### wrong number of output arguments.'); 0208 end 0209 0210 %% Get default params 0211 function pl_default = getDefaultPL() 0212 0213 disp('* creating default plist...'); 0214 pl_default = plist(param('filter', '')); 0215 pl_default = append(pl_default, 'gdoff' ,[]); 0216 disp('* done.');