FILTFILT overrides the filtfilt function for analysis objects. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DESCRIPTION: FILTFILT overrides the filtfilt function for analysis objects. Applies the input digital IIR filter to the input analysis object forwards and backwards. 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] = filtfilt(a,pl) >> b = filtfilt(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 REMARK: Uses ltpda_filtfilt() to do the filtering. VERSION: $Id: filtfilt.m,v 1.16 2008/03/12 13:16:25 ingo Exp $ The following call returns a parameter list object that contains the default parameter values: >> pl = filtfilt(ao, 'Params') The following call returns a string that contains the routine CVS version: >> version = filtfilt(ao,'Version') The following call returns a string that contains the routine category: >> category = filtfilt(ao,'Category') HISTORY: 11-02-07 M Hewitson Creation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function varargout = filtfilt(varargin) 0002 % FILTFILT overrides the filtfilt function for analysis objects. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: FILTFILT overrides the filtfilt function for analysis objects. 0007 % Applies the input digital IIR filter to the input analysis object 0008 % forwards and backwards. 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] = filtfilt(a,pl) 0019 % >> b = filtfilt(a,pl) 0020 % 0021 % INPUTS: pl - a parameter list 0022 % a - input analysis object 0023 % 0024 % OUTPUTS: filt - a copy of the input filter object with the 0025 % history values filled in. 0026 % b - output analysis object containing the filtered data. 0027 % 0028 % PARAMETERS: filter - the filter object to use to filter the data 0029 % 0030 % REMARK: Uses ltpda_filtfilt() to do the filtering. 0031 % 0032 % VERSION: $Id: filtfilt.m,v 1.16 2008/03/12 13:16:25 ingo Exp $ 0033 % 0034 % The following call returns a parameter list object that contains the 0035 % default parameter values: 0036 % 0037 % >> pl = filtfilt(ao, 'Params') 0038 % 0039 % The following call returns a string that contains the routine CVS version: 0040 % 0041 % >> version = filtfilt(ao,'Version') 0042 % 0043 % The following call returns a string that contains the routine category: 0044 % 0045 % >> category = filtfilt(ao,'Category') 0046 % 0047 % HISTORY: 11-02-07 M Hewitson 0048 % Creation 0049 % 0050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0051 0052 ALGONAME = mfilename; 0053 VERSION = '$Id: filtfilt.m,v 1.16 2008/03/12 13:16:25 ingo Exp $'; 0054 CATEGORY = 'Signal Processing'; 0055 bo = []; 0056 filt = []; 0057 0058 %% Check if this is a call for parameters 0059 if nargin == 2 0060 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0061 in = char(varargin{2}); 0062 if strcmp(in, 'Params') 0063 varargout{1} = getDefaultPL(); 0064 return 0065 elseif strcmp(in, 'Version') 0066 varargout{1} = VERSION; 0067 return 0068 elseif strcmp(in, 'Category') 0069 varargout{1} = CATEGORY; 0070 return 0071 end 0072 end 0073 end 0074 0075 % Collect input ao's, plist's and ao variable names 0076 in_names = {}; 0077 for ii = 1:nargin 0078 in_names{end+1} = inputname(ii); 0079 0080 if isa(varargin{ii}, 'miir') 0081 filt = varargin{ii}; 0082 end 0083 end 0084 0085 [as, ps, invars] = collect_inputs(varargin, in_names); 0086 0087 if isa(ps, 'plist') 0088 pl = combine(ps); 0089 else 0090 pl = plist(); 0091 end 0092 0093 if isempty(filt) 0094 filt = find(pl, 'filter'); 0095 end 0096 0097 % check inputs 0098 if ~isa(filt, 'miir') 0099 error('### the first input should be an miir object.'); 0100 end 0101 if ~isa(as, 'ao') 0102 error('### second input should be an analysis object.'); 0103 end 0104 0105 for ii=1:numel(as) 0106 0107 % get input data 0108 a = as(ii); 0109 d = a.data; 0110 0111 %--------- Time-series filter 0112 % 0113 if isa(d, 'tsdata') 0114 0115 % get input data 0116 y = d.y; 0117 fs = d.fs; 0118 if fs ~= get(filt, 'fs') 0119 warning('!!! Filter is designed for a different sample rate of data.'); 0120 % Adjust/redesign if this is a standard filter 0121 filt = redesign(filt, fs); 0122 end 0123 0124 % get filter coeffs 0125 ac = get(filt,'a'); 0126 bc = get(filt,'b'); 0127 0128 % apply filter 0129 y = filtfilt(ac, bc, y); 0130 % [fstruct, y] = ltpda_filtfilt(struct(filt), d.y, length(d.y)); 0131 % consolodate this structure with miir class before converting. 0132 % filt = set(filt, 'histin', fstruct.histin); 0133 % filt = set(filt, 'histout', fstruct.histout); 0134 0135 %----- Create output analysis object 0136 % make a new tsdata object 0137 ts = tsdata(d.x, y); 0138 ts = set(ts, 'name', sprintf('filtfilt %s with %s', d.name, get(filt,'name'))); 0139 ts = set(ts, 'xunits', d.xunits); 0140 ts = set(ts, 'yunits', d.yunits); 0141 ts = set(ts, 't0', d.t0); 0142 0143 % make a new history object 0144 pl = plist(); 0145 pl = append(pl, param('filter', filt)); 0146 h = history(ALGONAME, VERSION, pl, a.hist); 0147 h = set(h, 'invars', cellstr(invars{ii})); 0148 0149 % make output analysis object 0150 bs = ao(ts, h); 0151 % name for this object 0152 bs = setnh(bs, 'name', sprintf('%s(%s)', get(filt,'name'), invars{ii})); 0153 0154 bo = [bo bs]; 0155 0156 %--------- Frequency-series filter 0157 % 0158 elseif isa(d, 'fsdata') 0159 error('### I don''t work yet. Please code me up.'); 0160 0161 else 0162 error('### unknown data type.'); 0163 end 0164 0165 end 0166 0167 % Reshape the ouput to the same size of the input 0168 bo = reshape(bo, size(as)); 0169 0170 if nargout == 1 0171 varargout{1} = bo; 0172 elseif nargout == 2 0173 varargout{1} = bo; 0174 varargout{2} = filt; 0175 else 0176 error('### wrong number of output arguments.'); 0177 end 0178 0179 0180 %% Get default params 0181 function pl_default = getDefaultPL() 0182 0183 pl_default = plist(param('filter', ''));