Home > classes > @ao > filter.m

filter

PURPOSE ^

FILTER overides the filter function for analysis objects.

SYNOPSIS ^

function varargout = filter(varargin)

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.
 
 >> [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
 
 M Hewitson 11-02-07

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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

Generated on Fri 08-Jun-2007 16:09:11 by m2html © 2003