Home > classes > @ao > ao.m

ao

PURPOSE ^

AO analysis object class constructor.

SYNOPSIS ^

function a = ao(varargin)

DESCRIPTION ^

 AO analysis object class constructor.
 
     Create an analysis object.

     Properties:
       tag        - unique ID tag. (always -1 before submission to AO Repository)
       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(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.
       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)

     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(1:20); % get a matrix of data values [t x];
   [x,y] = a.data(1:20); % get x,y data from data object.


 See also tsdata, fsdata, xydata, cdata

 M Hewitson 30-01-07

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function a = ao(varargin)
0002 % AO analysis object class constructor.
0003 %
0004 %     Create an analysis object.
0005 %
0006 %     Properties:
0007 %       tag        - unique ID tag. (always -1 before submission to AO Repository)
0008 %       name       - name of analysis object
0009 %       data       - data object (tsdata, fsdata)
0010 %       history    - history object
0011 %       provenance - creation info, machine, user, etc
0012 %
0013 %
0014 %     Possible constructors:
0015 %
0016 %       a = ao()            - creates an empty analysis object
0017 %       a = ao('a1.xml')    - creates a new analysis object by loading the
0018 %                             analysis object from disk.
0019 %       a = ao('file.txt')  - creates a new analysis object by loading the
0020 %       a = ao('file.dat')    data in 'file.txt'. The ascii file is assumed
0021 %                             to be an equally sampled two-column file of
0022 %                             time and amplitude. By default, the amplitude
0023 %                             units are taken to be Volts ('V') and the
0024 %                             time samples are assumed to be in seconds.
0025 %       a = ao(plist)       - creates an analysis object with by reading
0026 %                             the file specified by the 'filename'
0027 %                             parameter in the plist.
0028 %       a = ao(data)        - creates an analysis object with a data
0029 %                             object. Data object can be one of tsdata,
0030 %                             fsdata.
0031 %       a = ao(data, hist)  - creates an analysis object with a data
0032 %                             object and a history object
0033 %
0034 %       a = ao(specwin)     - creates an analysis object from a specwin
0035 %                             object
0036 %
0037 % Parameters for plist constructor:
0038 %
0039 %   'filename' - construct an AO from a filename.
0040 %                Example: plist(param('filename', 'a1.xml'))
0041 %
0042 %   'fcn'      - construct an AO from some valid MATLAB function.
0043 %                Example: plist(param('fcn', 'randn(100)'))
0044 %
0045 %   'vals'     - construct an AO from a set of values.
0046 %                Example: plist(param('vals', [1 2 3]))
0047 %
0048 %   'tsfcn'    - construct an AO from a function of time. You need to also
0049 %                specify parameters 'fs' and 'nsecs' for this call.
0050 %                Example:
0051 %                  plist([param('fs', 10) param('nsecs', 10) param('tsfcn',
0052 %                  'sin(2*pi*1.4*t) + 0.1*randn(size(t))')])
0053 %
0054 %   'win'      - construct an AO from a specwin window function. Tjis
0055 %                creates a cdata type AO containing the window values.
0056 %                Example: plist(param('win', specwin('Hannning', 100)))
0057 %
0058 %   'waveform' - construct an AO from a waveform description. Here you need
0059 %                to specify additional parameters 'fs' and 'nsecs', and also,
0060 %                for the following waveform types:
0061 %                'sine wave'      - 'f', 'phi'
0062 %                'noise'          - 'type' (can be 'Normal' or 'Uniform')
0063 %                'chirp'          - 'f0', 'f1', 't1'      (help chirp)
0064 %                'Gaussian pulse' - 'f0', 'bw'            (help gauspuls)
0065 %                'Square wave'    - 'f', 'duty'           (help square)
0066 %                'Sawtooth'       - 'f', 'width'          (help sawtooth)
0067 %
0068 %     Indexing:
0069 %       b = a(1) % where a is an array of analysis objects
0070 %       d = a.data; % get the data object
0071 %       h = a.hist; % get the history object
0072 %       d = a.data(1:20); % get a matrix of data values [t x];
0073 %   [x,y] = a.data(1:20); % get x,y data from data object.
0074 %
0075 %
0076 % See also tsdata, fsdata, xydata, cdata
0077 %
0078 % M Hewitson 30-01-07
0079 %
0080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0081 
0082 %       a = ao('object.mat') - creates a new analysis object by loading the
0083 %                             analysis object from disk.
0084 
0085 ALGONAME = mfilename;
0086 VERSION  = '$Id: ao.html,v 1.1 2007/06/08 14:15:02 hewitson Exp $';
0087 
0088 % process input arguments
0089 args = [];
0090 k = 0;
0091 pls = [];
0092 for j=1:nargin
0093   if ~isempty(varargin{j})
0094     k = k + 1;
0095     if isa(varargin{j}, 'plist')
0096       pls = [pls varargin{j}];
0097     else
0098       args(k).val = varargin{j};
0099       args(k).n   = j;
0100     end
0101   end
0102 end
0103 
0104 % We could have multiple input plist objects. Combine them here.
0105 if isa(pls, 'plist')
0106   pl = combine(pls);
0107 else
0108   pl = [];
0109 end
0110 
0111 Nargs = length(args);
0112 if ~isempty(pl)
0113   args(Nargs+1).val = pl;
0114   args(Nargs+1).n   = j+1;
0115   Nargs = length(args);
0116 end
0117 
0118 %-----------------------------------------------------
0119 % create from SERVER
0120 if Nargs > 0
0121   if ischar(args(1).val)
0122     if strcmp(args(1).val, 'SERVER')
0123       a = aoFromServer(args, VERSION, ALGONAME);
0124       return
0125     end
0126   end
0127 end
0128 
0129 %-----------------------------------------------------
0130 % create empty AO
0131 if Nargs == 0
0132   a.tag         = -1;
0133   a.name        = 'None';
0134   a.data        = {};
0135   a.hist        = history(ALGONAME, VERSION);
0136   a.provenance  = provenance;
0137   a.comment     = '';
0138   a.mfile       = {};
0139   a.mfilename   = '';
0140   a.mdlfile     = '';
0141   a.mdlfilename = '';
0142   a = class(a, 'ao');
0143 %-----------------------------------------------------
0144 % copy AO, load from file, create from x-data
0145 elseif Nargs == 1              % create ao with data
0146   %-----------------------------------------------------
0147   % copy AO
0148   if isa(args(1).val, 'ao')
0149     a = args(1).val;
0150   %-----------------------------------------------------
0151   % create from struct
0152   elseif isstruct(args(1).val)
0153     astruct = args(1).val;
0154     a.tag  = astruct.tag;
0155     a.name = astruct.name;
0156     data = astruct.data;
0157     if isstruct(data)
0158       if isfield(data, {'t', 'x'})
0159         data = tsdata(data);
0160       elseif isfield(data, {'f', 'xx'})
0161         data = fsdata(data);
0162       elseif isfield(data, {'vals'})
0163         data = cdata(data);
0164       elseif isfield(data, {'x', 'y'})
0165         data = xydata(data);
0166       else
0167         error('### Unknown data type in input structure.');
0168       end
0169     end
0170     a.data = data;
0171     hist   = astruct.hist;
0172     if isstruct(hist)
0173       hist = history(hist);
0174     end
0175     a.hist = hist;
0176     prov = astruct.provenance;
0177     if isstruct(prov)
0178       prov = provenance(prov);
0179     end
0180     a.provenance  = prov;
0181     a.comment     = astruct.comment;
0182     a.mfile       = astruct.mfile;
0183     a.mfilename   = astruct.mfilename;
0184     a.mdlfile     = astruct.mdlfile;
0185     a.mdlfilename = astruct.mdlfilename;
0186     a = class(a, 'ao');
0187 
0188   %-----------------------------------------------------
0189   % create from constant
0190   elseif isnumeric(args(1).val)
0191 
0192     a.tag  = -1;
0193     a.name = 'const';
0194     a.data = cdata(args(1).val);
0195     a.hist = history(ALGONAME, VERSION, plist(param('vals', args(1).val)));
0196 %     a.hist = history(ALGONAME, VERSION, plist(param('fcn', ['[' sprintf('%g ', args(1).val) ']'])));
0197     a.provenance  = provenance;
0198     a.comment     = '';
0199     a.mfile     = {};
0200     a.mfilename = '';
0201     a.mdlfile   = '';
0202     a.mdlfilename = '';
0203     a = class(a, 'ao');
0204 
0205     %-----------------------------------------------------
0206     % create data using plist
0207   elseif isa(args(1).val, 'plist')
0208 
0209     pl       = args(1).val;
0210     filename = find(pl, 'filename');
0211     fcn      = find(pl, 'fcn');
0212     vals     = find(pl, 'vals');
0213     tsfcn    = find(pl, 'tsfcn');
0214     win      = find(pl, 'win');
0215     waveform = find(pl, 'waveform');
0216 
0217     if ~isempty(filename)
0218       % do filename constructor
0219       a = aoFromFilename(filename, VERSION, ALGONAME);
0220 
0221     elseif ~isempty(fcn)
0222       % do function constructor
0223       a = aoFromFcn(pl, VERSION, ALGONAME);
0224 
0225     elseif ~isempty(vals)
0226       % do vals constructor
0227       a = aoFromVals(vals, VERSION, ALGONAME);
0228 
0229     elseif ~isempty(tsfcn)
0230       % do tsfcn constructor
0231       a = aoFromTSfcn(pl, VERSION, ALGONAME);
0232 
0233     elseif ~isempty(win)
0234       % do win constructor
0235       a = aoFromSpecWin(win, VERSION, ALGONAME);
0236 
0237     elseif ~isempty(waveform)
0238       % do waveform constructor
0239       a = aoFromWaveform(pl, VERSION, ALGONAME);
0240 
0241     else
0242       error('### Unknown AO constructor method.');
0243     end
0244 
0245   %-----------------------------------------------------
0246   % create from spectral window
0247   elseif isa(args(1).val, 'specwin')
0248     a = aoFromSpecWin(args(1).val, VERSION, ALGONAME);
0249   %-----------------------------------------------------
0250   % create from filename
0251   elseif ischar(args(1).val)
0252     a = aoFromFilename(args(1).val, VERSION, ALGONAME);
0253   %-----------------------------------------------------
0254   % create from x-data
0255   else
0256     data        = args(1).val;
0257     a.tag       = -1;
0258     a.name      = 'None';
0259     a.data      = data;
0260     dinfo       = whos('data');
0261     a.hist      = history(ALGONAME, VERSION, plist([param('input', inputname(1)) param('type', ['AO/' dinfo.class])]));
0262     a.provenance  = provenance;
0263     a.comment     = '';
0264     a.mfile     = {};
0265     a.mfilename = '';
0266     a.mdlfile   = '';
0267     a.mdlfilename = '';
0268     a = class(a, 'ao');
0269     if ~isa(a.data, 'tsdata') &&...
0270        ~isa(a.data, 'fsdata') &&...
0271        ~isa(a.data, 'cdata')  &&...
0272        ~isa(a.data, 'xydata')
0273       error('### unknown data object.')
0274     end
0275   end
0276 %-----------------------------------------------------
0277 %  data, hist
0278 elseif Nargs == 2
0279   a.tag       = -1;
0280   a.name      = 'None';
0281   a.data      = args(1).val;
0282   a.hist      = args(2).val; %history(ALGONAME, VERSION, [], varargin{2});
0283   a.provenance  = provenance;
0284   a.comment     = '';
0285   a.mfile     = {};
0286   a.mfilename = '';
0287   a.mdlfile   = '';
0288   a.mdlfilename = '';
0289   a = class(a, 'ao');
0290   if ~isa(a.data, 'tsdata') &&...
0291      ~isa(a.data, 'fsdata') &&...
0292      ~isa(a.data, 'cdata')  &&...
0293      ~isa(a.data, 'xydata')
0294     error('### unknown data object.')
0295   end
0296   if ~isa(a.hist, 'history')
0297     error('### unknown history object.')
0298   end
0299 
0300 
0301 %-----------------------------------------------------
0302 %  full constructor - all fields
0303 elseif Nargs == 9
0304 
0305   a.tag         = args(1).val;
0306   a.name        = args(2).val;
0307   a.data        = args(3).val;
0308   a.hist        = args(4).val;
0309   a.provenance  = args(5).val;
0310   a.comment     = args(6).val;
0311   a.mfile       = args(7).val;
0312   a.mfilename   = args(8).val;
0313   a.mdlfile     = args(9).val;
0314   a.mdlfilename = args(10).val;
0315   a = class(a, 'ao');
0316 
0317 else
0318   error('### Unknown number of constructor arguments.');
0319 end
0320 
0321 %--------------------------------------------------------------------------
0322 % construct an ao from a value set
0323 %
0324 function a = aoFromVals(vals, VERSION, ALGONAME)
0325 
0326 % Make a cdata object
0327 ts = cdata(plist(param('vals', vals)));
0328 ts = set(ts, 'name', 'Data');
0329 
0330 % Make an analysis object
0331 a.tag  = -1;
0332 a.name = 'Data';
0333 a.data = ts;
0334 a.hist = history(ALGONAME, VERSION, plist(param('vals', vals)));
0335 a.provenance  = provenance;
0336 a.comment     = '';
0337 a.mfile     = {};
0338 a.mfilename = '';
0339 a.mdlfile   = '';
0340 a.mdlfilename = '';
0341 a = class(a, 'ao');
0342 
0343 
0344 %--------------------------------------------------------------------------
0345 % construct an ao from a waveform
0346 %
0347 function a = aoFromWaveform(pl, VERSION, ALGONAME)
0348 
0349 waveform = find(pl, 'waveform');
0350 switch waveform
0351   %------------ Sine Wave
0352   case 'sine wave'
0353     freq = find(pl, 'f');
0354     phi  = find(pl, 'phi');
0355     tsfcn = sprintf('sin(2*pi*%g*t + %g*pi/180)', freq, phi);
0356     %------------ Noise
0357   case 'noise'
0358     ntype = find(pl, 'type');
0359     switch ntype
0360       case 'Normal'
0361         tsfcn = 'randn(size(t))';
0362       case 'Uniform'
0363         tsfcn = 'rand(size(t))';
0364     end
0365     %------------ Chirp
0366   case 'chirp'
0367     f0  = find(pl, 'f0');
0368     fe  = find(pl, 'f1');
0369     te  = find(pl, 't1');
0370     tsfcn = sprintf('chirp(t,%g,%g,%g)', f0, fe, te);
0371     %------------ Gaussian pulse
0372   case 'Gaussian pulse'
0373     fc  = find(pl, 'f0');
0374     bw  = find(pl, 'bw');
0375     tsfcn = sprintf('gauspuls(t,%g,%g)', fc, bw);
0376   case 'Square wave'
0377     freq = find(pl, 'f');
0378     duty = find(pl, 'duty');
0379     tsfcn = sprintf('square(2*pi*%g*t,%g)', freq, duty);
0380   case 'Sawtooth'
0381     freq  = find(pl, 'f');
0382     width = find(pl, 'width');
0383     tsfcn = sprintf('sawtooth(2*pi*%g*t,%g)', freq, width);
0384 end
0385 
0386 nsecs  = find(pl, 'nsecs');
0387 fs     = find(pl, 'fs');
0388 
0389 % construct tsdata
0390 t = linspace(0, nsecs-1/fs, nsecs*fs);
0391 % make y data
0392 eval(['y=' tsfcn ';']);
0393 
0394 ts = tsdata(t,y);
0395 ts = set(ts, 'name', tsfcn);
0396 ts = set(ts, 'xunits', 's');
0397 ts = set(ts, 'yunits', 'V');
0398 
0399 % Make an analysis object
0400 a.tag  = -1;
0401 a.name = waveform;
0402 a.data = ts;
0403 a.hist = history(ALGONAME, VERSION, pl);
0404 a.provenance  = provenance;
0405 a.comment     = '';
0406 a.mfile     = {};
0407 a.mfilename = '';
0408 a.mdlfile   = '';
0409 a.mdlfilename = '';
0410 a = class(a, 'ao');
0411 
0412 
0413 
0414 %--------------------------------------------------------------------------
0415 % construct an ao from a ts-function string
0416 %
0417 function a = aoFromTSfcn(pl, VERSION, ALGONAME)
0418 
0419 nsecs  = find(pl, 'nsecs');
0420 fs     = find(pl, 'fs');
0421 fcn    = find(pl, 'tsfcn');
0422 
0423 % construct tsdata
0424 t = linspace(0, nsecs-1/fs, nsecs*fs);
0425 % make y data
0426 y = eval([fcn ';']);
0427 
0428 ts = tsdata(t,y);
0429 ts = set(ts, 'name', fcn);
0430 ts = set(ts, 'xunits', 's');
0431 ts = set(ts, 'yunits', 'V');
0432 
0433 % Make an analysis object
0434 a.tag  = -1;
0435 a.name = 'TSfcn';
0436 a.data = ts;
0437 a.hist = history(ALGONAME, VERSION, pl);
0438 a.provenance  = provenance;
0439 a.comment     = '';
0440 a.mfile     = {};
0441 a.mfilename = '';
0442 a.mdlfile   = '';
0443 a.mdlfilename = '';
0444 a = class(a, 'ao');
0445 
0446 %--------------------------------------------------------------------------
0447 % construct an ao from a function string
0448 %
0449 function a = aoFromFcn(pl, VERSION, ALGONAME)
0450 
0451 fcn = find(pl, 'fcn');
0452 
0453 % Make a cdata object
0454 ts = cdata(plist(param('fcn', fcn)));
0455 ts = set(ts, 'name', fcn);
0456 
0457 % Make an analysis object
0458 a.tag  = -1;
0459 a.name = fcn;
0460 a.data = ts;
0461 a.hist = history(ALGONAME, VERSION, pl);
0462 a.provenance  = provenance;
0463 a.comment     = '';
0464 a.mfile     = {};
0465 a.mfilename = '';
0466 a.mdlfile   = '';
0467 a.mdlfilename = '';
0468 a = class(a, 'ao');
0469 
0470 %--------------------------------------------------------------------------
0471 % construct an ao from filename
0472 %
0473 function a = aoFromFilename(filename, VERSION, ALGONAME)
0474 
0475 [path,fname,ext,versn] = fileparts(filename);
0476 switch ext
0477   case {'.txt','.dat'}
0478     disp(['  + creating AO from text file ' filename]);
0479 
0480     in = load(filename);
0481     t  = in(:,1);
0482     x  = in(:,2);
0483 
0484     % Make a tsdata object
0485     ts = tsdata(t,x);
0486     ts = set(ts, 'name', fname);
0487     ts = set(ts, 'xunits', 's');
0488     ts = set(ts, 'yunits', 'V');
0489 
0490     % Make an analysis object
0491     a.tag  = -1;
0492     a.name = fname;
0493     a.data = ts;
0494     a.hist = history(ALGONAME, VERSION, plist([param('filename', [fname ext]) param('filepath', path)]));
0495     a.provenance  = provenance;
0496     a.comment     = '';
0497     a.mfile     = {};
0498     a.mfilename = '';
0499     a.mdlfile   = '';
0500     a.mdlfilename = '';
0501     a = class(a, 'ao');
0502 
0503   case '.mat'
0504 %     disp('??? I do not work yet. Please code me up.');
0505     
0506     in = load(filename);
0507     fnames = fieldnames(in);
0508     eval(sprintf('a = in.%s;', fnames{1}));
0509     
0510   case '.xml'
0511 
0512     [tag, name, prov, comment, mfile, mfname, mdlfile, mdlfname, hist, data] = xml_read(filename);
0513     a.tag  = tag;
0514     a.name = name;
0515     a.data = data;
0516     a.hist = history(ALGONAME, VERSION, plist([param('filename', [fname ext]) param('filepath', path)]), hist);
0517     a.provenance  = prov;
0518     a.comment     = comment;
0519     a.mfile   = mfile;
0520     a.mfilename = mfname;
0521     a.mdlfile   = mdlfile;
0522     a.mdlfilename = mdlfname;
0523     a = class(a, 'ao');
0524 
0525   otherwise
0526     error('### unknown file type.');
0527 end
0528 
0529 %--------------------------------------------------------------------------
0530 % construct an ao from a Spectral window
0531 %
0532 function a = aoFromSpecWin(win, VERSION, ALGONAME)
0533 
0534 % Make a cdata object
0535 ts = cdata(win.win);
0536 ts = set(ts, 'name', win.name);
0537 
0538 % Make an analysis object
0539 a.tag  = -1;
0540 a.name = win.name;
0541 a.data = ts;
0542 a.hist = history(ALGONAME, VERSION, plist(param('win', win)));
0543 a.provenance  = provenance;
0544 a.comment     = '';
0545 a.mfile     = {};
0546 a.mfilename = '';
0547 a.mdlfile   = '';
0548 a.mdlfilename = '';
0549 a = class(a, 'ao');
0550 
0551 
0552 
0553 %--------------------------------------------------------------------------
0554 % construct an ao from server
0555 %
0556 function bs = aoFromServer(args, VERSION, ALGONAME);
0557 
0558 % retrieve ao from server
0559 bs = retrieve(args(2).val, [args(3:end).val]);
0560 
0561 % END

Generated on Fri 08-Jun-2007 16:09:11 by m2html © 2003