-
Notifications
You must be signed in to change notification settings - Fork 28
/
shackHartmann.m
executable file
·1458 lines (1276 loc) · 59.8 KB
/
shackHartmann.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
classdef shackHartmann < hgsetget
% SHACKHARTMANN Create a Shack-Hartmann object
%
% obj = shackHartmann(nLenslet,detectorResolution) creates a
% Shack-Hartmann object with a (nLenslet X nLenslet) lenslet array and
% a detector with a detectorResolution resolution
%
% obj = shackHartmann(nLenslet,detectorResolution,validLenslet) creates
% a Shack-Hartmann object with a (nLenslet X nLenslet) lenslet array, a
% detector with a detectorResolution resolution and a logical mask of
% size nLenslet setting the location of the valid lenslets inside the
% lenslet array
%
% See also: lensletArray, detector, source, lensletArrayHowto,
% detectorHowto
properties
% lenslet array object
lenslets;
% detector object
camera;
% camera flat field
flatField = 0;
% camera pixel gains
pixelGains = 1;
% use quad-cell
quadCell = false;
% use center of gravity
centroiding = true;
% use matched filter
matchedFilter = false;
% use correlation
correlation = false;
% centroiding mask
centroidingMask = 1;
% timer
paceMaker;
% slopes display handle
slopesDisplayHandle;
% slope listener
slopesListener;
% intensity display handle
intensityDisplayHandle;
% intensity listener
intensityListener;
% frame pixel threshold
framePixelThreshold = -inf;
% slopes units (default:1 is pixel)
slopesUnits = 1;
% wavefront units (default:1 is slopes pixel!)
wavefrontUnits = 1;
% zernike to slopes conversion matrix
zern2slopes;
% wavefront sensor tag
tag = 'SHACK-HARTMANN';
% if true the mean of the slopes are removed
rmMeanSlopes = false;
% mean slopes
meanSlopes;
% handle to the function processing the lenslet intensity
intensityFunction = @sum;
% inverse of the sparse gradient matrix
iG;
% finiteDifferenceWavefront listener
finiteDifferenceWavefrontListener
% finiteDifferenceWavefront handle
finiteDifferenceWavefrontHandle
% zernCoefs listener
zernCoefsListener
% zernCoefs handle
zernCoefsHandle
end
properties (SetAccess=private)
directionVector;
end
properties (SetObservable=true)
% measurements
slopes=0;
end
properties (Dependent)
% valid lenslet mask
validLenslet;
% measurements reference
referenceSlopes;
% pointing direction [zenith,azimuth]
pointingDirection;
end
properties (Dependent, SetAccess=private)
% number of valid lenslet
nValidLenslet;
% number of slopes
nSlope;
% intensity in each lenslet
lensletIntensity;
% valid actuatord
validActuator;
% zernike coefficients
zernCoefs;
% X slopes map
xSlopesMap
% Y slopes map
ySlopesMap
% wavefront estimate from finite difference in wavelength unit
finiteDifferenceWavefront
end
properties (Access=protected)
% p_slopes;
p_referenceSlopes=0;
p_validLenslet;
p_pointingDirection;
% index array to reshape a detector frame into a matrix with one
% raster imagelet per column
indexRasterLenslet = NaN;
% lenslet centers
lensletCenterX;
lensletCenterY;
log;
quadCellX = [1 ; 1 ; -1 ; -1];
quadCellY = [1 ; -1 ; 1 ; -1];
meanProjection;
spotTrail = zeros(2,10);
p_finiteDifferenceWavefront
end
methods
%% Constructor
function obj = shackHartmann(nLenslet,detectorResolution,minLightRatio)
if nargin>1
narginchk(1, 4)
obj.lenslets = lensletArray(nLenslet);
obj.camera = detector(detectorResolution);
if detectorResolution==2
obj.quadCell = true;
obj.centroiding = false;
end
obj.lenslets.nLensletWavePx = ...
detectorResolution/nLenslet;
if nargin>2
obj.lenslets.minLightRatio = minLightRatio;
else
obj.lenslets.minLightRatio = 0;
end
obj.validLenslet = true(nLenslet);
obj.camera.frameGrabber ...
= obj.lenslets;
obj.referenceSlopes = zeros(obj.nValidLenslet*2,1);
obj.p_referenceSlopes = ...
repmat(obj.p_referenceSlopes,obj.lenslets.nArray,1);
% % intensity listener (BROKEN: shackhartmann is not deleted after a clear)
obj.intensityListener = addlistener(obj.camera,'frame','PostSet',...
@(src,evnt) intensityDisplay(obj) );
obj.intensityListener.Enabled = false;
obj.finiteDifferenceWavefrontListener = addlistener(obj,...
'slopes','PostSet',...
@(src,evnt) wavefrontDisplay(obj) );
obj.finiteDifferenceWavefrontListener.Enabled = false;
obj.zernCoefsListener = addlistener(obj,...
'slopes','PostSet',...
@(src,evnt) bar(obj) );
obj.zernCoefsListener.Enabled = false;
% Timer settings
obj.paceMaker = timer;
obj.paceMaker.name = 'Shack-Hartmann Wavefront Sensor';
obj.paceMaker.TimerFcn = {@timerCallBack, obj};%(BROKEN: shackhartmann is not deleted after a clear)
obj.paceMaker.ExecutionMode = 'FixedSpacing';
obj.paceMaker.BusyMode = 'drop';
obj.paceMaker.Period = 3;
obj.paceMaker.ErrorFcn = 'disp('' @detector: frame rate too high!'')';
% function timerCallBack( timerObj, event, a)
% % fprintf(' @detector: %3.2fs\n',timerObj.instantPeriod)
% a.grabAndProcess
% end
display(obj)
obj.log = logBook.checkIn(obj);
end
setSlopesListener(obj)
function timerCallBack( timerObj, event, a)
% fprintf(' @detector: %3.2fs\n',timerObj.instantPeriod)
% a.grabAndProcess
uplus(a)
end
end
%% Destructor
function delete(obj)
if isvalid(obj.slopesListener)
delete(obj.slopesListener)
end
if isvalid(obj.intensityListener)
delete(obj.intensityListener)
end
if isvalid(obj.paceMaker)
if strcmp(obj.paceMaker.Running,'on')
stop(obj.paceMaker)
end
delete(obj.paceMaker)
end
if ishandle(obj.slopesDisplayHandle)
delete(obj.slopesDisplayHandle)
end
if ishandle(obj.intensityDisplayHandle)
delete(obj.intensityDisplayHandle)
end
if isvalid(obj.lenslets)
delete(obj.lenslets)
end
if isvalid(obj.camera)
delete(obj.camera)
end
if ~isempty(obj.log)
checkOut(obj.log,obj);
end
end
function display(obj)
%% DISPLAY Display object information
%
% display(obj) prints information about the Shack-Hartmann
% wavefront sensor object
fprintf('___ %s ___\n',obj.tag)
fprintf(' Shack-Hartmann wavefront sensor: \n . %d lenslets total on the pupil\n . %d pixels per lenslet \n',...
obj.nValidLenslet,obj.camera.resolution(1)/obj.lenslets.nLenslet)
if isinf(obj.framePixelThreshold)
algoProp = ', no thresholding!';
else
algoProp = sprintf(', pixel threshold: %d\n',obj.framePixelThreshold);
end
algo = {'quadCell','centroiding','matchedFilter','correlation'};
algoTF = [obj.quadCell,obj.centroiding,obj.matchedFilter,obj.correlation];
fprintf(' . spot algorithm: %s%s\n',algo{algoTF},algoProp);
fprintf('----------------------------------------------------\n')
display(obj.lenslets)
display(obj.camera)
end
%% Get and Set pointing direction
function pointingDirection = get.pointingDirection(obj)
pointingDirection = obj.p_pointingDirection;
end
function set.pointingDirection(obj,val)
obj.p_pointingDirection = val;
if ~isempty(obj.pointingDirection)
obj.directionVector = [...
tan(obj.p_pointingDirection(1,:)).*cos(obj.p_pointingDirection(2,:));...
tan(obj.p_pointingDirection(1,:)).*sin(obj.p_pointingDirection(2,:));...
ones(1,size(obj.p_pointingDirection,2))];
end
end
function obj = saveobj(obj)
%% SAVEOBJ
delete(obj.slopesListener)
add(obj.log,obj,'Save!')
end
function INIT(obj)
%% INIT WFS initialization
%
% obj.INIT computes the valid lenslet and set the reference
% slopes based on the last measurements
add(obj.log,obj,'Setting the valid lenslet and the reference slopes!')
setValidLenslet(obj);
obj.referenceSlopes = obj.slopes;
end
% %% Get and Set slopes
% function slopes = get.slopes(obj)
% slopes = obj.p_slopes;
% end
% function set.slopes(obj,val)
% obj.p_slopes = val;
% end
%% Get and Set valid lenslets
function validLenslet = get.validLenslet(obj)
validLenslet = obj.p_validLenslet;
end
function set.validLenslet(obj,val)
obj.p_validLenslet = logical(val);
index = ~[obj.p_validLenslet(:);obj.p_validLenslet(:)];
obj.p_referenceSlopes(index) = [];
obj.slopes(index) = [];
obj.meanProjection = [ ...
ones(obj.nValidLenslet,1) zeros(obj.nValidLenslet,1)
zeros(obj.nValidLenslet,1) ones(obj.nValidLenslet,1) ];
end
%% Get number of valid lenslet
function nValidLenslet = get.nValidLenslet(obj)
nValidLenslet = sum(obj.validLenslet(:));
end
%% Get number of slopes
function nSlope = get.nSlope(obj)
nSlope = obj.nValidLenslet*2;
end
%% Get X slopes map
function out = get.xSlopesMap(obj)
out = zeros(obj.lenslets.nLenslet);
out(obj.validLenslet) = obj.slopes(1:end/2);
end
%% Get Y slopes map
function out = get.ySlopesMap(obj)
out = zeros(obj.lenslets.nLenslet);
out(obj.validLenslet) = obj.slopes(1+end/2:end);
end
%% Get valid actuators
function val = get.validActuator(obj)
nElements = 2*obj.lenslets.nLenslet+1; % Linear number of lenslet+actuator
validLensletActuator = zeros(nElements);
index = 2:2:nElements; % Lenslet index
validLensletActuator(index,index) = obj.validLenslet;
for xLenslet = index
for yLenslet = index
if validLensletActuator(xLenslet,yLenslet)==1
xActuatorIndice = [xLenslet-1,xLenslet-1,...
xLenslet+1,xLenslet+1];
yActuatorIndice = [yLenslet-1,yLenslet+1,...
yLenslet+1,yLenslet-1];
validLensletActuator(xActuatorIndice,yActuatorIndice) = 1;
end
end
end
index = 1:2:nElements; % Actuator index
val = logical(validLensletActuator(index,index));
end
%% Get/Set the reference spots and update spots location display if
%% there is one
function val = get.referenceSlopes(obj)
val = obj.p_referenceSlopes;
end
function set.referenceSlopes(obj,val)
obj.slopes = obj.slopes + obj.p_referenceSlopes;
obj.p_referenceSlopes = val;
obj.slopes = obj.slopes - obj.p_referenceSlopes;
% if ishandle(obj.slopesDisplayHandle)
% hc = get(obj.slopesDisplayHandle,'children');
% u = obj.p_referenceSlopes(1:end/2)+obj.lensletCenterX;
% v = obj.p_referenceSlopes(1+end/2:end)+obj.lensletCenterY;
% set(hc(2),'xData',u,'yData',v)
% end
end
%% Get the zernike coeficients
function val = get.zernCoefs(obj)
val = obj.zern2slopes'*obj.slopes;
val = val*obj.wavefrontUnits;
val(1,:) = []; % piston=0 removed
end
%% Computes the intensity in each lenslet
function lensletIntensity = get.lensletIntensity(obj)
if isempty(obj.camera.frame)
lensletIntensity = [];
else
[nPx,mPx] = size(obj.camera.frame);
nLensletArray = obj.lenslets.nArray;
nPxLenslet = nPx/obj.lenslets.nLenslet;
mPxLenslet = mPx/obj.lenslets.nLenslet/nLensletArray;
try
buffer = obj.camera.frame(obj.indexRasterLenslet);
catch ME
fprintf( '@(shackHartmann)> %s\n',ME.identifier)
obj.indexRasterLenslet ...
= utilities.rearrange([nPx,mPx],[nPxLenslet,mPxLenslet]);
v = ~obj.validLenslet(:);
v = repmat(v,nLensletArray,1);
obj.indexRasterLenslet(:,v) = [];
buffer = obj.camera.frame(obj.indexRasterLenslet);
end
lensletIntensity = obj.intensityFunction(buffer);
end
end
%% Computes the finite difference wavefront
function out = get.finiteDifferenceWavefront(obj)
add(obj.log,obj,'Computing the finite differerence wavefront!')
if isempty(obj.iG)
add(obj.log,obj,'Computing the finite differerence wavefront!')
G = sparseGradientMatrix(obj);
modes = speye((obj.lenslets.nLenslet+1)^2);
obj.iG = calibrationVault(full(G),...
modes(:,obj.validActuator),obj.validActuator,...
'noshow',true);
obj.iG.cond = 100;
end
out = obj.iG.M*obj.slopes;
% if size(obj.slopes,2)>1
% out = obj.iG.M*obj.slopes;
% else
% out = zeros( obj.lenslets.nLenslet+1 );
% out(obj.validActuator) = obj.iG.M*obj.slopes;
% end
out = out*obj.wavefrontUnits;
end
function setValidLenslet(obj,pupilIntensity)
%% SETVALIDLENSLET Valid lenslet mask
%
% setValidLenslet(obj,pupilIntensity) sets the mask of valid
% lenslet based on the value of minLightRatio in the lenslets
% object providing the pupil intensity map
if nargin<2
pupilIntensity = obj.lensletIntensity./max(obj.lensletIntensity);
else
n = length(pupilIntensity);
nL = n/obj.lenslets.nLenslet;
pupilIntensity = reshape(pupilIntensity,nL,n*obj.lenslets.nLenslet);
pupilIntensity = sum(pupilIntensity);
pupilIntensity = reshape(pupilIntensity,obj.lenslets.nLenslet,obj.lenslets.nLenslet*nL);
pupilIntensity = reshape(pupilIntensity',nL,obj.lenslets.nLenslet^2);
pupilIntensity = sum(pupilIntensity);
surfPx = nL^2;
pupilIntensity = pupilIntensity/surfPx;
end
obj.validLenslet = logical( ...
reshape( pupilIntensity>=obj.lenslets.minLightRatio , ...
obj.lenslets.nLenslet,obj.lenslets.nLenslet));
% obj.referenceSlopes = zeros(2*obj.nValidLenslet,1);
% obj.p_referenceSlopes = ...
% repmat(obj.p_referenceSlopes,obj.lenslets.nArray,1);
% figure('Name',sprintf('%s valid lenslet',obj.tag)), spy(obj.p_validLenslet)
dataProcessing(obj)
end
function varargout = dataProcessing(obj)
%% DATAPROCESSING Processing a SH-WFS detector frame
%
% dataProcessing(obj) computes the WFS slopes
%
% out = dataProcessing(obj) computes and returns the WFS slopes
[nPx,mPx,nFrame] = size(obj.camera.frame);
nLensletArray = obj.lenslets.nArray;
nPxLenslet = nPx/obj.lenslets.nLenslet;
mPxLenslet = mPx/obj.lenslets.nLenslet/nLensletArray;
% siz(obj.indexRasterLenslet)
% obj.nValidLenslet*nLensletArray*nFrame
% if numel(obj.indexRasterLenslet)~=(nPxLenslet*mPxLenslet*obj.nValidLenslet*nLensletArray*nFrame)
if size(obj.indexRasterLenslet,1)~=(nPxLenslet*mPxLenslet) || ...
size(obj.indexRasterLenslet,2)~=(obj.nValidLenslet*nLensletArray*nFrame)
% try
% % u = obj.indexRasterLenslet;
% % if nFrame>1
% % u = repmat(u,[1,1,nFrame]);
% % end
% buffer = obj.camera.frame(obj.indexRasterLenslet);
% catch ME
fprintf( '@(shackHartmann)> Setting the raster index \n')
% get lenslet index
obj.indexRasterLenslet ...
= utilities.rearrange([nPx,mPx/nLensletArray,nLensletArray*nFrame],[nPxLenslet,mPxLenslet]);
% remove index from non-valid lenslets
v = ~obj.validLenslet(:);
v = repmat(v,nLensletArray,1);
v = repmat(v,nFrame,1);
obj.indexRasterLenslet(:,v) = [];
% u = obj.indexRasterLenslet;
% if nFrame>1
% u = repmat(u,[1,1,nFrame]);
% end
end
% Buffer pre-processing
buffer = obj.camera.frame(obj.indexRasterLenslet);
buffer = (buffer - obj.flatField)./obj.pixelGains;
buffer = bsxfun( @times, obj.centroidingMask(:) , buffer);
% % Thresholding
% if isfinite(obj.framePixelThreshold)
% buffer = buffer - obj.framePixelThreshold;
% buffer(buffer<0) = 0;
% end
% Thresholding
if isfinite(obj.framePixelThreshold)
if numel(obj.framePixelThreshold)>1
% intensity based thresholding
maxIntensity = max(buffer);
threshold = maxIntensity*obj.framePixelThreshold(2);
threshold(threshold<obj.framePixelThreshold(1)) = obj.framePixelThreshold(1);
v = obj.validLenslet(:);
v = repmat(v,nLensletArray,1);
% q = zeros(size(v));
% q(v) = threshold;
% figure,imagesc(reshape(q,obj.lenslets.nLenslet,[]));set(gca,'clim',[min(threshold),max(threshold)])
buffer = bsxfun( @minus , buffer , threshold);
else
% usual thresholding
buffer = buffer - obj.framePixelThreshold;
end
buffer(buffer<0) = 0;
% q = zeros(size(obj.camera.frame));
% q(obj.indexRasterLenslet) = buffer;
% figure,imagesc(q);
end
% Centroiding
if obj.quadCell
massLenslet ...
= sum(buffer)';
xBuffer = buffer'*obj.quadCellX./massLenslet;
yBuffer = buffer'*obj.quadCellY./massLenslet;
xBuffer = reshape(xBuffer,obj.nValidLenslet,nLensletArray*nFrame);
yBuffer = reshape(yBuffer,obj.nValidLenslet,nLensletArray*nFrame);
sBuffer ...
= bsxfun(@minus,[xBuffer ; yBuffer],obj.referenceSlopes).*obj.slopesUnits;
index = isnan(sBuffer);
if any(index(:)) % if all pixels threshold
warning('OOMAO:shackHartmann:dataProcessing',...
'Threshold (%f) is probably too high or simply there is no light on some of the lenslets',obj.framePixelThreshold)
if ~isempty(obj.slopes) && all(size(sBuffer)==size(obj.slopes))
sBuffer(index) = obj.slopes(index);
end
end
% obj.slopes = sBuffer;
elseif obj.centroiding
massLenslet = sum(buffer);
% massLenslet(~index) = [];
% buffer(:,~index) = [];
% size(buffer)
[x,y] = ...
meshgrid((0:(nPxLenslet-1)),(0:(mPxLenslet-1)));
% xyBuffer ...
% = zeros(2*obj.nValidLenslet,1);
xBuffer = bsxfun( @times , buffer , x(:) ) ;
xBuffer = sum( xBuffer ) ./ massLenslet ;
% xBuffer = squeeze((xBuffer));
yBuffer = bsxfun( @times , buffer , y(:) ) ;
yBuffer = sum( yBuffer ) ./ massLenslet ;
% yBuffer = squeeze((yBuffer));
% xyBuffer = squeeze([xBuffer yBuffer]);
% size(xyBuffer)
xBuffer = reshape(xBuffer,obj.nValidLenslet,nLensletArray*nFrame);
yBuffer = reshape(yBuffer,obj.nValidLenslet,nLensletArray*nFrame);
sBuffer = bsxfun(@minus,[xBuffer ; yBuffer],obj.referenceSlopes).*obj.slopesUnits;
index = isnan(sBuffer);
if any(index(:)) % if all pixels threshold
warning('OOMAO:shackHartmann:dataProcessing',...
'Threshold (%f) is probably too high or simply there is no light on some of the lenslets',obj.framePixelThreshold)
if ~isempty(obj.slopes) && all(size(sBuffer)==size(obj.slopes))
sBuffer(index) = obj.slopes(index);
end
end
% obj.slopes = sBuffer;
elseif obj.matchedFilter
elseif obj.correlation
end
if obj.rmMeanSlopes % remove mean slopes
obj.meanSlopes = (obj.meanProjection'*sBuffer)/obj.nValidLenslet;
obj.slopes = sBuffer - obj.meanProjection*obj.meanSlopes;
else
obj.slopes = sBuffer;
end
if nargout>0
varargout{1} = obj.slopes;
end
end
function zern = getZernike(obj,radialOrder)
zern = zernike(1:zernike.nModeFromRadialOrder(radialOrder),...
'resolution',obj.lenslets.nLenslet,...
'pupil',double(obj.validLenslet));
dzdxy = [zern.xDerivative(obj.validLenslet,:);zern.yDerivative(obj.validLenslet,:)];
zern.c = dzdxy\obj.slopes;
% zern.c = dzdxy'*obj.slopes;
end
function varargout = grabAndProcess(obj)
%% GRABANDPROCESS Frame grabbing and processing
%
% grabAndProcess(obj) grabs a frame and computes the slopes
%
% out = grabAndProcess(obj) grabs a frame, computes and returns
% the slopes
warning('OOMAO;shackHartmann:grabAndProcess',...
'DEPRECATED! Instead use the uplus operator (+obj)')
grab(obj.camera)
dataProcessing(obj);
if nargout>0
varargout{1} = obj.slopes;
end
end
function varargout = uplus(obj)
%% UPLUS + Update operator
%
% +obj grabs a frame and computes the slopes
%
% obj = +obj returns the shackHartmann object
grab(obj.camera)
dataProcessing(obj);
if nargout>0
varargout{1} = obj;
end
end
function relay(obj,src,~)
%% RELAY shackhartmann to source relay
%
% relay(obj,src) propagates the source through the
% Shack-Hartmann lenslet array, grab a frame from the detector
% adding noise if any and process the frame to get the
% wavefront slopes
if ~isempty(obj.pointingDirection)
nSrc = length(src);
m_directionVector = obj.directionVector;
if size(m_directionVector,2)<nSrc
m_directionVector = repmat( m_directionVector(:) , 1 , nSrc );
end
for kSrc = 1:nSrc
delta = m_directionVector(:,kSrc) - src(kSrc).directionVector;
if any(delta)
warning('oomao:shackHartmann:relay',...
'WFS not align on source, this induces an additional tip-tilt error!')
D = src(kSrc).opticalPath{1}.D;
nOutWavePx = obj.lenslets.nLensletWavePx*obj.lenslets.fftPad; % Pixel length of the output wave
evenOdd = rem(obj.lenslets.nLensletWavePx,2);
if ~rem(nOutWavePx,2) && evenOdd
nOutWavePx = nOutWavePx + evenOdd;
end
nOutWavePx = max(nOutWavePx,obj.lenslets.nLensletWavePx);
% fprintf(' - new nOutWavePx = %d\n',nOutWavePx);
alpha_p = (src(kSrc).wavelength/D)*obj.lenslets.nLensletWavePx*obj.lenslets.nLenslet/nOutWavePx;
delta = delta/alpha_p;
[u,v] = ndgrid((0:(obj.lenslets.nLensletWavePx*obj.lenslets.nLenslet-1)));
u = u*delta(2)/nOutWavePx;
v = v*delta(1)/nOutWavePx;
src(kSrc).phase = 2*pi*(u+v);
end
end
end
% if isempty(src(1).magnitude)
% obj.camera.photonNoise = false;
% else
% obj.camera.photonNoise = true;
% end
% propagateThrough(obj.lenslets,src)
if nargin<3
relay(obj.lenslets,src)
end
% grabAndProcess(obj)
spotsSrcKernelConvolution(obj,src)
grab(obj.camera)
if obj.camera.frameCount==0
dataProcessing(obj);
else
obj.slopes = zeros(obj.nSlope,1);
end
end
function spotsSrcKernelConvolution(obj,src)
if ~isempty(src(1).extent)
add(obj.log,obj,'Convolution of the spots by source kernel!')
srcExtent = src(1).extent;
picture = obj.lenslets.imagelets;
[nPx,mPx,nPicture] = size(picture);
nPxLenslet = nPx/obj.lenslets.nLenslet;
mPxLenslet = mPx/obj.lenslets.nLenslet/obj.lenslets.nArray;
mPxNPicture = mPx*nPicture;
picture = reshape(picture,nPx,mPxNPicture);
nLensletArray = obj.lenslets.nArray*nPicture;
indexRasterLenslet_ ...
= utilities.rearrange(size(picture),[nPxLenslet,mPxLenslet]);
v = ~obj.validLenslet(:);
v = repmat(v,nLensletArray,1);
indexRasterLenslet_(:,v) = [];
buffer = picture(indexRasterLenslet_);
buffer = reshape(buffer,nPxLenslet,nPxLenslet,[]);
tic
parfor kLenslet=1:size(buffer,3)
buffer(:,:,kLenslet) = conv2(buffer(:,:,kLenslet),srcExtent,'same');
end
toc
picture(indexRasterLenslet_) = buffer;
obj.lenslets.imagelets = reshape( picture , nPx , mPx , nPicture);
end
end
function out = framelets(obj,lensletI,lensletJ,lensletArrayK)
%% FRAMELETS Per lenslet detector frame
%
% out = framelets(obj,lensletI,lensletJ,lensletArrayK) returns
% the detector frame restricted to lenslet (I,J) of array # K
nLenslet = obj.lenslets.nLenslet;
cameraRes = obj.camera.resolution;
% nArray = obj.lenslets.nArray;
if nargin<4
lensletArrayK = 1;
end
nPxLenslet = cameraRes/nLenslet;
u = (1:nPxLenslet(1)) + (lensletI-1)*nPxLenslet(1);
v = (1:nPxLenslet(2)) + (lensletJ-1)*nPxLenslet(2) + (lensletArrayK-1)*cameraRes(1);
out = obj.camera.frame(u,v);
end
function varargout = slopesDisplay(obj,varargin)
%% SLOPESDISPLAY WFS slopes display
%
% slopesDisplay(obj) displays image plot of the slopes
%
% slopesDisplay(obj,'PropertyName',PropertyValue) displays
% image plot of the slopes and set the properties of the
% graphics object quiver
%
% h = slopesDisplay(obj,...) returns the graphics handle
if obj.lenslets.nLenslet>1
nSlopes = size(obj.slopes,2);
slopesMap = zeros(2*obj.lenslets.nLenslet^2,nSlopes);
p = repmat(obj.validLenslet(:),2,nSlopes);
slopesMap(p) = obj.slopes;
slopesMap = reshape(slopesMap,obj.lenslets.nLenslet,[]);
if nSlopes>3 && rem(nSlopes,2)==0
slopesMap = cell2mat(reshape( mat2cell( ...
slopesMap,obj.lenslets.nLenslet,2*obj.lenslets.nLenslet*ones(1,nSlopes)) , 2, []));
end
if ishandle(obj.slopesDisplayHandle)
nm = size(slopesMap);
if ~all(size(get(obj.slopesDisplayHandle(1),'Cdata'))==nm)
set(get(obj.slopesDisplayHandle(1),'parent'),'xlim',0.5+[0 nm(2)],'ylim',0.5+[0 nm(1)])
end
set(obj.slopesDisplayHandle,'CData',slopesMap)
else
obj.slopesDisplayHandle = imagesc(slopesMap,varargin{:});
ax = gca;
pos = get(ax,'position');
axis xy equal tight
ylabel(colorbar('location','EastOutside'),'Pixel')
set(ax,'position',pos)
hu = findobj(gcf,'Type','uimenu','Label','OOMAO');
if isempty(hu)
hu = uimenu('Label','OOMAO');
end
hus = uimenu(hu,'Label','Slopes Listener Off','Callback',@oomaoMenu);
if isvalid(obj.slopesListener) && obj.slopesListener.Enabled
set(hus,'Label','Slopes Listener On')
end
% set(gcf,'WindowButtonMotionFcn',@wbmcb)
end
else
if ishandle(obj.slopesDisplayHandle)
set(obj.slopesDisplayHandle(1),'XData',obj.slopes(1),'YData',obj.slopes(2))
obj.spotTrail = circshift(obj.spotTrail,[0,-1]);
obj.spotTrail(:,end) = obj.slopes;
set(obj.slopesDisplayHandle(2),'XData',obj.spotTrail(1,:),'YData',obj.spotTrail(2,:))
else
obj.slopesDisplayHandle(1) = plot(obj.slopes(1),obj.slopes(2),'+',...
'MarkerEdgeColor','k','MarkerFaceColor',[.49 1 .63],...
'MarkerSize',10,'LineWidth',2);
obj.spotTrail = zeros(2,10);
obj.spotTrail(:,end) = obj.slopes;
obj.slopesDisplayHandle(2) = ...
line(obj.spotTrail(1,:),obj.spotTrail(2,:),'color','r');
% set(gca,'xlim',[-1,1],'ylim',[-1,1])
grid on
axis square
hu = findobj(gcf,'Type','uimenu','Label','OOMAO');
if isempty(hu)
hu = uimenu('Label','OOMAO');
end
hus = uimenu(hu,'Label','Slopes Listener Off','Callback',@oomaoMenu);
if obj.slopesListener.Enabled
set(hus,'Label','Slopes Listener On')
end
end
end
if nargout>0
varargout{1} = obj.slopesDisplayHandle;
end
function oomaoMenu(src,~)
obj.slopesListener.Enabled = ~obj.slopesListener.Enabled;
if obj.slopesListener.Enabled
set(src,'Label','Slopes Listener On')
else
set(src,'Label','Slopes Listener Off')
end
end
% function wbmcb(src,evnt)
% cp = get(get(obj.slopesDisplayHandle,'parent'),'CurrentPoint');
% disp(round([cp(1,1),cp(1,2)]))
% % xdat = [xinit,cp(1,1)];
% % ydat = [yinit,cp(1,2)];
% % set(hl,'XData',xdat,'YData',ydat);drawnow
% end
end
function varargout = intensityDisplay(obj,varargin)
%% INTENSITYDISPLAY WFS lenslet intensity display
%
% intensityDisplay(obj) displays the intensity of the lenslets
%
% intensityDisplay(obj,'PropertyName',PropertyValue) displays
% the intensity of the lenslets and set the properties of the
% graphics object imagesc
%
% h = intensityDisplay(obj,...) returns the graphics handle
%
% See also: imagesc
intensity = zeros(obj.lenslets.nLenslet,obj.lenslets.nLenslet*obj.lenslets.nArray);
v = obj.validLenslet(:);
v = repmat(v,obj.lenslets.nArray,1);
intensity(v) = obj.lensletIntensity;
if ishandle(obj.intensityDisplayHandle)
set(obj.intensityDisplayHandle,...
'Cdata',intensity,varargin{:})
else
obj.intensityDisplayHandle = imagesc(intensity,varargin{:});
ax = gca;
pos = get(ax,'position');
axis equal tight xy
colorbar('location','EastOutside')
set(ax,'position',pos)
end
set(get(obj.intensityDisplayHandle,'parent'),'Clim',[floor(min(intensity(v))),ceil(max(intensity(v)))])
if nargout>0
varargout{1} = obj.intensityDisplayHandle;
end
end
function slopesAndFrameDisplay(obj,varargin)
imagesc(obj.camera,varargin{:});
slopesDisplay(obj,'matrix',...
makehgtform('translate',[1,1,0]),varargin{:});
% if isinf(obj.framePixelThreshold)
% clim = get(gca,'clim');
% set(gca,'clim',[obj.framePixelThreshold,clim(2)])
% end
end
function slopesAndIntensityDisplay(obj,varargin)
intensityDisplay(obj,varargin{:});
n = obj.lenslets.nLensletImagePx;
slopesDisplay(obj,'matrix',...
makehgtform('translate',-[(n-1)/2,(n-1)/2,0]/n,'scale',1/n,'translate',[1,1,0]*2),varargin{:});
end
function wavefrontDisplay(obj,varargin)
wft = zeros( obj.lenslets.nLenslet+1 );
wft__ = obj.finiteDifferenceWavefront;
wftRms = std(wft__);
wft(obj.validActuator) = wft__;
if any(ishandle(obj.finiteDifferenceWavefrontHandle))
set(obj.finiteDifferenceWavefrontHandle(1),...
'CData',wft)
set(obj.finiteDifferenceWavefrontHandle(2),...
'String',sprintf('F.D. Wavefront: %5.2f',wftRms))
else
obj.finiteDifferenceWavefrontHandle(1) = ...
imagesc(wft,...
varargin{:});
ax = gca;
pos = get(ax,'position');
axis equal tight xy
colorbar('location','EastOutside')
set(ax,'position',pos)
obj.finiteDifferenceWavefrontHandle(2) = ...
title(sprintf('F.D. Wavefront: %5.2f',wftRms))
end
end
function bar(obj,varargin)
if ishandle(obj.zernCoefsHandle)
set(obj.zernCoefsHandle,...
'YData',obj.zernCoefs)
else
obj.zernCoefsHandle = ...
bar((1:length(obj.zernCoefs))+1,obj.zernCoefs,...
varargin{:});
grid on
xlabel('Zernike modes')
ylabel('Zern. Coefs.')
end
end
function G = sparseGradientMatrix(obj)
%% SPARSEGRADIENTMATRIX
%
% Gamma = sparseGradientMatrix(obj) computes the sparse
% gradient such as a wavefront in wavelength units multiply
% by the gradient matrix gives the centroid in pixel units
nLenslet = obj.lenslets.nLenslet;
nPxPhase = obj.lenslets.nLenslet + 1;
nElPhase = nPxPhase^2;
% non zeros row index
rows = repmat( 1:nLenslet^2 , 4 , 1);
% non zeros column index
cols = [(1:2) (1:2) + nPxPhase]'; % 1st lenslet stencil
cols = bsxfun(@plus, cols, 0:nLenslet-1 ); % 1st lenslet column
p(1,1,1:nLenslet) = (0:nLenslet-1)*nPxPhase;
cols = bsxfun( @plus, cols , p); % lenslet rows
% non zeros values
nzv = repmat( [-1 1 -1 1]', 1 , nLenslet^2);
Gy = sparse(rows(:),cols(:),nzv(:),nLenslet^2,nElPhase);
nzv = repmat( [-1 -1 1 1]', 1 , nLenslet^2);
Gx = sparse(rows(:),cols(:),nzv(:),nLenslet^2,nElPhase);
mask = ~obj.validActuator;
Gx(:,mask) = [];
Gy(:,mask) = [];
mask = ~obj.validLenslet;
Gx(mask,:) = [];
Gy(mask,:) = [];
% scaling factor such as a phase in units of wavelength
% multiply by the gradient matrix will return the centroid in
% units of pixels
nOutWavePx = obj.lenslets.nLensletWavePx*obj.lenslets.fftPad; % Pixel length of the output wave
evenOdd = rem(obj.lenslets.nLensletWavePx,2);
if ~rem(nOutWavePx,2) && evenOdd
nOutWavePx = nOutWavePx + evenOdd;
end
nOutWavePx = max(nOutWavePx,obj.lenslets.nLensletWavePx);
a = obj.lenslets.nLensletWavePx/nOutWavePx;
G = 0.5*[Gx;Gy]/a;
% figure
% spy(G)
end
function gridMask = validLensletSamplingMask(obj,sample)
%% VALIDLENSLETSAMPLINGMASK
%
% mask = validLensletSamplingMask(obj,n) computes the mask
% corresponding to the nXn pixel sampling of the lenslets
nLenslet = obj.lenslets.nLenslet;
nMap = (sample-1)*nLenslet+1;
[iMap0,jMap0] = ndgrid(1:sample);
gridMask = false(nMap);
% Accumulation of x and y stencil row and col subscript and weight
for jLenslet = 1:nLenslet
jOffset = (sample-1)*(jLenslet-1);
for iLenslet = 1:nLenslet
if obj.validLenslet(iLenslet,jLenslet)