Home > classes > @ao > ao.m

ao

PURPOSE ^

AO analysis object class constructor.

SYNOPSIS ^

function varargout = ao(varargin)

DESCRIPTION ^

 AO analysis object class constructor.

     Create an analysis object.

     Properties:
       name       - name of analysis object
       data       - data object (tsdata, fsdata)
       history    - history object
       provenance - creation info, machine, user, etc


     Possible constructors:

       a = ao()            - creates an empty analysis object
       a = ao('a1.xml')    - creates a new analysis object by loading the
                             analysis object from disk.
       a = ao('file.txt')  - creates a new analysis object by loading the
       a = ao('file.dat')    data in 'file.txt'. The ascii file is assumed
                             to be an equally sampled two-column file of
                             time and amplitude. By default, the amplitude
                             units are taken to be Volts ('V') and the
                             time samples are assumed to be in seconds.
       a = ao('file',pl)   - creates a new analysis object by loading the
                             data in 'file'. The parameter list decide how the
                             analysis object is created. The valid key values
                             of the parameter list are:
                             'type'        'tsdata','fsdata','xydata'
                                            default = 'tsdata'
                             'use_fs'       if this value is set, the
                                            x-axes is computed by the fs value.
                             'columns'      [1 2 1 4]
                                            default = [1 2]
                                            Each pair represented the x- and y-axes.
                                            (Each column pair creates an analysis object)
                                            Is the value 'use_fs' is used then
                                            represent each column the y-axes.
                                            (Each column creates an analysis object)
                             'num_columns'  Maximum colums in the file
                                            default = 10
                             'comment_char' The comment character in the file
                                            default = '%'
                             'description'  To set the description in the analysis object
                             '...'          every property of the data object
                                            e.g. 'name'
                             If the constructor creates multiple ao's it is
                             possible to give each data class its own e.g.
                             'name'. In this case the parameter list with the
                             key 'name' must have cell of the different values
                             as the name of the different data objects. e.g.
                             pl = plist('columns', [1 2 1 3],        ...
                                        'name',   {'name1' 'name2'}, ...
                                        'xunits', 'sec',             ...
                                        'yunits', {'V' 'Hz'});
                             This parameter list creates two ao's with tsdata.
       a = ao(plist)       - creates an analysis object with by reading
                             the file specified by the 'filename'
                             parameter in the plist.
       a = ao(data)        - creates an analysis object with a data
                             object. Data object can be one of tsdata,
                             fsdata, cdata, xydata, xyzdata.
       a = ao(data, hist)  - creates an analysis object with a data
                             object and a history object

       a = ao(specwin)     - creates an analysis object from a specwin
                             object

 Parameters for plist constructor:

   'filename' - construct an AO from a filename.
                Example: plist(param('filename', 'a1.xml'))

   'fcn'      - construct an AO from some valid MATLAB function.
                Example: plist(param('fcn', 'randn(100)'))

   'vals'     - construct an AO from a set of values.
                Example: plist(param('vals', [1 2 3]))

   'tsfcn'    - construct an AO from a function of time. You need to also
                specify parameters 'fs' and 'nsecs' for this call.
                Example:
                  plist([param('fs', 10) param('nsecs', 10) param('tsfcn',
                  'sin(2*pi*1.4*t) + 0.1*randn(size(t))')])

   'win'      - construct an AO from a specwin window function. Tjis
                creates a cdata type AO containing the window values.
                Example: plist(param('win', specwin('Hannning', 100)))

   'waveform' - construct an AO from a waveform description. Here you need
                to specify additional parameters 'fs' and 'nsecs', and also,
                for the following waveform types:
                'sine wave'      - 'f', 'phi'
                'noise'          - 'type' (can be 'Normal' or 'Uniform')
                'chirp'          - 'f0', 'f1', 't1'      (help chirp)
                'Gaussian pulse' - 'f0', 'bw'            (help gauspuls)
                'Square wave'    - 'f', 'duty'           (help square)
                'Sawtooth'       - 'f', 'width'          (help sawtooth)

   Examples:

   1) Normally distributed random noise time-series

      p     = plist('waveform', 'noise', 'fs', 10, 'nsecs', 1000);
      rd10  = ao(p);



     Indexing:
       b = a(1)            % where a is an array of analysis objects
       d = a.data;         % get the data object
       h = a.hist;         % get the history object
       d = a.data.x(1:20); % get a matrix of data values x;

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

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


 See also tsdata, fsdata, xydata, cdata, xyzdata

 M Hewitson 30-01-07

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ao(varargin)
0002 % AO analysis object class constructor.
0003 %
0004 %     Create an analysis object.
0005 %
0006 %     Properties:
0007 %       name       - name of analysis object
0008 %       data       - data object (tsdata, fsdata)
0009 %       history    - history object
0010 %       provenance - creation info, machine, user, etc
0011 %
0012 %
0013 %     Possible constructors:
0014 %
0015 %       a = ao()            - creates an empty analysis object
0016 %       a = ao('a1.xml')    - creates a new analysis object by loading the
0017 %                             analysis object from disk.
0018 %       a = ao('file.txt')  - creates a new analysis object by loading the
0019 %       a = ao('file.dat')    data in 'file.txt'. The ascii file is assumed
0020 %                             to be an equally sampled two-column file of
0021 %                             time and amplitude. By default, the amplitude
0022 %                             units are taken to be Volts ('V') and the
0023 %                             time samples are assumed to be in seconds.
0024 %       a = ao('file',pl)   - creates a new analysis object by loading the
0025 %                             data in 'file'. The parameter list decide how the
0026 %                             analysis object is created. The valid key values
0027 %                             of the parameter list are:
0028 %                             'type'        'tsdata','fsdata','xydata'
0029 %                                            default = 'tsdata'
0030 %                             'use_fs'       if this value is set, the
0031 %                                            x-axes is computed by the fs value.
0032 %                             'columns'      [1 2 1 4]
0033 %                                            default = [1 2]
0034 %                                            Each pair represented the x- and y-axes.
0035 %                                            (Each column pair creates an analysis object)
0036 %                                            Is the value 'use_fs' is used then
0037 %                                            represent each column the y-axes.
0038 %                                            (Each column creates an analysis object)
0039 %                             'num_columns'  Maximum colums in the file
0040 %                                            default = 10
0041 %                             'comment_char' The comment character in the file
0042 %                                            default = '%'
0043 %                             'description'  To set the description in the analysis object
0044 %                             '...'          every property of the data object
0045 %                                            e.g. 'name'
0046 %                             If the constructor creates multiple ao's it is
0047 %                             possible to give each data class its own e.g.
0048 %                             'name'. In this case the parameter list with the
0049 %                             key 'name' must have cell of the different values
0050 %                             as the name of the different data objects. e.g.
0051 %                             pl = plist('columns', [1 2 1 3],        ...
0052 %                                        'name',   {'name1' 'name2'}, ...
0053 %                                        'xunits', 'sec',             ...
0054 %                                        'yunits', {'V' 'Hz'});
0055 %                             This parameter list creates two ao's with tsdata.
0056 %       a = ao(plist)       - creates an analysis object with by reading
0057 %                             the file specified by the 'filename'
0058 %                             parameter in the plist.
0059 %       a = ao(data)        - creates an analysis object with a data
0060 %                             object. Data object can be one of tsdata,
0061 %                             fsdata, cdata, xydata, xyzdata.
0062 %       a = ao(data, hist)  - creates an analysis object with a data
0063 %                             object and a history object
0064 %
0065 %       a = ao(specwin)     - creates an analysis object from a specwin
0066 %                             object
0067 %
0068 % Parameters for plist constructor:
0069 %
0070 %   'filename' - construct an AO from a filename.
0071 %                Example: plist(param('filename', 'a1.xml'))
0072 %
0073 %   'fcn'      - construct an AO from some valid MATLAB function.
0074 %                Example: plist(param('fcn', 'randn(100)'))
0075 %
0076 %   'vals'     - construct an AO from a set of values.
0077 %                Example: plist(param('vals', [1 2 3]))
0078 %
0079 %   'tsfcn'    - construct an AO from a function of time. You need to also
0080 %                specify parameters 'fs' and 'nsecs' for this call.
0081 %                Example:
0082 %                  plist([param('fs', 10) param('nsecs', 10) param('tsfcn',
0083 %                  'sin(2*pi*1.4*t) + 0.1*randn(size(t))')])
0084 %
0085 %   'win'      - construct an AO from a specwin window function. Tjis
0086 %                creates a cdata type AO containing the window values.
0087 %                Example: plist(param('win', specwin('Hannning', 100)))
0088 %
0089 %   'waveform' - construct an AO from a waveform description. Here you need
0090 %                to specify additional parameters 'fs' and 'nsecs', and also,
0091 %                for the following waveform types:
0092 %                'sine wave'      - 'f', 'phi'
0093 %                'noise'          - 'type' (can be 'Normal' or 'Uniform')
0094 %                'chirp'          - 'f0', 'f1', 't1'      (help chirp)
0095 %                'Gaussian pulse' - 'f0', 'bw'            (help gauspuls)
0096 %                'Square wave'    - 'f', 'duty'           (help square)
0097 %                'Sawtooth'       - 'f', 'width'          (help sawtooth)
0098 %
0099 %   Examples:
0100 %
0101 %   1) Normally distributed random noise time-series
0102 %
0103 %      p     = plist('waveform', 'noise', 'fs', 10, 'nsecs', 1000);
0104 %      rd10  = ao(p);
0105 %
0106 %
0107 %
0108 %     Indexing:
0109 %       b = a(1)            % where a is an array of analysis objects
0110 %       d = a.data;         % get the data object
0111 %       h = a.hist;         % get the history object
0112 %       d = a.data.x(1:20); % get a matrix of data values x;
0113 %
0114 % The following call returns a parameter list object that contains the
0115 % default parameter values:
0116 %
0117 % >> pl = ao(ao, 'Params')
0118 %
0119 %
0120 % See also tsdata, fsdata, xydata, cdata, xyzdata
0121 %
0122 % M Hewitson 30-01-07
0123 %
0124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0125 
0126 %       a = ao('object.mat') - creates a new analysis object by loading the
0127 %                             analysis object from disk .
0128 
0129 ALGONAME = mfilename;
0130 VERSION  = '$Id: ao.m,v 1.56 2007/12/24 19:48:09 hewitson Exp $';
0131 
0132 % Is this a 'Params' call?
0133 if  (nargin == 2 || nargin == 3) &&...
0134     isa(varargin{1}, 'ao') &&...
0135     ischar(varargin{2})
0136   if strcmp(varargin{2}, 'Params')
0137     if nargin == 2
0138       varargout{1} = getDefaultPlist();
0139     else
0140       varargout{1} = getDefaultPlist(varargin{3});
0141     end
0142     return
0143   elseif strcmp(varargin{2}, 'Version')
0144     varargout{1} = VERSION;
0145     return
0146   end
0147 end
0148 
0149 
0150 % process input arguments
0151 args = [];
0152 k = 0;
0153 pls = [];
0154 for j=1:nargin
0155   if ~isempty(varargin{j})
0156     k = k + 1;
0157     if isa(varargin{j}, 'plist')
0158       pls = [pls varargin{j}];
0159     else
0160       args(k).val = varargin{j};
0161       args(k).n   = j;
0162     end
0163   end
0164 end
0165 
0166 % We could have multiple input plist objects. Combine them here.
0167 if isa(pls, 'plist')
0168   pl = combine(pls);
0169 else
0170   pl = [];
0171 end
0172 
0173 Nargs = length(args);
0174 if ~isempty(pl)
0175   args(Nargs+1).val = pl;
0176   args(Nargs+1).n   = j+1;
0177   Nargs = length(args);
0178 end
0179 
0180 %-----------------------------------------------------
0181 % create from SERVER
0182 if Nargs > 0
0183   if ischar(args(1).val)
0184     if strcmp(args(1).val, 'SERVER')
0185       a = aoFromServer(args, VERSION, ALGONAME);
0186       varargout{1} = a;
0187       return
0188     end
0189   end
0190 end
0191 
0192 %-----------------------------------------------------
0193 % create empty AO
0194 if Nargs == 0
0195   a.name        = 'None';
0196   a.data        = {};
0197   a.hist        = history(ALGONAME, VERSION);
0198   a.provenance  = provenance;
0199   a.description = '';
0200   a.mfile       = {};
0201   a.mfilename   = '';
0202   a.mdlfile     = '';
0203   a.mdlfilename = '';
0204   a.version     = VERSION;
0205   a.created     = time;
0206   varargout{1} = class(a, 'ao');
0207 %-----------------------------------------------------
0208 % copy AO, load from file, create from x-data
0209 elseif Nargs == 1              % create ao with data
0210   %%%%%%%%%% Create from XML fragment %%%%%%%%%%%
0211   if isa(args(1).val, 'org.apache.xerces.dom.DeferredElementImpl')
0212     varargout{1} = fromxml(args(1).val);
0213   %%%%%%%%%%% From File %%%%%%%%%%%%%%%%
0214   elseif ischar(args(1).val)
0215 
0216     filename = args(1).val;
0217     [path, name, ext, vers] = fileparts(filename);
0218     switch ext
0219       case '.mat'
0220         a = load(filename);
0221         a = a.a;
0222       case '.xml'
0223         a = xmlparse(ao, filename);
0224       case {'.txt', '.dat'}
0225         a = aoFromFilenameAndPlist(args(1).val, plist, VERSION, ALGONAME);
0226       otherwise
0227         error('### Unknown file type.');
0228     end
0229     varargout{1} = a;
0230 
0231   %-----------------------------------------------------
0232   % copy AO
0233   elseif isa(args(1).val, 'ao')
0234     varargout{1} = args(1).val;
0235   %-----------------------------------------------------
0236   % create from struct
0237   elseif isstruct(args(1).val)
0238     astruct = args(1).val;
0239     a.name = astruct.name;
0240     data = astruct.data;
0241     if isstruct(data)
0242       if isfield(data, {'t', 'x'})
0243         data = tsdata(data);
0244       elseif isfield(data, {'f', 'xx'})
0245         data = fsdata(data);
0246       elseif isfield(data, {'vals'})
0247         data = cdata(data);
0248       elseif isfield(data, {'x', 'y'})
0249         data = xydata(data);
0250       elseif isfield(data, {'x', 'y', 'z'})
0251         data = xyzdata(data);
0252       else
0253         error('### Unknown data type in input structure.');
0254       end
0255     end
0256     a.data = data;
0257     hist   = astruct.hist;
0258     if isstruct(hist)
0259       hist = history(hist);
0260     end
0261     a.hist = hist;
0262     prov = astruct.provenance;
0263     if isstruct(prov)
0264       prov = provenance(prov);
0265     end
0266     a.provenance  = prov;
0267     a.description = astruct.description;
0268     a.mfile       = astruct.mfile;
0269     a.mfilename   = astruct.mfilename;
0270     a.mdlfile     = astruct.mdlfile;
0271     a.mdlfilename = astruct.mdlfilename;
0272     a.version     = astruct.version;
0273     created       = astruct.created;
0274     if isstruct(created)
0275       created = time(created);
0276     end
0277     a.created     = created;
0278     varargout{1}  = class(a, 'ao');
0279 
0280   %-----------------------------------------------------
0281   % create from constant
0282   elseif isnumeric(args(1).val)
0283 
0284     a.name = 'const';
0285     a.data = cdata(args(1).val);
0286     a.hist = history(ALGONAME, VERSION, plist(param('vals', args(1).val)));
0287 %     a.hist = history(ALGONAME, VERSION, plist(param('fcn', ['[' sprintf('%g ', args(1).val) ']'])));
0288     a.provenance  = provenance;
0289     a.description = '';
0290     a.mfile       = {};
0291     a.mfilename   = '';
0292     a.mdlfile     = '';
0293     a.mdlfilename = '';
0294     a.version     = VERSION;
0295     a.created     = time;
0296     varargout{1}  = class(a, 'ao');
0297 
0298     %-----------------------------------------------------
0299     % create data using plist
0300   elseif isa(args(1).val, 'plist')
0301 
0302     pl       = args(1).val;
0303     filename = find(pl, 'filename');
0304     fcn      = find(pl, 'fcn');
0305     vals     = find(pl, 'vals');
0306     tsfcn    = find(pl, 'tsfcn');
0307     win      = find(pl, 'win');
0308     waveform = find(pl, 'waveform');
0309 
0310     if ~isempty(filename)
0311       % do filename constructor
0312       [path, name, ext, vers] = fileparts(filename);
0313       switch ext
0314         case '.mat'
0315           a = load(filename);
0316           varargout{1} = a.a;
0317         case '.xml'
0318           varargout{1} = xmlparse(ao, filename);
0319         case {'.txt', '.dat'}
0320           varargout{1} = aoFromFilenameAndPlist(filename, pl, VERSION, ALGONAME);
0321         otherwise
0322           error('### Unknown file type.');
0323       end
0324 
0325     elseif ~isempty(fcn)
0326       % do function constructor
0327       varargout{1} = aoFromFcn(pl, VERSION, ALGONAME);
0328 
0329     elseif ~isempty(vals)
0330       % do vals constructor
0331       varargout{1} = aoFromVals(vals, VERSION, ALGONAME);
0332 
0333     elseif ~isempty(tsfcn)
0334       % do tsfcn constructor
0335       varargout{1} = aoFromTSfcn(pl, VERSION, ALGONAME);
0336 
0337     elseif ~isempty(win)
0338       % do win constructor
0339       varargout{1} = aoFromSpecWin(win, VERSION, ALGONAME);
0340 
0341     elseif ~isempty(waveform)
0342       % do waveform constructor
0343       varargout{1} = aoFromWaveform(pl, VERSION, ALGONAME);
0344 
0345     else
0346       error('### Unknown AO constructor method.');
0347     end
0348 
0349   %-----------------------------------------------------
0350   % create from spectral window
0351   elseif isa(args(1).val, 'specwin')
0352     varargout{1} = aoFromSpecWin(args(1).val, VERSION, ALGONAME);
0353   %-----------------------------------------------------
0354   % create from data object
0355   else
0356     data          = args(1).val;
0357     a.name        = 'None';
0358     a.data        = data;
0359     a.hist        = history(ALGONAME, VERSION);
0360     a.provenance  = provenance;
0361     a.description = '';
0362     a.mfile       = {};
0363     a.mfilename   = '';
0364     a.mdlfile     = '';
0365     a.mdlfilename = '';
0366     a.version     = VERSION;
0367     a.created     = time;
0368     varargout{1}  = class(a, 'ao');
0369     if ~isa(a.data, 'tsdata') &&...
0370        ~isa(a.data, 'fsdata') &&...
0371        ~isa(a.data, 'cdata')  &&...
0372        ~isa(a.data, 'xydata')  &&...
0373        ~isa(a.data, 'xyzdata')
0374       error('### unknown data object.')
0375     end
0376   end
0377 %-----------------------------------------------------
0378 %   data, hist
0379 %  'file_name.txt', pl
0380 elseif Nargs == 2
0381 
0382   %%%%%%%%%%% Database
0383   if isa(varargin{1}, 'database')
0384     varargout{1} = retrieve(varargin{1}, varargin{2:end});
0385 
0386   %%%%%%%%%%% 'file_name.txt', pl
0387   elseif ischar(args(1).val) && isa(args(2).val, 'plist')
0388 
0389     ao_out = aoFromFilenameAndPlist(args(1).val, args(2).val, VERSION, ALGONAME);
0390 
0391     if nargout == 1
0392       varargout{1} = ao_out;
0393     elseif nargout == length(ao_out)
0394       for ii = 1:length(ao_out)
0395         varargout{ii} = ao_out(ii);
0396       end
0397     else
0398       warning ('### Too few output variables --> useing only the first.');
0399       varargout{1} = ao_out;
0400     end
0401 
0402   % data, hist
0403   else
0404 
0405     a.name        = 'None';
0406     a.data        = args(1).val;
0407     a.hist        = args(2).val; %history(ALGONAME, VERSION, [], varargin{2});
0408     a.provenance  = provenance;
0409     a.description = '';
0410     a.mfile       = {};
0411     a.mfilename   = '';
0412     a.mdlfile     = '';
0413     a.mdlfilename = '';
0414     a.version     = VERSION;
0415     a.created     = time;
0416     varargout{1}  = class(a, 'ao');
0417     if ~isa(a.data, 'tsdata') &&...
0418         ~isa(a.data, 'fsdata') &&...
0419         ~isa(a.data, 'cdata')  &&...
0420         ~isa(a.data, 'xydata')
0421       error('### unknown data object.')
0422     end
0423     if ~isa(a.hist, 'history')
0424       error('### unknown history object.')
0425     end
0426 
0427   end
0428 
0429 
0430 %-----------------------------------------------------
0431 %  full constructor - all fields
0432 elseif Nargs == 9
0433 
0434   a.name        = args(1).val;
0435   a.data        = args(2).val;
0436   a.hist        = args(3).val;
0437   a.provenance  = args(4).val;
0438   a.description = args(5).val;
0439   a.mfile       = args(6).val;
0440   a.mfilename   = args(7).val;
0441   a.mdlfile     = args(8).val;
0442   a.mdlfilename = args(9).val;
0443   a.version     = VERSION;
0444   a.created     = time;
0445   varargout{1} = class(a, 'ao');
0446 
0447 else
0448   error('### Unknown number of constructor arguments.');
0449 end
0450 
0451 %--------------------------------------------------------------------------
0452 % construct an ao from a value set
0453 %
0454 function a = aoFromVals(vals, VERSION, ALGONAME)
0455 
0456 % Make a cdata object
0457 ts = cdata(plist(param('vals', vals)));
0458 ts = set(ts, 'name', 'Data');
0459 
0460 % Make an analysis object
0461 a.name = 'Data';
0462 a.data = ts;
0463 a.hist = history(ALGONAME, VERSION, plist(param('vals', vals)));
0464 a.provenance  = provenance;
0465 a.description = '';
0466 a.mfile       = {};
0467 a.mfilename   = '';
0468 a.mdlfile     = '';
0469 a.mdlfilename = '';
0470 a.version     = VERSION;
0471 a.created     = time;
0472 a = class(a, 'ao');
0473 
0474 
0475 
0476 %--------------------------------------------------------------------------
0477 % construct an ao from a waveform
0478 %
0479 function a = aoFromWaveform(pl, VERSION, ALGONAME)
0480 
0481 waveform = find(pl, 'waveform');
0482 switch waveform
0483   %------------ Sine Wave
0484   case 'sine wave'
0485     freq = find(pl, 'f');
0486     phi  = find(pl, 'phi');
0487     tsfcn = sprintf('sin(2*pi*%g*t + %g*pi/180)', freq, phi);
0488     %------------ Noise
0489   case 'noise'
0490     ntype = find(pl, 'type');
0491     if isempty(ntype)
0492       ntype = 'Normal';
0493     end
0494     switch ntype
0495       case 'Normal'
0496         tsfcn = 'randn(size(t))';
0497       case 'Uniform'
0498         tsfcn = 'rand(size(t))';
0499     end
0500     %------------ Chirp
0501   case 'chirp'
0502     f0  = find(pl, 'f0');
0503     fe  = find(pl, 'f1');
0504     te  = find(pl, 't1');
0505     tsfcn = sprintf('chirp(t,%g,%g,%g)', f0, fe, te);
0506     %------------ Gaussian pulse
0507   case 'Gaussian pulse'
0508     fc  = find(pl, 'f0');
0509     bw  = find(pl, 'bw');
0510     tsfcn = sprintf('gauspuls(t,%g,%g)', fc, bw);
0511   case 'Square wave'
0512     freq = find(pl, 'f');
0513     duty = find(pl, 'duty');
0514     tsfcn = sprintf('square(2*pi*%g*t,%g)', freq, duty);
0515   case 'Sawtooth'
0516     freq  = find(pl, 'f');
0517     width = find(pl, 'width');
0518     tsfcn = sprintf('sawtooth(2*pi*%g*t,%g)', freq, width);
0519 end
0520 
0521 nsecs  = find(pl, 'nsecs');
0522 fs     = find(pl, 'fs');
0523 
0524 % construct tsdata
0525 t = linspace(0, nsecs-1/fs, nsecs*fs);
0526 % make y data
0527 eval(['y=' tsfcn ';']);
0528 
0529 ts = tsdata(t,y);
0530 ts = set(ts, 'name', tsfcn);
0531 ts = set(ts, 'xunits', 's');
0532 ts = set(ts, 'yunits', 'V');
0533 
0534 % Make an analysis object
0535 a.name = waveform;
0536 a.data = ts;
0537 a.hist = history(ALGONAME, VERSION, pl);
0538 a.provenance  = provenance;
0539 a.description = '';
0540 a.mfile       = {};
0541 a.mfilename   = '';
0542 a.mdlfile     = '';
0543 a.mdlfilename = '';
0544 a.version     = VERSION;
0545 a.created     = time;
0546 a = class(a, 'ao');
0547 
0548 
0549 
0550 %--------------------------------------------------------------------------
0551 % construct an ao from a ts-function string
0552 %
0553 function a = aoFromTSfcn(pl, VERSION, ALGONAME)
0554 
0555 nsecs       = find(pl, 'nsecs');
0556 fs          = find(pl, 'fs');
0557 fcn         = find(pl, 'tsfcn');
0558 randn_state = find(pl, 'rand_state');
0559 
0560 % construct tsdata
0561 t = linspace(0, nsecs-1/fs, nsecs*fs);
0562 
0563 % make y data
0564 
0565 % If the randn state is in the parameter list then set the randn function to
0566 % this sate
0567 if ~isempty(randn_state)
0568   randn('state',randn_state);
0569 else % Store the state of the random function in the parameter list
0570   randn_state = randn('state');
0571   pl = append(pl , 'rand_state', randn_state);
0572 end
0573 
0574 y = eval([fcn ';']);
0575 
0576 ts = tsdata(t,y);
0577 ts = set(ts, 'name', fcn);
0578 ts = set(ts, 'xunits', 's');
0579 ts = set(ts, 'yunits', 'V');
0580 
0581 % Make an analysis object
0582 a.name = 'TSfcn';
0583 a.data = ts;
0584 a.hist = history(ALGONAME, VERSION, pl);
0585 a.provenance  = provenance;
0586 a.description = '';
0587 a.mfile       = {};
0588 a.mfilename   = '';
0589 a.mdlfile     = '';
0590 a.mdlfilename = '';
0591 a.version     = VERSION;
0592 a.created     = time;
0593 a = class(a, 'ao');
0594 
0595 %--------------------------------------------------------------------------
0596 % construct an ao from a function string
0597 %
0598 function a = aoFromFcn(pl, VERSION, ALGONAME)
0599 
0600 fcn = find(pl, 'fcn');
0601 
0602 % Make a cdata object
0603 ts = cdata(plist(param('fcn', fcn)));
0604 ts = set(ts, 'name', fcn);
0605 
0606 % Make an analysis object
0607 a.name = fcn;
0608 a.data = ts;
0609 a.hist = history(ALGONAME, VERSION, pl);
0610 a.provenance  = provenance;
0611 a.description = '';
0612 a.mfile       = {};
0613 a.mfilename   = '';
0614 a.mdlfile     = '';
0615 a.mdlfilename = '';
0616 a.version     = VERSION;
0617 a.created     = time;
0618 a = class(a, 'ao');
0619 
0620 %--------------------------------------------------------------------------
0621 % construct an ao from filename AND parameter list
0622 %
0623 function a = aoFromFilenameAndPlist(file_name, pl, VERSION, ALGONAME)
0624 
0625   a = [];
0626 
0627 %   disp(['  + creating AO(s) from text file: ' file_name]);
0628 
0629   [pathstr, f_name, ext] = fileparts(file_name);
0630 
0631   %%%%%%%%%%   Define default parameter list   %%%%%%%%%%
0632   pl_default = plist('columns',      [1 2],    ...
0633                      'num_columns',   10,      ...
0634                      'type',         'tsdata', ...
0635                      'use_fs',       '',       ...
0636                      'comment_char', '%',      ...
0637                      'xunits',       'sec',    ...
0638                      'yunits',       'V');
0639   pl = combine(pl, pl_default);
0640 
0641   pl = pset(pl, param('filename', [f_name ext]));
0642   pl = pset(pl, param('filepath', pathstr));
0643 
0644   data_type    = find (pl, 'type');
0645   columns      = find (pl, 'columns');
0646   num_columns  = find (pl, 'num_columns');
0647   comment_char = find (pl, 'comment_char');
0648   use_fs       = find (pl, 'use_fs');
0649 
0650   %%%%%%%%%%   read file   %%%%%%%%%%
0651   [fid,msg] = fopen (file_name, 'r');
0652   if (fid < 0)
0653     error ('### can not open file: %s \n\n### error msg:',file_name, msg);
0654   end
0655 
0656   %%%%%%%%%% create scan format: '%f %f %f %f %f'   %%%%%%%%%%
0657   scan_format = [];
0658   for ii=1:num_columns
0659     scan_format = [scan_format '%f '];
0660   end
0661   scan_format = scan_format(1:end-1); % remove the last whitespace
0662 
0663   %%%%%%%%%%   scan the file   %%%%%%%%%%
0664   raw_data = textscan( fid, scan_format,             ...
0665                       'commentStyle',  comment_char, ...
0666                       'CollectOutput', 1);
0667   f_data = raw_data{1};
0668 
0669   if isempty(f_data)
0670     error([char(10) '### There are no data. ' ...
0671                     'Did you use the right comment character?'            char(10) ...
0672                     '### The current comment character is: ' comment_char char(10) ...
0673                     '### Use a parameter list with the parameter:'        char(10) ...
0674                     '### plist(''comment_char'', ''%'')']);
0675   end
0676 
0677   fclose(fid);
0678 
0679   %%%%%%%%%%   Create for each column pair the data object   %%%%%%%%%%
0680   if isempty(use_fs)
0681 
0682     %%%%%%%%%%   The numbers in columns must be straight   %%%%%%%%%%
0683     if mod(length(columns),2) ~= 0
0684       error('### the numbers in columns must be straight');
0685     end
0686 
0687     for lauf = 1:length(columns)/2
0688 
0689       data_x_axes = f_data(:, columns(lauf*2-1));
0690       data_y_axes = f_data(:, columns(lauf*2));
0691 
0692       %% create data object corresponding to the parameter list
0693       ao_data = [];
0694       switch data_type
0695         case 'tsdata'
0696           ao_data = tsdata( data_x_axes, data_y_axes);
0697         case 'fsdata'
0698           ao_data = fsdata( data_x_axes, data_y_axes);
0699         case 'cdata'
0700           error ('### please code me up')
0701         case 'xydata'
0702           ao_data = xydata( data_x_axes, data_y_axes);
0703         otherwise
0704           error('### unknown data type ''%s''', data_type);
0705       end
0706       ao_data.name = sprintf('f_name_%02d', lauf);
0707 
0708       aa      = [];
0709       aa.name = sprintf('f_name_%02d', lauf);
0710       aa.data = ao_data;
0711       aa.hist = history(ALGONAME, VERSION, pl);
0712       aa.provenance  = provenance;
0713 
0714       %% Set the description fron the parameter list or from the file
0715       description_pl = find(pl, 'description');
0716       if ~isempty(description_pl)
0717         aa.description = description_pl;
0718       else
0719         aa.description     = '';
0720       end
0721 
0722       aa.mfile     = {};
0723       aa.mfilename = '';
0724       aa.mdlfile   = '';
0725       aa.mdlfilename = '';
0726       aa.version     = VERSION;
0727       aa.created     = time;
0728       aa = class(aa, 'ao');
0729 
0730       a = [a aa];
0731 
0732     end
0733 
0734   %%%%%%%%%%   Create for each column AND fs a data object   %%%%%%%%%%
0735   else % isempty(use_fs)
0736 
0737     for lauf = 1:length(columns)
0738 
0739       data_y_axes = f_data(:, columns(lauf));
0740 
0741       %% create data object corresponding to the parameter list
0742       ao_data = [];
0743       switch data_type
0744         case 'tsdata'
0745           ao_data = tsdata(data_y_axes, use_fs);
0746         case 'fsdata'
0747           ao_data = fsdata(data_y_axes, use_fs);
0748         case 'cdata'
0749           error ('### please code me up')
0750         case 'xydata'
0751           error ('### please code me up')
0752         otherwise
0753           error('### unknown data type ''%s''', data_type);
0754       end
0755       ao_data.name = sprintf('f_name_%02d', lauf);
0756 
0757       aa      = [];
0758       aa.name = sprintf('f_name_%02d', lauf);
0759       aa.data = ao_data;
0760       aa.hist = history(ALGONAME, VERSION, pl);
0761       aa.provenance  = provenance;
0762 
0763       %% Set the description fron the parameter list or from the file
0764       description_pl = find(pl, 'description');
0765       if ~isempty(description_pl)
0766         aa.description = description_pl;
0767       else
0768         aa.description     = '';
0769       end
0770 
0771       aa.mfile       = {};
0772       aa.mfilename   = '';
0773       aa.mdlfile     = '';
0774       aa.mdlfilename = '';
0775       aa.version     = VERSION;
0776       aa.created     = time;
0777       aa = class(aa, 'ao');
0778 
0779       a = [a aa];
0780 
0781     end
0782 
0783   end
0784 
0785   %%%%%%%%%%   set fields of the data object from the parameter list   %%%%%%%%%%
0786   for ii = 1:length(a)
0787     fields = fieldnames(a(ii).data);
0788 
0789     for jj = 1:length(pl.params)
0790       parameter = pl.params(jj);
0791 
0792       if ismember(parameter.key, fields)
0793 
0794         if iscell(parameter.val)
0795           if length(parameter.val) > 1
0796             if length(parameter.val) ~= length(a)
0797               warning([sprintf('The values of the key ''%s'' ', parameter.key) ...
0798                                'have not the same length as the lenthg of '    ...
0799                                'the output. Using the first value!']);
0800               a(ii).data.(parameter.key) = parameter.val{1};
0801             else
0802               a(ii).data.(parameter.key) = parameter.val{ii};
0803             end
0804           else
0805             a(ii).data.(parameter.key) = parameter.val{1};
0806           end
0807         else
0808           a(ii).data.(parameter.key) = parameter.val;
0809         end
0810 
0811       end % is parameter key member of the data object
0812     end % for-loop over all parameter
0813   end % for-loop over all ao's
0814 
0815 
0816 %--------------------------------------------------------------------------
0817 % construct an ao from a Spectral window
0818 %
0819 function a = aoFromSpecWin(win, VERSION, ALGONAME)
0820 
0821 % Make a cdata object
0822 ts = cdata(win.win);
0823 ts = set(ts, 'name', win.name);
0824 
0825 % Make an analysis object
0826 a.name = win.name;
0827 a.data = ts;
0828 a.hist = history(ALGONAME, VERSION, plist(param('win', win)));
0829 a.provenance  = provenance;
0830 a.description = '';
0831 a.mfile       = {};
0832 a.mfilename   = '';
0833 a.mdlfile     = '';
0834 a.mdlfilename = '';
0835 a.version     = VERSION;
0836 a.created     = time;
0837 a = class(a, 'ao');
0838 
0839 
0840 
0841 %--------------------------------------------------------------------------
0842 % construct an ao from server
0843 %
0844 function bs = aoFromServer(args, VERSION, ALGONAME);
0845 
0846 % retrieve ao from server
0847 bs = retrieve(args(2).val, [args(3:end).val]);
0848 
0849 
0850 %--------------------------------------------------------------------------
0851 % Default Parameter Lists
0852 %
0853 function out = getDefaultPlist(varargin)
0854 
0855 % list of available parameter sets
0856 sets = {'From XML file', 'From ASCII file', 'Function', 'Values',...
0857         'Time-series Function', 'From Window', 'Waveform'};
0858 
0859 if nargin == 0
0860   out = sets;
0861   return
0862 end
0863 
0864 set = varargin{1};
0865 
0866 switch set
0867   case 'From XML file'
0868     out = plist('filename', 'foo.xml');
0869   case 'From ASCII file'
0870     out = plist('filename', 'foo.txt',...
0871                 'type', 'tsdata',...
0872                 'columns', [1 2],...
0873                 'num_columns', 10,...
0874                 'comment_char', '%');
0875   case 'Function'
0876     out = plist('fcn', 'randn(100,1)');
0877   case 'Values'
0878     out = plist('vals', [1 2 3]);
0879   case 'Time-series Function'
0880     out = plist('tsfcn', 'randn(size(t))', 'fs', 10, 'nsecs', 100);
0881   case 'From Window'
0882     out = plist('win', specwin('Hanning', 100));
0883   case 'Waveform'
0884     out = plist('waveform', 'sine wave',...
0885                 'f', 1.23,...
0886                 'phi', 0,...
0887                 'fs', 10,...
0888                 'nsecs', 10);
0889   otherwise
0890     out = plist();
0891 end
0892 
0893 
0894 % END

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003