Changeset 8884 for trunk/psModules/src/detrend/pmShutterCorrection.c
- Timestamp:
- Sep 22, 2006, 11:00:53 AM (20 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/detrend/pmShutterCorrection.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/detrend/pmShutterCorrection.c
r8866 r8884 51 51 */ 52 52 53 #if HAVE_CONFIG_H 54 #include <config.h> 55 #endif 56 57 #include <stdio.h> 58 #include <math.h> 59 #include <strings.h> 60 #include <pslib.h> 61 62 #include "pmShutterCorrection.h" 63 53 64 static void pmShutterCorrParsFree (pmShutterCorrPars *pars) 54 65 { … … 133 144 return (pars); 134 145 } 146 147 // linear fit to the counts and exptime, given a value for offref 148 pmShutterCorrPars *pmShutterCorrectionLinFit (psVector *exptime, psVector *counts, float offref) 149 { 150 151 // this step is identical for all pixels: do it once and save? 152 psVector *x = psVectorAlloc (exptime->n, PS_TYPE_F32); 153 psVector *y = psVectorAlloc (exptime->n, PS_TYPE_F32); 154 155 for (int i = 0; i < exptime->n; i++) { 156 value = 1.0 / (exptime->data.F32[i] + offref); 157 x->data.F32[i] = exptime->data.F32[i] * value; 158 y->data.F32[i] = value; 159 } 160 161 psPolynomial2D *line = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, 1, 1); 162 163 // mask out the terms we will not fit 164 line->mask[0][0] = 1; 165 line->mask[1][1] = 1; 166 line->coeff[0][0] = 0; 167 line->coeff[1][1] = 0; 168 169 psVectorFitPolynomial2D (line, NULL, 0, counts, NULL, x, y); 170 171 pmShutterCorrPars *pars = pmShutterCorrParsAlloc (); 172 173 pars->offref = guess->offref; 174 pars->scale = line->coeff[1][0]; 175 pars->offset = line->coeff[0][1] / line->coeff[1][0]; 176 177 return (pars); 178 } 179 180 static psF32 pmShutterCorrectionModel(psVector *deriv, 181 const psVector *params, 182 const psVector *x) 183 { 184 psF32 A = params->data.F32[0]; 185 psF32 p = x->data.F32[0] + params->data.F32[1]; 186 psF32 q = 1.0 / (x->data.F32[0] + params->data.F32[2]); 187 psF32 f = A*p*q; 188 189 if (deriv != NULL) { 190 deriv->data.F32[0] = p*q; 191 deriv->data.F32[1] = A*q; 192 deriv->data.F32[2] = -A*p*q*q; 193 } 194 return(f); 195 } 196 197 // non-linear fit to the counts and exptime, given a guess for the three parameters 198 pmShutterCorrPars *pmShutterCorrectionFullFit (psVector *exptime, psVector *counts, pmShutterCorrPars *guess) 199 { 200 201 psMinimization *myMin = psMinimizationAlloc(15, 0.1); 202 203 psVector *params = psVectorAlloc (3, PS_TYPE_F32); 204 params->data.F32[0] = guess->scale; 205 params->data.F32[1] = guess->offset; 206 params->data.F32[2] = guess->offref; 207 params->n = 3; 208 209 // XXX for the moment, don't set any constraints 210 // psMinConstrain *constrain = psMinConstrainAlloc(); 211 // constrain->paramDelta = psVectorAlloc (3, PS_TYPE_F32); 212 // constrain->paramMin = psVectorAlloc (3, PS_TYPE_F32); 213 // constrain->paramMax = psVectorAlloc (3, PS_TYPE_F32); 214 // constrain->paramMask = NULL; 215 psMinConstrain *constrain = NULL; 216 217 // XXX ignore covariance matrix for the moment 218 // psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F64); 219 psImage *covar = NULL; 220 221 // construct the coordinate and value entries (y is counts) 222 psArray *x = psArrayAlloc(exptime->n); 223 psVector *y = counts; 224 psVector *yErr = psVectorAlloc(exptime->n, PS_TYPE_F32); 225 x->n = yErr->n = exptime->n; 226 227 for (int i = 0; i < exptime->n; i++) { 228 psVector *coord = psVectorAlloc(1, PS_TYPE_F32); 229 coord->n = 1; 230 231 coord->data.F32[0] = exptime->data.F32[i]; 232 233 x->data[i] = coord; 234 yErr->data.F32[i] = sqrt(y->data.F32[i]); 235 } 236 237 fitStatus = psMinimizeLMChi2(myMin, covar, params, constrain, x, y, yErr, pmShutterCorrectionModel); 238 239 pmShutterCorrPars *pars = pmShutterCorrParsAlloc (); 240 241 pars->scale = params->data.F32[0]; 242 pars->offset = params->data.F32[1]; 243 pars->offref = params->data.F32[2]; 244 245 return (pars); 246 }
Note:
See TracChangeset
for help on using the changeset viewer.
