Home > classes > @ao > filter.m

filter

PURPOSE ^

FILTER overrides the filter function for analysis objects.

SYNOPSIS ^

function varargout = filter(varargin)

DESCRIPTION ^

 FILTER overrides the filter function for analysis objects.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: FILTER overrides 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.27 2008/03/12 13:16:25 ingo Exp $

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = filter(ao, 'Params')

 The following call returns a string that contains the routine CVS version:

 >> version = filter(ao,'Version')

 The following call returns a string that contains the routine category:

 >> category = filter(ao,'Category')

 HISTORY: 11-02-07 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003