-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunSequenceTimeTagger.m
More file actions
1094 lines (962 loc) · 40.4 KB
/
Copy pathRunSequenceTimeTagger.m
File metadata and controls
1094 lines (962 loc) · 40.4 KB
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
function RunSequenceTimeTagger(hObject, eventdata, handles)
[y,Fs] = audioread('ExptCompleted.mp3');
BackupFile = 'C:\MATLAB_Code\Data\TempDataBackup\Temp.mat';
global gmSEQ gSG tmax hCPS gSG2 gSG3 % frequency information is already input - 1
% Stop the previous sequence if it is running
% if gmSEQ.bExp
% error("Previous experiment is running. Please first stop previous experiment.");
% end
% default setting
gmSEQ.bRaman = 0;
gmSEQ.bGo = 1;
gmSEQ.bExp = 1; % experiment status tag
getUserInputFromGUI(handles);
SequencePool(string(gmSEQ.name));
InitializeData();
disp(strcat('Commencing ',{' '},string(gmSEQ.name), ' sequence...'))
set(handles.runningText,'string','Running')
drawnow;
gmSEQ.bRandom = 0; % Shuffle the input, added by Weijie 04/20/2022
if gSG.bfixedPow && gSG.bfixedFreq %pulsed seq
CreateSavePath_Ave()
gmSEQ.bTomo = gmSEQ.Alternate;
if gmSEQ.bTomo
gmSEQ.dataN = gmSEQ.Ntomo*gmSEQ.ctrN;
disp("Tomographical measurement ongoing...")
else
gmSEQ.dataN = gmSEQ.ctrN;
end
numPDChan=0;
if gmSEQ.measPD
if strcmp(gmSEQ.meas2,'PD0')
numPDChan = numPDChan+1;
end
if strcmp(gmSEQ.meas3,'PD1')
numPDChan = numPDChan+1;
end
end
%append the voltage data to the counts data. Hence we need to expand
%the counts array by the corresponding voltage channels to read.
%TODO: store voltage data to a separate file
gmSEQ.signal_Ave = NaN(gmSEQ.dataN*(numPDChan+1), gmSEQ.NSweepParam);
gmSEQ.signal = NaN(gmSEQ.dataN*(numPDChan+1), gmSEQ.NSweepParam);
gmSEQ.refCounts=Track('Init');
SignalGeneratorFunctionPool('SetMod');
SignalGeneratorFunctionPool('WritePow');
SignalGeneratorFunctionPool('WriteFreq');
gSG.bOn=1; SignalGeneratorFunctionPool('RFOnOff');
CreateCaliLog();
if gmSEQ.bRandom
disp("Random measurement ongoing...")
end
try
% Setting up the Tagger
%TimeTagger.initialize()
tagger = TimeTagger.createTimeTagger();
bin_width = 10*1e3; %ps
readout_length =800e3*1e3; %ps
numCounterGates = gmSEQ.ctrN;
bin_number = 1 + round(readout_length / bin_width);
channel_spcm = 1;
channel_gate = 2;
TimeDiff = TTTimeDifferences(tagger, ...
channel_spcm, ...
channel_gate, ...
channel_gate, ...
tagger.CHANNEL_UNUSED, ...
bin_width, ...
bin_number, ...
numCounterGates);
pause(0.2);
if gmSEQ.measPD
[~, hCounterPD0] = SetPDCounters([],1*gmSEQ.ctrN*gmSEQ.Repeat,PortMap('Ctr Gate'),500000,numPDChan);
end
for i=1:gmSEQ.Average
gmSEQ.iAverage=i;
handles.biAverage.String=num2str(gmSEQ.iAverage);
% disp("Running the round " + num2str(i))
raw_j=1;
iwarmup=1;
randomList = randperm(gmSEQ.NSweepParam); % Shuffle the input, added by Weijie 04/20/2022
DD = int32(zeros(numCounterGates*gmSEQ.NSweepParam,bin_number));
%gmSEQ.TimeTaggerData = DD;
while raw_j<=gmSEQ.NSweepParam
% gmSEQ.refCounts=Track('Run');
if gmSEQ.bRandom
j = randomList(raw_j); % Shuffle the input, added by Weijie 04/20/2022
else
j = raw_j;
end
Calibration(hObject, eventdata, handles)
gmSEQ.m=gmSEQ.SweepParam(j); % Manually change the sweep range here.
if strcmp(gmSEQ.name, 'Special Cooling')
disp([' gmSEQ.m = ', num2str(gmSEQ.m)]);
end
if gmSEQ.bTomo
for axis = 0:gmSEQ.Ntomo-1
gmSEQ.CoolSwitch = axis;
SequencePool(string(gmSEQ.name));
DrawSequence(gmSEQ,hObject, eventdata, handles.axes1);
for k=1:numel(gmSEQ.CHN)
gmSEQ.CHN(k).T=gmSEQ.CHN(k).T/1e9;
gmSEQ.CHN(k).DT=gmSEQ.CHN(k).DT/1e9;
gmSEQ.CHN(k).Delays=gmSEQ.CHN(k).Delays/1e9;
end
PBFunctionPool('PreprocessPBSequence',gmSEQ); % todo: account for ns
%%%
StartCounters(hCounter);
Run_PB_Sequence();
[~, vec] = ReadCountersN(hCounter,(gmSEQ.ctrN*gmSEQ.Repeat),gmSEQ.Repeat*tmax/1e9*1.5);
DAQmxStopTask(hCounter);
%%%
sigDatum = ProcessData(vec);
for k = 1:gmSEQ.ctrN
gmSEQ.signal_Ave(gmSEQ.ctrN*axis + k, j) = sigDatum(k);
if i == 1
gmSEQ.signal(gmSEQ.ctrN*axis + k, j) = sigDatum(k);
else
gmSEQ.signal(gmSEQ.ctrN*axis + k, j) = (gmSEQ.signal(gmSEQ.ctrN*axis + k, j)*(i-1)+sigDatum(k))/i;
end
end
end
else
SequencePool(string(gmSEQ.name));
DrawSequence(gmSEQ,hObject, eventdata, handles.axes1);
for k=1:numel(gmSEQ.CHN)
gmSEQ.CHN(k).T=gmSEQ.CHN(k).T/1e9;
gmSEQ.CHN(k).DT=gmSEQ.CHN(k).DT/1e9;
gmSEQ.CHN(k).Delays=gmSEQ.CHN(k).Delays/1e9;
end
PBFunctionPool('PreprocessPBSequence',gmSEQ); % todo: account for ns
%%%
if gmSEQ.measPD;StartCounters(hCounterPD0);end
Run_PB_Sequence();
D=TimeDiff.getData();
if gmSEQ.measPD;[~, vec1] = ReadCountersPD(hCounterPD0,(1*gmSEQ.ctrN*gmSEQ.Repeat),gmSEQ.Repeat*tmax/1e9*1.5,numPDChan);end
if gmSEQ.measPD;DAQmxStopTask(hCounterPD0);end
%%%
j1 = numCounterGates*(raw_j-1)+1;
j2 = numCounterGates*(raw_j);
DD(j1:j2,:) = DD(j1:j2,:)+D;
sigDatum0 = ProcessDataTimeTagger(D,bin_width);
%process PD voltage data acquired
if gmSEQ.measPD
sigVoltDatum = NaN(numPDChan,gmSEQ.ctrN);
for iPD = 1:numPDChan
sigVoltProcessing = vec1(1+(gmSEQ.ctrN*gmSEQ.Repeat)*(iPD-1):(gmSEQ.ctrN*gmSEQ.Repeat)*iPD);
for ic = 1:gmSEQ.ctrN
sigVoltDatum(iPD,ic)=sum(sigVoltProcessing(ic:gmSEQ.ctrN:end))/(length(sigVoltProcessing)/gmSEQ.ctrN); %store the average voltage across "repeats"
end
%store voltage data
for k = 1:gmSEQ.ctrN
data_index = k+iPD*gmSEQ.ctrN; %the index of the data in the stored data
gmSEQ.signal_Ave(data_index, j) = sigVoltDatum(iPD,k);
if i == 1
gmSEQ.signal(data_index, j) = sigVoltDatum(iPD,k);
else
gmSEQ.signal(data_index, j) = (gmSEQ.signal(data_index, j)*(i-1)+sigVoltDatum(iPD,k))/i;
end
end
end
end
sigDatum = sigDatum0;
for k = 1:gmSEQ.ctrN
gmSEQ.signal_Ave(k, j) = sigDatum(k);
if i == 1
gmSEQ.signal(k, j) = sigDatum(k);
else
gmSEQ.signal(k, j) = (gmSEQ.signal(k, j)*(i-1)+sigDatum(k))/i;
end
end
if gmSEQ.saveRaw
csvwrite('D:\RawData.csv', mean(gmSEQ.signal,2));
end
end
% save a backup of the data here in case matlab crashes
TemporarySave(BackupFile);
if gmSEQ.ctrN<=20 %do not plot if too many counter gates
%PlotData(handles,raw_j);
PlotDataTimeTagger(handles,raw_j);
end
drawnow;
if ~gmSEQ.bGo
break
end
if (gmSEQ.bWarmUpAOM && iwarmup==3)||~gmSEQ.bWarmUpAOM||i~=1||raw_j~=1
raw_j=raw_j+1;
elseif gmSEQ.bWarmUpAOM
iwarmup=iwarmup+1;
end
end
PlotDataTimeTagger_axis2(handles,DD)
SaveIgorText_Average(handles);
SaveIgorText(handles);
if ~gmSEQ.bGo || ~gmSEQ.bGoAfterAvg
break
end
end
if gmSEQ.measPD;ClearCounters(hCounterPD0);end
% ClearCounters(hCounter2);
% ClearCounters(hCounter3);
catch ME
KillAllTasks;
freeTimeTagger(tagger);
gSG.bOn=0; SignalGeneratorFunctionPool('RFOnOff');
rethrow(ME);
end
if gmSEQ.bTrack
PBFunctionPool('PBON',2^SequencePool('PBDictionary','AOM'));
end
%gSG.bOn=0; SignalGeneratorFunctionPool('RFOnOff');
elseif isfield(gmSEQ,'bLiO') % activates for ESR
CreateSavePath_Ave()
gmSEQ.signal_Ave = NaN(gmSEQ.dataN, gmSEQ.NSweepParam);
gmSEQ.signal = NaN(gmSEQ.dataN, gmSEQ.NSweepParam);
gmSEQ.dataN = gmSEQ.ctrN;
gmSEQ.refCounts=Track('Init');
SequencePool(string(gmSEQ.name));
gmSEQ.SweepParam=gmSEQ.SweepParam*1e9;
gSG.sweepDev=(gmSEQ.SweepParam(gmSEQ.NSweepParam)-gmSEQ.SweepParam(1))/2;
gSG.Freq=(gmSEQ.SweepParam(gmSEQ.NSweepParam)+gmSEQ.SweepParam(1))/2;
SignalGeneratorFunctionPool('WriteFreq');
if gSG.Pow > -10%%%%%%
error("Too large MW power. ")
end
SignalGeneratorFunctionPool('WritePow');
SignalGeneratorFunctionPool('SetMod');
PBFunctionPool('PBON',2^SequencePool('PBDictionary','GreenAOM')+2^SequencePool('PBDictionary','MWSwitch'));
% if gmSEQ.bWarmUpAOM %%% probably not necessary for this method of ESR
% gSG.bOn=1; SignalGeneratorFunctionPool('RFOnOff');
% pause(30);
% end
try
[vec, NN]=MakeSweepVector();
gSG.bOn=1; SignalGeneratorFunctionPool('RFOnOff');
for i=1:gmSEQ.Average
% tracking disable
%gmSEQ.refCounts=Track('Run');
%%%%% Pulse Train %%%%%
[~, hPulse] = DigPulseTrainCont(gmSEQ.NSweepParam*gSG.sweepRate,0.5,NN);
hCPS.hPulse=hPulse;
%%%%% Analog write %%%%
[ ~, hScan ] = DAQmxFunctionPool('WriteAnalogVoltage',PortMap('SG ext mod'),vec, NN,gmSEQ.NSweepParam*gSG.sweepRate);
hCPS.hScan=hScan;
%%%%% Create counting channel %%%%
[~, hCounter] = SetNCounters(0,NN,'/Dev2/PFI13',gmSEQ.NSweepParam*gSG.sweepRate);
hCPS.hCounter=hCounter;
gmSEQ.iAverage=i;
handles.biAverage.String=num2str(gmSEQ.iAverage);
status = DAQmxStartTask(hScan); DAQmxErr(status); status = DAQmxStartTask(hCounter); DAQmxErr(status);
status = DAQmxStartTask(hPulse); DAQmxErr(status);
[~, A] = ReadCountersN(hCounter,NN, gmSEQ.misc*1.1);
pause(0.5)
DAQmxStopTask(hCounter);
DAQmxStopTask(hScan);
DAQmxStopTask(hPulse);
if gmSEQ.bAAR==1
gmSEQ.signal_Ave(1,:) = ProcessData(A);
if i==1
gmSEQ.signal(1,:) = ProcessData(A);
else
gmSEQ.signal(1,:) = (gmSEQ.signal(1,:)*(i-1)+ProcessData(A))/i;
end
else
gmSEQ.signal(1,:) = ProcessData(A);
gmSEQ.signal_Ave(1,:) = ProcessData(A);
end
TemporarySave(BackupFile);
PlotData(handles,0);
drawnow;
DAQmxClearTask(hCounter);
DAQmxClearTask(hPulse);
DAQmxClearTask(hScan);
if ~gmSEQ.bGo || ~gmSEQ.bGoAfterAvg
break
end
SaveIgorText_Average(handles);
end
catch ME
KillAllTasks;
gSG.bOn=0; SignalGeneratorFunctionPool('RFOnOff');
%gSG2.bOn=0; SignalGeneratorFunctionPool2('RFOnOff');
rethrow(ME);
end
if ~gmSEQ.bTrack
ExperimentFunctionPool('PBOFF',hObject, eventdata, handles);
end
gSG.bOn=0; SignalGeneratorFunctionPool('RFOnOff');
elseif gSG.bfixedPow && ~gSG.bfixedFreq % for ODMR
CreateSavePath_Ave()
gmSEQ.refCounts=Track('Init');
SignalGeneratorFunctionPool('SetMod');
SignalGeneratorFunctionPool('WritePow');
gSG.bOn=1; SignalGeneratorFunctionPool('RFOnOff');
gmSEQ.SweepParam=gmSEQ.SweepParam*1e9;
CreateCaliLog(hObject, eventdata, handles);
gmSEQ.bTomo = gmSEQ.Alternate;
if gmSEQ.bTomo
gmSEQ.dataN = 3*gmSEQ.ctrN;
disp("Tomographical measurement ongoing...")
else
gmSEQ.dataN = gmSEQ.ctrN;
end
gmSEQ.signal_Ave = NaN(gmSEQ.dataN, gmSEQ.NSweepParam);
gmSEQ.signal = NaN(gmSEQ.dataN, gmSEQ.NSweepParam);
if gmSEQ.bRandom
disp("Random measurement ongoing...")
end
try
[~, hCounter] = SetNCounters(gmSEQ.ctrN*gmSEQ.Repeat,PortMap('Ctr Gate'),500000);
for i=1:gmSEQ.Average
gmSEQ.iAverage=i;
handles.biAverage.String=num2str(gmSEQ.iAverage);
raw_j=1;
iwarmup=1;
randomList = randperm(gmSEQ.NSweepParam); % Shuffle the input, added by Weijie 04/20/2022
while raw_j<=gmSEQ.NSweepParam
% gmSEQ.refCounts=Track('Run');
if gmSEQ.bRandom
j = randomList(raw_j); % Shuffle the input, added by Weijie 04/20/2022
else
j = raw_j;
end
Calibration(hObject, eventdata, handles)
SequencePool(string(gmSEQ.name));
if gSG.ACmodAWG
gSG.Freq = gmSEQ.SweepParam(j)-str2double(get(handles.AWGFreq, 'String'))*1e9;
else
gSG.Freq = gmSEQ.SweepParam(j);
end
%
SignalGeneratorFunctionPool('WriteFreq');
% SignalGeneratorFunctionPool('WritePow');
% gmSEQ.m=gmSEQ.SweepParam(j);
SequencePool(string(gmSEQ.name));
DrawSequence(gmSEQ,hObject, eventdata, handles.axes1);
for k=1:numel(gmSEQ.CHN)
gmSEQ.CHN(k).T=gmSEQ.CHN(k).T/1e9;
gmSEQ.CHN(k).DT=gmSEQ.CHN(k).DT/1e9;
gmSEQ.CHN(k).Delays=gmSEQ.CHN(k).Delays/1e9;
end
PBFunctionPool('PreprocessPBSequence',gmSEQ); % todo: account for ns
StartCounters(hCounter);
Run_PB_Sequence();
%pause(gmSEQ.Repeat*tmax/1e9*1.1);
%ExperimentFunctionPool('PBOFF',hObject, eventdata, handles);
[~, vec] = ReadCountersN(hCounter,(gmSEQ.ctrN*gmSEQ.Repeat),gmSEQ.Repeat*tmax/1e9*1.5);
DAQmxStopTask(hCounter);
sigDatum = ProcessData(vec);
for k = 1:gmSEQ.ctrN
gmSEQ.signal_Ave(k, j) = sigDatum(k);
if i == 1
gmSEQ.signal(k, j) = sigDatum(k);
else
gmSEQ.signal(k, j) = (gmSEQ.signal(k, j)*(i-1)+sigDatum(k))/i;
end
end
% save a backup of the data here in case matlab crashes
TemporarySave(BackupFile);
PlotData(handles,raw_j);
drawnow;
if ~gmSEQ.bGo
break
end
if (gmSEQ.bWarmUpAOM && iwarmup==2)||~gmSEQ.bWarmUpAOM||i~=1||raw_j~=1
raw_j=raw_j+1;
elseif gmSEQ.bWarmUpAOM
iwarmup=iwarmup+1;
end
end
SaveIgorText(handles);
SaveIgorText_Average(handles);
if ~gmSEQ.bGo || ~gmSEQ.bGoAfterAvg
break
end
end
ClearCounters(hCounter);
catch ME
KillAllTasks;
gSG.bOn=0; SignalGeneratorFunctionPool('RFOnOff');
rethrow(ME);
end
end
gSG.bOn=0; SignalGeneratorFunctionPool('RFOnOff');
% gSG2.bOn=0; SignalGeneratorFunctionPool2('RFOnOff');
% gSG3.bOn=0; SignalGeneratorFunctionPool3('RFOnOff');
% The following stop is just for test, added by Weijie 07/30/2022
% chaseFunctionPool('stopChase', gmSEQ.MWAWG)
% chaseFunctionPool('stopChase', gmSEQ.P1AWG)
% chaseFunctionPool('stopChase', gmSEQ.MWAWG2)
gmSEQ.bGo = 0;
gmSEQ.bExp = 0;
SaveIgorText(handles);
disp('Experiment completed!')
set(handles.runningText,'string','Stopped')
%sound(y,Fs);
%SaveData;
function getUserInputFromGUI(handles)
global gmSEQ gSG gSG2 gSG3
% for two sweeping range, with option for log10 sampling
% Add by C. Zu on 9/29/2020
if (gmSEQ.bSweep1log)
gmSEQ.SweepParam=logspace(log10(gmSEQ.From),log10(gmSEQ.To),gmSEQ.N);
else
gmSEQ.SweepParam=linspace(gmSEQ.From,gmSEQ.To,gmSEQ.N);
end
if (gmSEQ.bSweep2)
if (gmSEQ.bSweep2log)
gmSEQ.SweepParam=[gmSEQ.SweepParam logspace(log10(gmSEQ.From2),log10(gmSEQ.To2),gmSEQ.N2)];
else
gmSEQ.SweepParam=[gmSEQ.SweepParam linspace(gmSEQ.From2,gmSEQ.To2,gmSEQ.N2)];
end
end
if (gmSEQ.bSweep3)
if (gmSEQ.bSweep3log)
gmSEQ.SweepParam=[gmSEQ.SweepParam logspace(log10(gmSEQ.From3),log10(gmSEQ.To3),gmSEQ.N3)];
else
gmSEQ.SweepParam=[gmSEQ.SweepParam linspace(gmSEQ.From3,gmSEQ.To3,gmSEQ.N3)];
end
end
% Customized in the input data here
% DEER ODMR Weijie 04/19/2022
gmSEQ.bCust = get(handles.useCustPoints,'Value');
if gmSEQ.bCust
% gmSEQ.SweepParam = [linspace(0.900,0.928, 15), linspace(0.930,0.969,40), linspace(0.970, 1.018, 25), linspace(1.020, 1.069, 51), linspace(1.070, 1.100, 16)];
gmSEQ.SweepParam = [linspace(1e7, 5e7, 4), linspace(1e8, 2e9, 10)];
gmSEQ.SweepParam = [linspace(1.50, 1.70, 201), linspace(4.00, 4.20, 201)];
% gmSEQ.SweepParam = [linspace(50, 20050, 5), linspace(40000, 100000, 4), linspace(150000, 400000, 6), linspace(500000, 800000, 4)];
% Seq F 100 ns
% gmSEQ.SweepParam = [linspace(0, 20, 6), linspace(30, 60, 4), linspace(80, 200, 7), linspace(240, 400, 5)];
% Seq F 200 ns
% gmSEQ.SweepParam = [linspace(0, 10, 6), linspace(15, 30, 4), linspace(40, 100, 7), linspace(120, 200, 5)];
% Seq F 300 ns
% gmSEQ.SweepParam = [linspace(0, 6, 4), linspace(10, 30, 6), linspace(40, 90, 6), linspace(110, 150, 3)];
% Seq G 100 ns
% gmSEQ.SweepParam = [linspace(0, 6, 4), linspace(10, 30, 6), linspace(40, 200, 9)];
% Cory 100 ns
% gmSEQ.SweepParam = [linspace(0, 5, 6), linspace(6, 20, 8), linspace(30, 80, 6)];
disp("Customized input, GUI values are overwritten.")
end
function InitializeData()
global gmSEQ
gmSEQ.NSweepParam=length(gmSEQ.SweepParam);
gmSEQ.signal=NaN(1,gmSEQ.NSweepParam);
gmSEQ.reference=NaN(1,gmSEQ.NSweepParam);
gmSEQ.reference2=NaN(1,gmSEQ.NSweepParam);
gmSEQ.reference3=NaN(1,gmSEQ.NSweepParam);
gmSEQ.bGo=1;
gmSEQ.bGoAfterAvg=1;
for i=1:numel(gmSEQ.CHN)
if gmSEQ.CHN(i).PBN==SequencePool('PBDictionary','ctr0')
gmSEQ.ctrN=gmSEQ.CHN(i).NRise;
end
end
function Run_PB_Sequence
% function RunPBSequence
PBesrInit(); %initialize PBesr
% sets the clock frequency. for PBESR-PRO-400, it's 400MHz
% for PBESR-PRO-333, it's 333.3MHz
PBesrSetClock(500);
PBesrStart(); %start pulsing. it will start pulse sequence which were progammed/loaded to PBESR card before.
PBesrClose(); %close PBesr
%set status to 0, implement in the future
status = 0;
function [status, task] = SetNCounters(varargin)
%varargin(1) is the number of total samples
%varargin(2) is the source of gating
%varargin(3) is the frequency of the gating to expect
% Initialize DAQ
global gmSEQ hCPS
if strcmp(gmSEQ.meas,'SPCM')
if isfield(gmSEQ,'bLiO')
[status, task ] = DAQmxFunctionPool('SetCounter',varargin{2},varargin{1});
else
[status, task ] = DAQmxFunctionPool('SetGatedNCounter',varargin{2},varargin{1});
end
elseif strcmp(gmSEQ.meas,'APD')
[status, task ] = DAQmxFunctionPool('CreateAIChannel',varargin{3},varargin{2},varargin{4});
end
hCPS.hCounter=task;
function [status, task] = SetPDCounters(varargin)
% Initialize DAQ
global hCPS
[status, task ] = DAQmxFunctionPool('CreateAIChannel',varargin{3},varargin{2},varargin{4},varargin{5});
hCPS.hPDCounter0=task;
function PlotData(handles,raw_j)
global gmSEQ
%axes(handles.axes2); %cla;
% Implement the special case and ESR later
for i = 1:gmSEQ.dataN
if gmSEQ.dataN == 1
plot(handles.axes2, single(gmSEQ.SweepParam)*gmSEQ.ScaleT,single(gmSEQ.signal(i, :)),'-', ...
'color', [0, 0, 1],'LineWidth', 0.5, 'DisplayName', sprintf('signal %d', i))
else
plot(handles.axes2, single(gmSEQ.SweepParam)*gmSEQ.ScaleT,single(gmSEQ.signal(i, :)),'-', ...
'color', [0, (i-1)/(gmSEQ.dataN - 1), 1-(i-1)/(gmSEQ.dataN - 1)],'LineWidth', 0.5, 'DisplayName', sprintf('signal %d', i))
end
if i == 1
hold(handles.axes2, 'on')
end
end
grid on;
set(handles.axes2,'FontSize',8);
ylabel(handles.axes2, 'Fluorescence counts');
xlabel(handles.axes2, gmSEQ.ScaleStr);
% don't rescale x axis of the plots if num of sweep param is set to 1
if length(gmSEQ.SweepParam) ~= 1
xlim(handles.axes2, [gmSEQ.SweepParam(1)*gmSEQ.ScaleT gmSEQ.SweepParam(gmSEQ.NSweepParam)*gmSEQ.ScaleT]);
end
if get(handles.bShowLegend,'Value')
legend(handles.axes2)
end
if raw_j~=0
xline(handles.axes2, single(gmSEQ.SweepParam(raw_j))*gmSEQ.ScaleT,'--', 'color','r','HandleVisibility','off')
end
hold(handles.axes2, 'off')
if ~isfield(gmSEQ,'bLiO')&&gmSEQ.ctrN~=1 % Do not plot ESR
% Remove NaN (empty data)
for i = 1:gmSEQ.dataN
signal(i,:) = gmSEQ.signal(i, ~isnan(gmSEQ.signal(i,:)));
end
if gmSEQ.bTomo
% Generalize version
if gmSEQ.ctrN ~= 4
error("This function has not been implemented.")
end
for i = 1:gmSEQ.Ntomo
[data(i,:), data_err(i,:)] = ContrastDiff(signal(4*(i-1)+1,:), signal(4*(i-1)+3,:),...
signal(4*(i-1)+2,:), signal(4*(i-1)+4,:), gmSEQ.iAverage);
errorbar(handles.axes3, gmSEQ.SweepParam(1:length(data(i,:))).*gmSEQ.ScaleT, data(i,:), data_err(i,:), ...
'color', [(i-1)/(gmSEQ.Ntomo - 1), 0, 1-(i-1)/(gmSEQ.Ntomo - 1)], ...
'DisplayName', num2str(i))
hold(handles.axes3, 'on')
end
legend(handles.axes3)
hold(handles.axes3, 'off')
else
if gmSEQ.ctrN==3
if strcmp(gmSEQ.name,'T1JC')
data=signal(1,:)-signal(2,:)./signal(3,:);
else
data=signal(1,:);
data_err = zeros(size(data));
% data=signal(1,:)-signal(3,:)./(signal(2,:)-signal(4,:));
end
elseif gmSEQ.ctrN==2
if strcmp(gmSEQ.name,'Rabi_Scan_counterGate_time') || strcmp(gmSEQ.name, 'Rabi_Scan_Ini_time')
sig = signal(2,:);
ref = signal(1,:);
data = (ref-sig)./ref.*sqrt(sig); % contrast / noise in signal
data_err =zeros(size(sig));
else
sig = signal(2,:);
ref = signal(1,:);
data = sig./ref;
%data = ref-sig;
ref_err = 1./sqrt(gmSEQ.iAverage * ref); % Relative error of reference
sig_err = 1./sqrt(gmSEQ.iAverage * sig); % Relative error of signal
rel_err = sqrt(ref_err.^2 + sig_err.^2);
data_err = rel_err .* data;
end
elseif gmSEQ.ctrN==4
if strcmp(gmSEQ.name,'Test_NV_Polarization') || strcmp(gmSEQ.name,'Special Cooling') || strcmp(gmSEQ.name,'Special Cooling_2') || strcmp(gmSEQ.name,'Special Cooling_P1_2_DurMeas')
data = (signal(1,:)-signal(3,:))./(signal(2,:)+signal(4,:))*2;
elseif strcmp(gmSEQ.name,'CtrDur') && gmSEQ.meas == "SPCM"
data = -(signal(2,:)-signal(4,:))./sqrt((signal(1,:)+signal(3,:))/2);
data_err = data .* 0;
elseif strcmp(gmSEQ.name,'Elec_Pol_Extract') || strcmp(gmSEQ.name,'Rabi_fix_MWDutyCycle')
sig1 = signal(2,:);
ref1 = signal(1,:);
data1 = sig1./ref1;
ref_err1 = 1./sqrt(gmSEQ.iAverage * ref1); % Relative error of reference
sig_err1 = 1./sqrt(gmSEQ.iAverage * sig1); % Relative error of signal
rel_err1 = sqrt(ref_err1.^2 + sig_err1.^2);
data_err1 = rel_err1 .* data1;
sig2 = signal(4,:);
ref2 = signal(3,:);
data2 = sig2./ref2;
ref_err2 = 1./sqrt(gmSEQ.iAverage * ref2); % Relative error of reference
sig_err2 = 1./sqrt(gmSEQ.iAverage * sig2); % Relative error of signal
rel_err2 = sqrt(ref_err2.^2 + sig_err2.^2);
data_err2 = rel_err2 .* data2;
elseif strcmp(gmSEQ.name,'T1_Rb_S00_S01_Rd')||strcmp(gmSEQ.name,'T1_Rb_S00_S01_Rd_newRef')
% data = (gmSEQ.reference(~isnan(gmSEQ.reference))-gmSEQ.reference3(~isnan(gmSEQ.reference3)))./(gmSEQ.signal(~isnan(gmSEQ.signal))+gmSEQ.reference2(~isnan(gmSEQ.reference2)))*2;
ref_B = signal(1,:);
ref_D = signal(4,:);
sig_B = signal(2,:);
sig_D = signal(3,:);
[data, data_err] = ContrastDiff(ref_B, ref_D,sig_B, sig_D, gmSEQ.iAverage);
else
% data = (gmSEQ.reference(~isnan(gmSEQ.reference))-gmSEQ.reference3(~isnan(gmSEQ.reference3)))./(gmSEQ.signal(~isnan(gmSEQ.signal))+gmSEQ.reference2(~isnan(gmSEQ.reference2)))*2;
ref_B = signal(1,:);
ref_D = signal(3,:);
sig_B = signal(2,:);
sig_D = signal(4,:);
[data, data_err] = ContrastDiff(ref_B, ref_D,sig_B, sig_D, gmSEQ.iAverage);
end
elseif gmSEQ.ctrN==5
sig_B = signal(2,:);
ref_B = signal(1,:);
sig_D = signal(4,:);
ref_D = signal(3,:);
data = sig_B-sig_D;
data_err = zeros(size(signal(1,:)));
else
data = zeros(size(signal(1,:)));
data_err = data;
end
% plot(handles.axes3, gmSEQ.SweepParam(1:length(data)).*ScaleT,data,'-g')
if strcmp(gmSEQ.name,'Elec_Pol_Extract')|| strcmp(gmSEQ.name,'Rabi_fix_MWDutyCycle')
errorbar(handles.axes3, gmSEQ.SweepParam(1:length(data1)).*gmSEQ.ScaleT, data1, data_err1,'-r')
hold(handles.axes3,'on')
errorbar(handles.axes3, gmSEQ.SweepParam(1:length(data2)).*gmSEQ.ScaleT, data2, data_err2,'-b')
hold(handles.axes3,'off')
else
errorbar(handles.axes3, gmSEQ.SweepParam(1:length(data)).*gmSEQ.ScaleT, data, data_err,'-g')
end
end
grid on;
set(handles.axes3,'FontSize',8);
ylabel(handles.axes3, 'Fluorescence contrast');
xlabel(handles.axes3, gmSEQ.ScaleStr);
if length(gmSEQ.SweepParam) ~= 1
xlim(handles.axes3, [gmSEQ.SweepParam(1)*gmSEQ.ScaleT gmSEQ.SweepParam(gmSEQ.NSweepParam)*gmSEQ.ScaleT]);
end
if raw_j~=0
xline(handles.axes3, single(gmSEQ.SweepParam(raw_j))*gmSEQ.ScaleT,'--', 'color','r','HandleVisibility','off')
end
end
function PlotDataTimeTagger_axis2(handles,DD)
global gmSEQ
plot(handles.axes2,DD(1,:))
hold(handles.axes2, 'on')
plot(handles.axes2,DD(2,:))
set(handles.axes2,'FontSize',8);
ylabel(handles.axes2, 'Fluorescence counts');
xlabel(handles.axes2, gmSEQ.ScaleStr);
hold(handles.axes2, 'off')
function PlotDataTimeTagger(handles,raw_j)
global gmSEQ
%axes(handles.axes2); %cla;
if ~isfield(gmSEQ,'bLiO')&&gmSEQ.ctrN~=1 % Do not plot ESR
% Remove NaN (empty data)
for i = 1:gmSEQ.dataN
signal(i,:) = gmSEQ.signal(i, ~isnan(gmSEQ.signal(i,:)));
end
if gmSEQ.bTomo
% Generalize version
if gmSEQ.ctrN ~= 4
error("This function has not been implemented.")
end
for i = 1:gmSEQ.Ntomo
[data(i,:), data_err(i,:)] = ContrastDiff(signal(4*(i-1)+1,:), signal(4*(i-1)+3,:),...
signal(4*(i-1)+2,:), signal(4*(i-1)+4,:), gmSEQ.iAverage);
errorbar(handles.axes3, gmSEQ.SweepParam(1:length(data(i,:))).*gmSEQ.ScaleT, data(i,:), data_err(i,:), ...
'color', [(i-1)/(gmSEQ.Ntomo - 1), 0, 1-(i-1)/(gmSEQ.Ntomo - 1)], ...
'DisplayName', num2str(i))
hold(handles.axes3, 'on')
end
legend(handles.axes3)
hold(handles.axes3, 'off')
else
if gmSEQ.ctrN==3
data=signal(1,:);
data_err = zeros(size(data));
% data=signal(1,:)-signal(3,:)./(signal(2,:)-signal(4,:));
elseif gmSEQ.ctrN==2
sig = signal(2,:);
ref = signal(1,:);
data = sig./ref;
%data = ref-sig;
ref_err = 1./sqrt(gmSEQ.iAverage * ref); % Relative error of reference
sig_err = 1./sqrt(gmSEQ.iAverage * sig); % Relative error of signal
rel_err = sqrt(ref_err.^2 + sig_err.^2);
data_err = rel_err .* data;
elseif gmSEQ.ctrN==4
% data = (gmSEQ.reference(~isnan(gmSEQ.reference))-gmSEQ.reference3(~isnan(gmSEQ.reference3)))./(gmSEQ.signal(~isnan(gmSEQ.signal))+gmSEQ.reference2(~isnan(gmSEQ.reference2)))*2;
ref_B = signal(1,:);
ref_D = signal(3,:);
sig_B = signal(2,:);
sig_D = signal(4,:);
[data, data_err] = ContrastDiff(ref_B, ref_D,sig_B, sig_D, gmSEQ.iAverage);
else
data = zeros(size(signal(1,:)));
data_err = data;
end
errorbar(handles.axes3, gmSEQ.SweepParam(1:length(data)).*gmSEQ.ScaleT, data, data_err,'-g')
end
grid on;
set(handles.axes3,'FontSize',8);
ylabel(handles.axes3, 'Fluorescence contrast');
xlabel(handles.axes3, gmSEQ.ScaleStr);
if length(gmSEQ.SweepParam) ~= 1
xlim(handles.axes3, [gmSEQ.SweepParam(1)*gmSEQ.ScaleT gmSEQ.SweepParam(gmSEQ.NSweepParam)*gmSEQ.ScaleT]);
end
if raw_j~=0
xline(handles.axes3, single(gmSEQ.SweepParam(raw_j))*gmSEQ.ScaleT,'--', 'color','r','HandleVisibility','off')
end
end
function [data, data_err] = ContrastDiff(ref_B, ref_D,sig_B, sig_D, N)
%data = 2*(sig_B - sig_D)./(ref_B + ref_D);
data = (sig_B - sig_D)./(ref_B - ref_D); %Normalize to 1
ref_B_err = 1./sqrt(N * ref_B); % Relative error of bright reference
ref_D_err = 1./sqrt(N * ref_D); % Relative error of dark reference
sig_B_err = 1./sqrt(N * sig_B); % Relative error of bright signal
sig_D_err = 1./sqrt(N * sig_D); % Relative error of dark signal
ref_err = sqrt((ref_B_err .* ref_B).^2 + (ref_D_err .* ref_D).^2)./(ref_B + ref_D);
sig_err = sqrt((sig_B_err .* sig_B).^2 + (sig_D_err .* sig_D).^2)./(sig_B - sig_D);
rel_err = sqrt(ref_err.^2 + sig_err.^2);
data_err = rel_err .* data;
function StartCounters(task)
DAQmxStartTask(task);
function [status,A] = ReadCountersN(task,samps,timeout)
global gmSEQ
if strcmp(gmSEQ.meas,'SPCM')
[status, A]= DAQmxReadCounterU32(task, samps, timeout, zeros(1,samps), samps, libpointer('int32Ptr',0) );
elseif strcmp(gmSEQ.meas,'APD')
[status, A] = DAQmxFunctionPool('ReadAnalogVoltage',task, samps, timeout);
end
DAQmxErr(status);
function [status,A] = ReadCountersPD(task,samps,timeout,numPDChan)
[status, A] = DAQmxFunctionPool('ReadAnalogVoltage',task, samps, timeout,numPDChan);
DAQmxErr(status);
function ClearCounters(task)
DAQmxClearTask(task);
function TemporarySave(BackupFile)
global gSG gmSEQ
% convert relevant globals to a bigger structure
BackupExp.gmSEQ=gmSEQ;
BackupExp.gSG=gSG;
%save the data in matlab binary format
save(BackupFile,'BackupExp');
function [vec, NN] = MakeSweepVector
global gSG gmSEQ
vecA=-1:(2/(gmSEQ.NSweepParam-1)):1;
vec=[vecA fliplr(vecA)];
vec=repmat(vec,1,ceil(gSG.sweepRate*gmSEQ.misc/2));
if strcmp(gmSEQ.meas,'SPCM')
vec=[vec(1) vec];
end
NN=length(vec);
function sigDatum = ProcessDataTimeTagger(D,bin_width)
global gmSEQ
% for ESR
if isfield(gmSEQ,'bLiO')
%not implemeted yet
sigDatum = [];
else
sumD = sum(D);
I1=find(sumD>0.1*max(sumD),1,'first'); %find the index of the bin of the rising edge of PL
integrationTime = 800e3*1e3; %ps
integrationBinTotNum = round(integrationTime/bin_width);
I2 = I1+integrationBinTotNum;
sigDatum = sum(D(:,I1:end),2); %sum over the integration window
end
function CreateCaliLog()
global gCaliLog gTrackLog gmSEQ gCaliCounter
if ~gmSEQ.bTrack
return
end
now = clock;
date = [num2str(now(1)),'-',num2str(now(2)),'-',num2str(round(now(3)))];
fullPath=fullfile('D:\Data\',date,'\');
if ~exist(fullPath,'dir')
mkdir(fullPath);
end
if gmSEQ.bCali
gCaliCounter.RFCali = 0;
gCaliLog.path = fullPath;
gCaliLog.file = ['_' date '_CaliLog.txt'];
name=regexprep(gmSEQ.name,'\W',''); % rewrite the sequence name without spaces/weird characters
%File name and prompt
B=fullfile(gCaliLog.path, strcat(name, gCaliLog.file));
file = strcat(name, gCaliLog.file);
%Prevent overwriting
mfile = strrep(B,'.txt','*');
mfilename = strrep(gCaliLog.file,'.txt','');
A = ls(char(mfile));
ImgN = 0;
for f = 1:size(A,1)
sImgN = sscanf(A(f,:),strcat(name, string(mfilename), '_%d.txt'));
if ~isempty(sImgN)
if sImgN > ImgN
ImgN = sImgN;
end
end
end
ImgN = ImgN + 1;
file = strrep(file,'.txt',sprintf('_%03d.txt',ImgN));
gCaliLog.final= fullfile(gCaliLog.path, file);
end
if gmSEQ.bTrack
gCaliCounter.ImageCorr = 0;
gTrackLog.path = fullPath;
gTrackLog.file = ['_' date '_TrackLog.txt'];
name=regexprep(gmSEQ.name,'\W',''); % rewrite the sequence name without spaces/weird characters
%File name and prompt
B=fullfile(gTrackLog.path, strcat(name, gTrackLog.file));
file = strcat(name, gTrackLog.file);
%Prevent overwriting
mfile = strrep(B,'.txt','*');
mfilename = strrep(gTrackLog.file,'.txt','');
A = ls(char(mfile));
ImgN = 0;
for f = 1:size(A,1)
sImgN = sscanf(A(f,:),strcat(name, string(mfilename), '_%d.txt'));
if ~isempty(sImgN)
if sImgN > ImgN
ImgN = sImgN;
end
end
end
ImgN = ImgN + 1;
file = strrep(file,'.txt',sprintf('_%03d.txt',ImgN));
gTrackLog.final= fullfile(gTrackLog.path, file);
end
function Calibration(hObject, eventdata, handles)
global gCaliCounter gmSEQ
if gmSEQ.bTrack
gCaliCounter.ImageCorr = gCaliCounter.ImageCorr + 1;
if (mod(gCaliCounter.ImageCorr, gmSEQ.TrackPointN)==1) % track at the starting of the measurement
Track('ImageCorr');
end
end
if gmSEQ.bCali
gCaliCounter.RFCali = gCaliCounter.RFCali + 1;
if (mod(gCaliCounter.RFCali, gmSEQ.CaliN)==1) || (gmSEQ.CaliN == 1)
if strcmp(gmSEQ.name, 'Special Cooling')
AutoCalibration(hObject, eventdata, handles);
end
end
end
function AutoCalibration(hObject, eventdata, handles)
global gmSEQ gSG gSG2 gSaveDataAve gCaliLog
if ~gmSEQ.bCali
return;
end
% Save the current parameters
gmSEQt = gmSEQ;
gSGt = gSG;
gSG2t = gSG2;
gSaveDataAvet = gSaveDataAve;
% Set Laser AWG power here
h = findobj('Tag','ImageNVCGUI');
if ~isempty(h)
% get handles and other user-defined data associated to Gui1
handles_ImageNVC = guidata(h);
end
PBFunctionPool('PBON',2^SequencePool('PBDictionary','AOM'));
ImageFunctionPool('UpdateVoltage',0, 0, handles_ImageNVC);
ExperimentFunctionPool('AutoCalibration', hObject, eventdata, handles, false);
gSGt.Freq = gSG.Freq;
gSGt.Pow = gSG.Pow;
gmSEQt.SAmp1 = gSG.AWGAmp;
gmSEQt.SAmp2 = gSG.AWGAmp;
gmSEQt.SAmp1_M = gSG.AWGAmp;
gmSEQt.SAmp2_M = gSG.AWGAmp;
% Log
fid = fopen(string(gCaliLog.final),'at'); % a means add data, w means new data
fprintf(fid,'%s', [datestr(datetime(clock))]);
fprintf(fid,' %4.3f %2.1f %.2f\n', [gSG.Freq/1e9 + 0.125, gSG.Pow, gSG.AWGAmp]);
fclose(fid);
% Load the parameters
gmSEQ = gmSEQt;
gSG = gSGt;
gSG2 = gSG2t;
gSaveDataAve = gSaveDataAvet;
% Reset MW
gmSEQ.refCounts=Track('Init');
SignalGeneratorFunctionPool('SetMod');
SignalGeneratorFunctionPool('WritePow');
SignalGeneratorFunctionPool('WriteFreq');
gSG.bOn=1; SignalGeneratorFunctionPool('RFOnOff');
function refCounts = Track(what)
global gmSEQ gSG gTrackLog gScan
if gmSEQ.bTrack
% get the handle of Gui1
h = findobj('Tag','ImageNVCGUI');