1 #ifndef directPhaseReconstructor_hpp
2 #define directPhaseReconstructor_hpp
5 #include "../../improc/eigenImage.hpp"
6 #include "../../improc/eigenCube.hpp"
7 #include "../../improc/fitsFile.hpp"
8 using namespace mx::improc;
10 #include "../../sigproc/signalWindows.hpp"
13 #define BREAD_CRUMB std::cout << "DEBUG: " << __FILE__ << " " << __LINE__ << "\n";
26 struct directPhaseReconstructorSpec
29 std::string basisName;
37 template<
typename realT>
38 class directPhaseReconstructor
46 typedef Eigen::Array<realT, -1, -1>
imageT;
49 typedef Eigen::Array<realT, -1, -1>
rmatT;
51 typedef directPhaseReconstructorSpec
specT;
54 Eigen::Array<realT,-1,-1> _recon;
103 template<
typename AOSysT>
104 void initialize(AOSysT & AOSys,
specT & spec)
107 _modes = &AOSys.dm._infF;
109 _nModes = _modes->planes();
110 _detRows = _modes->rows();
111 _detCols = _modes->cols();
113 _measurementSize = _detRows*_detCols;
115 _pupil = &AOSys._pupil;
120 template<
typename AOSysT>
121 void linkSystem(AOSysT & AOSys);
163 template<
typename measurementT,
typename wfsImageT>
167 template<
typename measurementT,
typename wfsImageT>
168 void reconstruct(measurementT & commandVect, wfsImageT & wfsImage);
184 template<
typename measurementT>
188 template<
typename measurementT,
typename wfsImageT>
189 void accumulateRMat(
int i, measurementT &measureVec, wfsImageT & wfsImage);
200 void saveRImages(std::string fname);
204 template<
typename realT>
216 template<
typename realT>
217 template<
typename AOSysT>
218 void directPhaseReconstructor<realT>::linkSystem(AOSysT & AOSys)
220 _modes = &AOSys.dm._modes;
222 _nModes = _modes->planes();
223 _detRows = _modes->rows();
224 _detCols = _modes->cols();
226 _measurementSize = _detRows*_detCols;
228 _pupil = &AOSys._pupil;
233 template<
typename realT>
234 realT directPhaseReconstructor<realT>::calAmp()
236 return 0.5*780.0e-9/two_pi<realT>();
239 template<
typename realT>
240 void directPhaseReconstructor<realT>::calAmp(realT ca)
245 template<
typename realT>
246 int directPhaseReconstructor<realT>::nModes()
251 template<
typename realT>
252 int directPhaseReconstructor<realT>::detRows()
257 template<
typename realT>
258 int directPhaseReconstructor<realT>::detCols()
263 template<
typename realT>
264 void directPhaseReconstructor<realT>::loadRecon(
const std::string & fname)
268 template<
typename realT>
269 int directPhaseReconstructor<realT>::measurementSize()
271 return _measurementSize;
274 template<
typename realT>
275 template<
typename measurementT,
typename wfsImageT>
276 void directPhaseReconstructor<realT>::calcMeasurement(measurementT & slopes, wfsImageT & wfsImage)
281 template<
typename realT>
282 template<
typename measurementT,
typename wfsImageT>
283 void directPhaseReconstructor<realT>::reconstruct(measurementT & commandVect, wfsImageT & wfsImage)
290 _npix = _pupil->sum();
296 wfsImage.image *= _mask;
304 #pragma omp parallel for
305 for(
int j=0; j< _nModes; ++j)
307 b[j] = (wfsImage.image*ortho.
image(j)).sum()/ _npix;
313 for(
int j=0; j< modes.planes(); ++j)
315 realT amp = b[0]*spectrum(0,j);
317 for(
int i=1;i<modes.planes();++i)
319 amp += b[i]*spectrum(i,j);
321 commandVect.measurement(0,j) = amp;
325 for(
int k=0;
k< _nModes; ++
k)
327 std::cerr << commandVect.measurement(0,
k) <<
" ";
331 commandVect.iterNo = wfsImage.iterNo;
334 template<
typename realT>
335 void directPhaseReconstructor<realT>::initializeRMat(
int nModes, realT calamp,
int detRows,
int detCols)
342 _rMat.resize(measurementSize(), nModes);
345 _rImages.resize(_detRows, _detCols, _nModes);
348 template<
typename realT>
349 template<
typename measurementT>
350 void directPhaseReconstructor<realT>::accumulateRMat(
int i, measurementT &measureVec)
353 for(
int j=0; j<measureVec.measurement.rows(); ++j)
355 for(
int k=0;
k<measureVec.measurement.cols(); ++
k)
357 _rMat(l, i) = measureVec.measurement(j,
k);
365 template<
typename realT>
366 template<
typename measurementT,
typename wfsImageT>
367 void directPhaseReconstructor<realT>::accumulateRMat(
int i, measurementT &measureVec, wfsImageT & wfsImage)
369 accumulateRMat(i, measureVec);
370 _rImages.
image(i) = wfsImage.image;
373 template<
typename realT>
374 void directPhaseReconstructor<realT>::saveRMat(std::string fname)
379 head.append(
"DETROWS", _detRows,
"WFS detector rows");
380 head.append(
"DETCOLS", _detCols,
"WFS detector cols");
381 head.append(
"CALAMP", _calAmp,
"DM Calibration amplitude");
382 head.append(
"NMODES", _nModes,
"Number of modes included in the response matrix.");
384 ff.write(fname, _rMat, head);
387 template<
typename realT>
388 void directPhaseReconstructor<realT>::saveRImages(std::string fname)
394 head.append(
"DETROWS", _detRows,
"WFS detector rows");
395 head.append(
"DETCOLS", _detCols,
"WFS detector cols");
396 head.append(
"CALAMP", _calAmp,
"DM Calibration amplitude");
397 head.append(
"NMODES", _nModes,
"Number of modes included in the response matrix.");
400 ff.write(fname, _rImages, head);
421 for(
int i = 0; i < wfsImage.rows(); ++i)
423 for(
int j=0; j < wfsImage.cols(); ++j)
425 norm += pow((i - 0.5*(wfsImage.rows()-1.0))*(*_pupil)(i,j),2);
438 realT ampx = 0, ampy = 0;
440 for(
int i = 0; i < wfsImage.rows(); ++i)
442 for(
int j=0; j < wfsImage.cols(); ++j)
444 ampx += wfsImage(i,j) * (i - 0.5*(wfsImage.rows()-1.0))/norm;
445 ampy += wfsImage(i,j) * (j - 0.5*(wfsImage.cols()-1.0))/norm;
449 for(
int i = 0; i < wfsImage.rows(); ++i)
451 for(
int j=0; j < wfsImage.cols(); ++j)
453 wfsImage(i,j) -= ampx * (i - 0.5*(wfsImage.rows()-1.0))/norm * (*_pupil)(i,j);
454 wfsImage(i,j) -= ampy * (j - 0.5*(wfsImage.cols()-1.0))/norm * (*_pupil)(i,j);
Direct Phase Reconstructor.
void saveRMat(std::string fname)
Write the accumulated response matrix to disk.
int detRows()
Get the number of detector rows (_detRows)
void loadRecon(const std::string &fname)
Load the reconstrutor from the specified FITS file.
void detCols(int dc)
Set the number of detector columns (_detCols)
Eigen::Array< realT, -1, -1 > imageT
The type of the WFS image.
realT _calAmp
The calibration amplitude used for response matrix acquisition.
int measurementSize()
Return the size of the unbinned measurement.
void calcMeasurement(measurementT &slopes, wfsImageT &wfsImage)
Calculate the slope measurement.
void initializeRMat(int nmodes, realT calamp, int detrows, int detcols)
Initialize the response matrix for acquisition.
realT calAmp()
Get the calibration amplitude used in response matrix acquisition (_calAmp)
directPhaseReconstructorSpec specT
The specificaion type.
void accumulateRMat(int i, measurementT &measureVec)
Accumalte the next measurement in the response matrix.
void reconstruct(measurementT &commandVect, wfsImageT &wfsImage)
Reconstruct the wavefront from the input image, producing the modal amplitude vector.
directPhaseReconstructor()
Default c'tor.
Eigen::Array< realT, -1, -1 > rmatT
The type of the response matrix.
int detCols()
Get the number of detector columns (_detCols)
void calAmp(realT ca)
Set the calibration amplitude used in response matrix acquisition (_calAmp)
int nModes()
Get the number of modes (_nModes)
void detRows(int dr)
Set the number of detector rows (_detRows)
Eigen::Map< Eigen::Array< dataT, Eigen::Dynamic, Eigen::Dynamic > > image(Index n)
Returns a 2D Eigen::Eigen::Map pointed at the specified image.
constexpr units::realT k()
Boltzmann Constant.
Eigen::Array< scalarT, -1, -1 > eigenImage
Definition of the eigenImage type, which is an alias for Eigen::Array.