AO analysis object class constructor. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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('a1.mat') - 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. [default: empty array] 'columns' [1 2 1 4] 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) [default: [1 2] ] '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(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 a = ao(plist) - creates an analysis object from the description given in the parameter list Parameter sets for plist constructor (in order of priority): From XML File ------------- Construct an AO by loading it from an XML file. 'filename' - construct an AO from a filename. Example: plist('filename', 'a1.xml') [default: empty string] From MAT File ------------- Construct an AO by loading it from a MAT file. 'filename' - construct an AO from a filename. Example: plist('filename', 'a1.mat') [default: empty string] From ASCII File --------------- Construct an AO by loading it from an ASCII text file. 'filename' - construct an AO from a filename. Example: plist('filename', 'a1.txt') [default: empty string] For additional parameters, see constructor ao(file, pl) above. From Function ------------- Construct an AO from the description of any valid MATLAB function. 'fcn' - any valid MATLAB function. Example: plist('fcn', 'randn(100,1)') You can pass additional parameters to the fcn as extra parameters in the parameter list: plist('fcn', 'a*b', 'a', 2, 'b', 1:20); ** Note: case is ignored in the function specification such the following: plist('fcn', 'a*A/b', 'a', 2, 'B', 1:20); results in: 2*2*[1:20] [default: 'randn(100,1)'] From Values ----------- Construct an AO from a set of values. 'vals' - a set of values. Example: plist('vals', [1 2 3]) optional parameter: repeat 'N' times Example: plist('vals', [1 2 3], 'N', 10) [default: vals: [1], N: [1] ] From Time-series Function ------------------------- Construct an AO from a function of time, t. 'tsfcn' - a function of time. You can also specify optional parameters 'fs' - sampling frequency [default: 10 Hz] 'nsecs' - length in seconds [default: 10 s] You can also specify the initial time (t0) associated with the time-series by passing a parameter 't0' with a value that is a time object [default: time(0)] Example: plist('fs', 10, 'nsecs', 10, ... 'tsfcn', 'sin(2*pi*1.4*t) + 0.1*randn(size(t))', ... 't0', time('1980-12-01 12:43:12')); From Frequency-series Function ------------------------------ Construct an AO from a function of frequency, f. 'fsfcn' - a function of frequency, f. [default: 'f'] You can also specify optional parameters: 'f1' - the initial frequency [default: 1e-9] 'f2' - the final frequency [default: 5] 'nf' - the number of frequency samples [default: 1000] 'scale' - 'log' or 'lin' frequency spacing [default: 'log'] or provide a frequency vector: 'f' - a vector of frequencies on which to evaluate the function [default: [] ] From Window ----------- Construct an AO from a spectral window object. 'win' - A specwin object. This creates a cdata type AO containing the window values. Example: plist('win', specwin('Hannning', 100)) [default: specwin('Hanning', 100)] From Waveform ------------- Construct an AO from a waveform description. 'waveform' - a waveform description (see options below). You can also specify additional parameters: 'fs' - sampling frequency [default: 10 Hz] 'nsecs' - length in seconds [default: 10 s] and, for the following waveform types: 'sine wave' - 'A', '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) You can also specify the initial time (t0) associated with the time-series by passing a parameter 't0' with a value that is a time object. [defaults: waveform: 'sine wave', A: 1, f: 1.23, phi: 0, fs: 10, nsecs: 10, t0: time(0) ] From Repository --------------- Construct an AO by retrieving it from an LTPDA repository. 'Hostname' - the repository hostname. Only those objects which are AOs are returned. [default: 'localhost']; Additional parameters: 'Database' - The database name [default: 'ltpda'] 'ID' - A vector of object IDs. [default: []] From Polynomial --------------- Construct an AO from a set of polynomial coefficients. 'polyval' - a set of polynomial coefficients. [default: [-0.0001 0.02 -1 -1] ] Additional parameters: 'Nsecs' and 'fs' - number of seconds, and sample rate [defaults: nsecs: 10, fs: 10] or 't' - vector of time vertices [default: [] ] Example: a = ao(plist('polyval', [1 2 3], 'Nsecs', 10, 'fs', 10); From Plist ---------- 'Plist' - construct from a plist. The value passed should be a plist object. [default: empty plist] 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') The following call returns a string that contains the routine CVS version: >> version = ao(ao,'Version') The following call returns a string that contains the routine category: >> category = ao(ao,'Category') See also tsdata, fsdata, xydata, cdata, xyzdata M Hewitson 30-01-07 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function varargout = ao(varargin) 0002 % AO analysis object class constructor. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: AO analysis object class constructor. 0007 % Create an analysis object. 0008 % 0009 % Properties: 0010 % name - name of analysis object 0011 % data - data object (tsdata, fsdata) 0012 % history - history object 0013 % provenance - creation info, machine, user, etc 0014 % 0015 % 0016 % Possible constructors: 0017 % 0018 % a = ao() - creates an empty analysis object 0019 % a = ao('a1.xml') - creates a new analysis object by loading the 0020 % analysis object from disk. 0021 % a = ao('a1.mat') - creates a new analysis object by loading the 0022 % analysis object from disk. 0023 % a = ao('file.txt') - creates a new analysis object by loading the 0024 % a = ao('file.dat') data in 'file.txt'. The ascii file is assumed 0025 % to be an equally sampled two-column file of 0026 % time and amplitude. By default, the amplitude 0027 % units are taken to be Volts ('V') and the 0028 % time samples are assumed to be in seconds. 0029 % a = ao('file',pl) - creates a new analysis object by loading the 0030 % data in 'file'. The parameter list decide how the 0031 % analysis object is created. The valid key values 0032 % of the parameter list are: 0033 % 'type' 'tsdata','fsdata','xydata' 0034 % [default: 'tsdata'] 0035 % 'use_fs' if this value is set, the 0036 % x-axes is computed by the fs value. 0037 % [default: empty array] 0038 % 'columns' [1 2 1 4] 0039 % Each pair represented the x- and y-axes. 0040 % (Each column pair creates an analysis object) 0041 % Is the value 'use_fs' is used then 0042 % represent each column the y-axes. 0043 % (Each column creates an analysis object) 0044 % [default: [1 2] ] 0045 % 'num_columns' Maximum colums in the file 0046 % [default: 10] 0047 % 'comment_char' The comment character in the file 0048 % [default: '%'] 0049 % 'description' To set the description in the analysis object 0050 % '...' every property of the data object 0051 % e.g. 'name' 0052 % If the constructor creates multiple ao's it is 0053 % possible to give each data class its own e.g. 0054 % 'name'. In this case the parameter list with the 0055 % key 'name' must have cell of the different values 0056 % as the name of the different data objects. e.g. 0057 % pl = plist('columns', [1 2 1 3], ... 0058 % 'name', {'name1' 'name2'}, ... 0059 % 'xunits', 'sec', ... 0060 % 'yunits', {'V' 'Hz'}); 0061 % This parameter list creates two ao's with tsdata. 0062 % a = ao(data) - creates an analysis object with a data 0063 % object. Data object can be one of tsdata, 0064 % fsdata, cdata, xydata, xyzdata. 0065 % a = ao(data, hist) - creates an analysis object with a data 0066 % object and a history object 0067 % a = ao(specwin) - creates an analysis object from a specwin 0068 % object 0069 % a = ao(plist) - creates an analysis object from the description 0070 % given in the parameter list 0071 % 0072 % Parameter sets for plist constructor (in order of priority): 0073 % 0074 % From XML File 0075 % ------------- 0076 % 0077 % Construct an AO by loading it from an XML file. 0078 % 0079 % 'filename' - construct an AO from a filename. 0080 % Example: plist('filename', 'a1.xml') 0081 % [default: empty string] 0082 % 0083 % From MAT File 0084 % ------------- 0085 % 0086 % Construct an AO by loading it from a MAT file. 0087 % 0088 % 'filename' - construct an AO from a filename. 0089 % Example: plist('filename', 'a1.mat') 0090 % [default: empty string] 0091 % 0092 % From ASCII File 0093 % --------------- 0094 % 0095 % Construct an AO by loading it from an ASCII text file. 0096 % 0097 % 'filename' - construct an AO from a filename. 0098 % Example: plist('filename', 'a1.txt') 0099 % [default: empty string] 0100 % 0101 % For additional parameters, see constructor ao(file, pl) above. 0102 % 0103 % From Function 0104 % ------------- 0105 % 0106 % Construct an AO from the description of any valid MATLAB function. 0107 % 0108 % 'fcn' - any valid MATLAB function. 0109 % Example: plist('fcn', 'randn(100,1)') 0110 % 0111 % You can pass additional parameters to the fcn as extra 0112 % parameters in the parameter list: 0113 % plist('fcn', 'a*b', 'a', 2, 'b', 1:20); 0114 % 0115 % ** Note: case is ignored in the function specification 0116 % such the following: 0117 % plist('fcn', 'a*A/b', 'a', 2, 'B', 1:20); 0118 % results in: 0119 % 2*2*[1:20] 0120 % 0121 % [default: 'randn(100,1)'] 0122 % 0123 % From Values 0124 % ----------- 0125 % 0126 % Construct an AO from a set of values. 0127 % 0128 % 'vals' - a set of values. 0129 % Example: plist('vals', [1 2 3]) 0130 % optional parameter: repeat 'N' times 0131 % Example: plist('vals', [1 2 3], 'N', 10) 0132 % 0133 % [default: vals: [1], N: [1] ] 0134 % 0135 % From Time-series Function 0136 % ------------------------- 0137 % 0138 % Construct an AO from a function of time, t. 0139 % 0140 % 'tsfcn' - a function of time. 0141 % 0142 % You can also specify optional parameters 0143 % 'fs' - sampling frequency [default: 10 Hz] 0144 % 'nsecs' - length in seconds [default: 10 s] 0145 % 0146 % You can also specify the initial time (t0) associated with 0147 % the time-series by passing a parameter 't0' with a value 0148 % that is a time object [default: time(0)] 0149 % Example: 0150 % plist('fs', 10, 'nsecs', 10, ... 0151 % 'tsfcn', 'sin(2*pi*1.4*t) + 0.1*randn(size(t))', ... 0152 % 't0', time('1980-12-01 12:43:12')); 0153 % 0154 % From Frequency-series Function 0155 % ------------------------------ 0156 % 0157 % Construct an AO from a function of frequency, f. 0158 % 0159 % 'fsfcn' - a function of frequency, f. [default: 'f'] 0160 % 0161 % You can also specify optional parameters: 0162 % 'f1' - the initial frequency [default: 1e-9] 0163 % 'f2' - the final frequency [default: 5] 0164 % 'nf' - the number of frequency samples [default: 1000] 0165 % 'scale' - 'log' or 'lin' frequency spacing [default: 'log'] 0166 % or provide a frequency vector: 0167 % 'f' - a vector of frequencies on which to evaluate the 0168 % function [default: [] ] 0169 % 0170 % From Window 0171 % ----------- 0172 % 0173 % Construct an AO from a spectral window object. 0174 % 0175 % 'win' - A specwin object. 0176 % 0177 % This creates a cdata type AO containing the window values. 0178 % Example: plist('win', specwin('Hannning', 100)) 0179 % 0180 % [default: specwin('Hanning', 100)] 0181 % 0182 % From Waveform 0183 % ------------- 0184 % 0185 % Construct an AO from a waveform description. 0186 % 0187 % 'waveform' - a waveform description (see options below). 0188 % 0189 % You can also specify additional parameters: 0190 % 'fs' - sampling frequency [default: 10 Hz] 0191 % 'nsecs' - length in seconds [default: 10 s] 0192 % 0193 % and, for the following waveform types: 0194 % 'sine wave' - 'A', 'f', 'phi' 0195 % 'noise' - 'type' (can be 'Normal' or 'Uniform') 0196 % 'chirp' - 'f0', 'f1', 't1' (help chirp) 0197 % 'Gaussian pulse' - 'f0', 'bw' (help gauspuls) 0198 % 'Square wave' - 'f', 'duty' (help square) 0199 % 'Sawtooth' - 'f', 'width' (help sawtooth) 0200 % 0201 % You can also specify the initial time (t0) associated with 0202 % the time-series by passing a parameter 't0' with a value 0203 % that is a time object. 0204 % 0205 % [defaults: waveform: 'sine wave', A: 1, f: 1.23, phi: 0, 0206 % fs: 10, nsecs: 10, t0: time(0) ] 0207 % 0208 % 0209 % From Repository 0210 % --------------- 0211 % 0212 % Construct an AO by retrieving it from an LTPDA repository. 0213 % 0214 % 'Hostname' - the repository hostname. Only those objects which 0215 % are AOs are returned. 0216 % [default: 'localhost']; 0217 % 0218 % Additional parameters: 0219 % 0220 % 'Database' - The database name [default: 'ltpda'] 0221 % 'ID' - A vector of object IDs. [default: []] 0222 % 0223 % From Polynomial 0224 % --------------- 0225 % 0226 % Construct an AO from a set of polynomial coefficients. 0227 % 0228 % 'polyval' - a set of polynomial coefficients. 0229 % [default: [-0.0001 0.02 -1 -1] ] 0230 % 0231 % Additional parameters: 0232 % 'Nsecs' and 'fs' - number of seconds, and sample rate 0233 % [defaults: nsecs: 10, fs: 10] 0234 % or 't' - vector of time vertices 0235 % [default: [] ] 0236 % 0237 % Example: a = ao(plist('polyval', [1 2 3], 'Nsecs', 10, 'fs', 10); 0238 % 0239 % 0240 % From Plist 0241 % ---------- 0242 % 0243 % 'Plist' - construct from a plist. The value passed should be a plist 0244 % object. 0245 % [default: empty plist] 0246 % 0247 % 0248 % 0249 % Examples: 0250 % 0251 % 1) Normally distributed random noise time-series 0252 % 0253 % p = plist('waveform', 'noise', 'fs', 10, 'nsecs', 1000); 0254 % rd10 = ao(p); 0255 % 0256 % 0257 % Indexing: 0258 % b = a(1) % where a is an array of analysis objects 0259 % d = a.data; % get the data object 0260 % h = a.hist; % get the history object 0261 % d = a.data.x(1:20); % get a matrix of data values x; 0262 % 0263 % The following call returns a parameter list object that contains the 0264 % default parameter values: 0265 % 0266 % >> pl = ao(ao, 'Params') 0267 % 0268 % The following call returns a string that contains the routine CVS version: 0269 % 0270 % >> version = ao(ao,'Version') 0271 % 0272 % The following call returns a string that contains the routine category: 0273 % 0274 % >> category = ao(ao,'Category') 0275 % 0276 % See also tsdata, fsdata, xydata, cdata, xyzdata 0277 % 0278 % M Hewitson 30-01-07 0279 % 0280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0281 0282 0283 ALGONAME = mfilename; 0284 VERSION = '$Id: ao.html,v 1.16 2008/03/31 10:27:32 hewitson Exp $'; 0285 CATEGORY = 'Constructor'; 0286 0287 %% Check if this is a special call: 0288 % default parameter list, cvs-version, category 0289 if (nargin == 2 || nargin == 3) &&... 0290 isa(varargin{1}, 'ao') &&... 0291 ischar(varargin{2}) 0292 if strcmp(varargin{2}, 'Params') 0293 if nargin == 2 0294 varargout{1} = getDefaultPlist(); 0295 else 0296 varargout{1} = getDefaultPlist(varargin{3}); 0297 end 0298 return 0299 elseif strcmp(varargin{2}, 'Version') 0300 varargout{1} = VERSION; 0301 return 0302 elseif strcmp(varargin{2}, 'Category') 0303 varargout{1} = CATEGORY; 0304 return 0305 end 0306 end 0307 0308 %% process input arguments 0309 args = []; 0310 k = 0; 0311 pls = []; 0312 for j=1:nargin 0313 if ~isempty(varargin{j}) 0314 k = k + 1; 0315 if isa(varargin{j}, 'plist') 0316 pls = [pls varargin{j}]; 0317 else 0318 args(k).val = varargin{j}; 0319 args(k).n = j; 0320 end 0321 end 0322 end 0323 0324 %% We could have multiple input plist objects. Combine them here. 0325 if isa(pls, 'plist') 0326 pl = combine(pls); 0327 else 0328 pl = []; 0329 end 0330 0331 Nargs = length(args); 0332 if ~isempty(pl) 0333 args(Nargs+1).val = pl; 0334 args(Nargs+1).n = j+1; 0335 Nargs = length(args); 0336 end 0337 0338 %% %%%%%%%%%%%%%%%%%% define parameter properties list %%%%%%%%%%%%%%%%%%%%% 0339 0340 function a = init(version) 0341 a.name = 'None'; 0342 a.data = {}; 0343 a.hist = history(ALGONAME, VERSION); 0344 a.provenance = provenance; 0345 a.description = ''; 0346 a.mfile = ''; 0347 a.mfilename = ''; 0348 a.mdlfile = ''; 0349 a.mdlfilename = ''; 0350 a.plist = plist(); 0351 a.version = version; 0352 a.created = time; 0353 a = class(a, 'ao'); 0354 end 0355 0356 0357 %% %%%%%%%%%%%%%%%%%%%%%%% Create analysis object %%%%%%%%%%%%%%%%%%%%%%%%%% 0358 0359 %%%%%%%%%% a1 = ao() %%%%%%%%%% 0360 if Nargs == 0 0361 0362 varargout{1} = init(VERSION); 0363 0364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0365 %%% One Parameter %%% 0366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0367 elseif Nargs == 1 % create ao with data 0368 0369 %%%%%%%%%% a1 = ao('foo.mat') %%%%%%%%%% 0370 %%%%%%%%%% a1 = ao('foo.xml') %%%%%%%%%% 0371 if ischar(args(1).val) 0372 0373 a = init(VERSION); 0374 0375 filename = args(1).val; 0376 [path, name, ext, vers] = fileparts(filename); 0377 switch ext 0378 case '.mat' 0379 a = load(filename); 0380 a = a.a; 0381 a.name = strcat(name, ext); 0382 case '.xml' 0383 root_node = xmlread(filename); 0384 a = ltpda_xmlread(root_node, 'ao'); 0385 case {'.txt', '.dat'} 0386 % Add default values 0387 a = aoFromFilenameAndPlist(a, args(1).val, getDefaultPlist('From ASCII File'), VERSION, ALGONAME); 0388 otherwise 0389 error('### Unknown file type.'); 0390 end 0391 varargout{1} = a; 0392 0393 %%%%%%%%%% a1 = ao(ao) %%%%%%%%%% 0394 elseif isa(args(1).val, 'ao') 0395 varargout{1} = args(1).val; 0396 0397 %%%%%%%%%% a1 = ao(struct) %%%%%%%%%% 0398 % create from struct 0399 elseif isstruct(args(1).val) 0400 a = init(VERSION); 0401 astruct = args(1).val; 0402 a.name = astruct.name; 0403 data = astruct.data; 0404 0405 if isstruct(data) 0406 if isfield(data, {'t', 'x'}) 0407 data = tsdata(data); 0408 elseif isfield(data, {'f', 'xx'}) 0409 data = fsdata(data); 0410 elseif isfield(data, {'vals'}) 0411 data = cdata(data); 0412 elseif isfield(data, {'x', 'y'}) 0413 data = xydata(data); 0414 elseif isfield(data, {'x', 'y', 'z'}) 0415 data = xyzdata(data); 0416 else 0417 error('### Unknown data type in input structure.'); 0418 end 0419 end 0420 0421 a.data = data; 0422 hist = astruct.hist; 0423 if isstruct(hist) 0424 hist = history(hist); 0425 end 0426 a.hist = hist; 0427 prov = astruct.provenance; 0428 if isstruct(prov) 0429 prov = provenance(prov); 0430 end 0431 0432 a.provenance = prov; 0433 a.description = astruct.description; 0434 a.mfile = astruct.mfile; 0435 a.mfilename = astruct.mfilename; 0436 a.mdlfile = astruct.mdlfile; 0437 a.mdlfilename = astruct.mdlfilename; 0438 a.plist = astruct.plist; 0439 a.version = astruct.version; 0440 created = astruct.created; 0441 if isstruct(created) 0442 created = time(created); 0443 end 0444 a.created = created; 0445 varargout{1} = a; 0446 0447 %%%%%%%%%% a1 = ao(1) %%%%%%%%%% 0448 % create from constant 0449 elseif isnumeric(args(1).val) 0450 0451 a = init(VERSION); 0452 a.data = cdata(args(1).val); 0453 a.hist = history(ALGONAME, VERSION, plist(param('vals', args(1).val))); 0454 0455 varargout{1} = a; 0456 0457 %%%%%%%%%% a1 = ao(plist) %%%%%%%%%% 0458 % create data using plist 0459 elseif isa(args(1).val, 'plist') 0460 0461 pl = args(1).val; 0462 filename = find(pl, 'filename'); 0463 fcn = find(pl, 'fcn'); 0464 vals = find(pl, 'vals'); 0465 tsfcn = find(pl, 'tsfcn'); 0466 fsfcn = find(pl, 'fsfcn'); 0467 win = find(pl, 'win'); 0468 waveform = find(pl, 'waveform'); 0469 polycoeffs = find(pl, 'polyval'); 0470 hostname = find(pl, 'hostname'); 0471 ipl = find(pl, 'Plist'); 0472 0473 a = init(VERSION); 0474 0475 if ~isempty(filename) 0476 0477 %----------------------------------------------------- 0478 %--- Construct from file 0479 %----------------------------------------------------- 0480 [path, name, ext, vers] = fileparts(filename); 0481 switch ext 0482 case '.mat' 0483 % Add default values 0484 % pl = combine(pl, getDefaultPlist('From MAT File')); 0485 a = load(filename); 0486 a.a.plist = pl; 0487 varargout{1} = a.a; 0488 case '.xml' 0489 % Add default values 0490 % pl = combine(pl, getDefaultPlist('From XML File')); 0491 root_node = xmlread(filename); 0492 a = ltpda_xmlread(root_node, 'ao'); 0493 a.plist = pl; 0494 varargout{1} = a; 0495 case {'.txt', '.dat'} 0496 ao_out = aoFromFilenameAndPlist(a, filename, pl, VERSION, ALGONAME); 0497 if nargout == 1 0498 varargout{1} = ao_out; 0499 elseif nargout == length(ao_out) 0500 for ii = 1:length(ao_out) 0501 varargout{ii} = ao_out(ii); 0502 end 0503 else 0504 warning ('### Too few output variables --> using only the first.'); 0505 varargout{1} = ao_out; 0506 end 0507 otherwise 0508 error('### Unknown file type.'); 0509 end 0510 0511 %----------------------------------------------------- 0512 %--- Construct from function 0513 %----------------------------------------------------- 0514 elseif ~isempty(fcn) 0515 % do function constructor 0516 varargout{1} = aoFromFcn(a, pl, VERSION, ALGONAME); 0517 0518 %----------------------------------------------------- 0519 %--- Construct from Values 0520 %----------------------------------------------------- 0521 elseif ~isempty(vals) 0522 % do vals constructor 0523 varargout{1} = aoFromVals(a, pl, VERSION, ALGONAME); 0524 0525 %----------------------------------------------------- 0526 %--- Construct from Time-series function 0527 %----------------------------------------------------- 0528 elseif ~isempty(tsfcn) 0529 % do tsfcn constructor 0530 varargout{1} = aoFromTSfcn(a, pl, VERSION, ALGONAME); 0531 0532 %----------------------------------------------------- 0533 %--- Construct from frequency-series function 0534 %----------------------------------------------------- 0535 elseif ~isempty(fsfcn) 0536 % do fsfcn constructor 0537 varargout{1} = aoFromFSfcn(a, pl, VERSION, ALGONAME); 0538 0539 %----------------------------------------------------- 0540 %--- Construct from Window 0541 %----------------------------------------------------- 0542 elseif ~isempty(win) 0543 % do win constructor 0544 varargout{1} = aoFromSpecWin(a, pl, VERSION, ALGONAME); 0545 0546 %----------------------------------------------------- 0547 %--- Construct from Waveform 0548 %----------------------------------------------------- 0549 elseif ~isempty(waveform) 0550 % do waveform constructor 0551 varargout{1} = aoFromWaveform(a, pl, VERSION, ALGONAME); 0552 0553 %----------------------------------------------------- 0554 %--- Construct from repository 0555 %----------------------------------------------------- 0556 elseif ~isempty(hostname) 0557 % do hostname constructor 0558 varargout{1} = aoFromRepository(a, pl, VERSION, ALGONAME); 0559 0560 %----------------------------------------------------- 0561 %--- Construct from polynomial 0562 %----------------------------------------------------- 0563 elseif ~isempty(polycoeffs) 0564 % do polyval constructor 0565 varargout{1} = aoFromPolyval(a, pl, VERSION, ALGONAME); 0566 0567 %----------------------------------------------------- 0568 %--- Construct from plist 0569 %----------------------------------------------------- 0570 elseif ~isempty(ipl) 0571 % if the plist is empty, we return an empty ao 0572 if nparams(ipl) == 0 0573 varargout{1} = ao; 0574 else 0575 % do plist constructor 0576 varargout{1} = ao(ipl); 0577 end 0578 0579 %----------------------------------------------------- 0580 %--- ERROR 0581 %----------------------------------------------------- 0582 else 0583 %% is the plist is empty then return an empty AO 0584 if nparams(pl) == 0 0585 varargout{1} = a; 0586 else 0587 error('### Unknown AO constructor method.'); 0588 end 0589 end 0590 0591 %%%%%%%%%% a1 = ao(specwin) %%%%%%%%%% 0592 % create from spectral window 0593 elseif isa(args(1).val, 'specwin') 0594 a = init(VERSION); 0595 varargout{1} = aoFromSpecWin(a, args(1).val, VERSION, ALGONAME); 0596 0597 %%%%%%%%%% a1 = ao(cdata) %%%%%%%%%% 0598 %%%%%%%%%% a1 = ao(fsdata) %%%%%%%%%% 0599 %%%%%%%%%% a1 = ao(tsdata) %%%%%%%%%% 0600 %%%%%%%%%% a1 = ao(xydata) %%%%%%%%%% 0601 % create from data object 0602 else 0603 a = init(VERSION); 0604 data = args(1).val; 0605 a.data = data; 0606 a.hist = history(ALGONAME, VERSION); 0607 0608 if ~isa(a.data, 'tsdata') &&... 0609 ~isa(a.data, 'fsdata') &&... 0610 ~isa(a.data, 'cdata') &&... 0611 ~isa(a.data, 'xydata') &&... 0612 ~isa(a.data, 'xyzdata') 0613 error('### unknown data object.') 0614 end 0615 varargout{1} = a; 0616 end 0617 0618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0619 %%% Two Parameter %%% 0620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0621 0622 elseif Nargs == 2 0623 0624 %%%%%%%%%% Database %%%%%%%%%% 0625 if isa(varargin{1}, 'database') 0626 varargout{1} = retrieve(varargin{1}, varargin{2:end}); 0627 0628 %%%%%%%%%%% ao('foo.txt', pl) %%%%%%%%%% 0629 elseif ischar(args(1).val) && isa(args(2).val, 'plist') 0630 0631 a = init(VERSION); 0632 pl = combine(args(2).val, getDefaultPlist('From ASCII File')); 0633 ao_out = aoFromFilenameAndPlist(a, args(1).val, pl, VERSION, ALGONAME); 0634 0635 for ii = numel(ao_out) 0636 ao_out(ii).plist = args(2).val; 0637 ao_out(ii).name = args(1).val; 0638 end 0639 0640 if nargout == 1 0641 varargout{1} = ao_out; 0642 elseif nargout == length(ao_out) 0643 for ii = 1:length(ao_out) 0644 varargout{ii} = ao_out(ii); 0645 end 0646 else 0647 warning ('### Too few output variables --> using only the first.'); 0648 varargout{1} = ao_out; 0649 end 0650 0651 %%%%%%%%%%% ao(data-object, history-object) %%%%%%%%%% 0652 else 0653 0654 a = init(VERSION); 0655 0656 a.data = args(1).val; 0657 a.hist = args(2).val; %history(ALGONAME, VERSION, [], varargin{2}); 0658 0659 if ~isa(a.data, 'tsdata') &&... 0660 ~isa(a.data, 'fsdata') &&... 0661 ~isa(a.data, 'cdata') &&... 0662 ~isa(a.data, 'xydata') 0663 error('### unknown data object.') 0664 end 0665 if ~isa(a.hist, 'history') 0666 error('### unknown history object.') 0667 end 0668 0669 varargout{1} = a; 0670 0671 end 0672 0673 0674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0675 %%% Nine Parameter %%% 0676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0677 % full constructor - all fields 0678 elseif Nargs == 9 0679 0680 a = init(VERSION); 0681 a.name = args(1).val; 0682 a.data = args(2).val; 0683 a.hist = args(3).val; 0684 a.provenance = args(4).val; 0685 a.description = args(5).val; 0686 a.mfile = args(6).val; 0687 a.mfilename = args(7).val; 0688 a.mdlfile = args(8).val; 0689 a.mdlfilename = args(9).val; 0690 varargout{1} = a; 0691 0692 else 0693 error('### Unknown number of constructor arguments.'); 0694 end 0695 0696 %% Helper functions 0697 0698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0699 % 0700 % FUNCTION: aoFromRepository 0701 % 0702 % DESCRIPTION: Construct an ao from a repository 0703 % 0704 % CALL: aos = aoFromRepository(a, pl, VERSION, ALGONAME) 0705 % 0706 % PARAMETER: a: Initialised analysis object 0707 % pl: Parameter list object 0708 % VERSION: cvs version string 0709 % ALGONAME: The m-file name (use the mfilename command) 0710 % 0711 % HISTORY: 07-05-2007 Hewitson 0712 % Creation 0713 % 0714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0715 function aos = aoFromRepository(a, pli, VERSION, ALGONAME) 0716 0717 % Add default values 0718 pl = combine(pli, getDefaultPlist('From Repository')); 0719 0720 % Get parameters 0721 conn = find(pl, 'conn'); 0722 hostname = find(pl, 'hostname'); 0723 database = find(pl, 'database'); 0724 ids = find(pl, 'id'); 0725 0726 % do we have a connection? 0727 closeConn = 0; 0728 if isempty(conn) 0729 closeConn = 1; 0730 % Connect to repository 0731 conn = mysql_connect(hostname, database); 0732 end 0733 if ~isa(conn, 'database') 0734 error('### connection failed.'); 0735 end 0736 0737 % Get each ID 0738 Nids = length(ids); 0739 aos = []; 0740 for kk=1:Nids 0741 0742 %---- This id 0743 id = ids(kk); 0744 disp(sprintf(' - retrieving ID %d', id)); 0745 0746 %---- check ID object type 0747 tt = mysql_getObjType(conn, id); 0748 %---- If this is an AO 0749 if strcmp(tt, mfilename) 0750 %---- call database constructor 0751 a = ltpda_obj_retrieve(conn, id); 0752 %---- Add history 0753 0754 % make a new history object 0755 % - remove connection object from plist first 0756 h = history(ALGONAME, VERSION, remove(pli, 'conn'), a.hist); 0757 0758 % set this to the AO 0759 a = setnh(a, 'hist', h); 0760 a.plist = remove(pli, 'conn'); 0761 0762 %---- Add to output array 0763 aos = [aos a]; 0764 else 0765 warning(' !skipping ID %d, type %s', id, tt); 0766 end 0767 0768 end 0769 0770 % close connection 0771 if closeConn 0772 close(conn); 0773 end 0774 end 0775 0776 0777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0778 % 0779 % FUNCTION: aoFromPolyval 0780 % 0781 % DESCRIPTION: Construct a time-series ao from polynomial coefficients 0782 % 0783 % CALL: a = aoFromPolyval(a, vals, VERSION, ALGONAME) 0784 % 0785 % PARAMETER: a: Initialised analysis object 0786 % pl: plist containing 'polyval', 'Nsecs', 'fs', or 't' 0787 % VERSION: cvs version string 0788 % ALGONAME: The m-file name (use the mfilename command) 0789 % 0790 % HISTORY: 07-05-2007 Hewitson 0791 % Creation 0792 % 0793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0794 function a = aoFromPolyval(a, pli, VERSION, ALGONAME) 0795 0796 % Add default values 0797 pl = combine(pli, getDefaultPlist('From Polynomial')); 0798 0799 coeffs = find(pl, 'polyval'); 0800 Nsecs = find(pl, 'Nsecs'); 0801 fs = find(pl, 'fs'); 0802 t = find(pl, 't'); 0803 0804 % Check t vector 0805 if isempty(t) 0806 if isempty(Nsecs) || isempty(fs) 0807 error('### Please provide either ''Nsecs'' and ''fs'', or ''t'' for polyval constructor.'); 0808 end 0809 t = linspace(0, Nsecs - 1/fs, Nsecs*fs); 0810 end 0811 0812 y = polyval(coeffs,t); 0813 0814 % Make a tsdata object 0815 ts = tsdata(t, y); 0816 ts = set(ts, 'name', 'Polyval'); 0817 ts = set(ts, 'xunits', 's'); 0818 ts = set(ts, 'yunits', 'V'); 0819 0820 % Make an analysis object 0821 a.data = ts; 0822 a.hist = history(ALGONAME, VERSION, pli); 0823 a.plist = pli; 0824 end 0825 0826 0827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0828 % 0829 % FUNCTION: aoFromVals 0830 % 0831 % DESCRIPTION: Construct an ao from a value set 0832 % 0833 % CALL: a = aoFromVals(a, vals, VERSION, ALGONAME) 0834 % 0835 % PARAMETER: a: Initialised analysis object 0836 % vals: Constant values 0837 % VERSION: cvs version string 0838 % ALGONAME: The m-file name (use the mfilename command) 0839 % 0840 % HISTORY: 07-05-2007 Hewitson 0841 % Creation 0842 % 0843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0844 function a = aoFromVals(a, pli, VERSION, ALGONAME) 0845 0846 % Add default values 0847 pl = combine(pli, getDefaultPlist('From Values')); 0848 0849 vals = find(pl, 'vals'); 0850 N = find(pl, 'N'); 0851 if isempty(N) 0852 N = 1; 0853 end 0854 0855 if ischar(vals) 0856 try 0857 vals = eval(vals); 0858 catch 0859 error('Could not convert char array to numeric array.'); 0860 end 0861 end 0862 0863 % Make a cdata object 0864 ts = cdata(plist(param('vals', repmat(vals, 1, N)))); 0865 ts = set(ts, 'name', 'Vals'); 0866 0867 % Make an analysis object 0868 a.data = ts; 0869 a.hist = history(ALGONAME, VERSION, pli); 0870 a.plist = pli; 0871 end 0872 0873 0874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0875 % 0876 % FUNCTION: aoFromWaveform 0877 % 0878 % DESCRIPTION: Construct an ao from a waveform 0879 % 0880 % CALL: a = aoFromWaveform(a, pl, VERSION, ALGONAME) 0881 % 0882 % PARAMETER: a: Initialised analysis object 0883 % pl: Parameter list object 0884 % VERSION: cvs version string 0885 % ALGONAME: The m-file name (use the mfilename command) 0886 % 0887 % HISTORY: 07-05-2007 Hewitson 0888 % Creation 0889 % 0890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0891 function a = aoFromWaveform(a, pli, VERSION, ALGONAME) 0892 0893 % Add default values 0894 pl = combine(pli, getDefaultPlist('From Waveform')); 0895 0896 nsecs = find(pl, 'nsecs'); 0897 fs = find(pl, 'fs'); 0898 t0 = find(pl, 't0'); 0899 if isempty(t0) 0900 t0 = time(0); 0901 end 0902 0903 waveform = lower(find(pl, 'waveform')); 0904 switch waveform 0905 %------------ Sine Wave 0906 case {'sine wave','sinewave','sine-wave'} 0907 ampl = find(pl, 'A'); 0908 freq = find(pl, 'f'); 0909 phi = find(pl, 'phi'); 0910 0911 if isempty(ampl) 0912 ampl = 1; 0913 end 0914 if isempty(freq) 0915 freq = 1; 0916 end 0917 if isempty(phi) 0918 phi = 0; 0919 end 0920 tsfcn = sprintf('%g*sin(2*pi*%g*t + %g*pi/180)', ampl, freq, phi); 0921 0922 %------------ Noise 0923 case 'noise' 0924 ntype = find(pl, 'type'); 0925 if isempty(ntype) 0926 ntype = 'Normal'; 0927 end 0928 switch lower(ntype) 0929 case 'normal' 0930 tsfcn = 'randn(size(t))'; 0931 case 'uniform' 0932 tsfcn = 'rand(size(t))'; 0933 end 0934 %------------ Chirp 0935 case 'chirp' 0936 f0 = find(pl, 'f0'); 0937 fe = find(pl, 'f1'); 0938 te = find(pl, 't1'); 0939 if isempty(f0) 0940 f0 = 0; 0941 end 0942 if isempty(fe) 0943 fe = fs/2; 0944 end 0945 if isempty(te) 0946 te = nsecs; 0947 end 0948 tsfcn = sprintf('chirp(t,%g,%g,%g)', f0, fe, te); 0949 %------------ Gaussian pulse 0950 case {'gaussian pulse','gaussian-pulse'} 0951 fc = find(pl, 'f0'); 0952 bw = find(pl, 'bw'); 0953 if isempty(fc) 0954 fc = 1; 0955 end 0956 if isempty(bw) 0957 bw = fs/2; 0958 end 0959 tsfcn = sprintf('gauspuls(t,%g,%g)', fc, bw); 0960 case {'square wave','squarewave','square-wave'} 0961 freq = find(pl, 'f'); 0962 duty = find(pl, 'duty'); 0963 if isempty(freq) 0964 freq = 1; 0965 end 0966 if isempty(duty) 0967 duty = 50; 0968 end 0969 tsfcn = sprintf('square(2*pi*%g*t,%g)', freq, duty); 0970 case {'saw tooth','sawtooth','saw-tooth'} 0971 freq = find(pl, 'f'); 0972 width = find(pl, 'width'); 0973 if isempty(freq) 0974 freq = 1; 0975 end 0976 if isempty(width) 0977 width = 0.5; 0978 end 0979 tsfcn = sprintf('sawtooth(2*pi*%g*t,%g)', freq, width); 0980 end 0981 0982 0983 % construct tsdata 0984 t = linspace(0, nsecs-1/fs, nsecs*fs); 0985 % make y data 0986 y = eval([tsfcn ';']); 0987 0988 ts = tsdata(t,y); 0989 ts = set(ts, 'name', tsfcn); 0990 ts = set(ts, 'xunits', 's'); 0991 ts = set(ts, 'yunits', 'V'); 0992 ts = set(ts, 't0', t0); 0993 0994 % Make an analysis object 0995 a.name = waveform; 0996 a.data = ts; 0997 a.hist = history(ALGONAME, VERSION, pli); 0998 a.plist = pli; 0999 end 1000 1001 1002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1003 % 1004 % FUNCTION: aoFromTSfcn 1005 % 1006 % DESCRIPTION: Construct an ao from a ts-function string 1007 % 1008 % CALL: a = aoFromTSfcn(a, pl, VERSION, ALGONAME) 1009 % 1010 % PARAMETER: a: Initialised analysis object 1011 % pl: Parameter list object 1012 % VERSION: cvs version string 1013 % ALGONAME: The m-file name (use the mfilename command) 1014 % 1015 % HISTORY: 07-05-2007 Hewitson 1016 % Creation 1017 % 1018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1019 function a = aoFromTSfcn(a, pli, VERSION, ALGONAME) 1020 1021 % Add default values 1022 pl = combine(pli, getDefaultPlist('From Time-series Function')); 1023 1024 nsecs = find(pl, 'nsecs'); 1025 fs = find(pl, 'fs'); 1026 fcn = find(pl, 'tsfcn'); 1027 randn_state = find(pl, 'rand_state'); 1028 t0 = find(pl, 't0'); 1029 if isempty(t0) 1030 t0 = time(0); 1031 end 1032 1033 % construct tsdata 1034 t = linspace(0, nsecs-1/fs, nsecs*fs); 1035 1036 % make y data 1037 1038 % If the randn state is in the parameter list then set the randn function to 1039 % this sate 1040 if ~isempty(randn_state) 1041 randn('state',randn_state); 1042 else % Store the state of the random function in the parameter list 1043 randn_state = randn('state'); 1044 pli = append(pli, 'rand_state', randn_state); 1045 end 1046 1047 y = eval([fcn ';']); 1048 1049 ts = tsdata(t,y); 1050 ts = set(ts, 'name', fcn); 1051 ts = set(ts, 'xunits', 's'); 1052 ts = set(ts, 'yunits', 'V'); 1053 ts = set(ts, 't0', t0); 1054 1055 % Make an analysis object 1056 a.data = ts; 1057 a.hist = history(ALGONAME, VERSION, pli); 1058 a.plist = pli; 1059 end 1060 1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1062 % 1063 % FUNCTION: aoFromFSfcn 1064 % 1065 % DESCRIPTION: Construct an ao from a fs-function string 1066 % 1067 % CALL: a = aoFromFSfcn(a, pl, VERSION, ALGONAME) 1068 % 1069 % PARAMETER: a: Initialised analysis object 1070 % pl: Parameter list object 1071 % VERSION: cvs version string 1072 % ALGONAME: The m-file name (use the mfilename command) 1073 % 1074 % HISTORY: 07-05-2007 Hewitson 1075 % Creation 1076 % 1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1078 function a = aoFromFSfcn(a, pli, VERSION, ALGONAME) 1079 1080 % Add default values 1081 pl = combine(pli, getDefaultPlist('From Frequency-series Function')); 1082 f = find(pl, 'f'); 1083 if isempty(f) 1084 disp('--- generating f') 1085 f1 = find(pl, 'f1'); 1086 f2 = find(pl, 'f2'); 1087 nf = find(pl, 'nf'); 1088 scale = find(pl, 'scale'); 1089 switch scale 1090 case 'log' 1091 f = logspace(log10(f1), log10(f2), nf); 1092 case 'lin' 1093 f = linspace(f1, f2, nf); 1094 otherwise 1095 error('### Unknown frequency scale specified'); 1096 end 1097 end 1098 1099 % Get the function 1100 fcn = find(pl, 'fsfcn'); 1101 1102 % make y data 1103 y = eval([fcn ';']); 1104 1105 fs = fsdata(f,y); 1106 fs = set(fs, 'name', fcn); 1107 fs = set(fs, 'xunits', 'Hz'); 1108 fs = set(fs, 'yunits', 'Arb'); 1109 1110 % Make an analysis object 1111 a.data = fs; 1112 a.hist = history(ALGONAME, VERSION, pli); 1113 a.plist = pli; 1114 end 1115 1116 1117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1118 % 1119 % FUNCTION: aoFromFcn 1120 % 1121 % DESCRIPTION: Construct an ao from a function string 1122 % 1123 % CALL: a = aoFromFcn(a, pl, VERSION, ALGONAME) 1124 % 1125 % PARAMETER: a: Initialised analysis object 1126 % pl: Parameter list object 1127 % VERSION: cvs version string 1128 % ALGONAME: The m-file name (use the mfilename command) 1129 % 1130 % HISTORY: 07-05-2007 Hewitson 1131 % Creation 1132 % 1133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1134 function a = aoFromFcn(a, pli, VERSION, ALGONAME) 1135 1136 % Add default values 1137 pl = combine(pli, getDefaultPlist('From Function')); 1138 1139 fcn = find(pl, 'fcn'); 1140 1141 % find all other parameters and try to evaluate them 1142 for jj=1:nparams(pl) 1143 p = pl.params(jj); 1144 if ~strcmpi(fcn, p.val) 1145 % try upper case replacement 1146 fcn = strrep(fcn, p.key, mat2str(p.val)); 1147 % try lower case replacement 1148 fcn = strrep(fcn, lower(p.key), mat2str(p.val)); 1149 end 1150 end 1151 1152 % Make a cdata object 1153 ts = cdata(plist(param('fcn', fcn))); 1154 ts = set(ts, 'name', fcn); 1155 1156 % Make an analysis object 1157 a.data = ts; 1158 a.hist = history(ALGONAME, VERSION, pli); 1159 a.plist = pli; 1160 end 1161 1162 1163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1164 % 1165 % FUNCTION: aoFromFilenameAndPlist 1166 % 1167 % DESCRIPTION: Construct an ao from filename AND parameter list 1168 % 1169 % CALL: a = aoFromFilenameAndPlist(init_ao, file_name, pl, VERSION, ALGONAME) 1170 % 1171 % PARAMETER: init_ao: Initialised analysis object 1172 % file_name: File name 1173 % pl: Parameter list object 1174 % VERSION: cvs version string 1175 % ALGONAME: The m-file name (use the mfilename command) 1176 % 1177 % HISTORY: 07-05-2007 Hewitson 1178 % Creation 1179 % 1180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1181 function a = aoFromFilenameAndPlist(init_ao, file_name, pli, VERSION, ALGONAME) 1182 1183 a = []; 1184 1185 [pathstr, f_name, ext] = fileparts(file_name); 1186 1187 %%%%%%%%%% Get default parameter list %%%%%%%%%% 1188 dpl = getDefaultPlist('From ASCII File'); 1189 pl = combine(pli, dpl); 1190 1191 pl = pset(pl, param('filename', [f_name ext])); 1192 pl = pset(pl, param('filepath', pathstr)); 1193 1194 data_type = find (pl, 'type'); 1195 columns = find (pl, 'columns'); 1196 num_columns = find (pl, 'num_columns'); 1197 comment_char = find (pl, 'comment_char'); 1198 use_fs = find (pl, 'use_fs'); 1199 xunits = find (pl, 'xunits'); 1200 yunits = find (pl, 'yunits'); 1201 t0 = find (pl, 't0'); 1202 1203 %%%%%%%%%% read file %%%%%%%%%% 1204 [fid,msg] = fopen (file_name, 'r'); 1205 if (fid < 0) 1206 error ('### can not open file: %s \n\n### error msg:',file_name, msg); 1207 end 1208 1209 %%%%%%%%%% create scan format: '%f %f %f %f %f' %%%%%%%%%% 1210 scan_format = []; 1211 for ii=1:num_columns 1212 scan_format = [scan_format '%f ']; 1213 end 1214 scan_format = scan_format(1:end-1); % remove the last whitespace 1215 1216 %%%%%%%%%% scan the file %%%%%%%%%% 1217 raw_data = textscan( fid, scan_format, ... 1218 'commentStyle', comment_char, ... 1219 'CollectOutput', 1); 1220 1221 f_data = raw_data{1}; 1222 1223 if ~isnan(f_data(1,num_columns)) 1224 error('### The file have more or equal than %d columns. Please set the parameter ''num_columns'' to a larger value', num_columns); 1225 end 1226 1227 if isempty(f_data) 1228 error([char(10) '### There are no data. ' ... 1229 'Did you use the right comment character?' char(10) ... 1230 '### The current comment character is: ' comment_char char(10) ... 1231 '### Use a parameter list with the parameter:' char(10) ... 1232 '### plist(''comment_char'', ''%'')']); 1233 end 1234 1235 fclose(fid); 1236 1237 %%%%%%%%%% Create for each column pair the data object %%%%%%%%%% 1238 if isempty(use_fs) 1239 1240 %%%%%%%%%% The numbers in columns must be straight %%%%%%%%%% 1241 if mod(length(columns),2) ~= 0 1242 error('### the numbers in columns must be straight'); 1243 end 1244 1245 for lauf = 1:length(columns)/2 1246 1247 data_x_axes = f_data(:, columns(lauf*2-1)); 1248 data_y_axes = f_data(:, columns(lauf*2)); 1249 1250 %% create data object corresponding to the parameter list 1251 ao_data = []; 1252 switch data_type 1253 case 'tsdata' 1254 ao_data = tsdata( data_x_axes, data_y_axes); 1255 case 'fsdata' 1256 ao_data = fsdata( data_x_axes, data_y_axes); 1257 case 'cdata' 1258 error ('### please code me up') 1259 case 'xydata' 1260 ao_data = xydata( data_x_axes, data_y_axes); 1261 otherwise 1262 error('### unknown data type ''%s''', data_type); 1263 end 1264 ao_data.name = sprintf('f_name_%02d', lauf); 1265 ao_data.xunits = xunits; 1266 ao_data.yunits = yunits; 1267 1268 aa = init_ao; 1269 1270 aa.name = sprintf('%s_%02d_%02d', file_name, columns(lauf*2-1), columns(lauf*2)); 1271 aa.data = ao_data; 1272 aa.hist = history(ALGONAME, VERSION, pl); 1273 1274 %% Set the description fron the parameter list or from the file 1275 description_pl = find(pl, 'description'); 1276 if ~isempty(description_pl) 1277 aa.description = description_pl; 1278 end 1279 1280 a = [a aa]; 1281 1282 end 1283 1284 %%%%%%%%%% Create for each column AND fs a data object %%%%%%%%%% 1285 else % isempty(use_fs) 1286 1287 for lauf = 1:length(columns) 1288 1289 data_y_axes = f_data(:, columns(lauf)); 1290 1291 %% create data object corresponding to the parameter list 1292 ao_data = []; 1293 switch data_type 1294 case 'tsdata' 1295 ao_data = tsdata(data_y_axes, use_fs); 1296 case 'fsdata' 1297 ao_data = fsdata(data_y_axes, use_fs); 1298 case 'cdata' 1299 error ('### please code me up') 1300 case 'xydata' 1301 error ('### please code me up') 1302 otherwise 1303 error('### unknown data type ''%s''', data_type); 1304 end 1305 ao_data.name = sprintf('f_name_%02d', lauf); 1306 ao_data.xunits = xunits; 1307 ao_data.yunits = yunits; 1308 1309 aa = init_ao; 1310 aa.name = sprintf('%s_%02d', file_name, columns(lauf)); 1311 aa.data = ao_data; 1312 aa.hist = history(ALGONAME, VERSION, pli); 1313 aa.plist = pli; 1314 1315 %% Set the description fron the parameter list or from the file 1316 description_pl = find(pl, 'description'); 1317 if ~isempty(description_pl) 1318 aa.description = description_pl; 1319 end 1320 1321 a = [a aa]; 1322 1323 end 1324 1325 end 1326 1327 %%%%%%%%%% set fields of the data object from the parameter list %%%%%%%%%% 1328 for ii = 1:length(a) 1329 fields = fieldnames(a(ii).data); 1330 for jj = 1:length(pl.params) 1331 parameter = lower(pl.params(jj)); 1332 if ismember(parameter.key, upper(fields)) 1333 if iscell(parameter.val) 1334 if length(parameter.val) > 1 1335 if length(parameter.val) ~= length(a) 1336 warning([sprintf('The values of the key ''%s'' ', parameter.key) ... 1337 'have not the same length as the lentgh of ' ... 1338 'the output. Using the first value!']); 1339 a(ii).data.(lower(parameter.key)) = parameter.val{1}; 1340 else 1341 a(ii).data.(lower(parameter.key)) = parameter.val{ii}; 1342 end 1343 else 1344 a(ii).data.(lower(parameter.key)) = parameter.val{1}; 1345 end 1346 else 1347 a(ii).data.(lower(parameter.key)) = parameter.val; 1348 end 1349 1350 end % is parameter key member of the data object 1351 end % for-loop over all parameter 1352 end % for-loop over all ao's 1353 end 1354 1355 1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1357 % 1358 % FUNCTION: aoFromSpecWin 1359 % 1360 % DESCRIPTION: Construct an ao from a Spectral window 1361 % 1362 % CALL: a = aoFromSpecWin(a, win, VERSION, ALGONAME) 1363 % 1364 % PARAMETER: a: Initialised analysis object 1365 % win: Spectral window object 1366 % VERSION: cvs version string 1367 % ALGONAME: The m-file name (use the mfilename command) 1368 % 1369 % HISTORY: 07-05-2007 Hewitson 1370 % Creation 1371 % 1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1373 function a = aoFromSpecWin(a, pli, VERSION, ALGONAME) 1374 1375 if isa(pli, 'plist') 1376 1377 % Add default values 1378 pl = combine(pli, getDefaultPlist('From Window')); 1379 1380 win = find(pl, 'win'); 1381 elseif isa(pli, 'specwin') 1382 win = pli; 1383 pli = plist('win', win); 1384 else 1385 error('### Unknow usage.'); 1386 end 1387 1388 % Make a cdata object 1389 ts = cdata(win.win); 1390 ts = set(ts, 'name', win.name); 1391 1392 % Make an analysis object 1393 a.name = win.name; 1394 a.data = ts; 1395 a.hist = history(ALGONAME, VERSION, pli); 1396 a.plist = pli; 1397 end 1398 1399 1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1401 % 1402 % FUNCTION: getDefaultPlist 1403 % 1404 % DESCRIPTION: Default Parameter Lists 1405 % 1406 % CALL: out = getDefaultPlist(set-string) 1407 % 1408 % PARAMETER: set-string: A string which defines the default parameter list. 1409 % 1410 % HISTORY: 07-05-2007 Hewitson 1411 % Creation 1412 % 1413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1414 function out = getDefaultPlist(varargin) 1415 1416 % list of available parameter sets 1417 sets = {'From XML File', 'From MAT File', 'From ASCII File',... 1418 'From Function', 'From Values',... 1419 'From Time-series Function', 'From Frequency-series Function', ... 1420 'From Window', 'From Waveform', 'From Polynomial', 'From Repository',... 1421 'From Plist'}; 1422 1423 if nargin == 0 1424 out = sets; 1425 return 1426 end 1427 1428 set = varargin{1}; 1429 1430 switch set 1431 %------------------------------------------ 1432 %--- Repository constructor 1433 %------------------------------------------ 1434 case 'From Repository' 1435 out = plist('hostname', 'localhost', 'database', 'ltpda', 'ID', []); 1436 1437 %------------------------------------------ 1438 %--- Read from XML file 1439 %------------------------------------------ 1440 case 'From XML File' 1441 out = plist('filename', ''); 1442 1443 %------------------------------------------ 1444 %--- Read from MAT file 1445 %------------------------------------------ 1446 case 'From MAT File' 1447 out = plist('filename', ''); 1448 1449 %------------------------------------------ 1450 %--- Read from ASCII file 1451 %------------------------------------------ 1452 case 'From ASCII File' 1453 out = plist('filename', '',... 1454 'type', 'tsdata',... 1455 'columns', [1 2],... 1456 'num_columns', 50,... 1457 'xunits', 's', ... 1458 'yunits', 'V', ... 1459 't0', time(0),... 1460 'comment_char', '%', ... 1461 'use_fs', ''); 1462 1463 %------------------------------------------ 1464 %--- Create from a function description 1465 %------------------------------------------ 1466 case 'From Function' 1467 out = plist('fcn', 'randn(100,1)'); 1468 1469 %------------------------------------------ 1470 %--- Create from a set of values 1471 %------------------------------------------ 1472 case 'From Values' 1473 out = plist('vals', [1], 'N', [1]); 1474 1475 %------------------------------------------ 1476 %--- Create from a time-series function 1477 %------------------------------------------ 1478 case 'From Time-series Function' 1479 out = plist('tsfcn', 't', 'fs', 10, 'nsecs', 1, 't0', time(0)); 1480 1481 %------------------------------------------ 1482 %--- Create from frequency-series function 1483 %------------------------------------------ 1484 case 'From Frequency-series Function' 1485 out = plist('fsfcn', 'f', 'f1', 1e-9, 'f2', 5, 'nf', 1000, 'scale', 'log', 'f', []); 1486 1487 %------------------------------------------ 1488 %--- Create from a window function 1489 %------------------------------------------ 1490 case 'From Window' 1491 out = plist('win', specwin('Hanning', 100)); 1492 1493 %------------------------------------------ 1494 %--- Create from a set of polynomial coefficients 1495 %------------------------------------------ 1496 case 'From Polynomial' 1497 out = plist('polyval', [-1e-4 .02 -1 -1], 'Nsecs', 10, 'fs', 10, 't', []); 1498 1499 %------------------------------------------ 1500 %--- Create from a plist 1501 %------------------------------------------ 1502 case 'From Plist' 1503 out = plist('Plist', plist); 1504 1505 %------------------------------------------ 1506 %--- Create from a waveform description 1507 %------------------------------------------ 1508 case 'From Waveform' 1509 out = plist('waveform', 'sine wave',... 1510 'A', 1, ... 1511 'f', 1.23,... 1512 'phi', 0,... 1513 'fs', 10,... 1514 'nsecs', 10,... 1515 't0', time(0)); 1516 otherwise 1517 out = plist(); 1518 end 1519 1520 end 1521 % END 1522 1523 end