Debian Codegraph @

Gaussian/gauss.c

raw symbols SCIP
1 /*LINTLIBRARY*/
2 /* gauss.c
3
4 This code provides gaussian fitting routines.
5
6 Copyright (C) 1997 Karl Glazebrook and Alison Offer Real code
7
8 Note it is not clear to me that this code is fully debugged. The reason
9 I say that is because I tried using the linear eqn solving routines called
10 elsewhere and they were giving erroneous results. So steal from this
11 code with caution! However it does give good fits to reasonable looking
12 gaussians and tests show correct parameters.
13
14 KGB 29/Oct/2002
15 */
16
17
18 #include <stdio.h>
19 #include <math.h>
20 #include <stdlib.h>
21
22 #define NPAR 3
23 #define MAXITER 1000
24
25
26 /* Malloc 2D ptr array e.g. a[nx][ny] */
27
28 static double **malloc2D (int nx, int ny)
29 {
30 int i;
31 double **p;
32 p = (double**) malloc( nx*sizeof(double*) ); /* 1D array of ptrs p[i] */
33 if (p==NULL)
34 return NULL;
35 for (i=0;i<nx;i++) {
36 p[i] = (double*) malloc( ny*sizeof(double) );
37 if (p[i] == NULL) {
38 free(p);
39 return(NULL);
40 }
41 }
42
43 return (double**) p;
44 }
45
46 /* Free a 2D ptr array */
47
48 static void free2D (double **p, int nx, int ny)
49 {
50 int i;
51
52 for (i=0;i<nx;i++)
53 free(p[i]);
54
55 free(p);
56 }
57
58
59 /* ======================================================================== */
60
61 static void seta (int npar, int npoints, double a[NPAR][NPAR], double **d,
62 double *sig)
63 {
64 int i,j,k;
65
66 for(i=0; i<npar; i++)
67 {
68 for (j=0; j<npar; j++)
69 {
70 a[i][j] = 0.0;
71 for(k=0; k<npoints; k++)
72 a[i][j] += d[k][i] * d[k][j] / sig[k];
73 }
74 }
75 }
76
77 static void setb (int npar, int npoints, double b[NPAR], double **d,
78 double *y, double *yfit, double *sig)
79 {
80 int i,k;
81
82 for(i=0; i<npar; i++) {
83 b[i]=0.0;
84 for(k=0; k<npoints; k++)
85 b[i] += d[k][i] * (y[k] - yfit[k]) / sig[k];
86 }
87 }
88
89 /* ========================================================================
90
91 returns the fitted function in yfit and the partial derivatives in d
92
93 here f = b exp ( - ((x - c)/a)^2)
94 */
95
96 static void funct (int npoints, int npar, double *x, double *yfit,
97 double **d, double par[NPAR])
98 {
99 int i;
100 double a,b,c,arg;
101
102 a=par[0]; b=par[1]; c=par[2];
103
104 for(i=0; i<npoints; i++) {
105 arg = (x[i]-c) /a; arg *= arg;
106 yfit[i] = b * exp(-arg);
107 d[i][0] = 2.0 * arg * yfit[i] / a;
108 d[i][1] = yfit[i] / b;
109 d[i][2] = 2.0 * arg * yfit[i] / (x[i]-c);
110 }
111 }
112
113
114 /* These have been checked */
115
116
117 /*
118
119 IMPORTANT NOTE: I tried using decomp()+leneq() routines
120 elsewhere and they were giving erroneous results.
121 So steal from this code with caution!
122
123 KGB 29/Oct/2002
124 */
125
126
127 /* =======================================================================
128
129 LU decomposition of matrix x using Crouts algotithm with partial
130 pivoting.
131
132 Returns decomposition in x and permutation of rows in iorder
133
134 */
135
136 static void decomp (int n, int ndim, double x[NPAR][NPAR], int iorder[NPAR])
137 {
138 int icol, irow, isum, iexc, ipivot;
139 double store, xmax, sum, eps=1e-16;
140
141 /* initialise iorder */
142
143 for(iexc=1; iexc<=n; iexc++)
144 iorder[iexc-1] = iexc;
145
146 /* loop over columns */
147
148 for (icol = 1; icol<=n; icol++) {
149
150 /* U - matrix */
151
152 if (icol>1) {
153 for (irow=1; irow<=icol-1; irow++) {
154 sum = x[irow-1][icol-1];
155 for (isum=1; isum<=irow-1; isum++)
156 sum -= x[irow-1][isum-1]*x[isum-1][irow-1];
157 x[irow-1][icol-1] = sum;
158 }
159 }
160
161 /* L - matrix plus diagonal element of U matrix */
162
163 xmax = 0; ipivot = icol;
164 for (irow=icol; irow<=n; irow++) {
165 sum = x[irow-1][icol-1];
166 if (icol>1) {
167 for(isum=1; isum<=icol-1; isum++)
168 sum -= x[irow-1][isum-1] * x[isum-1][icol-1];
169 }
170 if (fabs(sum)>xmax) {
171 xmax = sum;
172 ipivot = irow;
173 }
174 x[irow-1][icol-1] = sum;
175 }
176
177 /* if xmax is very small replace by epsilon to avoid
178 dividing by zero */
179
180 if (fabs(xmax)<eps)
181 x[ipivot-1][icol-1] = eps;
182
183 /* Partial Pivoting - look for maximum x(i,j<=i) and interchange
184 rows if necessary to put this on the diagonal */
185
186 if (ipivot!=icol) {
187 iexc = iorder[ipivot-1];
188 iorder[ipivot-1] = iorder[icol-1];
189 iorder[icol-1] = iexc;
190 for(iexc=1; iexc<=n; iexc++) {
191 store = x[ipivot-1][iexc-1];
192 x[ipivot-1][iexc-1] = x[icol-1][iexc-1];
193 x[icol-1][iexc-1] = store;
194 }
195 }
196
197 /* divide new L-matrix elements by new diagonal element */
198
199 if (icol<n) {
200 for (irow=icol+1; irow<=n; irow++)
201 x[irow-1][icol-1] /= x[icol-1][icol-1];
202 }
203 }
204 }
205
206
207 /*
208
209 IMPORTANT NOTE: I tried using decomp()+leneq() routines
210 elsewhere and they were giving erroneous results.
211 So steal from this code with caution!
212
213 KGB 29/Oct/2002
214 */
215
216 /* ========================================================================
217
218 x[i<=j[]j] is U-matrix from LU-decomposition
219 x[i>j][j] is L-matrix (diagonal elements are unity)
220 iorder is the permutation of the rows
221
222 b is the input vector, d is the solution vector
223 */
224
225 static void lineq (int n, int ndim, double x[NPAR][NPAR], double b[NPAR],
226 double d[NPAR], int iorder[NPAR])
227 {
228 int i,isum;
229 double sum;
230
231 /* solving X.b = d ==> (L.U).b = d
232 or L.(U.b) = d
233
234 first re-order the vector */
235
236 for (i=1; i<=n; i++)
237 d[i-1] = b[iorder[i-1]-1];
238
239 /* first find (U.b) */
240
241 for(i=2; i<=n; i++) {
242 sum = d[i-1];
243 for (isum=1; isum<=i-1; isum++)
244 sum -= x[i-1][isum-1] * d[isum-1];
245 d[i-1] = sum;
246 }
247
248 /* Now fill out d (solution of X.b) by back substitution */
249
250 d[n-1] /= x[n-1][n-1];
251 for (i=n-1; i>=1; i--){
252 sum = d[i-1];
253 for (isum=i+1; isum<=n; isum++)
254 sum -= x[i-1][isum-1] * d[isum-1];
255 d[i-1] = sum / x[i-1][i-1];
256 }
257
258 }
259
260
261 /* ========================================================================
262
263 My C version of Alison's subroutine to fit a non-linear functions using the
264 Levenberg-Marquardt algorithm
265
266 input: npoints = number of data points
267 npar = number of parameters in fit
268 par = initial estimates of parameters
269 sigma = errors on data (sigma^2)
270
271 output: par = output parameters
272 r = residuals (y(i) - yfit(i))
273 a = estimated covariance matrix of std errs
274 in fitted params.
275 */
276
277
278 int marquardt (int npoints, int npar, double*x, double *y,
279 double* sig, double par[NPAR], double* r,
280 double a[NPAR][NPAR])
281 {
282 int i,k, done, decrease, niter;
283 int iorder[NPAR];
284 double *yfit, **d, **d2, tmp;
285 double par2[NPAR], delta[NPAR], b[NPAR], aprime[NPAR][NPAR];
286
287 double lambda, chisq, chisq2, eps=0.001, lamfac=2.0;
288
289 /* Memory allocation */
290
291 yfit = (double*) malloc( npoints*sizeof(double));
292 if (yfit==NULL)
293 return(1);
294
295 d = malloc2D( npoints, NPAR);
296 if (d==NULL) {
297 free(yfit); return(1);
298 }
299
300 d2 = malloc2D( npoints, NPAR);
301 if (d2==NULL) {
302 free(yfit); free2D(d,npoints,NPAR); return(1);
303 }
304
305 /* Not enough points */
306
307 if (npoints < npar) {
308 free(yfit); free2D(d,npoints,NPAR); free2D(d2,npoints,NPAR);
309 return(2);
310 }
311
312 lambda = 0.001; done = 0; decrease = 0; niter = 1;
313
314 /* Get the value for the initial fit and the value of the derivatives
315 for the current estimate of the parameters */
316
317 funct(npoints, npar, x, yfit, d, par);
318
319 /* Calculate chi^2 */
320
321 chisq = 0;
322 for (k=0; k<npoints; k++) {
323 tmp = y[k] - yfit[k];
324 chisq += tmp*tmp / sig[k];
325 }
326
327 /* Set up initial matrices; curvature matrix a and b */
328
329 seta(npar,npoints,a,d,sig);
330 setb(npar,npoints,b,d,y,yfit,sig);
331
332 /* Main Loop */
333
334 while(!done) {
335
336 /* Set up a' */
337
338 for (k=0; k<npar; k++) {
339 for (i=0; i<npar; i++) {
340 aprime[i][k] = a[i][k];
341 }
342 aprime[k][k] = a[k][k] * (1.0 + lambda);
343 }
344
345 /* Solve Matrix Equation: a'.delta = b */
346
347
348 /*
349
350 IMPORTANT NOTE: I tried using decomp()+leneq() routines
351 elsewhere and they were giving erroneous results.
352 So steal from this code with caution!
353
354 KGB 29/Oct/2002
355 */
356 decomp(npar ,npar, aprime, iorder);
357 lineq(npar, npar, aprime, b, delta, iorder);
358
359 /* Increment parameters in work space and evaluate new chisq */
360
361 for (i=0; i<NPAR; i++){
362 if (i<npar)
363 par2[i] = par[i] + delta[i];
364 else
365 par2[i] = par[i];
366 }
367
368 funct(npoints, npar, x, r, d2, par2);
369 chisq2 = 0.0;
370
371 for (k=0; k<npoints; k++) {
372 tmp = y[k] - r[k];
373 chisq2 += tmp*tmp / sig[k];
374 }
375
376 /* SUccess ? */
377
378 /* printf ("diag: %lf %lf %lf\n", chisq, chisq2,
379 fabs(((chisq - chisq2)/chisq))); */
380
381 if (chisq2<=chisq) {
382
383 /* Test convergence */
384
385 if (chisq == chisq2)
386 done = 1;
387 if (fabs(((chisq - chisq2)/chisq)) < eps && decrease )
388 done = 1;
389 for (k=0; k<npoints; k++)
390 yfit[k] = r[k];
391
392 for (i=0; i<NPAR; i++) {
393 par[i] = par2[i];
394 for (k=0; k<npoints; k++) {
395 d[k][i] = d2[k][i];
396 }
397 }
398
399 if (!done) {
400 seta(npar,npoints,a,d,sig); setb(npar,npoints,b,d,y,yfit,sig);
401 chisq = chisq2; lambda = lambda / lamfac; decrease = 1;
402 }
403 }
404 else {
405
406 decrease = 0;
407 lambda *= lamfac;
408 }
409 niter++;
410
411 /* Failure to converge after numerous iterations */
412
413 if (niter>MAXITER) {
414 free(yfit); free2D(d,npoints,NPAR); free2D(d2,npoints,NPAR);
415 return(3);
416 }
417 }
418
419 /* Success!!! - compute residual and covariance matrix then return
420
421 first calculating inverse of aprime */
422
423 for (i=0; i<npar; i++)
424 delta[i] = 0.0;
425 for (i=0; i<npar; i++) {
426 delta[i] = 1.0;
427 lineq (npar, NPAR, aprime, delta, b, iorder);
428 for (k=0; k<npar; k++)
429 a[k][i] = b[k];
430 }
431
432 for (k=0; k<npoints; k++)
433 r[k] = y[k] - yfit[k];
434
435 free(yfit); free2D(d,npoints,NPAR); free2D(d2,npoints,NPAR);
436
437 return(0);
438 }
439
440 /* qsort doubles */
441 void lqsortD (double* xx, int a, int b)
442 {
443 int i,j;
444
445 double t, median;
446
447 i = a; j = b;
448 median = xx[(i+j) / 2];
449 do {
450 while (xx[i] < median)
451 i++;
452 while (median < xx[j])
453 j--;
454 if (i <= j) {
455 t = xx[i]; xx[i] = xx[j]; xx[j] = t;
456 i++; j--;
457 }
458 } while (i <= j);
459
460 if (a < j)
461 lqsortD(xx,a,j);
462 if (i < b)
463 lqsortD(xx,i,b);
464 }
465