-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNanoscope.m
More file actions
3086 lines (2327 loc) · 122 KB
/
Nanoscope.m
File metadata and controls
3086 lines (2327 loc) · 122 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
classdef Nanoscope
properties (Constant)
pixelUpsample = 1; % object-space pixel size upsampling factor
end
properties
% imaging parameters
%--------------------------------------------------------
pixelSize = 58.5; % object pixel size (nm)
numApt = 1.4; % numerical aperture
emissWavelength = 610; % central emission wavelength (nm)
ADcount = .49; % photoelectron conversion
offset = 0; % baseline of camera (in digital units)
refractiveIndx = 1.518; % refractive index of the medium
refractiveIndxSam = 1.334; % refractive index of the sample
imageSize = 95; % side length of the region of interest
%===========================
% phase mask parameters
%--------------------------------------------------------
% phase mask parameters
%--------------------------------------------------------
phaseMaskPara = struct('maskName', 'tri-spot', ... %parameters of the phase mask mounted on SLM
'pupilRadius', 80, ...
'x_shift_center_position', 0, ...
'y_shift_center_position', 0, ...
'maskRotation', 0);
%===========================
% phase mask mounted on SLM
%---------------------------------------------------------
phaseMask; %phase mask mounted on SLM
%===========================
%bases images of a dipole
%---------------------------------------------------------
XXxBasis %XX basis image at x_channel
XXyBasis %XX basis image at y_channel
YYxBasis %YY basis image at x_channel
YYyBasis %YY basis image at y_channel
ZZxBasis %ZZ basis image at x_channel
ZZyBasis %ZZ basis image at y_channel
XYxBasis %XY basis image at x_channel
XYyBasis %XY basis image at y_channel
XZxBasis %XZ basis image at x_channel
XZyBasis %XZ basis image at y_channel
YZxBasis %YZ basis image at x_channel
YZyBasis %YZ basis image at y_channel
brightnessScaling %Brightness scaling factor of a dipole
end
methods
% constructor
%--------------------------------------------------------
function obj = Nanoscope(varargin)
%Nanoscope initializes an instance of Nanoscope object
%option-value pairs:
% pixelSize: scalar- object
% pixel size (nm)
% (default=58.5)
%............................................................
% numApt: scalar- numerical
% aperture of the microscope
% (default=1.4)
%............................................................
% emissWavelength: scalar-
% emission wavelength of
% light radiated from
% emitters (in nm) (default=637)
%............................................................
% offset: scalar or array
% (m*m) with m the image size
% (values of the offset are in the raw
% camera units) (default=0)
%............................................................
% ADcount: scalar-
% photoelectron conversion
% (default=.49)
%............................................................
% refractiveIndx: scalar- the
% refractive index of the
% imaging medium
% (default=1.51)
%............................................................
% imageSize: scalar- size of
% the sqaure region of interest to
% be analyzed
%............................................................
% phaseMaskPara: structure
% with fields:
% maskname: string- name of
% the mask
%............................................................
% pupilRadius:
% integer- radius of
% the pupil in units
% of SLM pixels
% (default=40)
%............................................................
% x_shift_center_position:
% scalar- shift of
% the center pixel of
% the SLM w.r.t. the
% pupil in x
% direction (in units
% of SLM pixels)
% (default=0)
%............................................................
% y_shift_center_position:
% scalar- shift of
% the center pixel of
% the SLM w.r.t. the
% pupil in y
% direction (in units
% of SLM pixels)
% (default=0)
%............................................................
% maskRotation:
% integer- rotation of
% phase mask
% clockwise (in 90
% degree units)
%............................................................
% zeroorder:
% array(1,2) in [0,1]
% models the pupil
% (for left and
% right)
% like
% (1-zeroorder)*pmask+zeroorder*abs(pmask)
% that is a weighted
% sum of pupil and clear apertur
s = opt2struct(varargin);
%=========================
%intialize imaging parameters based on iput options
%=========================
if isfield(s, 'pixelsize')
if (isnumeric(s.pixelsize) && s.pixelsize > 0)
obj.pixelSize = s.pixelsize;
else
msg = sprintf(['Expecting a positive numeric type for pixelSize.\n', ...
'Setting pixelSize property to default %.2f'], obj.pixelSize);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'numapt')
if (isnumeric(s.numapt) && s.numapt > 0)
obj.numApt = s.numapt;
else
msg = sprintf(['Expecting a positive numeric type for numApt.\n', ...
'Setting numApt property to default %.2f'], obj.numApt);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'emisswavelength')
if (isnumeric(s.emisswavelength) && s.emisswavelength > 0)
obj.emissWavelength = s.emisswavelength;
else
msg = sprintf(['Expecting a positive numeric type for emissWavelength.\n', ...
'Setting emissWavelength property to default %.2f'], obj.emissWavelength);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'offset')
if (isnumeric(s.offset) && all(s.offset(:)) > 0)
obj.offset = s.offset;
else
msg = sprintf(['Expecting a positive numeric type for offset.\n', ...
'Setting offset property to default %.2f'], obj.offset);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'adcount')
if (isnumeric(s.adcount) && s.adcount > 0)
obj.ADcount = s.adcount;
else
msg = sprintf(['Expecting a positive numeric type for ADcount.\n', ...
'Setting ADcount property to default %.2f'], obj.ADcount);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'refractiveindx')
if (isnumeric(s.refractiveindx) && s.refractiveindx > 0)
obj.refractiveIndx = s.refractiveindx;
else
msg = sprintf(['Expecting a positive numeric type for refractiveIndx.\n', ...
'Setting refractiveIndx property to default %.2f'], obj.refractiveIndx);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'refractiveindxsam')
if (isnumeric(s.refractiveindxsam) && s.refractiveindxsam > 0)
obj.refractiveIndxSam = s.refractiveindxsam;
else
msg = sprintf(['Expecting a positive numeric type for refractiveIndxSam.\n', ...
'Setting refractiveIndxSam property to default %.2f'], obj.refractiveIndxSam);
warning('Nanoscope:InconsistentInputType', msg)
end
end
if isfield(s, 'imagesize')
if (isnumeric(s.imagesize) && s.imagesize > 0)
obj.imageSize = s.imagesize;
else
msg = sprintf(['Expecting a positive numeric type for imageSize.\n', ...
'Setting imageSize property to default %.2f'], obj.imageSize);
warning('Nanoscope:InconsistentInputType', msg)
end
end
%display imaging parameters
%--------------------------------------------------
ImagingInfo = sprintf(['pixel size: %.2f\n', 'numerical aperture: %.2f\n', ...
'emission wavelength: %.2f\n', 'offset: %.2f\n', ...
'AD count: %.2f\n', 'medium refractive index: %.2f\n', ...
'sample refractive index: %.2f\n', 'image size: %.2f\n'], ...
obj.pixelSize, obj.numApt, obj.emissWavelength, ...
obj.offset, obj.ADcount, obj.refractiveIndx, obj.refractiveIndxSam, obj.imageSize);
fprintf('%s\n', repmat('=', 1, 20));
fprintf('Imaging parameters\n');
fprintf('%s\n', repmat('=', 1, 20));
disp(ImagingInfo)
%=========================
%initialize phase mask parameters based on input options
%=========================
if isfield(s, 'phasemaskpara')
% if isfield(s.phasemaskpara,'maskname')
% if (ischar(s.phasemaskpara.maskname))
% obj.phaseMaskPara.maskName=s.phasemaskpara.maskname;
% else
% msg=sprintf(['Expecting a character array type for maskName.\n',...
% 'Setting pupilRadius property to default %s'],obj.phaseMaskPara.maskName);
% warning('Nanoscope:phaseMaskPara:InconsistentInputType',msg)
% end
% end
% if isfield(s.phasemaskpara,'pupilradius')
% if (isnumeric(s.phasemaskpara.pupilradius)&&s.phasemaskpara.pupilradius>0)
% obj.phaseMaskPara.pupilRadius=s.phasemaskpara.pupilradius;
% else
% msg=sprintf(['Expecting a positive numeric type for pupilRadius.\n',...
% 'Setting pupilRadius property to default %.2f', obj.phaseMaskPara.pupilRadius]);
% warning('Nanoscope:phaseMaskPara:InconsistentInputType',msg)
% end
% end
% if isfield(s.phasemaskpara,'x_shift_center_position')
% if (isnumeric(s.phasemaskpara.x_shift_center_position))
% obj.phaseMaskPara.x_shift_center_position=s.phasemaskpara.x_shift_center_position;
% else
% msg=sprintf(['Expecting a numeric type for x_shift_center_position.\n',...
% 'Setting x_shift_center_position property to default %.2f', obj.phaseMaskPara.x_shift_center_position]);
% warning('Nanoscope:phaseMaskPara:InconsistentInputType',msg)
% end
% end
% if isfield(s.phasemaskpara,'y_shift_center_position')
% if (isnumeric(s.phasemaskpara.y_shift_center_position))
% obj.phaseMaskPara.y_shift_center_position=s.phasemaskpara.y_shift_center_position;
% else
% msg=sprintf(['Expecting a numeric type for y_shift_center_position.\n',...
% 'Setting y_shift_center_position property to default %.2f', obj.phaseMaskPara.y_shift_center_position]);
% warning('Nanoscope:phaseMaskPara:InconsistentInputType',msg)
% end
% end
%
% if isfield(s.phasemaskpara,'maskrotation')
% if (isnumeric(s.phasemaskpara.maskrotation))
% obj.phaseMaskPara.maskRotation=s.phasemaskpara.maskrotation;
% else
% msg=sprintf(['Expecting a numeric type for maskRotation.\n',...
% 'Setting maskRotation property to default %.2f', obj.phaseMaskPara.maskRotation]);
% warning('Nanoscope:phaseMaskPara:InconsistentInputType',msg)
% end
% end
% end
obj.phaseMaskPara = s.phasemaskpara;
end
%display phase mask parameters
%--------------------------------------------------
phaseMaskInfo = sprintf(['mask name: %s\n', 'pupil radius: %.2f\n'], ...
obj.phaseMaskPara.maskName, obj.phaseMaskPara.pupilRadius);
fprintf('%s\n', repmat('=', 1, 20));
fprintf('Phase mask parameters\n');
fprintf('%s\n', repmat('=', 1, 20));
disp(phaseMaskInfo)
%=========================
%phase mask initialization
% apply zero order term
if isfield(s, 'phasemaskpara') && isfield(s.phasemaskpara, 'zeroorder')
obj.phaseMask = Nanoscope.mountPhaseMask(obj, 'zeroorder', ...
s.phasemaskpara.zeroorder);
elseif isfield(s, 'phasemaskpara') && isfield(s.phasemaskpara, 'zeroorder') && isfield(s.phasemaskpara, 'amplitudemask')
obj.phaseMask = Nanoscope.mountPhaseMask(obj, 'zeroorder', ...
s.phasemaskpara.zeroorder, 'amplitudemask', s.phasemaskpara.amplitudemask);
elseif isfield(s, 'phasemaskpara') && isfield(s.phasemaskpara, 'amplitudemask')
obj.phaseMask = Nanoscope.mountPhaseMask(obj, 'amplitudemask', ...
s.phasemaskpara.amplitudemask);
else
obj.phaseMask = Nanoscope.mountPhaseMask(obj);
end
%brightness scaling
obj.brightnessScaling = obj.brightnessScalingCompute();
%x_channel initialization
obj.XXxBasis = obj.computeBasis(obj, 'XX' ...
, true, 'x_channel', true, 'crop', true);
obj.YYxBasis = obj.computeBasis(obj, 'YY' ...
, true, 'x_channel', true, 'crop', true);
obj.ZZxBasis = obj.computeBasis(obj, 'ZZ' ...
, true, 'x_channel', true, 'crop', true);
obj.XYxBasis = obj.computeBasis(obj, 'XY' ...
, true, 'x_channel', true, 'crop', true);
obj.XZxBasis = obj.computeBasis(obj, 'XZ' ...
, true, 'x_channel', true, 'crop', true);
obj.YZxBasis = obj.computeBasis(obj, 'YZ' ...
, true, 'x_channel', true, 'crop', true);
%y_channel initialization
obj.XXyBasis = obj.computeBasis(obj, 'XX' ...
, true, 'y_channel', true, 'crop', true);
obj.YYyBasis = obj.computeBasis(obj, 'YY' ...
, true, 'y_channel', true, 'crop', true);
obj.ZZyBasis = obj.computeBasis(obj, 'ZZ' ...
, true, 'y_channel', true, 'crop', true);
obj.XYyBasis = obj.computeBasis(obj, 'XY' ...
, true, 'y_channel', true, 'crop', true);
obj.XZyBasis = obj.computeBasis(obj, 'XZ' ...
, true, 'y_channel', true, 'crop', true);
obj.YZyBasis = obj.computeBasis(obj, 'YZ' ...
, true, 'y_channel', true, 'crop', true);
end
% properties set methods
%---------------------------------------------------------
function obj = set.pixelSize(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for pixel size')
end
if ~(val > 0)
error('Nanoscope:InconsistentInputValue', 'Expecting a positive value for pixel size')
end
obj.pixelSize = val;
end
end
function obj = set.numApt(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for numerical aperture')
end
if ~(val > 0)
error('Nanoscope:InconsistentInputValue', 'Expecting a positive value for numerical aperture')
end
obj.numApt = val;
end
end
function obj = set.emissWavelength(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for emission wavelength')
end
if ~(val > 0)
error('Nanoscope:InconsistentInputValue', 'Expecting a positive value for emission wavelength')
end
obj.emissWavelength = val;
end
end
function obj = set.refractiveIndx(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for refractive index')
end
if ~(val > 0)
error('Nanoscope:InconsistentInputValue', 'Expecting a positive value for refractive index')
end
obj.refractiveIndx = val;
end
end
function obj = set.refractiveIndxSam(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for refractive index of sample')
end
if ~(val > 0)
error('Nanoscope:InconsistentInputValue', 'Expecting a positive value for refractive index of sample')
end
obj.refractiveIndxSam = val;
end
end
function obj = set.ADcount(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for A/D count')
end
if ~(val > 0)
error('Nanoscope:InconsistentInputValue', 'Expecting a positive value for A/D count')
end
obj.ADcount = val;
end
end
function obj = set.offset(obj, val)
if nargin > 0
if ~isnumeric(val)
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for offset')
end
if any(val < 0)
error('Nanoscope:InconsistentInputValue', 'Expecting positive values for offset')
end
obj.offset = val;
end
end
%---------------------------------------------------------
function obj = set.phaseMaskPara(obj, s)
if nargin > 0
s_t = struct();
if ~isstruct(s)
error('Nanoscope:InconsistentInputType', 'Expecting a structure type as input')
end
if isfield(s, 'maskname')
if ~(ischar(s.maskname))
error('Nanoscope:InconsistentInputType', 'Expecting a character array for maskName')
end
s_t.maskName = s.maskname;
else
s_t.maskName = obj.phaseMaskPara.maskName;
end
if isfield(s, 'pupilradius')
if ~((isnumeric(s.pupilradius) && (s.pupilradius > 0)))
error('Nanoscope:InconsistentInputType', 'Expecting a positive number for pupilRadius')
end
s_t.pupilRadius = s.pupilradius;
else
s_t.pupilRadius = obj.phaseMaskPara.pupilRadius;
end
if isfield(s, 'x_shift_center_position')
if ~(isnumeric(s.x_shift_center_position))
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for x_shift_center_position')
end
s_t.x_shift_center_position = s.x_shift_center_position;
else
s_t.x_shift_center_position = obj.phaseMaskPara.x_shift_center_position;
end
if isfield(s, 'y_shift_center_position')
if ~(isnumeric(s.y_shift_center_position))
error('Nanoscope:InconsistentInputType', 'Expecting a numeric type for y_shift_center_position')
end
s_t.y_shift_center_position = s.y_shift_center_position;
else
s_t.y_shift_center_position = obj.phaseMaskPara.y_shift_center_position;
end
if isfield(s, 'maskrotation')
if ~(floor(s.maskrotation) == s.maskrotation)
error('Nanoscope:InconsistentInputType', 'Expecting a positive integer for maskRotation')
end
s_t.maskRotation = s.maskrotation;
else
s_t.maskRotation = obj.phaseMaskPara.maskRotation;
end
end
obj.phaseMaskPara = struct(s_t);
end
end
%% methods for properties initialization
%--------------------------------------------------------
%%
methods (Access = protected)
function brightnessScaling = brightnessScalingCompute(obj)
%brightnessScalingCompute computes the brightness scaling for
%normalizing each basis (i.e., XX, YY , etc).
%brightnessScaling corresponds to the YYy basis image formed on the camera for a
%dipole with \theta=pi/2 and \phi=pi/2
%set Emitter properties to match YYy channel
Emitter.polar_para.phiD = pi / 2;
Emitter.polar_para.thetaD = pi / 2;
Emitter.position_para.x = 0;
Emitter.position_para.y = 0;
Emitter.position_para.z = 0;
% [~,brightnessScaling]=obj.simDipole_novotny(obj,Emitter);
[brightnessScalingX, brightnessScalingY] = obj.simDipole_novotny(obj, Emitter); % 190717 TD
brightnessScaling = brightnessScalingX + brightnessScalingY;
end
%%
function B = computeBasis(obj, varargin)
%computeBasis computes the bases images (i.e., XX, YY, etc) corresponding to a
%dipole.
%get options
opt = varargin(2:end);
s = opt2struct(opt);
Emitter.position_para.x = 0;
Emitter.position_para.y = 0;
Emitter.position_para.z = 0;
simDipole_novotny_h = @(Emitter)obj.simDipole_novotny(obj, Emitter);
if isfield(s, 'xx') || isfield(s, 'xy') || isfield(s, 'xz')
% XX
Emitter.polar_para.phiD = 0;
Emitter.polar_para.thetaD = pi / 2;
[BXXx, BXXy] = simDipole_novotny_h(Emitter);
if isfield(s, 'x_channel') && isfield(s, 'xx')
B = BXXx;
else
B = BXXy;
end
end
% YY
Emitter.polar_para.phiD = pi / 2;
Emitter.polar_para.thetaD = pi / 2;
if isfield(s, 'yy') || isfield(s, 'xy') || isfield(s, 'yz')
[BYYx, BYYy] = simDipole_novotny_h(Emitter);
if isfield(s, 'x_channel') && isfield(s, 'yy')
B = BYYx;
else
B = BYYy;
end
end
% ZZ
% Emitter.polar_para.phiD=pi/2;
Emitter.polar_para.phiD = 0;
Emitter.polar_para.thetaD = 0;
if isfield(s, 'zz') || isfield(s, 'xz') || isfield(s, 'yz')
[BZZx, BZZy] = simDipole_novotny_h(Emitter);
if isfield(s, 'x_channel') && isfield(s, 'zz')
B = BZZx;
else
B = BZZy;
end
end
% XY
Emitter.polar_para.phiD = pi / 4;
Emitter.polar_para.thetaD = pi / 2;
if isfield(s, 'xy')
[BXYxt, BXYyt] = simDipole_novotny_h(Emitter);
if isfield(s, 'x_channel')
B = 2 * BXYxt - BXXx - BYYx;
else
B = 2 * BXYyt - BXXy - BYYy;
end
end
% XZ
Emitter.polar_para.phiD = 0;
Emitter.polar_para.thetaD = pi / 4;
if isfield(s, 'xz')
[BXZxt, BXZyt] = simDipole_novotny_h(Emitter);
if isfield(s, 'x_channel')
B = 2 * BXZxt - BXXx - BZZx;
else
B = 2 * BXZyt - BXXy - BZZy;
end
end
% YZ
Emitter.polar_para.phiD = pi / 2;
Emitter.polar_para.thetaD = pi / 4;
if isfield(s, 'yz')
[BYZxt, BYZyt] = simDipole_novotny_h(Emitter);
if isfield(s, 'x_channel')
B = 2 * BYZxt - BYYx - BZZx;
else
B = 2 * BYZyt - BYYy - BZZy;
end
end
% crop the basis images to match the desired image size
%--------------------------------------------------------
%accounting for photon loss
brightness_scaling = obj.brightnessScaling;
N_pupil = size(B, 1);
up_sample = obj.pixelUpsample;
img_size = obj.imageSize;
%handle for corping region of interest
roi = @(img)img(-up_sample*(img_size - 1)/2+N_pupil/2+1:1:up_sample*(img_size - 1)/2+N_pupil/2+1, ... .
-up_sample*(img_size - 1)/2+N_pupil/2+1:1:up_sample*(img_size - 1)/2+N_pupil/2+1, :);
if isfield(s, 'crop') && s.crop
sumnorm = sum(sum(roi(brightness_scaling)));
B = roi(B) / sumnorm;
else
sumnorm = sum(sum(brightness_scaling));
B = B / sumnorm;
end
end
end
%%
methods (Static)
%---------------------------------------------------------
function phaseMask_out = mountPhaseMask(obj, varargin)
s = opt2struct(varargin);
rho_max = obj.phaseMaskPara.pupilRadius;
xShift = obj.phaseMaskPara.x_shift_center_position;
yShift = obj.phaseMaskPara.y_shift_center_position;
maskRot = obj.phaseMaskPara.maskRotation;
maskName = obj.phaseMaskPara.maskName;
maskSize = round((obj.pixelSize)^-1*(obj.emissWavelength * rho_max)/obj.numApt);
if mod(maskSize, 2) ~= 0
maskSize = maskSize + 1;
end
pmask = imread([maskName, '.bmp']);
bias = double(pmask(1, 1));
pmask = double(pmask) - bias;
pmaskSize = size(pmask);
pmask = pmask / bias * pi;
%Pick up the aperture size of the mask
mask_nonZeroStartV = find(pmask(:, pmaskSize(2) / 2) ~= 0, 1, 'first');
mask_nonZeroEndV = find(flipud(pmask(:, pmaskSize(2) / 2)) ~= 0, 1, 'first');
mask_nonZeroStartH = find(pmask(pmaskSize(1) / 2, :) ~= 0, 1, 'first');
mask_nonZeroEndH = find(flipud(pmask(pmaskSize(2) / 2, :)) ~= 0, 1, 'first');
mask_aper = pmaskSize(1) - min((mask_nonZeroStartV-1)+(mask_nonZeroEndV - 1), ...
(mask_nonZeroStartH - 1)+(mask_nonZeroEndH - 1));
scaleFactor = (rho_max + 1) * 2 / mask_aper; % 190814 TD for solving mismatch of BFP size with vectorial forward model
% scaleFactor = rho_max*2 / mask_aper;
newMaskSize = ceil(pmaskSize(1)*scaleFactor);
if mod(newMaskSize, 2) ~= 0
newMaskSize = newMaskSize + 1;
% set the new mask size as even number
end
%adjust the phase mask size using nearest neighbor interpolation
MaskResized = imresize(pmask, [newMaskSize, NaN], 'nearest');
%amplitude modulation
if isfield(s, 'amplitudemask')
nameAmpMask = s.amplitudemask;
amplitude_mask_struct = load(fullfile('phasemask', nameAmpMask));
amplitude_mask = imresize(amplitude_mask_struct.xAmpMask_padded, [newMaskSize, NaN], 'nearest');
else
amplitude_mask = ones(size(MaskResized));
end
if maskRot ~= 0
MaskResized = rot90(MaskResized, maskRot);
amplitude_mask = rot90(amplitude_mask, maskRot);
end
if xShift > 0
MaskResized = [zeros(size(MaskResized, 1), 2 * xShift), MaskResized];
amplitude_mask = [zeros(size(amplitude_mask, 1), 2 * xShift), amplitude_mask];
elseif xShift < 0
MaskResized = [MaskResized, zeros(size(MaskResized, 1), 2 * -xShift)];
amplitude_mask = [amplitude_mask, zeros(size(amplitude_mask, 1), 2 * -xShift)];
end
if yShift > 0
MaskResized = [zeros(2 * yShift, size(MaskResized, 2)); MaskResized];
amplitude_mask = [zeros(2 * yShift, size(amplitude_mask, 2)); amplitude_mask];
elseif yShift < 0
MaskResized = [MaskResized; zeros(2 * -yShift, size(MaskResized, 2))];
amplitude_mask = [amplitude_mask; zeros(2 * -yShift, size(amplitude_mask, 2))];
end
if size(MaskResized, 1) < maskSize
MaskResized = [zeros(floor((maskSize-size(MaskResized, 1)) / 2), size(MaskResized, 2)); ...
MaskResized; zeros(floor((maskSize-size(MaskResized, 1)) / 2), size(MaskResized, 2))];
amplitude_mask = [zeros(floor((maskSize-size(amplitude_mask, 1)) / 2), size(amplitude_mask, 2)); ...
amplitude_mask; zeros(floor((maskSize-size(amplitude_mask, 1)) / 2), size(amplitude_mask, 2))];
end
if size(MaskResized, 2) < maskSize
MaskResized = [zeros(size(MaskResized, 1), floor((maskSize-size(MaskResized, 2)) / 2)), ...
MaskResized, zeros(size(MaskResized, 1), floor((maskSize-size(MaskResized, 2)) / 2))];
amplitude_mask = [zeros(size(amplitude_mask, 1), floor((maskSize-size(amplitude_mask, 2)) / 2)), ...
amplitude_mask, zeros(size(amplitude_mask, 1), floor((maskSize-size(amplitude_mask, 2)) / 2))];
end
maskNew = MaskResized(size(MaskResized, 1)/2-floor(maskSize / 2)+1:size(MaskResized, 1)/2+floor(maskSize / 2), ...
size(MaskResized, 2)/2-floor(maskSize / 2)+1:size(MaskResized, 2)/2+floor(maskSize / 2));
amplitude_maskNew = amplitude_mask(size(amplitude_mask, 1)/2-floor(maskSize / 2)+1:size(amplitude_mask, 1)/2+floor(maskSize / 2), ...
size(amplitude_mask, 2)/2-floor(maskSize / 2)+1:size(amplitude_mask, 2)/2+floor(maskSize / 2));
%the initil amplitude was inverse of the true amplitude!
phaseMask = (amplitude_maskNew) .* exp(1i*maskNew);
if isfield(s, 'zeroorder')
phaseMask_x = (1 - s.zeroorder(1)) * phaseMask + s.zeroorder(1) * abs(phaseMask);
phaseMask_y = (1 - s.zeroorder(2)) * phaseMask + s.zeroorder(2) * abs(phaseMask);
else
phaseMask_x = phaseMask;
phaseMask_y = phaseMask;
end
phaseMask_out(:, :, 1) = phaseMask_x;
phaseMask_out(:, :, 2) = phaseMask_y;
end
end
%% methods for image formation
%----------------------------------------------------
methods (Static)
%%
function [imgx, imgy, Ex, Ey] = simDipole_novotny(Nanoscope, Emitter, varargin)
%simDipole_novotny computes
s = opt2struct(varargin);
% get position parameters
%--------------------------------------------------------
fileds = {'z', 'x', 'y'};
position_para = Emitter.position_para; % in (nm)
checkFileds(position_para, fileds); %validate fields
z = position_para.z;
deltax = position_para.x;
deltay = position_para.y;
% get molecular orientation parameters
%--------------------------------------------------------
fileds = {'phiD', 'thetaD'};
polar_para = Emitter.polar_para;
checkFileds(polar_para, fileds); %validate fields
phiD = polar_para.phiD;
thetaD = polar_para.thetaD;
% get emitter and imaging parameters
%--------------------------------------------------------
n1 = Nanoscope.refractiveIndx;
zh = 0; % thickness of film
z2 = 0; % distance from the emitter to the interface
n2 = 1.33; % sample refractive index
nh = n1; % thin film refractive index
lambda = Nanoscope.emissWavelength; %wavelength (nm)
NA = Nanoscope.numApt; %numerical aperture
pmask = Nanoscope.phaseMask;
N = size(pmask, 1);
pixel_size = Nanoscope.pixelSize; %object pixel size (nm)
if isfield(s, 'upsampling')
upsampling = s.upsampling;
else
upsampling = 1; %upsampling factor of image space
end
molecule_num = numel(deltax);
%calculate both pupil and image plane sampling,
%one will affect the other, so make sure not to introduce aliasing
dx = n1 * (pixel_size / (upsampling)); % in (nm) due to Abbe sine...
% condition, scale by imaging medium r.i.
dv = 1 / (N * dx); %pupil sampling, related to image plane by FFT
% define pupil coordinates
%--------------------------------------------------------
[eta, xi] = meshgrid(((-1 / (2 * dx)) + (1 / (2 * N * dx))):dv:(-(1 / (2 * N * dx)) ...
+(1 / (2 * dx))), ((-1 / (2 * dx)) + (1 / (2 * N * dx))):dv:(-(1 / (N * 2 * dx)) + (1 / (2 * dx))));
x = lambda * (eta);
y = lambda * (xi);
[phi, rho] = cart2pol(x, y);
rho_max = NA / n1; %pupil region of support determined by NA and imaging medium r.i.
k1 = n1 * (2 * pi / lambda);
kh = nh * (2 * pi / lambda);
k2 = n2 * (2 * pi / lambda);
theta1 = asin(rho); %theta in matched medium
thetah = asin((n1/nh)*sin(theta1)); %theta in thin film
theta2 = asin((n1/n2)*sin(theta1)); %theta in mismatched medium
% cache fixed terms
costheta2 = cos(theta2);
costhetah = cos(thetah);
costheta1 = cos(theta1);
sintheta1 = sin(theta1);
cosphi = cos(phi);
sinphi = sin(phi);
% Fresnel coefficients
%--------------------------------------------------------
tp_2h = 2 * n2 * costheta2 ./ (n2 * costhetah + nh * costheta2);
ts_2h = 2 * n2 * costheta2 ./ (nh * costhetah + n2 * costheta2);
tp_h1 = 2 * nh * costhetah ./ (nh * costheta1 + n1 * costhetah);
ts_h1 = 2 * nh * costhetah ./ (n1 * costheta1 + nh * costhetah);
rp_2h = (n2 * costheta2 - nh * costhetah) ./ (n2 * costheta2 + nh * costhetah);
rs_2h = (nh * costheta2 - n2 * costhetah) ./ (nh * costheta2 + n2 * costhetah);
rp_h1 = (nh * costhetah - n1 * costheta1) ./ (nh * costhetah + n1 * costheta1);
rs_h1 = (n1 * costhetah - nh * costheta1) ./ (n1 * costhetah + nh * costheta1);
% Axelrod's equations for E-fields at pupil plane
%--------------------------------------------------------
mux = reshape(sin(thetaD).*cos(phiD), 1, 1, molecule_num);
muy = reshape(sin(thetaD).*sin(phiD), 1, 1, molecule_num);
muz = reshape(cos(thetaD), 1, 1, molecule_num);
tp = tp_2h .* tp_h1 .* exp(1i*kh*costhetah*zh) ./ (1 + rp_2h .* rp_h1 .* exp(2i * kh * zh * costhetah));
ts = ts_2h .* ts_h1 .* exp(1i*kh*costhetah*zh) ./ (1 + rs_2h .* rs_h1 .* exp(2i * kh * zh * costhetah));
Es = bsxfun(@times, ts.*(costheta1 ./ costheta2).*(n1 / n2), ...
(bsxfun(@times, muy, cosphi) - bsxfun(@times, mux, sinphi)));
Ep = bsxfun(@times, tp, bsxfun(@times, (n1 / n2) .* costheta1, (bsxfun(@times, mux, cosphi) + ...
bsxfun(@times, muy, sinphi)))- ...
bsxfun(@times, bsxfun(@times, muz, sintheta1), (n1 / n2)^2 .* (costheta1 ./ costheta2)));
PupilFilt = (rho < rho_max) .* 1; %casting to numeric
%computing E-fields
%--------------------------------------------------------
E_common = PupilFilt .* (1 ./ sqrt(costheta1)) .* exp(1i*kh*zh*costhetah) .* ...
exp(1i*k2*z2*costheta2);
Ey_common = bsxfun(@times, E_common, ...
(bsxfun(@times, cosphi, Es) + bsxfun(@times, sinphi, Ep)));
Ex_common = bsxfun(@times, E_common, ...
(bsxfun(@times, cosphi, Ep) + bsxfun(@times, -sinphi, Es)));
Ey_1 = exp(1i*k1*(bsxfun(@times, reshape(z, 1, 1, molecule_num), costheta1))); %defocus term at pupil plane
Ex_1 = exp(1i*k1*(bsxfun(@times, reshape(z, 1, 1, molecule_num), costheta1))); %defocus term at pupil plane
% aplly channel mismatch
Ex_2 = exp(1i*k1*(bsxfun(@times, reshape(deltax, 1, 1, molecule_num), sintheta1 .* cosphi))); %phase shift
Ex_3 = exp(1i*k1*(bsxfun(@times, reshape(-deltay, 1, 1, molecule_num), sintheta1 .* sinphi))); %phase shift
if isfield(s, 'channel_mismatch')
deltax = deltax + s.channel_mismatch(1);
deltay = deltay + s.channel_mismatch(2);
end
Ey_2 = exp(1i*k1*(bsxfun(@times, reshape(deltax, 1, 1, molecule_num), sintheta1 .* cosphi))); %phase shift
Ey_3 = exp(1i*k1*(bsxfun(@times, reshape(-deltay, 1, 1, molecule_num), sintheta1 .* sinphi))); %phase shift
Ey_t = Ey_1 .* Ey_2 .* Ey_3;
Ex_t = Ex_1 .* Ex_2 .* Ex_3;
Ex = bsxfun(@times, Ex_t, Ex_common);
Ey = bsxfun(@times, Ey_t, Ey_common);
% for propagation from pupil plane E-field to image plane via tube-lens, paraxial
% approximation is in force.
%--------------------------------------------------------
%
% pmask_x=pmask(:,:,1);
% pmask_y=pmask(:,:,2);
% pmaskRot_y=rot90(pmask_y,2);
% pmaskRot_x=rot90(pmask_x,1);
%
% imgy = fftshift(fft2(ifftshift(Ey.*repmat((pmaskRot_y),1,1,molecule_num))));
% imgx= fliplr((fftshift(fft2(ifftshift(Ex.*repmat((pmaskRot_x),1,1,molecule_num))))));
% 1- rotate electric field in x channel
Ex_rotated = rot90(Ex);
% 2- flip the electric field in y channel
Ey_flipped = fliplr(Ey);
pmask_x = pmask(:, :, 1);
pmask_y = pmask(:, :, 2);
imgy = flipud(fftshift(fft2(ifftshift(Ey_flipped .* repmat(pmask_y, 1, 1, molecule_num)))));
imgx = rot90(fftshift(fft2(ifftshift(Ex_rotated .* repmat(pmask_x, 1, 1, molecule_num))))', 2);