forked from rconan/OOMAO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparseOpenLoopAdaptiveOpticsHowto.m
executable file
·343 lines (330 loc) · 10 KB
/
sparseOpenLoopAdaptiveOpticsHowto.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
%% ADAPTIVE OPTICS MODELING WITH OOMAO
% Demonstrate how to build a simple closed-loop single conjugated adaptive
% optics system
%% Definition of the atmosphere
% The atmosphere class constructor has 2 required input:
%
% * the wavelength [m]
% * the Fried parameter for the previously given wavelength [m]
%
% 1 optionnal input: [m]
%
% * the outer scale
%
% and parameter/value pairs of optional inputs:
%
% * the altitudes of the turbulence layers [m]
% * the fractionnal r0 which is the ratio of the r0 at altitude h on the
% integrated r0: $(r_0(h)/r_0)^{5/3}$
% * the wind speeds [m/s]
% * the wind directions [rd]
%
% In the following the atmosphere is given for a r0=15cm in V band and an
% outer scale of 30m with 3 turbulence layers.
atm = atmosphere(photometry.V,0.15,30,...
'altitude',[0,4,10]*1e3,...
'fractionnalR0',[0.7,0.25,0.05],...
'windSpeed',[5,10,20],...
'windDirection',[0,pi/4,pi]);
%% Definition of the telescope
% The telescope class constructor has 1 required input:
%
% * the telescope diameter [m]
%
% 1 optionnal input:
%
% * the central obscuration ratio
%
% and parameter/value pairs of optional inputs:
%
% * the field of view either in arcminute or arcsecond
% * the pupil sampling or resolution in pixels
% * the atmopheric layer motion sampling time [s]
nPx = 160;
tel = telescope(8,...
'fieldOfViewInArcMin',2.5,...
'resolution',nPx,...
'samplingTime',1/500);
%% Definition of a calibration source
% The source class constructor has parameters/value pairs of optional inputs:
%
% * the zenith angle [rd] (default: 0rd)
% * the azimuth angle [rd] (default: 0rd)
% * the wavelength [m] (default: V band)
% * the magnitude
%
% In the following, an on-axis natural guide star in V band is defined.
ngs = source('wavelength',photometry.J);
%% Definition of the wavefront sensor
% Up to now, only the Shack--Hartmann WFS has been implemented in OOMAO.
% The shackHartmann class constructor has 2 required inputs:
%
% * the number of lenslet on one side of the square lenslet array
% * the resolution of the camera
%
% and 1 optionnal input:
%
% * the minimum light ratio that is the ratio between a partially
% illuminated subaperture and a fully illuminated aperture
nLenslet = 16;
wfs = shackHartmann(nLenslet,nPx,0.75);
%%
% Propagation of the calibration source to the WFS through the telescope
ngs = ngs.*tel*wfs;
%%
% Selecting the subapertures illuminated at 75% or more with respect to a
% fully illuminated subaperture
setValidLenslet(wfs)
%%
% A new frame read-out and slopes computing:
+wfs;
%%
% Setting the reference slopes to the current slopes that corresponds to a
% flat wavefront
wfs.referenceSlopes = wfs.slopes;
%%
% A new frame read-out and slopes computing:
+wfs;
%%
% The WFS camera display:
figure
subplot(1,2,1)
imagesc(wfs.camera)
%%
% The WFS slopes display:
subplot(1,2,2)
slopesDisplay(wfs)
%% Definition of the deformable mirror
% The influence functions are made of two cubic Bezier curves. This
% parametric influence function allows modeling a large range of influence
% function types. As examples two influence functions are pre--defined, the
% "monotonic" and "overshoot" models. The second parameter is the
% mechanical coupling between two adjacent actuators
bif = influenceFunction('monotonic',25/100);
%%
% Cut of the influence function
figure
show(bif)
%%
% The deformableMirror constructor class has 1 required input:
%
% * the number of actuator on one side of the square array of actuators
%
% and parameter/value pairs of optional inputs:
%
% * the influence function object (modes)
% * the influence function resolution in pixels
% * the map of valid actuator
%
% Here, the actuators to lenslets registration obeys Fried geometry so the
% map of valid actuator in the square can be retrieved from the WFS object
nActuator = nLenslet + 1;
dm = deformableMirror(nActuator,...
'modes',bif,...
'resolution',nPx,...
'validActuator',wfs.validActuator);
%% Interaction matrix: DM/WFS calibration
% The influence functions are normalized to 1, the actuator are then
% controlled in stroke in meter, here we choose a half a wavelength stroke.
stroke = ngs.wavelength/2;
%%
% The DM actuator commands or coefficients is set to an identity matrix
% scaled to the required stroke; each column of the matrix is one set of
% actuator coefficients (commands)
dm.coefs = eye(dm.nValidActuator)*stroke;
%%
% Propagation of the source through the telescope and the DM to the WFS
ngs=ngs.*tel*dm*wfs;
%%
% The source has been propagated through the DM as many times as the number
% of column in the DM coefficients matrix. As a result, the slopes in the
% WFs object is also a matrix, each column correspond to one actuactor. The
% interaction matrix is then easily derived from the slopes:
interactionMatrix = wfs.slopes./stroke;
figure(10)
subplot(1,2,1)
imagesc(interactionMatrix)
xlabel('DM actuators')
ylabel('WFS slopes [px]')
ylabel(colorbar,'slopes/actuator stroke')
%% Command matrix derivation
% The command matrix is obtained by computing first the singular value
% decomposition of the interaction matrix,
[U,S,V] = svd(interactionMatrix);
eigenValues = diag(S);
subplot(1,2,2)
semilogy(eigenValues,'.')
xlabel('Eigen modes')
ylabel('Eigen values')
%%
% the 4 last eigen values are filtered out
nThresholded = 4;
iS = diag(1./eigenValues(1:end-nThresholded));
[nS,nC] = size(interactionMatrix);
iS(nC,nS) = 0;
%%
% and then the command matrix is derived.
commandMatrix = V*iS*U';
%% The closed loop
% Combining the atmosphere and the telescope
tel = tel+atm;
figure
imagesc(tel)
%%
% Resetting the DM command
dm.coefs = 0;
%%
% Propagation throught the atmosphere to the telescope
ngs=ngs.*tel;
%%
% Saving the turbulence aberrated phase
turbPhase = ngs.meanRmPhase;
%%
% Propagation to the WFS
ngs=ngs*dm*wfs;
%%
% Display of turbulence and residual phase
figure(11)
h = imagesc([turbPhase,ngs.meanRmPhase]);
axis equal tight
colorbar
snapnow
%%
% closing the loop
nIteration = 200;
total = zeros(1,nIteration);
residue = zeros(1,nIteration);
for kIteration=1:nIteration
% Propagation throught the atmosphere to the telescope, +tel means that
% all the layers move of one step based on the sampling time and the
% wind vectors of the layers
ngs=ngs.*+tel;
% Saving the turbulence aberrated phase
turbPhase = ngs.meanRmPhase;
% Variance of the atmospheric wavefront
total(kIteration) = var(ngs);
% Propagation to the WFS
ngs=ngs*wfs*dm;
% Variance of the residual wavefront
residue(kIteration) = var(ngs);
% Computing the DM residual coefficients
dm.coefs = -commandMatrix*wfs.slopes;
% % Integrating the DM coefficients
% dm.coefs = dm.coefs - loopGain*residualDmCoefs;
% Display of turbulence and residual phase
set(h,'Cdata',[turbPhase,ngs.meanRmPhase])
drawnow
end
snapnow
u = (0:nIteration-1).*tel.samplingTime;
atm.wavelength = ngs.wavelength;
%%
% Piston removed phase variance
totalTheory = phaseStats.zernikeResidualVariance(1,atm,tel);
atm.wavelength = photometry.V;
%%
% Phase variance to micron rms converter
rmsMicron = @(x) 1e6*sqrt(x).*ngs.wavelength/2/pi;
figure(13)
plot(u,rmsMicron(total),u([1,end]),rmsMicron(totalTheory)*ones(1,2),u,rmsMicron(residue))
grid
legend('Full','Full (theory)','Residue',0)
xlabel('Time [s]')
ylabel('Wavefront rms [\mum]')
%% WFS noise
% Noise can be added to the wavefront sensor but first we need to set the
% star magnitude.
ngs.magnitude = 0;
%%
% It can be useful to know the number of photon per subaperture. To do so,
% let separate the atmosphere from the telescope
tel = tel - atm;
%%
% re-propagate the source,
ngs = ngs.*tel*wfs;
%%
% and display the subaperture intensity
figure
intensityDisplay(wfs)
%%
% Now the readout noise in photo-electron per pixel per frame rms is set
wfs.camera.readOutNoise = 5;
%%
% Photon-noise is enabled.
wfs.camera.photonNoise = true;
%%
% A pixel threshold is defined
wfs.framePixelThreshold = 5;
%%
% Geometric model sampling
nGeom = 2*nLenslet+1;
%%
% DM with down--sampled influence functions
bifGeom = influenceFunction('monotonic',25/100);
dmGeom = deformableMirror(nActuator,...
'modes',bifGeom,...
'resolution',nGeom,...
'validActuator',wfs.validActuator);
%%
% Sparse Gradient Matrix
[Gamma,gridMask] = sparseGradientMatrix(wfs);
d = tel.D/nLenslet;
Gamma = Gamma/d;
%%
% Laplacian matrix (approx. of inverse of phase covariance matrix)
L2 = phaseStats.sparseInverseCovarianceMatrix({gridMask},atm);
L2 = cell2mat(L2);
%%
% WFS noise covariance matrix
wfs.camera.readOutNoise = 1;
wfs.framePixelThreshold = 0;wfs.camera.readOutNoise;
nMeas = 250;
slopes = zeros(wfs.nSlope,nMeas);
ngs = ngs.*tel*wfs;
for kMeas=1:nMeas
+ngs;
slopes(:,kMeas) = wfs.slopes;
end
q = 2*pi/d/(wfs.lenslets.nyquistSampling*2);
slopes = slopes.*q;
Cn = slopes*slopes'/nMeas;
Cn = diag(diag(Cn));
% iCn = diag(1./diag(Cn));
iCn = sparse(1:wfs.nSlope,1:wfs.nSlope, 1./diag(Cn) );
%%
% The loop is closed again
nIteration = 200;
total = zeros(1,nIteration);
residue = zeros(1,nIteration);
dm.coefs = 0;
tel = tel + atm;
% M = (Gamma'*iCn*Gamma+L2)\(Gamma'*iCn);
A = Gamma'*iCn*Gamma+L2;
b = Gamma'*iCn;
for kIteration=1:nIteration
% Propagation throught the atmosphere to the telescope, +tel means that
% all the layers move of one step based on the sampling time and the
% wind vectors of the layers
ngs=ngs.*+tel;
% Saving the turbulence aberrated phase
turbPhase = ngs.meanRmPhase;
% Variance of the atmospheric wavefront
total(kIteration) = var(ngs);
% Propagation to the WFS
ngs=ngs*wfs*dm;
% Variance of the residual wavefront
residue(kIteration) = var(ngs);
% phase estimation
psEst = A\( b* (wfs.slopes*q) );
% Computing the DM residual coefficients
dm.coefs = (dmGeom.modes.modes(gridMask(:),:)\psEst(:))/ngs.waveNumber;
% Display of turbulence and residual phase
set(h,'Cdata',[turbPhase,ngs.meanRmPhase])
drawnow
end
%%
% Updating the display
set(0,'CurrentFigure',13)
hold on
plot(u,rmsMicron(total),'b--',u,rmsMicron(residue),'r--')
legend('Full','Full (theory)','Residue (Poke Matrix)','Full (noise)','Residue (Sparse Matrix)',0)