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.

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

 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.20 2007/11/02 12:26:24 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

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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.20 2007/11/02 12:26:24 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 % Standard history variable
0044 ALGONAME = mfilename;
0045 VERSION  = '$Id: filter.m,v 1.20 2007/11/02 12:26:24 ingo 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 invars = {};
0063 as     = [];
0064 ps     = [];
0065 fobj   = [];
0066 % Initialise output
0067 bs     = [];
0068 
0069 for j=1:nargin
0070   if isa(varargin{j}, 'ao')
0071     invars = [invars cellstr(inputname(j))];
0072   end
0073   if isa(varargin{j}, 'ao')
0074     as = [as varargin{j}];
0075   end
0076   if isa(varargin{j}, 'plist')
0077     ps = [ps varargin{j}];
0078   end
0079   if isa(varargin{j}, 'miir') || isa(varargin{j}, 'mfir')
0080     fobj = varargin{j};
0081   end
0082 end
0083 
0084 if isa(ps, 'plist')
0085   pl = combine(ps);
0086 else
0087   pl = plist();
0088 end
0089 if isempty(fobj)
0090   fobj = find(pl, 'filter');
0091 end
0092 
0093 % check inputs
0094 if ~isa(fobj, 'miir') && ~isa(fobj, 'mfir')
0095   error('### the filter input should be an miir/mfir object.');
0096 end
0097 
0098 for j=1:numel(as)
0099 
0100   % get input data
0101   a = as(j);
0102   d = a.data;
0103 
0104   %--------- Time-series filter
0105   %
0106   if isa(d, 'tsdata')
0107 
0108     % get input data
0109     fs = d.fs;
0110 
0111     if isa(fobj, 'mfir')
0112 
0113       % apply filter
0114       disp('* filtering with FIR filter...');
0115       G  = get(fobj, 'gain');
0116 
0117       %     [fstruct, y] = ltpda_firfilter(struct(filt), d.x);
0118       coeffs  = get(fobj, 'a');
0119       Zi      = get(fobj, 'histout');
0120       [y, Zf] = filter(coeffs, 1, G.*d.x, Zi);
0121 
0122       % remove group delay
0123       if isempty(find(pl, 'gdoff'))
0124         gd = floor(get(fobj, 'gd'));
0125         t = d.t(1:end-gd);
0126         y = y(1+gd:end);
0127       else
0128         t = d.t;
0129       end
0130 
0131       % consolodate this structure with mfir class before converting.
0132       fobj = set(fobj, 'histout', Zf.');
0133 
0134     else %if isa(fobj, 'miir')
0135 
0136       if fs ~= get(fobj, 'fs')
0137         warning('!!! Filter is designed for a different sample rate of data.');
0138         % Adjust/redesign if this is a standard filter
0139         fobj = redesign(fobj, fs);
0140       end
0141 
0142       % apply filter
0143 %       [fstruct, y] = ltpda_iirfilter(struct(fobj), d.x, length(d.x));
0144 
0145       acoeffs  = get(fobj, 'a');
0146       bcoeffs  = get(fobj, 'b');
0147       Zi       = get(fobj, 'histout');
0148       [y, Zf] = filter(acoeffs, bcoeffs, d.x, Zi);
0149       t = d.t;
0150       % consolodate this structure with miir class before converting.
0151 %       fobj = set(fobj, 'histin', fstruct.histin);
0152 %       fobj = set(fobj, 'histout', fstruct.histout);
0153       fobj = set(fobj, 'histout', Zf);
0154     end
0155 
0156     %----- Create output analysis object
0157     % make a new tsdata object
0158     ts = tsdata(t, y);
0159     ts = set(ts, 'name', sprintf('filter %s with %s', d.name, get(fobj,'name')));
0160     ts = set(ts, 'xunits', d.xunits);
0161     ts = set(ts, 'yunits', d.yunits);
0162     ts = set(ts, 't0', d.t0);
0163 
0164     % make a new history object
0165     pl = plist();
0166     pl = append(pl, param('filter', fobj));
0167     h = history(ALGONAME, VERSION, pl, a.hist);
0168     h = set(h, 'invars', invars);
0169 
0170     % make output analysis object
0171     b = ao(ts, h);
0172     % name for this object
0173     if isempty(invars{1})
0174       n1 = a.name;
0175     else
0176       n1 = invars{1};
0177     end
0178     b  = setnh(b, 'name', sprintf('%s(%s)', get(fobj,'name'), n1));
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 Fri 02-Nov-2007 19:39:27 by m2html © 2003