0001 function [Sim, state] = graven(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096 inLength = length(varargin);
0097
0098
0099 input = varargin;
0100 errorcheck(input);
0101
0102 preGen = ~true;
0103
0104 if inLength == 6
0105 [logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0106 deal(varargin{:});
0107
0108 rand('state', seed);
0109
0110 list = makelist(inputFileName, detId);
0111
0112 elseif inLength == 7
0113 [nSim, logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0114 deal(varargin{:});
0115
0116 rand('state', seed);
0117
0118 list = makelist(nSim, inputFileName, detId);
0119
0120 elseif inLength == 11
0121 [nSim, logFileName, gps, detId, simId, ampl, startSamp, internal, ...
0122 external, sampFreq, seed] = deal(varargin{:});
0123
0124 rand('state', seed);
0125
0126 list = makelist(nSim, simId, ampl, startSamp, internal, external, detId);
0127
0128 elseif inLength == 9
0129 [nSim, logFileName, gps, detId, inputFileName, startSamp, external, ...
0130 sampFreq, seed] = deal(varargin{:});
0131
0132 rand('state', seed);
0133 list = pregenlist(nSim, inputFileName, startSamp, external, detId);
0134
0135 else
0136 error('graven: Number of input arguments can only be 6, 7, 9 or 11')
0137 end
0138
0139
0140
0141
0142
0143
0144
0145
0146 listLength = size(list, 1);
0147
0148 if listLength == 0
0149 error('graven: listing error, list has zero length')
0150 end
0151
0152
0153 [Sim(1:listLength)] = deal(struct('name', [], 'nstr', [], 'sig', [], ...
0154 'gps0', [], 'pkgps' ,[]));
0155 Internal = deal(struct('x', [], 'phi', []));
0156 External = deal(struct('x', [], 'phi', [], 'psi', []));
0157
0158
0159 abcache = [];
0160
0161
0162 fid = fopen(logFileName, 'a');
0163 if fid == -1
0164 error('graven: Failed to open log file')
0165 end
0166
0167 date = clock;
0168
0169 fprintf(fid, ['# This simulation was run on %2.0f/%2.0f/%4.0f ' ...
0170 'at %2.0f:%2.0f:%2.2f EDT\n'], date(2), date(3), date(1), ...
0171 date(4), date(5), date(6));
0172 fprintf(fid, ['# (simId) (ampl) (startSamp1) (startSamp2) (Internal.x) '...
0173 '(Internal.phi) (External.x) (External.phi) (External.psi)' ...
0174 ' (gps) (gps0 s) (gps0 ns) (pkampl) (pkgps s) (pkgps ns) '...
0175 '(detId)\n']);
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 RAD2DEG = 180/pi;
0196
0197 for iSim = 1:listLength
0198
0199 simId = list{iSim, 1};
0200 ampl = list{iSim, 2};
0201 startSamp = list{iSim, 3};
0202 Internal.x = list{iSim, 4};
0203 Internal.phi = list{iSim, 5};
0204 External.x = list{iSim, 6};
0205 External.phi = list{iSim, 7};
0206 External.psi = list{iSim, 8};
0207
0208 if sampFreq ~= 16384 & (upper(detId) == 'H1' | upper(detId) == 'H2' ...
0209 | upper(detId) == 'L1')
0210 warning(['graven: sampFreq does not match the sampling frequency ' ...
0211 'of the detId entered'])
0212 end
0213
0214
0215 if ampl == -1
0216
0217 preGen = true;
0218 end
0219
0220
0221
0222
0223 samp = ifodelay(detId, External.x, External.phi, sampFreq);
0224 if isempty(samp)
0225 error('graven: ifodelay returned empty samp')
0226 end
0227 stimeIfo = startSamp+samp;
0228
0229
0230
0231
0232
0233 if preGen
0234 stimeIfo = round(stimeIfo);
0235 htt = simidfile(simId);
0236 end
0237
0238
0239 if ~preGen
0240
0241 hij = makeh(simId, ampl, stimeIfo, sampFreq);
0242 if isempty(hij)
0243 error('graven: makeh returned empty hij')
0244 end
0245
0246 htt = makett(hij, Internal);
0247 if isempty(htt)
0248 error('graven: makett returned empty htt')
0249 end
0250 end
0251
0252
0253 h = detproj(htt, detId, External);
0254 if isempty(h)
0255 error('graven: detproj returned empty h')
0256 end
0257
0258
0259 [maxStrain, maxSamp] = max(h);
0260 maxTimeNdx = ceil(stimeIfo)+maxSamp-1;
0261
0262 maxTimeGps = ndx2gps(maxTimeNdx, sampFreq, gps);
0263
0264
0265
0266 [asqSeries, abcache] = calibsimfd(detId, fix(gps+(stimeIfo/sampFreq)), h, ...
0267 sampFreq, abcache);
0268
0269 if isempty(asqSeries)
0270 error('graven: calibsimfd returned empty asqSeries')
0271 end
0272
0273 Sim(iSim).name = upper(simId);
0274 Sim(iSim).nstr = ceil(stimeIfo);
0275 Sim(iSim).sig = asqSeries;
0276 Sim(iSim).gps0 = ndx2gps(stimeIfo, sampFreq, gps);
0277 Sim(iSim).pkgps = maxTimeGps;
0278
0279
0280 fprintf(fid, ['%s %0.5g %9.15f %9.15f %d %d %d %d %d %d %d ' ...
0281 '%8.0f %d %d %8.0f %s \n'], simId, ampl, startSamp, startSamp, ...
0282 Internal.x, Internal.phi, External.x, External.phi, ...
0283 External.psi, gps, Sim(iSim).gps0.sec, Sim(iSim).gps0.ns, ...
0284 maxStrain, Sim(iSim).pkgps.sec, Sim(iSim).pkgps.ns, detId);
0285 end
0286
0287
0288 fclose(fid);
0289
0290
0291 state = rand('state');
0292
0293 return
0294
0295
0296
0297 function errorcheck(input)
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323 inputLength=length(input);
0324
0325 if inputLength == 6
0326 [logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0327 deal(input{:});
0328
0329
0330
0331
0332
0333 if ~strcmp(class(inputFileName), 'char')
0334 inputFileName
0335 error('graven: inputFileName must be a string')
0336 end
0337
0338 elseif inputLength == 7
0339 [nSim, logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0340 deal(input{:});
0341
0342
0343
0344
0345
0346 if ~strcmp(class(inputFileName), 'char')
0347 inputFileName
0348 error('graven: inputFileName must be a string')
0349 end
0350
0351
0352 if nSim < 1 | mod(nSim, 1) > 0 | isinf(nSim) | isnan(nSim)
0353
0354 nSim
0355 error('graven: nSim must be a real finite integer >= 1')
0356 end
0357
0358 elseif inputLength == 11
0359 [nSim, logFileName, gps, detId, simId, ampl, startSamp, internal, ...
0360 external, sampFreq, seed] = deal(input{:});
0361
0362
0363
0364
0365 if nSim < 1 | mod(nSim, 1) > 0 | isinf(nSim) | isnan(nSim)
0366
0367 nSim
0368 error('graven: nSim must be a real finite integer >= 1')
0369 end
0370
0371
0372 if ~strcmp(class(simId), 'char')
0373 simId
0374 error('graven: simId must be a string')
0375 end
0376
0377
0378 if size(ampl, 1) == 1 & size(ampl, 2) == 1
0379 if ampl < 0 | isinf(ampl) | isnan(ampl) | ~isreal(ampl)
0380
0381 ampl
0382 error('graven: ampl must be a real positive fitite number')
0383 end
0384 elseif size(ampl, 1) == 1 & size(ampl, 2) == 2
0385 if ampl < 0 | isinf(ampl(1)) | isinf(ampl(2)) | isnan(ampl(1)) ...
0386 | isnan(ampl(2)) | ~isreal(ampl(1)) | ~isreal(ampl(2))
0387
0388 ampl
0389 error(['graven: ampl components must be real positive finite '...
0390 'numbers'])
0391 end
0392 elseif size(ampl, 1) == 1 & size(ampl, 2) == 3
0393 if ampl < 0 | isinf(ampl(1)) | isinf(ampl(2)) | isinf(ampl(3)) | ...
0394 isnan(ampl(1)) | isnan(ampl(2)) | isnan(ampl(3)) | ...
0395 ~isreal(ampl(1)) | ~isreal(ampl(2)) | ~isreal(ampl(3))
0396
0397 ampl
0398 error(['graven: ampl components must be real positive finite '...
0399 'numbers'])
0400 end
0401 else
0402 error('graven: ampl must be a scalar or a 1x2 or 1x3 vector')
0403 end
0404
0405
0406 if size(startSamp, 1) ~= 1 | size(startSamp, 2) ~= 2
0407 startSamp
0408 error('graven: startSamp must be a 1x2 vector')
0409 elseif startSamp(1) < 1 | mod(startSamp(1), 1) > 0 | ...
0410 mod(startSamp(2), 1) > 0 | isinf(startSamp(1)) | ...
0411 isinf(startSamp(2)) | isnan(startSamp(1)) | isnan(startSamp(2))
0412
0413
0414 startSamp
0415 error(['graven: startSamp(1) and startSamp(2) must be real '...
0416 'finite integers >= 1'])
0417 end
0418
0419
0420 if strcmp(class(internal), 'double')
0421
0422 if size(internal, 1) ~= 1 | size(internal, 2) ~= 2
0423 internal
0424 error(['graven: internal must be a 1x2 vector when not a ' ...
0425 'struct or string'])
0426 elseif abs(internal(1)) > 1 | ~isreal(internal(1))
0427
0428 internal
0429 error('graven: internal(1) must be real and bewteen -1 and 1')
0430 elseif internal(2) < -pi | internal(2) > pi | ~isreal(internal(2))
0431
0432 internal
0433 error('graven: internal(2) must be real and between -pi and pi')
0434 end
0435 elseif strcmp(class(internal), 'char')
0436
0437 if ~strcmp(upper(internal), 'RANDOM') & ...
0438 ~strcmp(upper(internal), 'OPTIMAL')
0439 internal
0440 error(['graven: internal must be ''RANDOM'' or ''OPTIMAL'' ' ...
0441 'when a string'])
0442 end
0443 elseif strcmp(class(internal), 'struct')
0444
0445 if ~isfield(internal, 'x')
0446 internal
0447 error('graven: internal must contain field x when a struct')
0448 elseif abs(internal.x) > 1 | ~isreal(internal.x)
0449
0450 internal.x
0451 error('graven: internal.x must be real and between -1 and 1')
0452 end
0453 if ~isfield(internal, 'phi')
0454 internal
0455 error('graven: internal must contain field phi when a struct')
0456 elseif internal.phi < -pi | internal.phi > pi | ~isreal(internal.phi)
0457
0458
0459 internal.phi
0460 error(['graven: internal.phi must be real and betweeen -pi '...
0461 'and pi'])
0462 end
0463 end
0464
0465
0466 checkexternal(external);
0467
0468 elseif inputLength == 9
0469 [nSim, logFileName, gps, detId, inputFileName, startSamp, external, ...
0470 sampFreq, seed] = deal(input{:});
0471
0472
0473 if ~strcmp(class(inputFileName), 'char') & ~iscell(inputFileName)
0474 inputFileName
0475 error('graven: inputFileName must be a string')
0476 end
0477
0478
0479 if size(startSamp, 1) ~= 1 | size(startSamp, 2) ~= 2
0480 startSamp
0481 error('graven: startSamp must be a 1x2 vector')
0482 elseif startSamp(1) < 1 | mod(startSamp(1), 1) > 0 | ...
0483 mod(startSamp(2), 1) > 0 | isinf(startSamp(1)) | ...
0484 isinf(startSamp(2)) | isnan(startSamp(1)) | isnan(startSamp(2))
0485
0486
0487 startSamp
0488 error(['graven: startSamp(1) and startSamp(2) must be real '...
0489 'finite integers >= 1'])
0490 end
0491
0492
0493 checkexternal(external);
0494
0495
0496 if nSim < 1 | mod(nSim, 1) > 0 | isinf(nSim) | isnan(nSim)
0497
0498 nSim
0499 error('graven: nSim must be a real finite integer >= 1')
0500 end
0501 end
0502
0503
0504
0505
0506 if ~strcmp(class(logFileName), 'char')
0507 logFileName
0508 error('graven: logFileName must be a string')
0509 end
0510
0511
0512 if gps < 0 | mod(gps, 1) > 0 | isinf(gps) | isnan(gps)
0513
0514 gps
0515 error('graven: gps must be a real positive finite integer')
0516 end
0517
0518
0519 if ~strcmp(class(detId), 'char')
0520 detId
0521 error('graven: detId must be a string')
0522 end
0523
0524
0525 if sampFreq <= 0 | isinf(sampFreq) | isnan(sampFreq) | ~isreal(sampFreq)
0526 sampFreq
0527 error('graven: sampFreq must be a real finite number > 0')
0528 end
0529
0530
0531 if ~strcmp(class(seed), 'double')
0532 seed
0533 error('graven: seed must be a number')
0534 end
0535
0536 return
0537
0538
0539
0540 function checkexternal(external)
0541
0542
0543 if strcmp(class(external), 'double')
0544
0545 if size(external, 1) ~= 1 | size(external, 2) ~= 3
0546 external
0547 error(['graven: external must be a 1x3 vector when not a ' ...
0548 'struct or string'])
0549 elseif abs(external(1)) > 1 | ~isreal(external(1))
0550
0551 external
0552 error('graven: external(1) must be real and bewteen -1 and 1')
0553 elseif external(2) < -pi | external(2) > pi | ~isreal(external(2))
0554
0555 external
0556 error('graven: external(2) must be real and between -pi and pi')
0557 elseif external(3) < 0 | external(3) > 2*pi | ~isreal(external(3))
0558
0559 external
0560 error('graven: external(3) must be real and between 0 and 2*pi')
0561 end
0562 elseif strcmp(class(external), 'char')
0563
0564 if ~strcmp(upper(external), 'RANDOM') & ...
0565 ~strcmp(upper(external), 'OPTIMAL')
0566 external
0567 error(['graven: external must be ''RANDOM'' or ''OPTIMAL'' ' ...
0568 'when a string'])
0569 end
0570 elseif strcmp(class(external), 'struct')
0571
0572 if ~isfield(external, 'x')
0573 external
0574 error('graven: external must contain field x when a struct')
0575 elseif abs(external.x) > 1 | ~isreal(external.x)
0576
0577
0578 external.x
0579 error('graven: external.x must be real and between -1 and 1')
0580 end
0581 if ~isfield(external, 'phi')
0582 external
0583 error('graven: external must contain field phi when a struct')
0584 elseif external.phi < -pi | external.phi > pi | ~isreal(external.phi)
0585
0586
0587 external.phi
0588 error(['graven: external.phi must be real and betweeen -pi '...
0589 'and pi'])
0590 end
0591 if ~isfield(external, 'psi')
0592 external
0593 error('graven: external must contain field psi when a struct')
0594 elseif external.psi < 0 | external.psi > (2*pi) | ...
0595 ~isreal(external.psi)
0596
0597
0598 external.psi
0599 error('graven: external.psi must be real and between 0 and 2*pi')
0600 end
0601 end
0602 return
0603
0604
0605
0606 function htt = simidfile(simId)
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633 if strcmp(class(simId), 'cell')
0634
0635 htt = loadpregendata(simId);
0636 elseif strcmp(class(simId), 'char')
0637
0638 I = find(simId == ',');
0639 if length(I) == 0
0640 file = simId(1:end);
0641 elseif length(I) == 1
0642 file{1,1} = simId(1:I(1)-1);
0643 file{1,2} = simId(I(1)+1:end);
0644 else
0645 simId
0646 error('graven -> simidfile: Invalid simId')
0647 end
0648
0649 htt = loadpregendata(file);
0650 else
0651 simId
0652 error('graven -> simidfile: simId must be cell or string')
0653 end
0654
0655 return
0656
0657
0658
0659 function htt = loadpregendata(inputFileName)
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688 [fileName, ext, dirPath] = fileext(inputFileName);
0689 if isempty(fileName) | isempty(ext)
0690 error('graven: fileext returned empty file or ext')
0691 end
0692
0693 if strcmp(ext, 'ilwd')
0694 if exist([dirPath fileName '.' ext]) ~= 2
0695 file = [dirPath fileName '.' ext];
0696 error('graven: File does not exist on search path')
0697 end
0698
0699 waveform = ilwdread([dirPath fileName '.' ext]);
0700
0701 if length(inputFileName) == 2
0702 ilwdString = inputFileName{2};
0703 if strcmp(lower(ilwdString), 'default');
0704
0705
0706 ilwdString = 'm1.real_8';
0707 end
0708 else
0709
0710
0711 ilwdString = 'm1.real_8';
0712 end
0713
0714
0715
0716
0717 I = find(ilwdString == '.');
0718 if ~isempty(I) & I(end) == length(ilwdString)
0719 ilwdString
0720 error('graven: Invalid ilwdString')
0721 end
0722
0723 nFields = length(I)+1;
0724 for iFields = 1:nFields
0725 if iFields == 1 & nFields ~= 1
0726 temp = getfield(waveform, ilwdString(1:I(iFields)-1));
0727 elseif iFields == 1 & nFields == 1
0728 temp = getfield(waveform, ilwdString);
0729 elseif iFields == nFields & nFields ~= 1
0730 temp = getfield(temp, ilwdString(I(iFields-1)+1:end));
0731 else
0732 temp = getfield(temp, ilwdString(I(iFields-1)+1:I(iFields)-1));
0733 end
0734 end
0735 if ~strcmp(class(temp), 'double')
0736 ilwdString
0737 error('graven: Retrieved data not class double')
0738 end
0739 htt(1,:) = temp(:)';
0740
0741
0742
0743 htt(2,:) = zeros(1,length(htt(1,:)));
0744 elseif strcmp(ext, 'txt')
0745 if exist([dirPath fileName '.' ext]) ~= 2
0746 file = [dirPath fileName '.' ext];
0747 error('graven: File does not exist on search path')
0748 end
0749
0750 waveform = load([dirPath fileName '.' ext]);
0751 htt(1,:) = waveform(:)';
0752 htt(2,:) = zeros(1, length(waveform));
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770 else
0771 ext
0772 error('graven: inputFileName must have extention *.ilwd or *.txt')
0773 end
0774
0775 return