| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
|
| 30 |
.IDENT fitnol.c |
| 31 |
.MODULE subroutines |
| 32 |
.LANGUAGE C |
| 33 |
.AUTHOR Cristian Levin - ESO La Silla |
| 34 |
.PURPOSE gaussian fitting. |
| 35 |
.KEYWORDS gaussian fitting. |
| 36 |
.ENVIRONMENT UNIX |
| 37 |
.VERSION 1.0 1-Apr-1991 Implementation |
| 38 |
|
| 39 |
090723 last modif |
| 40 |
------------------------------------------------------------*/ |
| 41 |
|
| 42 |
#include <math.h> |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
/************************************************************ |
| 49 |
* |
| 50 |
* fit_gauss(): Gaussian fitting. |
| 51 |
* |
| 52 |
************************************************************/ |
| 53 |
|
| 54 |
#include "mpfit.h" |
| 55 |
#include <stdlib.h> |
| 56 |
|
| 57 |
static double gauss(double x, double amp, double mean, double sigma) |
| 58 |
{ |
| 59 |
double v = x - mean; |
| 60 |
return amp * exp(-(v * v) / (2. * (sigma * sigma))); |
| 61 |
} |
| 62 |
|
| 63 |
typedef struct { |
| 64 |
double * x; |
| 65 |
double * y; |
| 66 |
} priv_data; |
| 67 |
|
| 68 |
int myfunct(int m, int n, double *p, double *deviates, |
| 69 |
double **derivs, priv_data *d) |
| 70 |
{ |
| 71 |
int i; |
| 72 |
/* Compute function deviates */ |
| 73 |
for (i=0; i<m; i++) { |
| 74 |
deviates[i] = d->y[i] - gauss(d->x[i], p[0], p[1], p[2]); |
| 75 |
} |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
int fit_gauss (double *x, double *y, int n, double *a, int nfp) |
| 81 |
{ |
| 82 |
priv_data pdata = {&x[1], &y[1]}; |
| 83 |
mpfit((mp_func)&myfunct, n, nfp, |
| 84 |
&a[1], NULL, NULL, |
| 85 |
&pdata, |
| 86 |
NULL); |
| 87 |
} |
| 88 |
|