MFIR FIR filter object class constructor. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DESCRIPTION: MFIR FIR filter object class constructor. Create a mfir object. CONSTRUCTOR: f = mfir() creates an empty mfir object. f = mfir(fi) creates a copy of the input mfir object, fi. f = mfir(a) creates an mfir object based on the magnitude of the input AO/fsdata object a. f = mfir(c,fs) creates an mfir object based on the vector of input coefficients c. The sample rate for which the filter is designed should be specified as well. f = mfir(filename) creates an mfir object loading the mfir object from disk f = mfir(pl) creates an mfir object from the description given in the parameter list. Parameter sets for plist constructor (in order of priority): From XML File ------------- Construct an MFIR by loading it from an XML file. 'filename' - construct an MFIR from a filename. Example: plist('filename', 'm1.xml') [default: empty string] From MAT File ------------- Construct an MFIR by loading it from a MAT file. 'filename' - construct an MFIR from a filename. Example: plist('filename', 'm1.mat') [default: empty string] From Repository --------------- Construct an MFIR by retrieving it from an LTPDA repository. 'Hostname' - the repository hostname. Only those objects which are MFIRs are returned. [default: 'localhost']; Additional parameters: 'Database' - The database name [default: 'ltpda'] 'ID' - A vector of object IDs. [default: []] From Standard Type ------------------ Construct an MFIR of a standard type. 'type' - one of the types: 'highpass', 'lowpass', 'bandpass', 'bandreject' [default: 'lowpass'] You can also specify optional parameters: 'gain' - the gain of the filter [default: 1] 'fc' - the roll-off frequency [default: 0.1 Hz] 'fs' - the sampling frequency to design for [default: 1 Hz] 'order' - the filter order [default: 64] 'win' - Specify window function used in filter design [default: 'Hamming'] From Pzmodel ------------ Construct an MFIR from a pzmodel. 'pzmodel' - a pzmodel object to construct the filter from [default: empty pzmodel] From AO ------------ Construct an MFIR based on the magnitude of the input AO/fsdata object a 'method' - the design method: 'frequency-sampling' - uses fir2() 'least-squares' - uses firls() 'Parks-McClellan' - uses firpm() [default: 'frequency-sampling'] 'Win' - Window function for frequency-sampling method [default: 'Hanning'] 'N' - filter order [default: 512] From Plist ---------- 'Plist' - construct from a plist. The value passed should be a plist object. [default: empty plist] EXAMPLE 1: Create an order 1 highpass filter with high frequency gain 2. Filter is designed for 10 Hz sampled data and has a cut-off frequency of 0.2 Hz. >> pl = plist('type', 'highpass', ... 'order', 1, ... 'gain', 2.0, ... 'fs', 10, ... 'fc', 0.2); >> f = mfir(pl) NOTES: ** The convention used here for naming the filter coefficients is the opposite to MATLAB's convention. The recursion formula for this convention is b(1)*y(n) = a(1)*x(n) + a(2)*x(n-1) + ... + a(na+1)*x(n-na) - b(2)*y(n-1) - ... - b(nb+1)*y(n-nb) VERSION: $Id: mfir.m,v 1.36 2008/03/24 23:51:38 mauro Exp $ HISTORY: 09-02-2007 M Hewitson Creation 11-02-2008 M Hueller Help fixed Default parameter list defined The following call returns a parameter list object that contains the default parameter values: >> pl = mfir(mfir,'Params') The following call returns a string that contains the routine CVS version: >> version = mfir(mfir,'Version') The following call returns a string that contains the routine category: >> category = mfir(mfir,'Category') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function f = mfir(varargin) 0002 % MFIR FIR filter object class constructor. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: MFIR FIR filter object class constructor. 0007 % Create a mfir object. 0008 % 0009 % CONSTRUCTOR: f = mfir() 0010 % creates an empty mfir object. 0011 % 0012 % f = mfir(fi) 0013 % creates a copy of the input mfir object, fi. 0014 % 0015 % f = mfir(a) 0016 % creates an mfir object based on the magnitude of the 0017 % input AO/fsdata object a. 0018 % 0019 % f = mfir(c,fs) 0020 % creates an mfir object based on the vector of input 0021 % coefficients c. 0022 % The sample rate for which the filter is designed should 0023 % be specified as well. 0024 % 0025 % f = mfir(filename) 0026 % creates an mfir object loading the mfir object from disk 0027 % 0028 % f = mfir(pl) 0029 % creates an mfir object from the description given in the 0030 % parameter list. 0031 % 0032 % Parameter sets for plist constructor (in order of priority): 0033 % 0034 % From XML File 0035 % ------------- 0036 % 0037 % Construct an MFIR by loading it from an XML file. 0038 % 0039 % 'filename' - construct an MFIR from a filename. 0040 % Example: plist('filename', 'm1.xml') 0041 % [default: empty string] 0042 % 0043 % 0044 % From MAT File 0045 % ------------- 0046 % 0047 % Construct an MFIR by loading it from a MAT file. 0048 % 0049 % 'filename' - construct an MFIR from a filename. 0050 % Example: plist('filename', 'm1.mat') 0051 % [default: empty string] 0052 % 0053 % 0054 % From Repository 0055 % --------------- 0056 % 0057 % Construct an MFIR by retrieving it from an LTPDA repository. 0058 % 0059 % 'Hostname' - the repository hostname. Only those objects which 0060 % are MFIRs are returned. 0061 % [default: 'localhost']; 0062 % 0063 % Additional parameters: 0064 % 0065 % 'Database' - The database name [default: 'ltpda'] 0066 % 'ID' - A vector of object IDs. [default: []] 0067 % 0068 % 0069 % From Standard Type 0070 % ------------------ 0071 % 0072 % Construct an MFIR of a standard type. 0073 % 0074 % 'type' - one of the types: 'highpass', 'lowpass', 0075 % 'bandpass', 'bandreject' [default: 'lowpass'] 0076 % 0077 % You can also specify optional parameters: 0078 % 'gain' - the gain of the filter [default: 1] 0079 % 'fc' - the roll-off frequency [default: 0.1 Hz] 0080 % 'fs' - the sampling frequency to design for [default: 1 Hz] 0081 % 'order' - the filter order [default: 64] 0082 % 'win' - Specify window function used in filter 0083 % design [default: 'Hamming'] 0084 % 0085 % 0086 % From Pzmodel 0087 % ------------ 0088 % 0089 % Construct an MFIR from a pzmodel. 0090 % 0091 % 'pzmodel' - a pzmodel object to construct the filter from [default: empty pzmodel] 0092 % 0093 % 0094 % From AO 0095 % ------------ 0096 % 0097 % Construct an MFIR based on the magnitude of the input AO/fsdata object a 0098 % 0099 % 'method' - the design method: 0100 % 'frequency-sampling' - uses fir2() 0101 % 'least-squares' - uses firls() 0102 % 'Parks-McClellan' - uses firpm() 0103 % [default: 'frequency-sampling'] 0104 % 'Win' - Window function for frequency-sampling method 0105 % [default: 'Hanning'] 0106 % 'N' - filter order [default: 512] 0107 % 0108 % 0109 % From Plist 0110 % ---------- 0111 % 0112 % 'Plist' - construct from a plist. The value passed should be a plist 0113 % object. 0114 % [default: empty plist] 0115 % 0116 % 0117 % 0118 % EXAMPLE 1: Create an order 1 highpass filter with high frequency gain 2. 0119 % Filter is designed for 10 Hz sampled data and has a cut-off 0120 % frequency of 0.2 Hz. 0121 % 0122 % >> pl = plist('type', 'highpass', ... 0123 % 'order', 1, ... 0124 % 'gain', 2.0, ... 0125 % 'fs', 10, ... 0126 % 'fc', 0.2); 0127 % >> f = mfir(pl) 0128 % 0129 % NOTES: 0130 % ** The convention used here for naming the filter coefficients is 0131 % the opposite to MATLAB's convention. The recursion formula 0132 % for this convention is 0133 % 0134 % b(1)*y(n) = a(1)*x(n) + a(2)*x(n-1) + ... + a(na+1)*x(n-na) 0135 % - b(2)*y(n-1) - ... - b(nb+1)*y(n-nb) 0136 % 0137 % 0138 % VERSION: $Id: mfir.m,v 1.36 2008/03/24 23:51:38 mauro Exp $ 0139 % 0140 % HISTORY: 09-02-2007 M Hewitson 0141 % Creation 0142 % 11-02-2008 M Hueller 0143 % Help fixed 0144 % Default parameter list defined 0145 % 0146 % The following call returns a parameter list object that contains the 0147 % default parameter values: 0148 % 0149 % >> pl = mfir(mfir,'Params') 0150 % 0151 % The following call returns a string that contains the routine CVS version: 0152 % 0153 % >> version = mfir(mfir,'Version') 0154 % 0155 % The following call returns a string that contains the routine category: 0156 % 0157 % >> category = mfir(mfir,'Category') 0158 % 0159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0160 0161 ALGONAME = mfilename; 0162 VERSION = '$Id: mfir.m,v 1.36 2008/03/24 23:51:38 mauro Exp $'; 0163 CATEGORY = 'Constructor'; 0164 0165 %% Check if this is a special call: 0166 % default parameter list, cvs-version, category 0167 if (nargin == 2 || nargin == 3) && ischar(varargin{2}) 0168 if isa(varargin{1}, 'mfir') 0169 if strcmpi(varargin{2}, 'Params') 0170 if nargin == 2 0171 f = getDefaultPlist(); 0172 else 0173 f = getDefaultPlist(varargin{3}); 0174 end 0175 return 0176 elseif strcmpi(varargin{2}, 'Version') 0177 f = VERSION; 0178 return 0179 elseif strcmpi(varargin{2}, 'Category') 0180 f = CATEGORY; 0181 return 0182 end 0183 elseif isa(varargin{1}, 'ao') 0184 if strcmpi(varargin{2}, 'Params') 0185 f = getDefaultPlist('From AO'); 0186 return 0187 end 0188 end 0189 end 0190 0191 %% process input arguments 0192 args = []; 0193 k = 0; 0194 pls = []; 0195 for j=1:nargin 0196 if ~isempty(varargin{j}) 0197 k = k + 1; 0198 if isa(varargin{j}, 'plist') 0199 pls = [pls varargin{j}]; 0200 else 0201 args(k).val = varargin{j}; 0202 args(k).n = j; 0203 end 0204 end 0205 end 0206 0207 %% We could have multiple input plist objects. Combine them here. 0208 if isa(pls, 'plist') 0209 pl = combine(pls); 0210 else 0211 pl = []; 0212 end 0213 0214 Nargs = length(args); 0215 if ~isempty(pl) 0216 args(Nargs+1).val = pl; 0217 args(Nargs+1).n = j+1; 0218 Nargs = length(args); 0219 end 0220 0221 0222 %% %%%%%%%%%%%%%%%%%%% Create mfir object %%%%%%%%%%%%%%%%%%% 0223 0224 0225 switch nargin 0226 0227 case 0 0228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0229 %%% No Parameters %%% 0230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0231 0232 %%%%%%%%%% f = mfir() %%%%%%%%%% 0233 % create empty mfir object 0234 0235 f = init(VERSION); 0236 0237 case 1 0238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0239 %%% One Parameter %%% 0240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0241 0242 if ischar(varargin{1}) 0243 %%%%%%%%%% f = mfir('foo.xml') %%%%%%%%%% 0244 %%%%%%%%%% f = mfir('foo.mat') %%%%%%%%%% 0245 % from filename: XML, MAT 0246 0247 filename = varargin{1}; 0248 [path, name, ext, vers] = fileparts(filename); 0249 switch ext 0250 case '.mat' 0251 f = load(filename); 0252 f = f.a; 0253 case '.xml' 0254 root_node = xmlread(filename); 0255 f = ltpda_xmlread(root_node, 'mfir'); 0256 otherwise 0257 error('### Unknown file type.'); 0258 end 0259 0260 elseif isa(varargin{1}, 'mfir') 0261 %%%%%%%%%% f = mfir(mfir) %%%%%%%%%% 0262 % copy existing mfir 0263 f = varargin{1}; 0264 f.version = VERSION; 0265 0266 elseif isa(varargin{1}, 'ao') 0267 %%%%%%%%%% f = mfir(ao) %%%%%%%%%% 0268 pl = getDefaultPlist('From AO'); 0269 f = mfirFromAO(varargin{1}, pl, VERSION, ALGONAME); 0270 0271 elseif isstruct(varargin{1}) 0272 %%%%%%%%%% f = mfir(struct) %%%%%%%%%% 0273 % struct 0274 f = init(VERSION); 0275 0276 fields = fieldnames(varargin{1}); 0277 for ii = 1:length(fields) 0278 field = fields{ii}; 0279 0280 if strcmp(field, 'plist') 0281 %%% plist -> plist-object 0282 if isstruct(varargin{1}.(field)) 0283 f.(field) = plist(varargin{1}.(field)); 0284 else 0285 f.(field) = varargin{1}.(field); 0286 end 0287 elseif strcmp(field, 'created') 0288 %%% created -> time-object 0289 created = varargin{1}.created; 0290 if isstruct(created) 0291 created = time(created); 0292 end 0293 f.created = created; 0294 else 0295 %%% All other 0296 try 0297 f.(field) = varargin{1}.(field); 0298 catch 0299 error('### The field ''%s'' in the struct is not a mfir property.', field) 0300 end 0301 end 0302 end 0303 0304 elseif isnumeric(varargin{1}) 0305 %%%%%%%%%%%% From a coeffients %%%%%%%%%%%%% 0306 0307 a = varargin{1}; 0308 fs = varargin{2}; 0309 0310 % Checking the coefficients are listed in rows 0311 if size(a,1)~=1 0312 a = a'; 0313 end 0314 0315 f = init(VERSION); 0316 f.name = 'from coeffs'; 0317 f.fs = fs; 0318 f.ntaps = length(a); 0319 f.a = a; 0320 f.gd = (f.ntaps)/2; 0321 f.gain = 1; 0322 f.histout = zeros(1,f.ntaps-1); % initialise output history 0323 0324 elseif isa(varargin{1}, 'plist') 0325 %%%%%%%%%% f = mfir(plist) %%%%%%%%%% 0326 % Parameter list 0327 0328 pl = varargin{1}; 0329 0330 filename = find(pl, 'filename'); 0331 hostname = find(pl, 'hostname'); 0332 type = find(pl, 'type'); 0333 pzm = find(pl, 'pzmodel'); 0334 a = find(pl, 'ao'); 0335 ipl = find(pl, 'plist'); 0336 0337 % Selection of construction method 0338 if ~isempty(filename) 0339 0340 %----------------------------------------------------- 0341 %--- Construct from file 0342 %----------------------------------------------------- 0343 [path, name, ext, vers] = fileparts(filename); 0344 switch ext 0345 case '.mat' 0346 % Do a filename constructor 0347 f = load(filename); 0348 f = f.a; 0349 f.plist = pl; 0350 case '.xml' 0351 % Do a filename constructor 0352 root_node = xmlread(filename); 0353 f = ltpda_xmlread(root_node, 'mfir'); 0354 f.plist = pl; 0355 otherwise 0356 error('### Unknown file type.'); 0357 end 0358 0359 elseif ~isempty(hostname) 0360 0361 %----------------------------------------------------- 0362 %--- Construct from repository 0363 %----------------------------------------------------- 0364 0365 % do hostname constructor 0366 f = mfirFromRepository(pl, VERSION, ALGONAME); 0367 f.plist = remove(pl, 'conn'); 0368 0369 elseif ~isempty(type) 0370 0371 %----------------------------------------------------- 0372 %--- Construct from standard type 0373 %----------------------------------------------------- 0374 0375 % construct from standar type 0376 f = mfirFromStandardType(type, pl, VERSION, ALGONAME); 0377 0378 elseif ~isempty(pzm) 0379 0380 %----------------------------------------------------- 0381 %--- Construct from pzmodel 0382 %----------------------------------------------------- 0383 0384 f = mfirFromPzmodel(pzm, pl, VERSION, ALGONAME); 0385 0386 elseif ~isempty(a) 0387 0388 %----------------------------------------------------- 0389 %--- Construct from frequency series AO 0390 %----------------------------------------------------- 0391 0392 f = mfirFromAO(a, pl, VERSION, ALGONAME); 0393 0394 elseif ~isempty(ipl) 0395 0396 %----------------------------------------------------- 0397 %--- Construct from plist 0398 %----------------------------------------------------- 0399 0400 % if the plist is empty, we return an empty MFIR 0401 if nparams(ipl) == 0 0402 f = init(VERSION); 0403 else 0404 % do plist constructor 0405 f = mfir(ipl); 0406 end 0407 0408 else 0409 0410 %----------------------------------------------------- 0411 %--- ERROR 0412 %----------------------------------------------------- 0413 0414 %% is the plist is empty then return an empty MFIR 0415 if nparams(pl) == 0 0416 f = init(VERSION); 0417 else 0418 error('### Unknown MFIR constructor method.'); 0419 end 0420 end 0421 0422 else 0423 error('### Unknown MFIR constructor method.'); 0424 end 0425 0426 0427 case 2 0428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0429 %%% Two Parameter %%% 0430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0431 0432 %%%%%%%%%% f = mfir(database, IDs) %%%%%%%%%% 0433 % From DATABASE 0434 if isa(varargin{1}, 'database') 0435 f = retrieve(varargin{1}, varargin{2:end}); 0436 0437 elseif isa(varargin{1}, 'pzmodel') 0438 %%%%%%%%%% f = mfir(pzmodel, plist) %%%%%%%%%% 0439 % pzmodel followed by plist 0440 pzm = varargin{1}; 0441 0442 if isa(varargin{2}, 'plist') 0443 pl = combine(varargin{2}, getDefaultPlist('From Pzmodel')); 0444 else 0445 pl = getDefaultPlist('From Pzmodel'); 0446 end 0447 0448 f = mfirFromPzmodel(pzm, pl, VERSION, ALGONAME); 0449 0450 elseif isa(varargin{1}, 'ao') 0451 a = varargin{1}; 0452 0453 if isa(varargin{2}, 'plist') 0454 pl = combine(varargin{2}, getDefaultPlist('From AO')); 0455 else 0456 pl = getDefaultPlist('From AO'); 0457 end 0458 f = mfirFromAO(a, pl, VERSION, ALGONAME); 0459 0460 else 0461 error('### unknown constructor type for mfir.'); 0462 end 0463 0464 otherwise 0465 error('### incorrect input arguments for mfir constructor.'); 0466 end 0467 0468 end % function f = mfir(varargin) 0469 0470 0471 %% Helper functions 0472 0473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0474 % 0475 % FUNCTION: mfirFromRepository 0476 % 0477 % DESCRIPTION: Construct an mfir from a repository 0478 % 0479 % CALL: f = mfirFromRepository(pli, version, algoname) 0480 % 0481 % PARAMETER: pli: Parameter list object 0482 % version: cvs version string 0483 % algoname: The m-file name (use the mfilename command) 0484 % 0485 % HISTORY: 22-03-2008 M Hewitson 0486 % Creation 0487 % 0488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0489 function fs = mfirFromRepository(pli, version, algoname) 0490 0491 % Add default values 0492 pl = combine(pli, getDefaultPlist('From Repository')); 0493 0494 % Get parameters 0495 conn = find(pl, 'conn'); 0496 hostname = find(pl, 'hostname'); 0497 database = find(pl, 'database'); 0498 ids = find(pl, 'id'); 0499 0500 % do we have a connection? 0501 closeConn = 0; 0502 if isempty(conn) 0503 closeConn = 1; 0504 % Connect to repository 0505 conn = mysql_connect(hostname, database); 0506 end 0507 if ~isa(conn, 'database') 0508 error('### connection failed.'); 0509 end 0510 % Get each ID 0511 Nids = length(ids); 0512 fs = []; 0513 for kk=1:Nids 0514 0515 %---- This id 0516 id = ids(kk); 0517 disp(sprintf(' - retrieving ID %d', id)); 0518 0519 %---- check ID object type 0520 tt = mysql_getObjType(conn, id); 0521 %---- If this is an mfir 0522 if strcmp(tt, mfilename) 0523 %---- call database constructor 0524 a = ltpda_obj_retrieve(conn, id); 0525 %---- Add history 0526 %---- Add to output array 0527 fs = [fs a]; 0528 else 0529 warning(' !skipping ID %d, type %s', id, tt); 0530 end 0531 0532 end 0533 0534 % close connection 0535 if closeConn 0536 close(conn); 0537 end 0538 0539 end % function f = mfirFromRepository(pli, version, algoname) 0540 0541 0542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0543 % 0544 % FUNCTION: mfirFromStandardType 0545 % 0546 % DESCRIPTION: Construct an mfir from a standard types 0547 % 0548 % CALL: f = mfirFromRepository(type, pl, version, algoname) 0549 % 0550 % PARAMETER: type: String with filter ype description 0551 % pli: Parameter list object 0552 % version: cvs version string 0553 % algoname: The m-file name (use the mfilename command) 0554 % 0555 % HISTORY: 22-03-2008 M Hueller 0556 % Creation 0557 % 0558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0559 function f = mfirFromStandardType(type, pli, version, algoname) 0560 0561 % Add default values 0562 pl = combine(pli, getDefaultPlist('From Standard Type')); 0563 0564 f = init(version); 0565 0566 % check and fill parameter list 0567 plo = parseFilterParams(pl); 0568 switch type 0569 case 'lowpass' 0570 f = mklowpass(f, plo); 0571 case 'highpass' 0572 f = mkhighpass(f, plo); 0573 case 'bandpass' 0574 f = mkbandpass(f, plo); 0575 case 'bandreject' 0576 f = mkbandreject(f, plo); 0577 otherwise 0578 error('### unknown standard filter type in mfir constructor.'); 0579 end 0580 f.infile = ''; 0581 f.plist = pli; 0582 0583 end % function f = mfirFromStandardType(type, pli, version, algoname) 0584 0585 0586 0587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0588 % 0589 % FUNCTION: mfirFromPzmodel 0590 % 0591 % DESCRIPTION: Construct an mfir from a pzmodel 0592 % 0593 % CALL: f = mfirFromPzmodel(pzm, pli, version, algoname) 0594 % 0595 % PARAMETER: pzm: Pzmodel object 0596 % pli: Parameter list object 0597 % version: cvs version string 0598 % algoname: The m-file name (use the mfilename command) 0599 % 0600 % HISTORY: 22-03-2008 M Hueller 0601 % Creation 0602 % 0603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0604 function f = mfirFromPzmodel(pzm, pli, version, algoname) 0605 0606 % Add default values 0607 pl = combine(pli, getDefaultPlist('From Pzmodel')); 0608 0609 fs = find(pl, 'fs'); 0610 if isempty(fs) 0611 % get max freq in pzmodel 0612 fs = 8*getupperFreq(pzm); 0613 warning([sprintf('!!! no sample rate specified. Designing for fs=%2.2f Hz.', fs)... 0614 sprintf('\nThe filter will be redesigned later when used.')]); 0615 end 0616 % make MFIR filter 0617 pl = combine(pl, plist('fs', fs)); 0618 f = tomfir(pzm, pl); 0619 f.name = pzm.name; 0620 f.plist = pli; 0621 0622 end % function f = mfirFromPzmodel(pzm, pli, version, algoname) 0623 0624 0625 0626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0627 % 0628 % FUNCTION: mfirFromAO 0629 % 0630 % DESCRIPTION: create FIR filter from magnitude of input AO/fsdata 0631 % 0632 % CALL: f = mfirFromAO(a, pli, version, algoname) 0633 % 0634 % PARAMETER: a: Analysis object 0635 % pli: Parameter list object 0636 % version: cvs version string 0637 % algoname: The m-file name (use the mfilename command) 0638 % 0639 % HISTORY: 22-03-2008 M Hueller 0640 % Creation 0641 % 0642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0643 function filt = mfirFromAO(a, pli, version, algoname) 0644 0645 % Add default values 0646 pl = combine(pli, getDefaultPlist('From AO')); 0647 0648 % Check that a.data is a fsdata object 0649 if ~isa(a.data, 'fsdata') 0650 error('### Please use an analysis object with a fsdata data object to create a mfir object.'); 0651 end 0652 0653 N = find(pl, 'N'); 0654 win = find(pl, 'Win'); 0655 method = find(pl, 'method'); 0656 0657 fs = a.data.fs; 0658 f = a.data.x; 0659 xx = abs(a.data.y); 0660 g = 1; %median(xx); 0661 0662 ffm = f/(fs/2); 0663 switch method 0664 case 'frequency-sampling' 0665 % check window 0666 if length(win.win) ~= N+1 0667 warning('!!! resizing window function to match desired filter order.'); 0668 if strcmp(win.name, 'Kaiser') || strcmp(win.name, 'Flattop') 0669 win = specwin(win.name, N+1, win.psll); 0670 else 0671 win = specwin(win.name, N+1); 0672 end 0673 end 0674 disp('** designing filter using frequency-sampling method [help fir2]'); 0675 mtaps = fir2(N, ffm, xx, win.win); 0676 case 'least-squares' 0677 error('### this design method is not working properly yet.'); 0678 if mod(length(ffm),2) 0679 ffm = ffm(1:end-1); 0680 xx = xx(1:end-1); 0681 end 0682 disp('** designing filter using least-squares method [help firls]'); 0683 mtaps = firls(N, ffm, xx); 0684 case 'Parks-McClellan' 0685 error('### this design method is not working properly yet.'); 0686 disp('** designing filter using Parks-McClellan method [help firpm]'); 0687 mtaps = firpm(N, ffm, xx); 0688 otherwise 0689 error('### unknown filter design method.'); 0690 end 0691 0692 % Make mfir object 0693 filt = init(version); 0694 filt.name = sprintf('fir(%s)', a.name); 0695 filt.fs = fs; 0696 filt.ntaps = length(mtaps); 0697 filt.a = mtaps; 0698 filt.gd = (filt.ntaps+1)/2; 0699 filt.gain = g; 0700 filt.histout = zeros(1,filt.ntaps-1); % initialise output history 0701 filt.infile = ''; 0702 filt.plist = pli; 0703 0704 end % function f = mfirFromAO(a, pli, version, algoname) 0705 0706 0707 0708 %%%%%%%%%%%%%%%%%%%%%%%% define mfir properties %%%%%%%%%%%%%%%%%%%%%%%%% 0709 0710 function f = init(version) 0711 f.name = 'None'; 0712 f.fs = 0; 0713 f.ntaps = 0; 0714 f.a = []; 0715 f.gd = 0; 0716 f.gain = 0; 0717 f.histout = []; 0718 f.infile = ''; 0719 f.plist = plist(); 0720 f.created = time; 0721 f.version = version; 0722 f = class(f, 'mfir'); 0723 end % function f = init(version) 0724 0725 0726 0727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0728 % 0729 % FUNCTION: getDefaultPlist 0730 % 0731 % DESCRIPTION: Default Parameter Lists 0732 % 0733 % CALL: out = getDefaultPlist(set-string) 0734 % 0735 % PARAMETER: set-string: A string which defines the default parameter list. 0736 % 0737 % HISTORY: 07-05-2007 Hewitson 0738 % Creation 0739 % 0740 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0741 function out = getDefaultPlist(varargin) 0742 0743 0744 % list of available parameter sets 0745 sets = {'From XML File', 'From MAT File', ... 0746 'From Repository', ... 0747 'From Standard Type', ... 0748 'From Pzmodel', 'From AO', ... 0749 'From Plist'}; 0750 0751 if nargin == 0 0752 out = sets; 0753 return 0754 end 0755 0756 set = varargin{1}; 0757 0758 switch set 0759 0760 %------------------------------------------ 0761 %--- Repository constructor 0762 %------------------------------------------ 0763 case 'From Repository' 0764 out = plist('hostname', 'localhost', 'database', 'ltpda', 'ID', []); 0765 0766 %------------------------------------------ 0767 %--- Create from description 0768 %------------------------------------------ 0769 case 'From Standard Type' 0770 order = 64; 0771 out = plist('type','lowpass',... 0772 'fc', 0.1, ... 0773 'gain', 1.0, ... 0774 'fs', 1.0, ... 0775 'order', order, ... 0776 'win', specwin('Hamming', order+1)); 0777 0778 %------------------------------------------ 0779 %--- Create from a pzmodel 0780 %------------------------------------------ 0781 case 'From Pzmodel' 0782 out = plist('pzmodel', pzmodel()); 0783 0784 %------------------------------------------ 0785 %--- Create from frequency series AO 0786 %------------------------------------------ 0787 case 'From AO' 0788 N = 512; 0789 out = plist('AO', ao(), ... 0790 'N', N, ... 0791 'method', 'frequency-sampling',... 0792 'Win', specwin('Hanning', N+1)); 0793 0794 %------------------------------------------ 0795 %--- Read from XML file 0796 %------------------------------------------ 0797 case 'From XML File' 0798 out = plist('filename', ''); 0799 0800 %------------------------------------------ 0801 %--- Read from MAT file 0802 %------------------------------------------ 0803 case 'From MAT File' 0804 out = plist('filename', ''); 0805 0806 %------------------------------------------ 0807 %--- Create from a plist 0808 %------------------------------------------ 0809 case 'From Plist' 0810 out = plist('Plist', []); 0811 0812 otherwise 0813 out = plist(); 0814 end 0815 0816 0817 end % function out = getDefaultPlist(varargin) 0818