


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.24 2008/01/21 15:50:26 hewitson 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.24 2008/01/21 15:50:26 hewitson 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 % Standard history variable 0044 ALGONAME = mfilename; 0045 VERSION = '$Id: filter.m,v 1.24 2008/01/21 15:50:26 hewitson Exp $'; 0046 0047 %% Check if this is a call for parameters 0048 if nargin == 2 0049 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0050 in = char(varargin{2}); 0051 if strcmp(in, 'Params') 0052 varargout{1} = getDefaultPL(); 0053 return 0054 elseif strcmp(in, 'Version') 0055 varargout{1} = VERSION; 0056 return 0057 end 0058 end 0059 end 0060 0061 %% capture input variable names 0062 fobj = []; 0063 bs = []; 0064 0065 % Collect input ao's, plist's and ao variable names 0066 in_names = {}; 0067 for ii = 1:nargin 0068 in_names{end+1} = inputname(ii); 0069 0070 if isa(varargin{ii}, 'miir') || isa(varargin{ii}, 'mfir') 0071 fobj = varargin{ii}; 0072 end 0073 end 0074 0075 [as, ps, invars] = collect_inputs(varargin, in_names); 0076 0077 if isa(ps, 'plist') 0078 pl = combine(ps); 0079 else 0080 pl = plist(); 0081 end 0082 if isempty(fobj) 0083 fobj = find(pl, 'filter'); 0084 end 0085 0086 % check inputs 0087 if ~isa(fobj, 'miir') && ~isa(fobj, 'mfir') 0088 error('### the filter input should be an miir/mfir object.'); 0089 end 0090 0091 for j=1:numel(as) 0092 0093 % get input data 0094 a = as(j); 0095 d = a.data; 0096 0097 %--------- Time-series filter 0098 % 0099 if isa(d, 'tsdata') 0100 0101 % get input data 0102 fs = d.fs; 0103 0104 if isa(fobj, 'mfir') 0105 0106 % apply filter 0107 disp('* filtering with FIR filter...'); 0108 G = get(fobj, 'gain'); 0109 0110 % [fstruct, y] = ltpda_firfilter(struct(filt), d.y); 0111 coeffs = get(fobj, 'a'); 0112 Zi = get(fobj, 'histout'); 0113 [y, Zf] = filter(coeffs, 1, G.*d.y, Zi); 0114 0115 % remove group delay 0116 if isempty(find(pl, 'gdoff')) 0117 gd = floor(get(fobj, 'gd')); 0118 x = d.x(1:end-gd); 0119 y = y(1+gd:end); 0120 else 0121 x = d.x; 0122 end 0123 0124 % consolodate this structure with mfir class before converting. 0125 fobj = set(fobj, 'histout', Zf.'); 0126 0127 else %if isa(fobj, 'miir') 0128 0129 if fs ~= get(fobj, 'fs') 0130 warning('!!! Filter is designed for a different sample rate of data.'); 0131 % Adjust/redesign if this is a standard filter 0132 fobj = redesign(fobj, fs); 0133 end 0134 0135 % apply filter 0136 % [fstruct, y] = ltpda_iirfilter(struct(fobj), d.y, length(d.y)); 0137 0138 acoeffs = get(fobj, 'a'); 0139 bcoeffs = get(fobj, 'b'); 0140 Zi = get(fobj, 'histout'); 0141 [y, Zf] = filter(acoeffs, bcoeffs, d.y, Zi); 0142 x = d.x; 0143 % consolodate this structure with miir class before converting. 0144 % fobj = set(fobj, 'histin', fstruct.histin); 0145 % fobj = set(fobj, 'histout', fstruct.histout); 0146 fobj = set(fobj, 'histout', Zf); 0147 end 0148 0149 %----- Create output analysis object 0150 % make a new tsdata object 0151 ts = tsdata(y, fs); 0152 ts = set(ts, 'name', sprintf('filter %s with %s', d.name, get(fobj,'name'))); 0153 ts = set(ts, 'xunits', d.xunits); 0154 ts = set(ts, 'yunits', d.yunits); 0155 ts = set(ts, 't0', d.t0); 0156 0157 % make a new history object 0158 pl = plist(); 0159 pl = append(pl, param('filter', fobj)); 0160 h = history(ALGONAME, VERSION, pl, a.hist); 0161 h = set(h, 'invars', cellstr(invars{j})); 0162 0163 % make output analysis object 0164 b = ao(ts, h); 0165 % name for this object 0166 b = setnh(b, 'name', sprintf('%s(%s)', get(fobj,'name'), invars{j})); 0167 bs = [bs b]; 0168 0169 %--------- Frequency-series filter 0170 % 0171 elseif isa(d, 'fsdata') 0172 error('### I don''t work yet. Please code me up.'); 0173 0174 else 0175 error('### unknown data type.'); 0176 end 0177 end 0178 0179 % Reshape the ouput to the same size of the input 0180 bs = reshape(bs, size(as)); 0181 0182 if nargout == 1 0183 varargout{1} = bs; 0184 elseif nargout == 2 0185 varargout{1} = bs; 0186 varargout{2} = fobj; 0187 else 0188 error('### wrong number of output arguments.'); 0189 end 0190 0191 %% Get default params 0192 function pl_default = getDefaultPL() 0193 0194 pl_default = plist(param('filter', '')); 0195 pl_default = append(pl_default, 'gdoff' ,[]);