Home > classes > @pzmodel > pzmodel.m

pzmodel

PURPOSE ^

PZMODEL constructor for pzmodel class.

SYNOPSIS ^

function pzm = pzmodel(varargin)

DESCRIPTION ^

 PZMODEL constructor for pzmodel class.

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

 DESCRIPTION: PZMODEL constructor for pzmodel class.

 CONSTRUCTOR: pzm = pzmodel(g, p, z)          % construct from gain, poles, zeros
              pzm = pzmodel(g, p, z, 'name')  % construct including name
              pzm = pzmodel(pl);              % construct from plist
              pzm = pzmodel('foo.fil');       % construct from LISO .fil file


 PARAMETERS:  'gain'     - model gain
              'poles'    - vector of pole objects
              'zeros'    - vector of zero objects
              'name'     - name of model
              'filename' - name of LISO .fil file

 VERSION:     $Id: pzmodel.m,v 1.21 2008/02/24 10:15:55 hewitson Exp $

 HISTORY:     03-04-2007 M Hewitson
                 Creation

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = pzmodel(pzmodel,'Params')

 The following call returns a string that contains the routine CVS version:

 >> version = pzmodel(pzmodel,'Version')

 The following call returns a string that contains the routine category:

 >> category = pzmodel(pzmodel,'Category')

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function pzm = pzmodel(varargin)
0002 % PZMODEL constructor for pzmodel class.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: PZMODEL constructor for pzmodel class.
0007 %
0008 % CONSTRUCTOR: pzm = pzmodel(g, p, z)          % construct from gain, poles, zeros
0009 %              pzm = pzmodel(g, p, z, 'name')  % construct including name
0010 %              pzm = pzmodel(pl);              % construct from plist
0011 %              pzm = pzmodel('foo.fil');       % construct from LISO .fil file
0012 %
0013 %
0014 % PARAMETERS:  'gain'     - model gain
0015 %              'poles'    - vector of pole objects
0016 %              'zeros'    - vector of zero objects
0017 %              'name'     - name of model
0018 %              'filename' - name of LISO .fil file
0019 %
0020 % VERSION:     $Id: pzmodel.m,v 1.21 2008/02/24 10:15:55 hewitson Exp $
0021 %
0022 % HISTORY:     03-04-2007 M Hewitson
0023 %                 Creation
0024 %
0025 % The following call returns a parameter list object that contains the
0026 % default parameter values:
0027 %
0028 % >> pl = pzmodel(pzmodel,'Params')
0029 %
0030 % The following call returns a string that contains the routine CVS version:
0031 %
0032 % >> version = pzmodel(pzmodel,'Version')
0033 %
0034 % The following call returns a string that contains the routine category:
0035 %
0036 % >> category = pzmodel(pzmodel,'Category')
0037 %
0038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0039 
0040 ALGONAME = mfilename;
0041 VERSION  = '$Id: pzmodel.m,v 1.21 2008/02/24 10:15:55 hewitson Exp $';
0042 CATEGORY = 'Constructor';
0043 
0044 %%%%%   Check if this is a call for parameters   %%%%%
0045 if (nargin == 2 || nargin == 3)
0046   if isa(varargin{1}, 'pzmodel') && ischar(varargin{2})
0047     in = char(varargin{2});
0048     if strcmpi(in, 'Params')
0049       if nargin == 2
0050         pzm = getDefaultPlist();
0051       else
0052         pzm = getDefaultPlist(varargin{3});
0053       end
0054       return
0055     elseif strcmpi(in, 'Version')
0056       pzm = VERSION;
0057       return
0058     elseif strcmpi(in, 'Category')
0059       pzm = CATEGORY;
0060       return
0061     end
0062   end
0063 end
0064 
0065 %%%%%%%%%%%%%%%%%%%%%%%%   define pzmodel properties   %%%%%%%%%%%%%%%%%%%%%%%%%
0066 
0067   function pz = init()
0068     pz.name    = 'pzmodel object';
0069     pz.gain    = 0;
0070     pz.poles   = pole;
0071     pz.zeros   = zero;
0072     pz.created = time;
0073     pz.version = VERSION;
0074     pz.plist   = '';
0075     pz         = class(pz, 'pzmodel');
0076   end
0077 
0078 %%%%%%%%%%%%%%%%%%%%%%%%%%   Create pzmodel object   %%%%%%%%%%%%%%%%%%%%%%%%%%%
0079 
0080 %%%%%%%%%%   pzm = pzmodel()   %%%%%%%%%%
0081 if nargin == 0
0082 
0083   pzm = init();
0084 
0085 elseif nargin == 1
0086 
0087   %%%%%%%%%%   p = pzmodel(pzmodel)   %%%%%%%%%%
0088   if isa(varargin{1}, 'pzmodel')
0089     pzm = varargin{1};
0090 
0091     %%%%%%%%%% From File  %%%%%%%%%%%%
0092   elseif ischar(varargin{1})
0093 
0094     filename = varargin{1};
0095     [path, name, ext, vers] = fileparts(filename);
0096     switch ext
0097       case '.fil'
0098         pzm = pzmFromLISO(filename);
0099       case '.mat'
0100         pzm = load(filename);
0101         pzm = pzm.a;
0102       case '.xml'
0103         root_node = xmlread(filename);
0104         pzm = ltpda_xmlread(root_node, 'pzmodel');
0105       otherwise
0106         error('### Unknown file type.');
0107     end
0108 
0109     %%%%%%%%%%   pzm = pzmodel(struct)   %%%%%%%%%%
0110   elseif isstruct(varargin{1})
0111 
0112     pzm = init();
0113 
0114     fields = fieldnames(varargin{1});
0115     for ii = 1:length(fields)
0116       field = fields{ii};
0117 
0118       %%% pole -> pole-object
0119       if strcmp(field, 'poles')
0120 
0121         ps = varargin{1}.poles;
0122         poles = [];
0123         for jj = 1:length(ps)
0124           if isstruct(ps(jj))
0125             poles = [poles pole(ps(jj))];
0126           else
0127             poles = [poles ps(jj)];
0128           end
0129         end
0130         pzm.poles = poles;
0131 
0132         %%% zero -> pole-object
0133       elseif strcmp(field, 'zeros')
0134 
0135         zs = varargin{1}.zeros;
0136         zeros = [];
0137         for jj = 1:length(zs)
0138           if isstruct(zs(jj))
0139             zeros = [zeros zero(zs(jj))];
0140           else
0141             zeros = [zeros zs(jj)];
0142           end
0143         end
0144         pzm.zeros = zeros;
0145 
0146         %%% created -> time-object
0147       elseif strcmp(field, 'created')
0148         if isstruct(varargin{1}.created)
0149           pzm.created = time(varargin{1}.created);
0150         else
0151           pzm.created = varargin{1}.created;
0152         end
0153         %%% All other
0154       else
0155         try
0156           pzm.(field) = varargin{1}.(field);
0157         catch
0158           error('### The field ''%s'' in the struct is not a pole property.', field)
0159         end
0160       end
0161     end
0162 
0163     %%%%%%%%%%   pzm = pzmodel(plist)   %%%%%%%%%%
0164   else
0165     pl  = varargin{1};
0166 
0167     filename    = find(pl, 'filename');
0168     hostname    = find(pl, 'hostname');
0169     
0170     if ~isempty(filename)
0171 
0172       pzm = pzmodel(filename);
0173 
0174     elseif ~isempty(hostname)
0175 
0176       pzm = pzmodelFromRepository(pl, VERSION, ALGONAME);
0177 
0178     else
0179 
0180       pzm = init();
0181 
0182       dpl = getDefaultPlist('From Poles/Zeros');
0183       pl  = combine(pl, dpl);
0184       pzm.gain = find(pl, 'gain');
0185       ps  = find(pl, 'poles');
0186       pso=[];
0187       for j=1:length(ps)
0188           if ~isnan(ps(j).f)
0189               pso = [pso ps(j)];
0190           end
0191       end
0192       pzm.poles = pso;
0193       zs = find(pl, 'zeros');
0194       zso=[];
0195       for j=1:length(zs)
0196           if ~isnan(zs(j).f)
0197               zso = [zso zs(j)];
0198           end
0199       end
0200       pzm.zeros = zso;
0201 
0202       % If the following properties are not set in the plist then use the default
0203       % values
0204       if ~isempty(find(pl, 'version'))
0205         pzm.version = find(pl, 'version');
0206       end
0207       if ~isempty(find(pl, 'created'))
0208         pzm.created = find(pl, 'created');
0209       end
0210       if ~isempty(find(pl, 'name'))
0211         pzm.name = find(pl, 'name');
0212       end
0213     end
0214     
0215     % Store the input plist
0216     pzm.plist = remove(pl, 'conn');
0217     
0218   end
0219 
0220 elseif nargin == 2
0221   %%%%%%%%%%%   From DATABASE   %%%%%%%%%%%
0222   if isa(varargin{1}, 'database')
0223     pzm = retrieve(varargin{1}, varargin{2:end});
0224   else
0225     error('### incorrect pzmodel constructor.');
0226   end
0227 
0228 
0229   %%%%%%%%%%%   3 or 4 inputs                             %%%%%%%%%%%
0230   %%%%%%%%%%%   pzm = pzmodel(gain, poles, zeros)         %%%%%%%%%%%
0231   %%%%%%%%%%%   pzm = pzmodel(gain, poles, zeros, name)   %%%%%%%%%%%
0232   %%%%%%%%%%%   pzm = pzmodel(ao, poles, zeros)           %%%%%%%%%%%
0233   %%%%%%%%%%%   pzm = pzmodel(ao, poles, zeros, name)     %%%%%%%%%%%
0234 elseif nargin >= 3
0235 
0236   pzm = init();
0237 
0238   a  = varargin{1};
0239   if isa(a, 'ao')
0240     gain = a.data.y(1);
0241   else
0242     gain = a;
0243   end
0244   poles = varargin{2};
0245   zeros = varargin{3};
0246   if nargin == 4
0247     name = char(varargin{4});
0248   else
0249     name = 'pzmodel';
0250   end
0251 
0252   % construct pz model object
0253   pzm.name  = name;
0254   pzm.gain  = gain;
0255   pzm.poles = poles;
0256   pzm.zeros = zeros;
0257   pzm.plist = plist('gain', gain, 'poles', poles, 'zeros', zeros);
0258 
0259 else
0260   error('### incorrect number of inputs.');
0261 end
0262 
0263 %--------------------------------------------------------------------------
0264 % construct a pzmodel from a repository
0265 %
0266   function aos = pzmodelFromRepository(pl, VERSION, ALGONAME)
0267 
0268     dpl = getDefaultPlist('From Repository');
0269     pl  = combine(pl, dpl);
0270 
0271     % Get parameters
0272     conn = find(pl, 'conn');
0273     hostname = find(pl, 'hostname');
0274     database = find(pl, 'database');
0275     ids      = find(pl, 'id');
0276 
0277     % do we have a connection?
0278     closeConn = 0;
0279     if isempty(conn)
0280       closeConn = 1;
0281       % Connect to repository
0282       conn = mysql_connect(hostname, database);
0283     end
0284     if ~isa(conn, 'database')
0285       error('### connection failed.');
0286     end
0287     % Get each ID
0288     Nids = length(ids);
0289     aos  = [];
0290     for kk=1:Nids
0291 
0292       %---- This id
0293       id = ids(kk);
0294       disp(sprintf('  - retrieving ID %d', id));
0295 
0296       %---- check ID object type
0297       tt = mysql_getObjType(conn, id);
0298       %---- If this is an AO
0299       if strcmp(tt, mfilename)
0300         %---- call database constructor
0301         a = ltpda_obj_retrieve(conn, id);
0302         %---- Add history
0303         a.plist = pl;
0304         %---- Add to output array
0305         aos = [aos a];
0306       else
0307         warning('    !skipping ID %d, type %s', id, tt);
0308       end
0309 
0310     end
0311     
0312     % close connection
0313     if closeConn
0314       close(conn);
0315     end
0316     
0317   end
0318 
0319 %--------------------------------------------------------------------------
0320 %
0321 % Function to read a pole/zero model from a LISO file
0322 %
0323   function pzm = pzmFromLISO(filename)
0324 
0325     pzm = init();
0326 
0327     poles = [];
0328     zeros = [];
0329     gain  = [];
0330 
0331     % Open the file for reading
0332     fd = fopen(filename, 'r');
0333     disp(['--- reading ' filename]);
0334     while ~feof(fd)
0335 
0336       line = fgetl(fd);
0337 
0338       % get first token
0339       [s,r] = strtok(line);
0340 
0341       if strcmp(s, 'pole')
0342 
0343         % get next token as frequency
0344         [s,r] = strtok(r);
0345         pf    = lisoStr2Num(s);
0346         % does this pole have a Q?
0347         pq = NaN;
0348         if ~isempty(r)
0349           [s,r] = strtok(r);
0350           if isnumeric(lisoStr2Num(s))
0351             pq = lisoStr2Num(s);
0352           end
0353         end
0354         % make pole
0355         if isnan(pq)
0356           p = pole(pf);
0357           disp(sprintf('   -- found pole: %g', pf));
0358         else
0359           p = pole(pf, pq);
0360           disp(sprintf('   -- found pole: %g, %g', pf, pq));
0361         end
0362         poles = [poles p];
0363 
0364       elseif strcmp(s, 'zero')
0365         % get next token as frequency
0366         [s,r] = strtok(r);
0367         zf    = lisoStr2Num(s);
0368         % does this pole have a Q?
0369         zq = NaN;
0370         if ~isempty(r)
0371           [s,r] = strtok(r);
0372           if isnumeric(lisoStr2Num(s))
0373             zq = lisoStr2Num(s);
0374           end
0375         end
0376         % make zero
0377         if isnan(zq)
0378           z = zero(zf);
0379           disp(sprintf('   -- found zero: %g', zf));
0380         else
0381           z = zero(zf, zq);
0382           disp(sprintf('   -- found zero: %g, %g', zf, zq));
0383         end
0384         zeros = [zeros z];
0385 
0386       elseif strcmp(s, 'factor')
0387         % get next token as gain
0388         [s, r] = strtok(r);
0389         gain   = lisoStr2Num(s);
0390         disp(sprintf('   -- found factor: %g', gain));
0391       end
0392 
0393     end
0394 
0395     % Close file
0396     fclose(fd);
0397 
0398     % get model name
0399     [path, name, ext, vers] = fileparts(filename);
0400 
0401     pzm = pzmodel(gain, poles, zeros, name);
0402     pzm.plist = plist('filename', filename);
0403 
0404   end
0405 
0406 % A function to convert LISO number strings to doubles
0407   function d = lisoStr2Num(s)
0408 
0409     s = strrep(s, 'm', 'e-3');
0410     s = strrep(s, 'u', 'e-6');
0411     s = strrep(s, 'n', 'e-9');
0412     s = strrep(s, 'p', 'e-12');
0413     s = strrep(s, 'f', 'e-15');
0414     s = strrep(s, 'k', 'e3');
0415     s = strrep(s, 'M', 'e6');
0416     s = strrep(s, 'G', 'e9');
0417 
0418     d = str2double(s);
0419 
0420   end
0421 
0422 
0423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0424 %
0425 % FUNCTION:    getDefaultPL
0426 %
0427 % DESCRIPTION: Get default params
0428 %
0429 % HISTORY:     11-02-2008 M Hueller
0430 %                 Creation
0431 %
0432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0433   function out = getDefaultPlist(varargin)
0434 
0435     % list of available parameter sets
0436     sets = {'From Poles/Zeros', 'From LISO file', 'From Repository'};
0437 
0438     if nargin == 0
0439       out = sets;
0440       return
0441     end
0442 
0443     set = varargin{1};
0444     switch set
0445       case 'From Repository'
0446         out = plist('hostname', 'localhost', 'database', 'ltpda', 'ID', []);
0447       case 'From LISO file'
0448         out = plist('filename', 'foo.fil');
0449       case 'From Poles/Zeros'
0450         out = plist('gain', 1, ...
0451           'poles', pole, ...
0452           'zeros', zero, ...
0453           'name', '');
0454       otherwise
0455         out = plist();
0456     end
0457 
0458   end
0459 end
0460 % END
0461 
0462

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003