mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
fitAiry.hpp
Go to the documentation of this file.
1 /** \file fitAiry.hpp
2  * \author Jared R. Males
3  * \brief Tools for fitting the Airy pattern to PSF images.
4  * \ingroup fitting_files
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef fitAiry_hpp
28 #define fitAiry_hpp
29 
30 #include "levmarInterface.hpp"
31 #include "../func/airyPattern.hpp"
32 
33 namespace mx
34 {
35 namespace math
36 {
37 namespace fit
38 {
39 
40 template<typename realT>
41 struct array2FitAiry;
42 
43 template<typename _realT>
44 struct airy2D_obs_fitter;
45 
46 template<typename _realT>
47 struct airy2D_obs_fitter_ps;
48 
49 /** \defgroup airy_peak_fit Airy Patterns
50  * \brief Fitting Airy patterns to data.
51  *
52  * The Airy pattern is fit to data, typically diffraction-limited images of point sources.
53  *
54  * \ingroup peak_fit
55  */
56 
57 ///Class to manage fitting a 2D Airy pattern to data via the \ref levmarInterface
58 /** In addition to the requirements on fitterT specified by \ref levmarInterface
59  * this class also requires this definition in fitterT
60  * \code
61  * static const int nparams = 5;
62  * \endcode
63  * where the number 5 is replaced by the number of parameters that fitterT expects to fit.
64  *
65  * \tparam fitterT a type meeting the above requirements.
66  *
67  * \ingroup airy_peak_fit
68  *
69  */
70 template<typename fitterT>
71 class fitAiry2D : public levmarInterface<fitterT>
72 {
73 
74 public:
75 
76  typedef typename fitterT::realT realT;
77 
78  static const int nparams = fitterT::nparams;
79 
81 
82  void initialize()
83  {
84  this->allocate_params(nparams);
85  this->adata = &arr;
86  }
87 
88  fitAiry2D()
89  {
90  initialize();
91  }
92 
93  ~fitAiry2D()
94  {
95  }
96 
97  ///Set the initial guess when platescale and central obscuration are fixed.
98  void setGuess( realT A0, ///< [in] the constant background
99  realT A, ///< [in] the peak scaling
100  realT x0, ///< [in] the center x-coordinate [pixels]
101  realT y0 ///< [in] the center y-coordinate [pixels]
102  )
103  {
104  static_assert( nparams==4 , "fitAiry2D: Wrong setGuess called for platescale and/or cen-obs variable.");
105 
106  this->p[0] = A0;
107  this->p[1] = A;
108  this->p[2] = x0;
109  this->p[3] = y0;
110  }
111 
112  ///Set the initial guess when platescale is variable, and central obscuration is fixed.
113  void setGuess( realT A0, ///< [in] the constant background
114  realT A, ///< [in] the peak scaling
115  realT x0, ///< [in] the center x-coordinate [pixels]
116  realT y0, ///< [in] the center y-coordinate [pixels]
117  realT ps ///< [in] the platescale [ (lambda/D) / pixel]
118  )
119  {
120  static_assert( nparams==5 , "fitAiry2D: Wrong setGuess called for only platescale variable.");
121 
122  this->p[0] = A0;
123  this->p[1] = A;
124  this->p[2] = x0;
125  this->p[3] = y0;
126  this->p[4] = ps;
127  }
128 
129  ///Set the initial guess when central-obscuration is variable.
130  void setGuess( realT A0, ///< [in] the constant background
131  realT A, ///< [in] the peak scaling
132  realT x0, ///< [in] the center x-coordinate [pixels]
133  realT y0, ///< [in] the center y-coordinate [pixels]
134  realT ps, ///< [in] the platescale [ (lambda/D) / pixel]
135  realT co ///< [in] the central obscuration [ratio]
136  )
137  {
138  static_assert( nparams==6 , "fitAiry2D: Wrong setGuess called for cen-obs not variable.");
139 
140  this->p[0] = A0;
141  this->p[1] = A;
142  this->p[2] = x0;
143  this->p[3] = y0;
144  this->p[4] = ps;
145  this->p[5] = co;
146  }
147 
148  void setArray(realT *data, int nx, int ny)
149  {
150  arr.data = data;
151  arr.nx = nx;
152  arr.ny = ny;
153 
154  this->n = nx*ny;
155 
156  }
157 
158  void cenObs( realT co )
159  {
160  arr.cenObs = co;
161  }
162 
163  void ps( realT ps )
164  {
165  arr.ps = ps;
166  }
167 
168  int fit()
169  {
171  }
172 
173  realT A0()
174  {
175  return this->p[0];
176  }
177 
178  realT A()
179  {
180  return this->p[1];
181  }
182 
183  realT x0()
184  {
185  return this->p[2];
186  }
187 
188  realT y0()
189  {
190  return this->p[3];
191  }
192 
193  realT ps()
194  {
195  if(nparams < 5)
196  {
197  return arr.ps;
198  }
199  else
200  {
201  return this->p[4];
202  }
203  }
204 
205  realT cenObs()
206  {
207  if(nparams < 6)
208  {
209  return arr.cenObs;
210  }
211  else
212  {
213  return this->p[5];
214  }
215  }
216 
217 
218 };
219 
220 
221 ///Wrapper for a native array to pass to \ref levmarInterface, with Airy details.
222 /** \ingroup airy_peak_fit
223  */
224 template<typename realT>
226 {
227  realT * data {nullptr}; ///< Pointer to the array
228  size_t nx {0}; ///< X dimension of the array
229  size_t ny {0}; ///< Y dimension of the array
230 
231  realT cenObs {0}; ///< is the ratio of the circular central obscuration diameter to the diameter.
232  realT ps {0}; ///< the platescale in \f$ (\lambda/D)/pixel \f$
233 };
234 
235 ///\ref levmarInterface fitter structure for the centrally obscured Airy pattern.
236 /**
237  * Platescale and central obscuration are fixed.
238  *
239  * \ingroup airy_peak_fit
240  *
241  */
242 template<typename _realT>
244 {
245  typedef _realT realT;
246 
247  static const int nparams = 4;
248 
249  static void func(realT *p, realT *hx, int m, int n, void *adata)
250  {
251  array2FitAiry<realT> * arr = (array2FitAiry<realT> *) adata;
252 
253  size_t idx_mat, idx_dat;
254 
255  idx_dat = 0;
256 
257  /*
258  p[0] = floor
259  p[1] = scale
260  p[2] = x0
261  p[3] = y0
262  */
263 
264  realT r;
265 
266  for(int i=0;i<arr->nx; i++)
267  {
268  for(int j=0;j<arr->ny;j++)
269  {
270  idx_mat = i+j*arr->nx;
271 
272  r = sqrt( pow( i-p[2],2) + pow(j-p[3],2));
273 
274  hx[idx_dat] = func::airyPattern( static_cast<realT>(i), static_cast<realT>(j), p[0], p[1], p[2], p[3], arr->ps, arr->cenObs) - arr->data[idx_mat];
275 
276  //hx[idx_dat] *= fabs(arr->data[idx_mat]);
277 
278  idx_dat++;
279  }
280  }
281  }
282 
283 
284 };
285 
286 ///\ref levmarInterface fitter structure for the obstructed Airy pattern, including platescale.
287 /** Central obscuration is fixed.
288  *
289  * \ingroup airy_peak_fit
290  *
291  */
292 template<typename _realT>
294 {
295  typedef _realT realT;
296 
297  static const int nparams = 5;
298 
299  static void func(realT *p, realT *hx, int m, int n, void *adata)
300  {
301  array2FitAiry<realT> * arr = (array2FitAiry<realT> *) adata;
302 
303  size_t idx_mat, idx_dat;
304 
305  idx_dat = 0;
306 
307  /*
308  p[0] = floor
309  p[1] = scale
310  p[2] = x0
311  p[3] = y0
312  p[4] = ps [(lam/D)/pix]
313  */
314 
315  realT r;
316 
317  for(int i=0;i<arr->nx; i++)
318  {
319  for(int j=0;j<arr->ny;j++)
320  {
321  idx_mat = i+j*arr->nx;
322 
323  r = sqrt( pow( i-p[2],2) + pow(j-p[3],2));
324 
325  hx[idx_dat] = func::airyPattern( static_cast<realT>(i), static_cast<realT>(j), p[0], p[1], p[2], p[3], p[4], arr->cenObs) - arr->data[idx_mat];
326 
327  //hx[idx_dat] *= fabs( pow(arr->data[idx_mat],2));
328 
329  idx_dat++;
330  }
331  }
332  }
333 
334 
335 };
336 
337 
338 ///\ref levmarInterface fitter structure for the obstructed Airy pattern, including fitting platescale and central obscuration.
339 /** \ingroup airy_peak_fit
340  *
341  */
342 template<typename _realT>
344 {
345  typedef _realT realT;
346 
347  static const int nparams = 6;
348 
349  static void func(realT *p, realT *hx, int m, int n, void *adata)
350  {
351  array2FitAiry<realT> * arr = (array2FitAiry<realT> *) adata;
352 
353  size_t idx_mat, idx_dat;
354 
355  idx_dat = 0;
356 
357  /*
358  p[0] = floor
359  p[1] = scale
360  p[2] = x0
361  p[3] = y0
362  p[4] = ps [(lam/D)/pix]
363  p[5] = cen-obs
364  */
365 
366  //realT r;
367 
368  for(int i=0;i<arr->nx; ++i)
369  {
370  for(int j=0;j<arr->ny; ++j)
371  {
372  idx_mat = i+j*arr->nx;
373 
374  //r = sqrt( pow( i-p[2],2) + pow(j-p[3],2));
375 
376  hx[idx_dat] = func::airyPattern( static_cast<realT>(i), static_cast<realT>(j), p[0], p[1], p[2], p[3], p[4], p[5]) - arr->data[idx_mat];
377 
378  //hx[idx_dat] *= fabs( pow(arr->data[idx_mat],2));
379 
380  idx_dat++;
381  }
382  }
383  }
384 
385 
386 };
387 
388 ///Alias for the fitAiry2D type with both platescale and cen-obs fixed.
389 /** \ingroup airy_peak_fit
390  */
391 template<typename realT>
393 
394 ///Alias for the fitAiry2D type with cen-obs fixed.
395 /** \ingroup airy_peak_fit
396  */
397 template<typename realT>
399 
400 ///Alias for the fitAiry2D type with none fixed.
401 /** \ingroup airy_peak_fit
402  */
403 template<typename realT>
405 
406 
407 } //namespace fit
408 } //namespace math
409 } //namespace mx
410 
411 #endif //fitAiry_hpp
412 
Class to manage fitting a 2D Airy pattern to data via the levmarInterface.
Definition: fitAiry.hpp:72
void setGuess(realT A0, realT A, realT x0, realT y0, realT ps)
Set the initial guess when platescale is variable, and central obscuration is fixed.
Definition: fitAiry.hpp:113
void setGuess(realT A0, realT A, realT x0, realT y0)
Set the initial guess when platescale and central obscuration are fixed.
Definition: fitAiry.hpp:98
void setGuess(realT A0, realT A, realT x0, realT y0, realT ps, realT co)
Set the initial guess when central-obscuration is variable.
Definition: fitAiry.hpp:130
A templatized interface to the levmar package.
void allocate_params()
Allocate parameters array based on previous call to nParams.
realT * p
Parameter array. On input is the initial estimates. On output has the estimated solution.
int n
I: measurement vector dimension.
void * adata
Pointer to possibly additional data, passed uninterpreted to func & jacf.
realT airyPattern(realT x)
The classical Airy pattern.
Definition: airyPattern.hpp:59
A c++ interface to the templatized levmar minimization routines..
The mxlib c++ namespace.
Definition: mxError.hpp:107
levmarInterface fitter structure for the obstructed Airy pattern, including fitting platescale and ce...
Definition: fitAiry.hpp:344
levmarInterface fitter structure for the obstructed Airy pattern, including platescale.
Definition: fitAiry.hpp:294
levmarInterface fitter structure for the centrally obscured Airy pattern.
Definition: fitAiry.hpp:244
Wrapper for a native array to pass to levmarInterface, with Airy details.
Definition: fitAiry.hpp:226
size_t ny
Y dimension of the array.
Definition: fitAiry.hpp:229
realT * data
Pointer to the array.
Definition: fitAiry.hpp:227
realT cenObs
is the ratio of the circular central obscuration diameter to the diameter.
Definition: fitAiry.hpp:231
realT ps
the platescale in
Definition: fitAiry.hpp:232
size_t nx
X dimension of the array.
Definition: fitAiry.hpp:228