-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiltering.m
1283 lines (1187 loc) · 53 KB
/
Filtering.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Filtering:
% This script is intended to use the data generated by readMATandsort.m by
% the data of the TD26CC structure, which is under test now in the dogleg.
%
% In details it works in two steps:
% - read the matfiles with the data of the experiment 'Exp_<experiment Name>.mat'
% 1) Process one by one the events, building data lists
% - 2 lists for the metric values
% - a list with spike flag
% - a list with beam charge
% - a list of the number of pulses past after the previous BD
% - a list of the time past after the previous BD
% 2) Set the thresholds and convert lists above into lists of flags
% - inc_tra_flag and inc_ref_flag are 1 if the event is respecting the metric
% - bpm1_flag and bpm2_flag are 1 if the charge from BPM is
% trepassing the treshold.
% - hasBeam is the logical AND of bpm1_flag and bpm2_flag
% - isSpike inherits from the precedent analysis
%
% --------AND STUFF-------
%
% REV. 1. by Eugenio Senes and Theodoros Argyropoulos
%
% Last modified 29.08.2016 by Eugenio Senes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% Read setup file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all; clearvars; clc;
%include folder to path
[dirpath,~,~]=fileparts(mfilename('fullpath'));
addpath(genpath(dirpath))
%read setup
[~, ~, datapath_read, datapath_write_plot, datapath_write_fig ] = readSetup();
datapath_write = datapath_read;
%%%%%%%%%%%%%%%%%%%%%%%%% End of setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% User input %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
expname = 'Exp_Loaded43MW_11';
savename = expname;
positionAnalysis = true;
manualCorrection = true;
doPlots = true;
mode = 'Loaded';
%%%%%%%%%%%%%%%%%%%%%%% End of user input %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkMode;% valid mode check
%%%%%%%%%%%%%%%%%%%% Parameters %%%%%%%%%%%%%%%%%%%%%%%
% METRIC TRESHOLDS
if strcmpi(mode,'Loaded')
inc_ref_thr = 0.48;
inc_ref_thr = 0.5;
inc_tra_thr = -0.02;
elseif strcmpi(mode,'UnLoaded')
inc_ref_thr = 0.48;
inc_tra_thr = -0.02;
elseif strcmpi(mode,'Antiloaded')
inc_ref_thr = 0.6;
inc_tra_thr = -0.02;
end
% BPM CHARGE THRESHOLDS
bpm1_thr = -100;
bpm2_thr = -90;
% DELTA TIME FOR SECONDARY DUE TO BEAM LOST
deltaTime_spike = 90;
deltaTime_beam_lost = 90;
deltaTime_cluster = 90;
% PULSE DELAY
init_delay = 60e-9;
max_delay = 80e-9;
step_len = 4e-9;
comp_start = 5e-7; %ROI start and end
comp_end = 5.5e-7;
%JITTER
sf = 4e-9;
sROI = 100;
eROI = 200;
%POSITIONING
INC_TRA_delay = 72e-9;
winStart = 1.6e-6;
winStart_corr = 2.0e-6;
wind = (1:100);
%RAMP-UP DETECTION
xstart = 430;
xend = 460;
rampThr = 0.9; % 90%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pulse begin/end for probability
pbeg = 400;
pend = 474;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create log file
%create log file and add initial parameters
initLog([datapath_write filesep savename '.log'], expname, inc_ref_thr, inc_tra_thr, bpm1_thr, bpm2_thr,deltaTime_spike,deltaTime_beam_lost );
%% Load the BD files
tic
disp('Loading the data file ....')
load([datapath_read filesep expname '.mat']);
disp('Done.')
toc
disp(' ')
%% Get field names and list of B0 events in the file
event_name = {};
j = 1;
foo = fieldnames(data_struct);
for i = 1:length(foo)
if strcmp(foo{i}(end-1:end),'B0')
event_name{j} = foo{i};
j = j+1;
end
end
clear j, foo;
%% Parse the interesting event one by one and build the arrays of data for selection
% allocation
%metric
inc_tra = zeros(1,length(event_name));
inc_ref = zeros(1,length(event_name));
%bool for the spike flag
isSpike = false(1,length(event_name));
%beam charge
bpm1_ch = zeros(1,length(event_name));
bpm2_ch = zeros(1,length(event_name));
%timestamps list
ts_array = zeros(1,length(event_name));
%pulse past from previous BD list
prev_pulse = zeros(1,length(event_name));
%beam lost events
beam_lost = false(1,length(event_name));
%beam lost events
prob = zeros(1,length(event_name));
% filling
for i = 1:length(event_name)
inc_tra(i) = data_struct.(event_name{i}).inc_tra;
inc_ref(i) = data_struct.(event_name{i}).inc_ref;
isSpike(i) = data_struct.(event_name{i}).spike.flag;
bpm1_ch(i) = data_struct.(event_name{i}).BPM1.sum_cal;
bpm2_ch(i) = data_struct.(event_name{i}).BPM2.sum_cal;
% build a timestamps array
[~, ts_array(i)] = getFileTimeStamp(data_struct.(event_name{i}).name);
%build the number of pulse pulse between BD array
prev_pulse(i) = data_struct.(event_name{i}).Props.Prev_BD_Pulse_Delay;
%look for beam lost events and flag it
beam_lost(i) = beamWasLost(data_struct.(event_name{i}).name, bpm1_ch(i), bpm2_ch(i), bpm1_thr, bpm2_thr);
%probability of BD is int(P^3 dt)
p3 = data_struct.(event_name{i}).INC.data_cal (pbeg:pend);
p3 = p3.^3;
dt = 4e-9;
prob(i) = sum(p3*dt);
end
%% filling for plotting
%peak and average power
pk_pwr = zeros(1,length(event_name));
avg_pwr = zeros(1,length(event_name));
pk_tra = zeros(1,length(event_name));
pk_ref = zeros(1,length(event_name));
%tuning
tuning_slope = zeros(1,length(event_name));
tuning_delta = zeros(1,length(event_name));
failSlope = 0;
failDelta = 0;
%pulse length
top_len = zeros(1,length(event_name));
mid_len = zeros(1,length(event_name));
bot_len = zeros(1,length(event_name));
fail_m1=0;
for i = 1:length(event_name)
pk_pwr(i) = data_struct.(event_name{i}).INC.max;
avg_pwr(i) = data_struct.(event_name{i}).INC.avg.INC_avg;
pk_tra(i) = max(data_struct.(event_name{i}).TRA.data_cal);
pk_ref(i) = max(data_struct.(event_name{i}).REF.data_cal);
ft_end = 462; %change it if pulse length changes from nominal
if data_struct.(event_name{i}).tuning.fail_m2 ~= true
tuning_slope(i) = data_struct.(event_name{i}).tuning.slope;
tuning_delta(i) = getDeltaPower(tuning_slope(i),...
data_struct.(event_name{i}).tuning.x1,ft_end);
else
tuning_slope(i) = NaN;
tuning_delta(i) = NaN;
failSlope = failSlope+1;
failDelta = failDelta+1;
end
if data_struct.(event_name{i}).tuning.fail_m1 ~= true
top_len(i) = 4e-9*(data_struct.(event_name{i}).tuning.top.x2 - data_struct.(event_name{i}).tuning.top.x1);
mid_len(i) = 4e-9*(data_struct.(event_name{i}).tuning.mid.x2 - data_struct.(event_name{i}).tuning.mid.x1);
bot_len(i) = 4e-9*(data_struct.(event_name{i}).tuning.bot.x2 - data_struct.(event_name{i}).tuning.bot.x1);
else
top_len(i) = NaN;
mid_len(i) = NaN;
bot_len(i) = NaN;
fail_m1 = fail_m1+1;
end
end
%% Parameters check plots
%Get screen parameters in order to resize the plots
% screensizes = get(groot,'screensize'); %only MATLAB r2014b+
% screenWidth = screensizes(3);
% screenHeight = screensizes(4);
% winW = screenWidth/2;
% winH = screenHeight/2;
winW = 1420;
winH = 760;
moff = 0.05;%metric offset
%Metric plotting to check the tresholds
f0 = figure('position',[0 0 winW winH]);
figure(f0)
p1 = plot(inc_tra, inc_ref,'b .','MarkerSize',16);
xlabel('$$ \frac{\int INC - \int TRA}{\int INC + \int TRA} $$','interpreter','latex')
ylabel('$$ \frac{\int INC - \int REF}{\int INC + \int REF} $$','interpreter','latex')
axis([min(inc_tra)-moff max(inc_tra)+moff min(inc_ref)-moff max(inc_ref)+moff])
line(xlim, [inc_ref_thr inc_ref_thr], 'Color', 'r','LineWidth',1) %horizontal line
line([inc_tra_thr inc_tra_thr], ylim, 'Color', 'r','LineWidth',1) %vertical line
title('Interlock criteria review')
legend('Interlocks','Threshold')
%Charge distribution plot
f1 = figure('position',[0 0 winW winH]);
figure(f1)
subplot(1,2,1)
plot(bpm1_ch,'.','MarkerSize',12);
line(xlim, [bpm1_thr bpm1_thr], 'Color', 'r','LineWidth',1) %horizontal line
title('BPM1 charge distribution')
xlabel('Event number')
ylabel('Integrated charge')
legend('Interlocks','threshold')
subplot(1,2,2)
plot(bpm2_ch,'.','MarkerSize',12)
line(xlim, [bpm2_thr bpm2_thr], 'Color', 'r','LineWidth',1) %horizontal line
title('BPM2 charge distribution')
xlabel('Event number')
ylabel('Integrated charge')
legend('Interlocks','threshold')
print(f1,[datapath_write_plot filesep expname '_charge_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_charge_distribution'])
%% Start the filtering
if strcmpi(mode,'Loaded') || strcmpi(mode,'AntiLoaded')
% filling bool arrays
%metric criteria
[inMetric,~,~] = metricCheck(inc_tra, inc_tra_thr, inc_ref, inc_ref_thr);
%beam charge
[hasBeam,~,~] = beamCheck(bpm1_ch, bpm1_thr, bpm2_ch, bpm2_thr,'bpm1');
%find indexes and elements for metric and non-metric events
metr_idx = find(inMetric);
nonmetr_idx = find(~inMetric);
%secondary filter by time after SPIKE
[~, sec_spike_in] = filterSecondary(ts_array(metr_idx),deltaTime_spike,isSpike(metr_idx));
[~, sec_spike_out] = filterSecondary(ts_array(nonmetr_idx),deltaTime_spike,isSpike(nonmetr_idx));
sec_spike = recomp_array(sec_spike_in,metr_idx,sec_spike_out,nonmetr_idx);
%secondary filter by time after BEAM LOST
[~, sec_beam_lost_in] = filterSecondary(ts_array(metr_idx),deltaTime_beam_lost,beam_lost(metr_idx));
sec_beam_lost = recomp_array(sec_beam_lost_in,metr_idx,zeros(1,length(nonmetr_idx)),nonmetr_idx);
%secondary after a normal BD WITHOUT BEAM
BD_idx_met = inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & ~hasBeam;
[~,clusters]=filterSecondary(ts_array,deltaTime_cluster,BD_idx_met);
% filling event arrays
%in the metric
intoMetr = event_name(inMetric);
outOfMetr = event_name(~inMetric);
%candidates = inMetric, withBeam, not beam lost and not after spike and
%not clusters
BD_candidates = event_name(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost));
BD_candidates_beam = event_name(inMetric & hasBeam & ~isSpike & ~(sec_spike) & ~(sec_beam_lost));
BD_candidates_nobeam = event_name(inMetric & ~hasBeam & ~isSpike & ~(sec_spike) & ~(sec_beam_lost));
%clusters
clusters_wb = event_name(inMetric & clusters & hasBeam);
clusters_wob = event_name(inMetric & clusters & ~hasBeam);
%final breakdowns
BDs_flag = inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters;
BDs = event_name(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters);
%interlocks = "candidates" out of metric
interlocks_out = event_name(~inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost));
%spikes
spikes_inMetric = event_name(inMetric & isSpike);
spikes_outMetric = event_name(~inMetric & isSpike);
%missed beams
missed_beam_in = event_name(inMetric &beam_lost);
missed_beam_out = event_name(~inMetric &beam_lost);
%clusters
missed_beam_cluster = event_name(inMetric &sec_beam_lost);
spike_cluster = event_name(inMetric & sec_spike & ~isSpike);
spike_cluster_out = event_name(~inMetric & sec_spike & ~isSpike);
elseif strcmpi(mode,'UnLoaded')
% filling bool arrays
%metric criteria
[inMetric,~,~] = metricCheck(inc_tra, inc_tra_thr, inc_ref, inc_ref_thr);
%find indexes and elements for metric and non-metric events
metr_idx = find(inMetric);
nonmetr_idx = find(~inMetric);
%secondary filter by time after SPIKE
[~, sec_spike_in] = filterSecondary(ts_array(metr_idx),deltaTime_spike,isSpike(metr_idx));
[~, sec_spike_out] = filterSecondary(ts_array(nonmetr_idx),deltaTime_spike,isSpike(nonmetr_idx));
sec_spike = recomp_array(sec_spike_in,metr_idx,sec_spike_out,nonmetr_idx);
%secondary after a normal BD
BD_idx_met = inMetric & ~isSpike & ~(sec_spike) ;
[~,clusters] = filterSecondary(ts_array,deltaTime_cluster,BD_idx_met);
% filling event arrays
%in the metric
intoMetr = event_name(inMetric);
outOfMetr = event_name(~inMetric);
%candidates = inMetric, withBeam, not beam lost and not after spike and
%not clusters
BD_candidates = event_name(inMetric & ~isSpike & ~(sec_spike) );
%clusters
clusters_wb = event_name(inMetric & clusters );
clusters_wob = event_name(inMetric & clusters );
%final breakdowns
BDs_flag = inMetric & ~isSpike & ~(sec_spike) ;
BDs = event_name(inMetric & ~isSpike & ~(sec_spike) );
%interlocks = "candidates" out of metric
interlocks_out = event_name(~inMetric & ~isSpike & ~(sec_spike) );
%spikes
spikes_inMetric = event_name(inMetric & isSpike);
spikes_outMetric = event_name(~inMetric & isSpike);
%clusters
spike_cluster = event_name(inMetric & sec_spike & ~isSpike);
spike_cluster_out = event_name(~inMetric & sec_spike & ~isSpike);
end
%% Cluster length detection
% Clusters from spikes, just for plotting
clust_spike_length = clusterDistribution( isSpike(metr_idx), sec_spike_in );
%% Distributions plots
if doPlots
% peak power distribution
if strcmpi(mode,'Loaded') || strcmpi(mode,'AntiLoaded')
f3 = figure('position',[0 0 winW winH]);
figure(f3)
xbins = linspace(0,round(max(pk_pwr),-6),(1e-6*round(max(pk_pwr),-6)+1));
h1 = hist(pk_pwr(inMetric & isSpike),xbins);
h2 = hist(pk_pwr(inMetric & sec_spike & ~isSpike),xbins);
h3 = hist(pk_pwr(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters),xbins);
bar([h3;h1;h2]','stack')
legend({'BDs','Spikes', 'Secondaries after spikes'},'Position',[.15 .8 .085 .085])
xlabel('Power (MW)')
ylabel('Counts')
title('Overall distribution of peak incident power')
print(f3,[datapath_write_plot filesep expname '_peak_power_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_peak_power_distribution'])
elseif strcmpi(mode,'UnLoaded')
f3 = figure('position',[0 0 winW winH]);
figure(f3)
xbins = linspace(0,round(max(pk_pwr),-6),(1e-6*round(max(pk_pwr),-6)+1));
h1 = hist(pk_pwr(inMetric & isSpike),xbins);
h2 = hist(pk_pwr(inMetric & sec_spike & ~isSpike),xbins);
h3 = hist(pk_pwr(inMetric & ~isSpike & ~(sec_spike) & ~clusters),xbins);
bar([h3;h1;h2]','stack')
legend({'BDs','Spikes', 'Secondaries after spikes'},'Position',[.15 .8 .085 .085])
xlabel('Power (MW)')
ylabel('Counts')
title('Overall distribution of peak incident power')
print(f3,[datapath_write_plot filesep expname '_peak_power_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_peak_power_distribution'])
end
% Peak REF distribution
if strcmpi(mode,'Loaded') || strcmpi(mode,'AntiLoaded')
f60 = figure('position',[0 0 winW winH]);
figure(f60)
xbins = linspace(0,round(max(pk_ref(inMetric & ~isSpike)),-6),(1e-6*round(max(pk_ref(inMetric & ~isSpike)),-6)+1));
h1 = hist(pk_ref(inMetric & hasBeam & ~isSpike),xbins);
h2 = hist(pk_ref(inMetric & ~hasBeam & ~isSpike),xbins);
bar([h1;h2]','stack')
legend({'With Beam','Without Beam'},'Position',[.15 .8 .085 .085])
xlabel('Power (MW)')
ylabel('Counts')
title('Distribution of peak reflected power of the BDs ')
% path60 = [datapath_write_plot filesep fileName '_peak_TRA_power_distribution'];
% print(f60,path60,'-djpeg')
% savefig([datapath_write_fig filesep fileName '_peak_TRA_power_distribution'])
elseif strcmpi(mode,'UnLoaded')
f60 = figure('position',[0 0 winW winH]);
figure(f60)
xbins = linspace(0,round(max(pk_ref(inMetric & ~isSpike)),-6),(1e-6*round(max(pk_ref(inMetric & ~isSpike)),-6)+1));
h1 = hist(pk_ref(inMetric & ~isSpike),xbins);
bar(h1','stack')
xlabel('Power (MW)')
ylabel('Counts')
title('Distribution of peak reflected power of the BDs ')
path60 = [datapath_write_plot filesep expname '_peak_TRA_power_distribution'];
print(f60,path60,'-djpeg')
savefig([datapath_write_plot filesep expname '_peak_TRA_power_distribution'])
end
% average power distribution
if strcmpi(mode,'Loaded') || strcmpi(mode,'AntiLoaded')
f4 = figure('position',[0 0 winW winH]);
figure(f4)
xbins = linspace(0,round(max(pk_pwr),-6),(1e-6*round(max(pk_pwr),-6)+1)); %1MW per bin
h1 = hist(avg_pwr(inMetric & isSpike),xbins);
h2 = hist(avg_pwr(inMetric & sec_spike & ~isSpike),xbins);
h3 = hist(avg_pwr(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters),xbins);
bar([h3;h1;h2]','stack')
legend({'BDs','Spikes', 'Secondaries after spikes'},'Position',[.15 .8 .085 .085])
xlabel('Power (MW)')
ylabel('Counts')
title('Overall distribution of average incident power')
print(f4,[datapath_write_plot filesep expname '_average_power_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_average_power_distribution'])
elseif strcmpi(mode,'UnLoaded')
f4 = figure('position',[0 0 winW winH]);
figure(f4)
xbins = linspace(0,round(max(pk_pwr),-6),(1e-6*round(max(pk_pwr),-6)+1)); %1MW per bin
h1 = hist(avg_pwr(inMetric & isSpike),xbins);
h2 = hist(avg_pwr(inMetric & sec_spike & ~isSpike),xbins);
h3 = hist(avg_pwr(inMetric & ~isSpike & ~(sec_spike) & ~clusters),xbins);
bar([h3;h1;h2]','stack')
legend({'BDs','Spikes', 'Secondaries after spikes'},'Position',[.15 .8 .085 .085])
xlabel('Power (MW)')
ylabel('Counts')
title('Overall distribution of average incident power')
print(f4,[datapath_write_plot filesep expname '_average_power_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_average_power_distribution'])
end
% Probability plot
% f5 = figure('position',[0 0 winW winH]);
% figure(f5)
% %xbins = 0:4:(round(max(bot_len)*1e9)+2);
% histogram(prob(inMetric));
% xlabel('$$ \int P^3 d \tau $$','interpreter','latex')
% ylabel('Counts')
% title('BD probability')
% print(f5,[datapath_write_plot filesep expname '_BD_probability_metric'],'-djpeg')
% savefig([datapath_write_fig filesep expname '_BD_probability_metric'])
%
%
% % Spikes clusters length
% f6 = figure('position',[0 0 winW winH]);
% figure(f6)
% xb=1:max(clust_spike_length)+1;
% hist(clust_spike_length,xb)
% title({'Spike induced BDs distribution';['Interval duration = ' num2str(deltaTime_spike) ' s']})
% xlabel('# of BDs in the cluster')
% ylabel('Counts')
% print(f6,[datapath_write_plot filesep expname '_spike_clusters_length'],'-djpeg')
% savefig([datapath_write_fig filesep expname '_spike_clusters_length'])
% % BD induced clusters with no beam length
% f7 = figure('position',[0 0 winW winH]);
% figure(f7)
% xb=1:max(clust_BD_no_beam_wbeam_after)+1;
% h1 = hist(clust_BD_no_beam_wbeam_after,xb);
% h2 = hist(clust_BD_no_beam_wobeam_after,xb);
% bar([h1;h2]','stack')
% legend('Cluster with beam','Cluster w/o beam')
% title({'Normal BD induced BDs distribution';['Interval duration = ' num2str(deltaTime_cluster) ' s']})
% xlabel('# of BDs in the cluster')
% ylabel('Frequency')
% print(f7,[datapath_write_plot filesep expname '_BD_induced_clusters_length'],'-djpeg')
% savefig([datapath_write_fig filesep expname '_BD_induced_clusters_length'])
% % tuning delta power distribution
% f8 = figure('position',[0 0 winW winH]);
% figure(f8)
% subplot(2,1,1)
% tmp_tuning = tuning_delta(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters);
% xbins = linspace(-20e6,20e6,41); %1M per bin
% histogram(tmp_tuning,xbins)
% title({'BD pulses tuning distribution';['fitting errors = ' num2str(failSlope) ' on ' num2str(length(event_name))]})
% xlabel('Power delta (W)')
% ylabel('Counts')
% subplot(2,1,2)
% tmp_tuning = tuning_delta(inMetric & ~isSpike );
% xbins = linspace(-20e6,20e6,41); %1M per bin
% histogram(tmp_tuning,xbins)
% title({'Pulses in metric tuning distribution (Spikes sorted out)';...
% ['fitting errors = ' num2str(failSlope) ' on ' num2str(length(event_name))]})
% xlabel('Power delta (W)')
% ylabel('Counts')
% print(f8,[datapath_write_plot filesep expname '_tuning_delta_power_distribution'],'-djpeg')
% savefig([datapath_write_fig filesep expname '_tuning_delta_power_distribution'])
% % peak normalized power distribution vs BDR
% f9 = figure('position',[0 0 winW winH]);
% figure(f9)
% xbins = linspace(0,round(max(pk_pwr),-6),(1e-6*round(max(pk_pwr),-6)+1));
% h3 = hist(pk_pwr(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters),xbins);
% h3 = h3/sum(h3);
% bar(h3,'stack')
% hold on
% BDR = h3(43) * ( ((xbins+1e6)/(43e6)).^15 );
% plot(BDR,'r')
% axis([0 50 0 max(h3)+.01])
% axis autox
% hold off
% legend({'BDs','BDR distribution'},'Position',[.15 .8 .085 .085])
% xlabel('Power (MW)')
% ylabel('Normalized frequency')
% title('Overall distribution of peak incident power')
% print(f9,[datapath_write_plot filesep expname '_peak_power_distribution_vs_BDR'],'-djpeg')
% savefig([datapath_write_fig filesep expname '_peak_power_distribution_vs_BDR'])
% pulse length
if strcmpi(mode,'Loaded') || strcmpi(mode,'AntiLoaded')
f10 = figure('position',[0 0 winW winH]);
figure(f10)
top_tmp = top_len(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters);
mid_tmp = mid_len(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters);
bot_tmp = bot_len(inMetric & ~isSpike & ~(sec_spike) & ~beam_lost & ~(sec_beam_lost) & hasBeam & ~clusters);
xbins = 0:4:(round(max(bot_len)*1e9)+2);
histogram(top_tmp*1e9,xbins);
title('Pulse width at various heights')
hold on
histogram(mid_tmp*1e9,xbins);
hold on
histogram(bot_tmp*1e9,xbins);
l = legend({'85%','65%','40%'},'Position',[.15 .8 .085 .085]);
xlabel('Pulse width (ns)')
ylabel('Counts')
hold off
print(f10,[datapath_write_plot filesep expname '_pulse_width_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_pulse_width_distribution'])
elseif strcmpi(mode,'UnLoaded')
f10 = figure('position',[0 0 winW winH]);
figure(f10)
top_tmp = top_len(inMetric & ~isSpike & ~(sec_spike) & ~clusters);
mid_tmp = mid_len(inMetric & ~isSpike & ~(sec_spike) & ~clusters);
bot_tmp = bot_len(inMetric & ~isSpike & ~(sec_spike) & ~clusters);
xbins = 0:4:(round(max(bot_len)*1e9)+2);
histogram(top_tmp*1e9,xbins);
title('Pulse width at various heights')
hold on
histogram(mid_tmp*1e9,xbins);
hold on
histogram(bot_tmp*1e9,xbins);
l = legend({'85%','65%','40%'},'Position',[.15 .8 .085 .085]);
xlabel('Pulse width (ns)')
ylabel('Counts')
hold off
print(f10,[datapath_write_plot filesep expname '_pulse_width_distribution'],'-djpeg')
savefig([datapath_write_fig filesep expname '_pulse_width_distribution'])
end
end %of plots
%% Interactive plot (read version)
%Build the dataset to plot (IN METRIC):
%candidates
BDC_in_x = zeros(1,length(BD_candidates));
BDC_in_y = zeros(1,length(BD_candidates));
for k = 1:length(BD_candidates)
BDC_in_x(k) = data_struct.(BD_candidates{k}).inc_tra;
BDC_in_y(k) = data_struct.(BD_candidates{k}).inc_ref;
end
%spikes
sp_in_x = zeros(1,length(spikes_inMetric));
sp_in_y = zeros(1,length(spikes_inMetric));
for k = 1:length(spikes_inMetric)
sp_in_x(k) = data_struct.(spikes_inMetric{k}).inc_tra;
sp_in_y(k) = data_struct.(spikes_inMetric{k}).inc_ref;
end
%spike cluster
sp_c_in_x = zeros(1,length(spike_cluster));
sp_c_in_y = zeros(1,length(spike_cluster));
for k = 1:length(spike_cluster)
sp_c_in_x(k) = data_struct.(spike_cluster{k}).inc_tra;
sp_c_in_y(k) = data_struct.(spike_cluster{k}).inc_ref;
end
%missed beam
if strcmpi(mode, 'Loaded')
miss_in_x = zeros(1,length(missed_beam_in));
miss_in_y = zeros(1,length(missed_beam_in));
for k = 1:length(missed_beam_in)
miss_in_x(k) = data_struct.(missed_beam_in{k}).inc_tra;
miss_in_y(k) = data_struct.(missed_beam_in{k}).inc_ref;
end
end
%missed beam cluster
if strcmpi(mode, 'Loaded')
miss_c_in_x = zeros(1,length(missed_beam_cluster));
miss_c_in_y = zeros(1,length(missed_beam_cluster));
for k = 1:length(missed_beam_cluster)
miss_c_in_x(k) = data_struct.(missed_beam_cluster{k}).inc_tra;
miss_c_in_y(k) = data_struct.(missed_beam_cluster{k}).inc_ref;
end
end
%OUT OF METRIC:
%interlocks
BDC_out_x = zeros(1,length(interlocks_out));
BDC_out_y = zeros(1,length(interlocks_out));
for k = 1:length(interlocks_out)
BDC_out_x(k) = data_struct.(interlocks_out{k}).inc_tra;
BDC_out_y(k) = data_struct.(interlocks_out{k}).inc_ref;
end
%spikes
sp_out_x = zeros(1,length(spikes_outMetric));
sp_out_y = zeros(1,length(spikes_outMetric));
for k = 1:length(spikes_outMetric)
sp_out_x(k) = data_struct.(spikes_outMetric{k}).inc_tra;
sp_out_y(k) = data_struct.(spikes_outMetric{k}).inc_ref;
end
%spike cluster
sp_c_out_x = zeros(1,length(spike_cluster_out));
sp_c_out_y = zeros(1,length(spike_cluster_out));
for k = 1:length(spike_cluster_out)
sp_c_out_x(k) = data_struct.(spike_cluster_out{k}).inc_tra;
sp_c_out_y(k) = data_struct.(spike_cluster_out{k}).inc_ref;
end
% merge same type, in metric before
BDC_x = [BDC_in_x BDC_out_x];
BDC_y = [BDC_in_y BDC_out_y];
sp_x = [sp_in_x sp_out_x];
sp_y = [sp_in_y sp_out_y];
sp_c_x = [sp_c_in_x sp_c_out_x];
sp_c_y = [sp_c_in_y sp_c_out_y];
%finally plot
prompt = 'Select an event with the cursor and press ENTER (any other to exit)';
f1 = figure('Position',[50 50 1450 700]);
figure(f1);
datacursormode on;
subplot(5,5,[1 2 3 6 7 8 11 12 13])
if strcmpi(mode, 'Loaded')
plot(BDC_x, BDC_y,'r .',sp_x,sp_y,'g .', sp_c_x,sp_c_y,'b .',...
miss_in_x,miss_in_y,'c.',miss_c_in_x,miss_c_in_y,'m .','MarkerSize',15);
elseif strcmpi(mode, 'UnLoaded')
plot(BDC_x, BDC_y,'r .',sp_x,sp_y,'g .', sp_c_x,sp_c_y,'b .',...
'MarkerSize',15);
end
legend('BDs','Spikes','After spike','Missed beam','After missed beam')
xlabel('$$ \frac{\int INC - \int TRA}{\int INC + \int TRA} $$','interpreter','latex')
ylabel('$$ \frac{\int INC - \int REF}{\int INC + \int REF} $$','interpreter','latex')
axis([min(inc_tra)-moff max(inc_tra)+moff min(inc_ref)-moff max(inc_ref)+moff]);
line(xlim, [inc_ref_thr inc_ref_thr], 'Color', 'r','LineWidth',1) %horizontal line
line([inc_tra_thr inc_tra_thr], ylim, 'Color', 'r','LineWidth',1) %vertical line
title('Interlock distribution');
%color plot for savingy
f2 = figure('Position',[50 50 1450 700]);
figure(f2);
if strcmpi(mode, 'Loaded')
plot(BDC_x, BDC_y,'r .',sp_x,sp_y,'g .', sp_c_x,sp_c_y,'b .',...
miss_in_x,miss_in_y,'c.',miss_c_in_x,miss_c_in_y,'m .','MarkerSize',15);
elseif strcmpi(mode, 'UnLoaded')
plot(BDC_x, BDC_y,'r .',sp_x,sp_y,'g .', sp_c_x,sp_c_y,'b .',...
'MarkerSize',15);
end
legend('BDs','Spikes','After spike','Missed beam','After missed beam')
xlabel('$$ \frac{\int INC - \int TRA}{\int INC + \int TRA} $$','interpreter','latex')
ylabel('$$ \frac{\int INC - \int REF}{\int INC + \int REF} $$','interpreter','latex')
axis([min(inc_tra)-moff max(inc_tra)+moff min(inc_ref)-moff max(inc_ref)+moff]);
line(xlim, [inc_ref_thr inc_ref_thr], 'Color', 'r','LineWidth',1) %horizontal line
line([inc_tra_thr inc_tra_thr], ylim, 'Color', 'r','LineWidth',1) %vertical line
title('Interlock distribution');
print(f2,[datapath_write_plot filesep expname '_metric_check_color'],'-djpeg')
savefig([datapath_write_fig filesep expname '_metric_check_color'])
close(f2);
%x axis thicks for signals plotting
timescale = 1:800;
timescale = timescale*data_struct.(event_name{1}).INC.Props.wf_increment;
%init the small graphs
subplot(5,5,[4 5 9 10 14 15]) %RF signals plot
title('RF signals');
sp6 = subplot(5,5,[19 20 24 25]); %pulse tuning plot
title('Pulse tuning')
sp7 = subplot(5,5,[16 17 18 21 22 23]); %BPMs plot
ylim(sp7, [-1.8 0.05]);
title('BPM signals');
% user interaction
exitCond = false;
while isempty ( input(prompt,'s') )%keep on spinning while pressing enter
%get cursor position
dcm_obj = datacursormode(f1);
info_struct = getCursorInfo(dcm_obj);
switch info_struct.Target.DisplayName
% !!!! Must match the legend
case 'BDs'
if info_struct.DataIndex <= length(BDC_in_x)
fname = BD_candidates{info_struct.DataIndex};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
else
fname = interlocks_out{info_struct.DataIndex-length(BDC_in_x)};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
end
case 'Spikes'
if info_struct.DataIndex <= length(sp_in_x)
fname = spikes_inMetric{info_struct.DataIndex};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
else
fname = spikes_outMetric{info_struct.DataIndex-length(sp_in_x)};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
end
case 'After spike'
if info_struct.DataIndex <= length(sp_c_in_x)
fname = spike_cluster{info_struct.DataIndex};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
else
fname = spike_cluster_out{info_struct.DataIndex-length(sp_c_in_x)};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
end
case 'Missed beam'
fname = missed_beam_in{info_struct.DataIndex};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
case 'After missed beam'
fname = missed_beam_cluster{info_struct.DataIndex};
disp(fname)
print_subPlots(fname, timescale, data_struct,bpm1_thr,bpm2_thr)
otherwise
warning('Type not recognized')
end
end
%% Online modification of the BDs list
fprintf(1,'\n')
%ask if manual remove something
modMan = false;
correct = false;
while true
str_input = input('Need to modify manually the BDs list ? (Y/N)','s');
switch lower(str_input)
case 'y'
modMan = true;
correct = true;
break
case 'n'
modMan = false;
correct = false;
break
otherwise
disp('Enter a valid character')
continue
end
end
%build the ts list to remove
if modMan
gone = false;
rem_ts = {};
while ~gone
ts_input = input('Enter the timestamp to remove, q to exit: ','s');
switch lower(ts_input)
case 'q'
gone = true;
break
otherwise
rem_ts = [rem_ts ts_input];
continue
end
end
end
if correct
%print rem_ts
fprintf(1,'\nThe following timestamps are going to be removed: \n')
for k = 1:length(rem_ts)
fprintf(1,[rem_ts{k} '\n'])
end
%remove the timestamps
rm_idx = [];
for k = 1:length(rem_ts)
[succ, idx] = ismember(rem_ts{k},BDs);
if succ
BDs(idx) = [];
end
end
end
%% Positioning
if positionAnalysis
%figure setup
f666 = figure('position',[0 0 1920 1080]);
figure(f666);
set(gcf,'numbertitle','off','name','BD positioning check')
subplot(4,6,[13 14 19 20])
plot(inc_tra(BDs_flag), inc_ref(BDs_flag),'b .','MarkerSize',20);
xlabel('$$ \frac{\int INC - \int TRA}{\int INC + \int TRA} $$','interpreter','latex')
ylabel('$$ \frac{\int INC - \int REF}{\int INC + \int REF} $$','interpreter','latex')
axis([min(inc_tra(BDs_flag))-moff max(inc_tra(BDs_flag))+moff min(inc_ref(BDs_flag))-moff max(inc_ref(BDs_flag))+moff])
line(xlim, [inc_ref_thr inc_ref_thr], 'Color', 'r','LineWidth',1) %horizontal line
line([inc_tra_thr inc_tra_thr], ylim, 'Color', 'r','LineWidth',1) %vertical line
title('Metric')
legend('BDs','Threshold')
hold on
%data part
timescale = 0:4e-9:799*4e-9;
for n=1:length(BDs)
%display timestamp
disp([ BDs{n} ' is the number ' num2str(n) ' on ' num2str(length(BDs)) ])
%get the current data
INC_c = data_struct.(BDs{n}).INC.data;
TRA_c = data_struct.(BDs{n}).TRA.data;
REF_c = data_struct.(BDs{n}).REF.data;
%check if the backup pulses are recorded
precName = [BDs{n}(1:end-2) 'L1'];
if isfield(data_struct,precName)% The backup pulse is present
%grasp data
INC_prev = data_struct.(precName).INC.data;
TRA_prev = data_struct.(precName).TRA.data;
REF_prev = data_struct.(precName).REF.data;
INC_prev_cal = data_struct.(precName).INC.data_cal;
TRA_prev_cal = data_struct.(precName).TRA.data_cal;
REF_prev_cal = data_struct.(precName).REF.data_cal;
INC_c_cal = data_struct.(BDs{n}).INC.data_cal;
TRA_c_cal = data_struct.(BDs{n}).TRA.data_cal;
REF_c_cal = data_struct.(BDs{n}).REF.data_cal;
BPM1_c = data_struct.(BDs{n}).BPM1.data_cal;
BPM2_c = data_struct.(BDs{n}).BPM2.data_cal;
inc_tra_c = data_struct.(BDs{n}).inc_tra;
inc_ref_c = data_struct.(BDs{n}).inc_ref;
%plotting
positionPlot_prev( timescale, INC_c, INC_prev, TRA_c, TRA_prev, REF_c, REF_prev,...
INC_c_cal, INC_prev_cal, TRA_c_cal, TRA_prev_cal, REF_c_cal, REF_prev_cal,...
BPM1_c, BPM2_c, INC_TRA_delay)
% plot metric red dot
subplot(4,6,[13 14 19 20])
if n~=1
delete(pm)
end
pm = plot(inc_tra_c, inc_ref_c,'m .','MarkerSize',20);
%%%%%%%%%%%% Calculate the delay
%ramp-up test
[isRamping, ~] = rampUpTest( INC_c_cal, INC_prev_cal, xstart, xend, rampThr );
%calculate integrals
%TRA EDGE
if ~isRamping
[ind_TRA, time_TRA] = getDeviationPoint(timescale,TRA_c,TRA_prev,winStart,0.07,0.005);
else
[~, ind_TRA] = max(TRA_c);
time_TRA = timescale(ind_TRA);
end
subplot(4,6,[3 4])
plot(timescale, TRA_c, 'r -',timescale, TRA_prev, 'r --', timescale, TRA_prev-TRA_c, 'b .')
line([time_TRA time_TRA], ylim, 'Color', 'r','LineWidth',1) %vertical line
legend({'TRA','prev TRA','difference'})
title('Edge method for TRA')
xlim([400*4e-9 550*4e-9])
xlabel('time (s)')
ylabel('Power (a.u.)')
%REF EDGE
[ind_REF, time_REF] = getDeviationPoint(timescale,REF_c,REF_prev,winStart,0.1,0.02);
%%%%% calculate time_delay for the edge method
INC_TRA_timeOffset = 72e-9;
td = 1e9*(time_REF-time_TRA+INC_TRA_timeOffset);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,6,[9 10])
plot(timescale, REF_c, 'k -',timescale, REF_prev, 'k --',timescale,REF_c-REF_prev,'b .' )
line([time_REF time_REF], ylim, 'Color', 'k','LineWidth',1) %vertical line
legend({'REF','prev REF','difference'})
title(['Edge method for REF - td = ',num2str(td),' ns'])
xlim([400*4e-9 550*4e-9])
xlabel('time (s)')
ylabel('Power (a.u.)')
%add vertical bars to plots
subplot(4,6,[1 2 7 8])
hold on;
line([time_TRA-INC_TRA_timeOffset time_TRA-INC_TRA_timeOffset], ylim, 'Color', 'r','LineWidth',1) %vertical line
line([time_REF time_REF], ylim, 'Color', 'k','LineWidth',1) %vertical line
subplot(4,6,[5 6 11 12])
hold on;
line([time_TRA-INC_TRA_timeOffset time_TRA-INC_TRA_timeOffset], ylim, 'Color', 'r','LineWidth',1) %vertical line
line([time_REF time_REF], ylim, 'Color', 'k','LineWidth',1) %vertical line
%CORRELATION
[coeff_corr,gof,corr_err,y1_offset,y2_offset] = correlationMethod(timescale,INC_c',timescale,REF_c',wind,winStart_corr);
delay = round(coeff_corr(1))*1e-9; %rounded to integer [ns]
failFlag = false;
subplot(4,6,[15 16 21 22])
plot( timescale, REF_c-y2_offset, 'r', timescale, INC_c-y1_offset, 'k',...
timescale-(coeff_corr(1)*1e-9), coeff_corr(2)*(REF_c-y2_offset), 'g','LineWidth', 2 ...
)
xlim([1.848e-6 3.2e-6])
title(['Correlation method - tc ',num2str(delay),' ns'])
legend({'REF','INC', 'REF delayed'})
%JITTER check
[ ~, delay_time ] = jitterCheck( data_struct.(BDs{n}).INC.data_cal, data_struct.(precName).INC.data_cal, sf, sROI, eROI);
disp(['jitter = ' num2str(delay_time) ' ns'])
if delay_time ~= 0
warning('Jitter detected !')
end
% manual correction
if manualCorrection
str = input('Will you do any correction ? ','s');
if strcmp(str,'y')
str = input('Correct Edge Method ? ','s');
if strcmp(str,'y')
str = input('time_ind_TRA = ','s');
dcm_obj = datacursormode;
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
time_TRA = info_struct.Position(1);
ind_TRA = find(timescale<=time_TRA,1,'last');%get index from time
gone = true;
else
disp('Please type: ')
str = input('time_ind_TRA = ','s');
time_TRA = str2double(str);
ind_TRA = find(timescale<=time_TRA,1,'last');%get index from time
end
str = input('time_ind_REF = ','s');
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
time_REF = info_struct.Position(1);
ind_REF = time_REF/sf +1; %get index from time
else
disp('Please type: ')
str = input('time_ind_REF = ','s');
time_REF = str2double(str);
ind_REF = time_REF/sf +1; %get index from time
end
end
str = input('Correct Correlation Method ? ','s');
if strcmp(str,'y')
str = input('time_peak_INC = ','s');
dcm_obj = datacursormode;
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
time_inc = info_struct.Position(1)
else
disp('Please type: ')
str = input('time_peak_INC = ','s');
time_inc = str2double(str);
end
str = input('time_peak_REF = ','s');
dcm_obj = datacursormode;
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
time_ref = info_struct.Position(1)
else
disp('Please type: ')
str = input('time_peak_INC = ','s');
time_inc = str2double(str);
end
delay = time_ref - time_inc;
failFlag = false;
elseif strcmp(str,'f')
failFlag = true;
end
end
end
%CREATE OUTPUT STRUCT
%edge
data_struct.(BDs{n}).position.edge.ind_REF = ind_REF;
data_struct.(BDs{n}).position.edge.time_REF = time_REF;
data_struct.(BDs{n}).position.edge.ind_TRA = ind_TRA;
data_struct.(BDs{n}).position.edge.time_TRA = time_TRA;
%correlation
data_struct.(BDs{n}).position.correlation.backupPulse = true;
data_struct.(BDs{n}).position.correlation.delay_time = delay;
data_struct.(BDs{n}).position.correlation.gain = coeff_corr(2);
data_struct.(BDs{n}).position.correlation.fail = failFlag;
else % NO PREV PULSE
%grasp data
INC_c_cal = data_struct.(BDs{n}).INC.data_cal;
TRA_c_cal = data_struct.(BDs{n}).TRA.data_cal;
REF_c_cal = data_struct.(BDs{n}).REF.data_cal;
BPM1_c = data_struct.(BDs{n}).BPM1.data_cal;