mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
array2FitGaussian1D.hpp
Go to the documentation of this file.
1 /** \file array2FitGaussian1D.hpp
2  * \author Jared R. Males
3  * \brief Wrapper for a native array to pass to \ref levmarInterface, with 1D Gaussian details.
4  * \ingroup fitting_files
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2022 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 math_fit_array2FitGaussian1D_hpp
28 #define math_fit_array2FitGaussian1D_hpp
29 
30 #include "../../mxError.hpp"
31 
32 namespace mx
33 {
34 namespace math
35 {
36 namespace fit
37 {
38 
39 
40 /// Wrapper for a native array to pass to \ref levmarInterface, with !D Gaussian details.
41 /** Supports fixing G0, G, x0, and sigma independently.
42  * \ingroup gaussian_peak_fit
43  */
44 template<typename realT>
46 {
47  realT * m_data {nullptr}; ///< ///< Pointer to the array of y values
48  realT * m_coords {nullptr}; ///< Pointer to the array of x values (optional)
49  size_t m_nx {0}; ///< X dimension of the array
50 
51  realT * m_mask {nullptr}; ///< Pointer to the (optional) mask array. Any 0 pixels are excluded from the fit.
52 
53  realT m_G0 {0};
54  realT m_G {0};
55  realT m_x0 {0};
56  realT m_sigma {0};
57 
58  int m_G0_idx {0};
59  int m_G_idx {1};
60  int m_x0_idx {2};
61  int m_sigma_idx {3};
62 
63  int m_nparams {4};
64  int m_maxNparams {4};
65 
66  /// Set whether each parameter is fixed.
67  /** Sets the parameter indices appropriately.
68  */
69  void setFixed( bool G0,
70  bool G,
71  bool x0,
72  bool sigma
73  );
74 
75  realT G0( realT * p );
76 
77  void G0( realT * p,
78  realT nG0
79  );
80 
81  realT G( realT * p );
82 
83  void G( realT * p,
84  realT nG
85  );
86 
87  realT x0( realT * p );
88 
89  void x0( realT * p,
90  realT nx0
91  );
92 
93  realT sigma( realT * p );
94 
95  void sigma( realT * p,
96  realT nsigma_x
97  );
98 
99  int nparams();
100 
101 };
102 
103 template<typename realT>
105  bool G,
106  bool x0,
107  bool sigma
108  )
109 {
110  int idx = 0;
111 
112  if(G0) m_G0_idx = -1;
113  else m_G0_idx = idx++;
114 
115  if(G) m_G_idx = -1;
116  else m_G_idx = idx++;
117 
118  if(x0) m_x0_idx = -1;
119  else m_x0_idx = idx++;
120 
121  if(sigma) m_sigma_idx = -1;
122  else m_sigma_idx = idx++;
123 
124  m_nparams = idx;
125 }
126 
127 template<typename realT>
128 realT array2FitGaussian1D<realT>::G0( realT * p )
129 {
130  if( m_G0_idx < 0 )
131  {
132  return m_G0;
133  }
134  else
135  {
136  return p[m_G0_idx];
137  }
138 }
139 
140 template<typename realT>
141 void array2FitGaussian1D<realT>::G0( realT * p,
142  realT nG0
143  )
144 {
145  if( m_G0_idx < 0 )
146  {
147  m_G0 = nG0;
148  }
149  else
150  {
151  p[m_G0_idx]=nG0;
152  }
153 }
154 
155 template<typename realT>
156 realT array2FitGaussian1D<realT>::G( realT * p )
157 {
158  if( m_G_idx < 0 )
159  {
160  return m_G;
161  }
162  else
163  {
164  return p[m_G_idx];
165  }
166 }
167 
168 template<typename realT>
169 void array2FitGaussian1D<realT>::G( realT * p,
170  realT nG
171  )
172 {
173  if( m_G_idx < 0 )
174  {
175  m_G = nG;
176  }
177  else
178  {
179  p[m_G_idx] = nG;
180  }
181 }
182 
183 template<typename realT>
184 realT array2FitGaussian1D<realT>::x0( realT * p )
185 {
186  if( m_x0_idx < 0 )
187  {
188  return m_x0;
189  }
190  else
191  {
192  return p[m_x0_idx];
193  }
194 }
195 
196 template<typename realT>
197 void array2FitGaussian1D<realT>::x0( realT * p,
198  realT nx0
199  )
200 {
201  if( m_x0_idx < 0 )
202  {
203  m_x0 = nx0;
204  }
205  else
206  {
207  p[m_x0_idx] = nx0;
208  }
209 }
210 
211 template<typename realT>
212 realT array2FitGaussian1D<realT>::sigma( realT * p )
213 {
214  if( m_sigma_idx < 0 )
215  {
216  return m_sigma;
217  }
218  else
219  {
220  return p[m_sigma_idx];
221  }
222 }
223 
224 template<typename realT>
225 void array2FitGaussian1D<realT>::sigma( realT * p,
226  realT nsigma
227  )
228 {
229  if( m_sigma_idx < 0 )
230  {
231  m_sigma = nsigma;
232  }
233  else
234  {
235  p[m_sigma_idx] = nsigma;
236  }
237 }
238 
239 template<typename realT>
240 int array2FitGaussian1D<realT>::nparams()
241 {
242  return m_nparams;
243 }
244 
245 } //namespace fit
246 } //namespace math
247 
248 } //namespace mx
249 
250 #endif //math_fit_array2FitGaussian1D_hpp
251 
constexpr units::realT G()
Newton's Gravitational Constant.
Definition: constants.hpp:51
constexpr units::realT sigma()
Stefan-Boltzmann Constant.
Definition: constants.hpp:82
The mxlib c++ namespace.
Definition: mxError.hpp:107
Wrapper for a native array to pass to levmarInterface, with !D Gaussian details.
size_t m_nx
X dimension of the array.
realT * m_mask
Pointer to the (optional) mask array. Any 0 pixels are excluded from the fit.
realT * m_coords
Pointer to the array of x values (optional)
realT * m_data
///< Pointer to the array of y values
void setFixed(bool G0, bool G, bool x0, bool sigma)
Set whether each parameter is fixed.