RESP Make a frequency response of the filter. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DESCRIPTION: RESP Make a frequency response of the filter. The input filter should be a mfir object. The response is returned as a frequency-series in an analysis object. If no outputs are specified, the frequency-series is plotted as mag/deg. CALL: a = resp(filt); a = resp(filt, pl); resp(filt, pl); PARAMETERS: 'f1' - the start frequency 'f2' - the stop frequency 'nf' - number of evaluation points 'scale' - spacing of frequencies: 'lin' or 'log' or 'f' - a vector of frequency values OUTPUTS: a - an analysis object The response is calculated as: H(s) = gain * (a0 + a1*exp(s/fs) + ... + an*exp((n-1)s/fs)) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The following call returns a cell array that contains the default parameter sets names: >> sets = resp(mfir, 'Params') The following call returns a parameter list object that contains the default parameters for the jj-th set: >> pl = resp(mfir, 'Params', sets{jj}) The following call returns a string that contains the routine CVS version: >> version = resp(mfir, 'Version') The following call returns a string that contains the routine category: >> category = resp(mfir, 'Category') VERSION: $Id: resp.html,v 1.14 2008/03/31 10:27:39 hewitson Exp $ HISTORY: 27-08-02 M Hewitson Creation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function [varargout] = resp(varargin) 0002 % RESP Make a frequency response of the filter. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: RESP Make a frequency response of the filter. 0007 % The input filter should be a mfir object. 0008 % 0009 % The response is returned as a frequency-series in an analysis object. 0010 % 0011 % If no outputs are specified, the frequency-series is plotted as 0012 % mag/deg. 0013 % 0014 % CALL: a = resp(filt); 0015 % a = resp(filt, pl); 0016 % resp(filt, pl); 0017 % 0018 % PARAMETERS: 'f1' - the start frequency 0019 % 'f2' - the stop frequency 0020 % 'nf' - number of evaluation points 0021 % 'scale' - spacing of frequencies: 'lin' or 'log' 0022 % or 0023 % 'f' - a vector of frequency values 0024 % 0025 % OUTPUTS: a - an analysis object 0026 % 0027 % The response is calculated as: 0028 % 0029 % H(s) = gain * (a0 + a1*exp(s/fs) + ... + an*exp((n-1)s/fs)) 0030 % 0031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0032 % 0033 % The following call returns a cell array that contains the default parameter 0034 % sets names: 0035 % 0036 % >> sets = resp(mfir, 'Params') 0037 % 0038 % The following call returns a parameter list object that contains the 0039 % default parameters for the jj-th set: 0040 % 0041 % >> pl = resp(mfir, 'Params', sets{jj}) 0042 % 0043 % The following call returns a string that contains the routine CVS version: 0044 % 0045 % >> version = resp(mfir, 'Version') 0046 % 0047 % The following call returns a string that contains the routine category: 0048 % 0049 % >> category = resp(mfir, 'Category') 0050 % 0051 % 0052 % VERSION: $Id: resp.html,v 1.14 2008/03/31 10:27:39 hewitson Exp $ 0053 % 0054 % HISTORY: 27-08-02 M Hewitson 0055 % Creation 0056 % 0057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0058 0059 ALGONAME = mfilename; 0060 VERSION = '$Id: resp.html,v 1.14 2008/03/31 10:27:39 hewitson Exp $'; 0061 CATEGORY = 'Signal Processing'; 0062 0063 %% 'Params', 'Version' and 'Category' Call 0064 if (nargin == 2 || nargin == 3) && ... 0065 isa(varargin{1}, 'mfir') && ... 0066 ischar(varargin{2}) 0067 in = char(varargin{2}); 0068 if strcmp(in, 'Params') 0069 if nargin == 2 0070 varargout{1} = getDefaultPL(); 0071 else 0072 varargout{1} = getDefaultPL(varargin{3}); 0073 end 0074 return 0075 elseif strcmp(in, 'Version') 0076 varargout{1} = VERSION; 0077 return 0078 elseif strcmp(in, 'Category') 0079 varargout{1} = CATEGORY; 0080 return 0081 end 0082 end 0083 0084 0085 %% get input filter 0086 filt = varargin{1}; 0087 if ~isa(filt, 'mfir') 0088 error('### first input must be an mfir filter object.'); 0089 end 0090 0091 0092 %% Collects parameters 0093 % create empty input if it is not specified 0094 if nargin == 1 0095 pl = plist(); 0096 else 0097 pl = varargin{2}; 0098 end 0099 0100 % parse input parameter list 0101 if ~isempty(find(pl, 'f')) 0102 pl = combine(pl, getDefaultPL('List')); 0103 else 0104 pl = combine(pl, getDefaultPL('Range')); 0105 end 0106 0107 0108 % fill parameter list 0109 if isempty(find(pl, 'f')) 0110 % Compute from frequency range 0111 f1 = find(pl, 'f1'); 0112 f2 = find(pl, 'f2'); 0113 ndata = find(pl, 'nf'); 0114 scale = find(pl, 'scale'); 0115 switch scale 0116 case 'lin' 0117 f = linspace(f1, f2, ndata); 0118 case 'log' 0119 f = logspace(log10(f1), log10(f2), ndata); 0120 end 0121 else 0122 % Compute from frequency list 0123 f = find(pl, 'f'); 0124 % Want to deal with rows 0125 if size(f,1) > 1 0126 f = f.'; 0127 end 0128 ndata = length(f); 0129 end 0130 0131 % create output parameter list, with the frequency array values and the 0132 % input filter 0133 plo = plist('f', f, ... 0134 'filter', filt); 0135 0136 0137 %% compute Laplace vector 0138 s = -1i.*2*pi.*f; 0139 0140 0141 %% Compute filter response 0142 num = zeros(1, ndata); 0143 0144 for n=1:length(filt.a) 0145 num = num + filt.a(n).*exp(s.*(n-1)/filt.fs); 0146 end 0147 dresp = num .* filt.gain; % apply the gain 0148 0149 % num = zeros(1, ndata); 0150 % for n=1:length(filt.a) 0151 % num = num + filt.a(n).*exp(s.*(n-1)/filt.fs); 0152 % end 0153 % denom = zeros(1, ndata); 0154 % for n=1:length(filt.b) 0155 % denom = denom + filt.b(n).*exp(s.*(n-1)/filt.fs); 0156 % end 0157 % dresp = num ./ denom; 0158 % dresp = dresp .* filt.g; % apply the gain 0159 0160 % mag = 20*log10(abs(dresp)); 0161 % phase = angle(dresp)*180/pi; 0162 0163 0164 %% Create an analysis object 0165 0166 % create new output fsdata 0167 fs = fsdata(f, dresp, filt.fs); 0168 fs = set(fs, 'name', sprintf('resp(%s)', filt.name)); 0169 0170 % create new output history 0171 h = history(ALGONAME, VERSION, plo); 0172 0173 % make output analysis object 0174 b = ao(fs, h); 0175 0176 % set name 0177 b = setnh(b, 'name', sprintf('resp(%s)', filt.name)); 0178 0179 % Outputs 0180 if nargout == 0 0181 iplot(b) 0182 end 0183 0184 if nargout == 1 0185 varargout{1} = b; 0186 end 0187 if nargout > 1 0188 error('incorrect output arguments'); 0189 end 0190 0191 0192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0193 function out = getDefaultPL(varargin) 0194 0195 % List of available parameter sets 0196 sets = {'List', 'Range'}; 0197 0198 if nargin == 0 0199 out = sets; 0200 return 0201 end 0202 0203 set = varargin{1}; 0204 0205 switch set 0206 case 'List' 0207 out = plist('f', []); 0208 case 'Range' 0209 out = plist('f1', 0.001,... 0210 'f2', 1,... 0211 'nf', 1000,... 0212 'scale', 'log'); 0213 otherwise 0214 out = plist(); 0215 end 0216 0217 % END