8 #ifndef directPhaseSensor_hpp
9 #define directPhaseSensor_hpp
11 #include "../../math/randomT.hpp"
13 #include "../../ioutils/fits/fitsFile.hpp"
14 #include "../../improc/eigenCube.hpp"
17 #include "../../sigproc/psdFilter.hpp"
19 #include "wavefront.hpp"
23 #define BREAD_CRUMB std::cout << "DEBUG: " << __FILE__ << " " << __LINE__ << "\n";
37 template<
typename _realT>
45 typedef Eigen::Array< realT, Eigen::Dynamic, Eigen::Dynamic> imageT;
51 template<
typename _realT,
typename _detectorT>
52 class directPhaseSensor
57 typedef std::complex<_realT> complexT;
60 typedef wavefront<_realT> wavefrontT;
63 typedef improc::eigenImage<realT> pupilT;
66 typedef mx::wfp::imagingArray<std::complex<_realT>,mx::wfp::fftwAllocator<std::complex<_realT> >, 0> complexFieldT;
68 typedef _detectorT detectorT;
91 realT m_simStep {0.001};
99 int m_iTime_counter {0};
103 int m_roTime_counter {0};
105 bool m_firstRun {
true};
107 std::vector<wavefrontT> m_wavefronts;
112 wfsImageT<realT> m_wfsImage;
115 bool m_poissonNoise {
true};
122 detectorT m_detector;
125 wfsImageT<realT> m_detectorImage;
146 void detRows(
int sz);
158 void detCols(
int sz);
164 void detSize(
int nrows,
int ncols);
176 void lambda(realT l);
195 void simStep(realT st);
206 void npix( realT np );
217 void Fbg( realT bg );
219 template<
typename AOSysT>
220 void linkSystem(AOSysT & AOSys);
226 int recordWavefront(wavefrontT & pupilPlane);
232 bool senseWavefront(wavefrontT & pupilPlane);
237 bool senseWavefrontCal(wavefrontT & pupilPlane);
239 void doSenseWavefront();
245 bool m_applyFilter {
false};
247 int m_filterWidth {0};
249 sigproc::psdFilter<realT,2> m_filter;
254 void applyFilter(
bool af );
267 void filterWidth(
int width );
279 const sigproc::psdFilter<realT, 2> & filter();
291 norm_distT m_normVar;
292 poisson_distT m_poissonVar;
304 pupilT * m_pupil {
nullptr};
307 typename wfsImageT<realT>::imageT m_noiseIm;
312 void beta_p(realT bp );
323 void pupil(pupilT * pupil );
328 void pupil(pupilT & pupil );
339 template<
typename _realT,
typename _detectorT>
340 directPhaseSensor<_realT, _detectorT>::directPhaseSensor()
349 template<
typename _realT,
typename _detectorT>
350 int directPhaseSensor<_realT, _detectorT>::wfSz()
355 template<
typename _realT,
typename _detectorT>
356 void directPhaseSensor<_realT, _detectorT>::wfSz(
int sz)
358 if( m_wfSz == sz)
return;
363 template<
typename _realT,
typename _detectorT>
364 int directPhaseSensor<_realT, _detectorT>::detRows()
369 template<
typename _realT,
typename _detectorT>
370 int directPhaseSensor<_realT, _detectorT>::detCols()
375 template<
typename _realT,
typename _detectorT>
376 void directPhaseSensor<_realT, _detectorT>::detSize(
int nrows,
int ncols)
378 if( m_detRows == nrows && m_detCols == ncols)
return;
383 m_detector.setSize(m_detRows, m_detCols);
384 m_detectorImage.image.resize(m_detRows, m_detCols);
389 template<
typename _realT,
typename _detectorT>
390 _realT directPhaseSensor<_realT, _detectorT>::lambda()
395 template<
typename _realT,
typename _detectorT>
396 void directPhaseSensor<_realT, _detectorT>::lambda(_realT l)
401 template<
typename _realT,
typename _detectorT>
402 template<
typename AOSysT>
403 void directPhaseSensor<_realT, _detectorT>::linkSystem(AOSysT & AOSys)
405 static_cast<void>(AOSys);
408 template<
typename _realT,
typename _detectorT>
409 int directPhaseSensor<_realT, _detectorT>::iTime()
414 template<
typename _realT,
typename _detectorT>
415 void directPhaseSensor<_realT, _detectorT>::iTime(
int it)
419 std::cerr <<
"iTime must be >= 1. Correcting\n";
425 m_wavefronts.resize(m_iTime+2+100);
426 m_lastWavefront = -1;
428 m_detector.expTime(m_simStep*m_iTime);
432 template<
typename _realT,
typename _detectorT>
433 int directPhaseSensor<_realT, _detectorT>::roTime()
438 template<
typename _realT,
typename _detectorT>
439 void directPhaseSensor<_realT, _detectorT>::roTime(
int rt)
443 std::cerr <<
"roTime must be >= 1. Correcting\n";
452 template<
typename _realT,
typename _detectorT>
453 _realT directPhaseSensor<_realT, _detectorT>::simStep()
458 template<
typename _realT,
typename _detectorT>
459 void directPhaseSensor<_realT, _detectorT>::simStep(_realT st)
464 m_detector.expTime(m_simStep*m_iTime);
469 template<
typename realT,
typename _detectorT>
470 realT directPhaseSensor<realT, _detectorT>::npix()
475 template<
typename realT,
typename _detectorT>
476 void directPhaseSensor<realT, _detectorT>::npix( realT np )
481 template<
typename realT,
typename _detectorT>
482 realT directPhaseSensor<realT, _detectorT>::Fbg()
487 template<
typename realT,
typename _detectorT>
488 void directPhaseSensor<realT, _detectorT>::Fbg( realT bg)
493 template<
typename _realT,
typename _detectorT>
494 int directPhaseSensor<_realT, _detectorT>::recordWavefront(wavefrontT & pupilPlane)
498 if((
size_t) m_lastWavefront >= m_wavefronts.size()) m_lastWavefront = 0;
501 wavefrontT pPlane = pupilPlane;
503 m_wavefronts[m_lastWavefront].amplitude = pPlane.amplitude;
504 m_wavefronts[m_lastWavefront].phase = pPlane.phase;
505 m_wavefronts[m_lastWavefront].iterNo = pPlane.iterNo;
511 template<
typename _realT,
typename _detectorT>
512 bool directPhaseSensor<_realT, _detectorT>::senseWavefront(wavefrontT & pupilPlane)
514 using poisson_param_t =
typename std::poisson_distribution<long>::param_type;
517 if((
size_t) m_lastWavefront >= m_wavefronts.size()) m_lastWavefront = 0;
520 wavefrontT pPlane = pupilPlane;
522 m_wavefronts[m_lastWavefront].amplitude = pPlane.amplitude;
523 m_wavefronts[m_lastWavefront].phase = pPlane.phase;
524 m_wavefronts[m_lastWavefront].iterNo = pPlane.iterNo;
543 if(m_roTime_counter >= m_roTime)
545 m_detectorImage.image = m_wfsImage.image.block( 0.5*(m_wfsImage.image.rows()-1) - 0.5*(m_detectorImage.image.rows()-1), 0.5*(m_wfsImage.image.cols()-1) - 0.5*(m_detectorImage.image.cols()-1), m_detectorImage.image.rows(), m_detectorImage.image.cols());
547 m_detectorImage.iterNo = m_wfsImage.iterNo;
549 m_roTime_counter = 0;
555 if( m_iTime_counter >= m_iTime)
562 m_roTime_counter = 0;
565 m_detectorImage.image = m_wfsImage.image.block( 0.5*(m_wfsImage.image.rows()-1) - 0.5*(m_detectorImage.image.rows()-1), 0.5*(m_wfsImage.image.cols()-1) - 0.5*(m_detectorImage.image.cols()-1), m_detectorImage.image.rows(), m_detectorImage.image.cols());
571 m_filter.filter(m_detectorImage.image);
572 if(m_pupil !=
nullptr) m_detectorImage.image *= *m_pupil;
577 if(m_beta_p > 0 && m_pupil !=
nullptr)
579 m_noiseIm.resize(m_detectorImage.image.rows(), m_detectorImage.image.cols());
582 for(
int c=0;
c<m_noiseIm.cols();++
c)
584 for(
int r=0;r<m_noiseIm.rows();++r)
586 m_noiseIm(r,
c) = pow(pupilPlane.amplitude(r,
c),2)*(*m_pupil)(r,
c) * m_detector.expTime();
591 realT sqrtFbg = sqrt(m_Fbg*m_detector.expTime());
594 for(
int c=0;
c<m_noiseIm.cols();++
c)
596 for(
int r=0;r<m_noiseIm.rows();++r)
598 if(m_noiseIm(r,
c) == 0)
604 realT err = sqrt(m_noiseIm(r,
c))*m_normVar;
605 if(sqrtFbg > 0) err += sqrtFbg*m_normVar;
606 if(m_detector.ron() > 0) err += m_detector.ron()*m_normVar;
608 m_detectorImage.image(r,
c) += m_beta_p * err/m_noiseIm(r,
c);
616 m_detectorImage.iterNo = m_wfsImage.iterNo;
618 m_roTime_counter = 0;
631 template<
typename _realT,
typename _detectorT>
632 bool directPhaseSensor<_realT, _detectorT>::senseWavefrontCal(wavefrontT & pupilPlane)
637 m_wavefronts[0].amplitude = pupilPlane.amplitude;
638 m_wavefronts[0].phase = pupilPlane.phase;
640 m_wavefronts[1].amplitude = pupilPlane.amplitude;
641 m_wavefronts[1].phase = pupilPlane.phase;
645 m_detectorImage.image = m_wfsImage.image.block( 0.5*(m_wfsImage.image.rows()-1) - 0.5*(m_detectorImage.image.rows()-1), 0.5*(m_wfsImage.image.cols()-1) - 0.5*(m_detectorImage.image.cols()-1), m_detectorImage.image.rows(), m_detectorImage.image.cols());
649 m_filter.filter(m_detectorImage.image);
658 template<
typename _realT,
typename _detectorT>
659 void directPhaseSensor<_realT, _detectorT>::doSenseWavefront()
661 wavefrontT pupilPlane;
666 int _firstWavefront = m_lastWavefront - m_iTime;
667 if(_firstWavefront < 0) _firstWavefront += m_wavefronts.size();
671 pupilPlane.amplitude = 0.5*m_wavefronts[_firstWavefront].amplitude;
672 pupilPlane.phase = 0.5*m_wavefronts[_firstWavefront].phase;
674 pupilPlane.iterNo = 0.5*m_wavefronts[_firstWavefront].iterNo;
680 if(m_wavefronts[_firstWavefront].iterNo < m_iTime)
682 m_wfsImage.image = pupilPlane.phase;
683 m_wfsImage.iterNo = pupilPlane.iterNo;
687 for(
int i=0; i < m_iTime - 1; ++i)
690 if( (
size_t) _firstWavefront >= m_wavefronts.size()) _firstWavefront = 0;
694 pupilPlane.amplitude += m_wavefronts[_firstWavefront].amplitude;
695 pupilPlane.phase += m_wavefronts[_firstWavefront].phase;
696 pupilPlane.iterNo += m_wavefronts[_firstWavefront].iterNo;
701 if( (
size_t) _firstWavefront >= m_wavefronts.size()) _firstWavefront = 0;
705 pupilPlane.amplitude += 0.5*m_wavefronts[_firstWavefront].amplitude;
706 pupilPlane.phase += 0.5*m_wavefronts[_firstWavefront].phase;
707 pupilPlane.iterNo += 0.5*m_wavefronts[_firstWavefront].iterNo;
709 pupilPlane.amplitude /= (m_iTime);
710 pupilPlane.phase /= (m_iTime);
711 pupilPlane.iterNo /= (m_iTime);
713 m_wfsImage.image = pupilPlane.phase;
714 m_wfsImage.iterNo = pupilPlane.iterNo;
718 template<
typename _realT,
typename _detectorT>
719 void directPhaseSensor<_realT, _detectorT>::applyFilter(
bool af)
724 template<
typename _realT,
typename _detectorT>
725 bool directPhaseSensor<_realT, _detectorT>::applyFilter()
727 return m_applyFilter;
731 template<
typename _realT,
typename _detectorT>
732 void directPhaseSensor<_realT, _detectorT>::filterWidth(
int width )
734 int nr = m_detectorImage.image.rows();
735 int nc = m_detectorImage.image.cols();
737 typename wfsImageT<_realT>::imageT filterMask;
739 filterMask.resize( nr, nc );
740 filterMask.setZero();
742 filterMask.block(0,0, width+1, width+1).setConstant(1.0);
743 filterMask.block(0, nc - width, width+1, width).setConstant(1.0);
744 filterMask.block(nr - width, 0, width, width+1).setConstant(1.0);
745 filterMask.block(nr - width, nc - width, width, width).setConstant(1.0);
747 m_filter.psdSqrt(filterMask,
static_cast<realT
>(1)/nr,
static_cast<realT
>(1)/nc);
749 m_filterWidth = width;
753 template<
typename _realT,
typename _detectorT>
754 int directPhaseSensor<_realT, _detectorT>::filterWidth()
756 return m_filterWidth;
759 template<
typename realT,
typename detectorT>
760 const sigproc::psdFilter<realT,2> & directPhaseSensor<realT, detectorT>::filter()
766 template<
typename _realT,
typename _detectorT>
767 void directPhaseSensor<_realT, _detectorT>::beta_p(realT bp)
772 template<
typename realT,
typename detectorT>
773 realT directPhaseSensor<realT, detectorT>::beta_p()
778 template<
typename realT,
typename detectorT>
779 void directPhaseSensor<realT, detectorT>::pupil(pupilT * pupil)
784 template<
typename realT,
typename detectorT>
785 void directPhaseSensor<realT, detectorT>::pupil(pupilT & pupil)
790 template<
typename realT,
typename detectorT>
791 typename directPhaseSensor<realT, detectorT>::pupilT * directPhaseSensor<realT, detectorT>::pupil()
A random number type, which functions like any other arithmetic type.
constexpr units::realT c()
The speed of light.