Home > classes > @pzmodel > fromFile.m

fromFile

PURPOSE ^

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

SYNOPSIS ^

function pzm = fromFile(pzm, pli)

DESCRIPTION ^

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

 FUNCTION:    fromFile

 DESCRIPTION: Construct a pzmodel from a file

 CALL:        pzm = pzmodel.fromFile(pzm, filename)
              pzm = pzmodel.fromFile(pzm, pl)

 HISTORY:     07-05-2007 Hewitson
              Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0002 %
0003 % FUNCTION:    fromFile
0004 %
0005 % DESCRIPTION: Construct a pzmodel from a file
0006 %
0007 % CALL:        pzm = pzmodel.fromFile(pzm, filename)
0008 %              pzm = pzmodel.fromFile(pzm, pl)
0009 %
0010 % HISTORY:     07-05-2007 Hewitson
0011 %              Creation
0012 %
0013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0014 
0015 function pzm = fromFile(pzm, pli)
0016 
0017   VERSION = '$Id: fromFile.m,v 1.5 2008/08/08 14:52:24 hewitson Exp $';
0018 
0019   % Which file type are we dealing with?
0020   if ischar(pli)
0021     pli = plist('filename', pli);
0022   end
0023 
0024   % get filename
0025   filename = find(pli, 'filename');
0026 
0027   % Get the correct parameter set
0028   [path, name, ext] = fileparts(filename);
0029   switch ext
0030     case '.fil'
0031       ii = pzmodel.getInfo('pzmodel', 'From LISO File');
0032       dpl = ii.plists(strcmp(ii.sets, 'From LISO File'));
0033     case '.mat'
0034       ii = pzmodel.getInfo('pzmodel', 'From MAT File');
0035       dpl = ii.plists(strcmp(ii.sets, 'From MAT File'));
0036     case '.xml'
0037       ii = pzmodel.getInfo('pzmodel', 'From XML File');
0038       dpl = ii.plists(strcmp(ii.sets, 'From XML File'));
0039     otherwise
0040       error('### Unknown file type.');
0041   end
0042 
0043   % Set the method version string in the minfo object
0044   ii.setMversion([VERSION '-->' ii.mversion]);
0045 
0046   % Combine input plist with default values
0047   pl = combine(pli, dpl);
0048 
0049   % Process the file
0050   switch ext
0051     case '.fil'
0052       pzm = fromLISO(pzm, filename);
0053     case '.mat'
0054       pzm = load(filename);
0055       pzm = pzm.a;
0056     case '.xml'
0057       root_node = xmlread(filename);
0058       pzm = utils.helper.xmlread(root_node, 'pzmodel');
0059     otherwise
0060       error('### Unknown file type.');
0061   end
0062 
0063   % Add history
0064   % CALL:        obj = addHistory(obj, minfo, h_pl, var_name, inhists, ...);
0065   % pzm = pzm.addHistory(ii, pli, [], []);
0066 end
0067 
0068 
0069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0070 %
0071 % FUNCTION:    fromLISO
0072 %
0073 % DESCRIPTION: Construct a pzmodel from a LISO file
0074 %
0075 % CALL:        f = fromLISO(filename, version, algoname)
0076 %
0077 % PARAMETER:   filename: File name containing the object
0078 %              VERSION:  cvs version string
0079 %              ALGONAME: The m-file name (use the mfilename command)
0080 %
0081 % HISTORY:     22-03-2008 M Hewitson
0082 %              Creation
0083 %
0084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0085 
0086 function pzm = fromLISO(pzm, filename)
0087   poles = [];
0088   zeros = [];
0089   gain  = [];
0090   % Open the file for reading
0091   fd = fopen(filename, 'r');
0092   utils.helper.msg(msg.OPROC2, ['reading ' filename]);
0093   while ~feof(fd)
0094     line = fgetl(fd);
0095     % get first token
0096     [s,r] = strtok(line);
0097     if strcmp(s, 'pole')
0098       % get next token as frequency
0099       [s,r] = strtok(r);
0100       pf    = lisoStr2Num(s);
0101       % does this pole have a Q?
0102       pq = NaN;
0103       if ~isempty(r)
0104         [s,r] = strtok(r);
0105         if isnumeric(lisoStr2Num(s))
0106           pq = lisoStr2Num(s);
0107         end
0108       end
0109       % make pole
0110       if isnan(pq)
0111         p = pz(pf);
0112         utils.helper.msg(msg.OPROC2, 'found pole: %g', pf);
0113       else
0114         p = pz(pf, pq);
0115         utils.helper.msg(msg.OPROC2, 'found pole: %g, %g', pf, pq);
0116       end
0117       poles = [poles p];
0118     elseif strcmp(s, 'zero')
0119       % get next token as frequency
0120       [s,r] = strtok(r);
0121       zf    = lisoStr2Num(s);
0122       % does this pole have a Q?
0123       zq = NaN;
0124       if ~isempty(r)
0125         [s,r] = strtok(r);
0126         if isnumeric(lisoStr2Num(s))
0127           zq = lisoStr2Num(s);
0128         end
0129       end
0130       % make zero
0131       if isnan(zq)
0132         z = pz(zf);
0133         utils.helper.msg(msg.OPROC2, 'found zero: %g', zf);
0134       else
0135         z = pz(zf, zq);
0136         utils.helper.msg(msg.OPROC2, 'found zero: %g, %g', zf, zq);
0137       end
0138       zeros = [zeros z];
0139     elseif strcmp(s, 'factor')
0140       % get next token as gain
0141       [s, r] = strtok(r);
0142       gain   = lisoStr2Num(s);
0143       utils.helper.msg(msg.OPROC2, 'found factor: %g', gain);
0144     end
0145   end
0146   % Close file
0147   fclose(fd);
0148   % get model name
0149   [path, name, ext, vers] = fileparts(filename);
0150   % set object
0151   pzm.gain  = gain;
0152   pzm.poles = poles;
0153   pzm.zeros = zeros;
0154   pzm.name  = name;
0155 end % pzm = pzmFromLISO(filename, version, algoname)
0156 
0157 %%%%%%%%%%% A function to convert LISO number strings to doubles %%%%%%%%%%
0158 
0159 function d = lisoStr2Num(s)
0160 
0161   s = strrep(s, 'm', 'e-3');
0162   s = strrep(s, 'u', 'e-6');
0163   s = strrep(s, 'n', 'e-9');
0164   s = strrep(s, 'p', 'e-12');
0165   s = strrep(s, 'f', 'e-15');
0166   s = strrep(s, 'k', 'e3');
0167   s = strrep(s, 'M', 'e6');
0168   s = strrep(s, 'G', 'e9');
0169 
0170   d = str2double(s);
0171 
0172 end % function d = lisoStr2Num(s)
0173 
0174 
0175

Generated on Thu 14-Aug-2008 14:29:45 by m2html © 2003