Debian Codegraph @

Gaussian/gaussian.pd

raw
1 use strict;
2 use warnings;
3
4 pp_addpm({At=>'Top'},<<'EOD');
5 =head1 NAME
6
7 PDL::Fit::Gaussian - routines for fitting gaussians
8
9 =head1 DESCRIPTION
10
11
12 This module contains some custom gaussian fitting routines.
13 These were developed in collaboration with Alison Offer,
14 they do a reasonably robust job and are quite useful.
15
16 Gaussian fitting is something I do a lot of, so I figured
17 it was worth putting in my special code.
18
19 Note it is not clear to me that this code is fully debugged. The reason
20 I say that is because I tried using the internal linear eqn solving
21 C routines called elsewhere and they were giving erroneous results.
22 So steal from this code with caution! However it does give good fits to
23 reasonable looking gaussians and tests show correct parameters.
24
25 KGB 29/Oct/2002
26
27 =head1 SYNOPSIS
28
29 use PDL;
30 use PDL::Fit::Gaussian;
31 ($cen, $pk, $fwhm, $back, $err, $fit) = fitgauss1d($x, $data);
32 ($pk, $fwhm, $back, $err, $fit) = fitgauss1dr($r, $data);
33
34 =head1 FUNCTIONS
35
36 =head2 fitgauss1d
37
38 =for ref
39
40 Fit 1D Gassian to data ndarray
41
42 =for example
43
44 ($cen, $pk, $fwhm, $back, $err, $fit) = fitgauss1d($x, $data);
45
46 =for usage
47
48 ($cen, $pk, $fwhm, $back, $err, $fit) = fitgauss1d($x, $data);
49
50 =for signature
51
52 xval(n); data(n); [o]xcentre();[o]peak_ht(); [o]fwhm();
53 [o]background();int [o]err(); [o]datafit(n);
54 [t]sig(n); [t]ytmp(n); [t]yytmp(n); [t]rtmp(n);
55
56 Fits a 1D Gaussian robustly free parameters are the centre, peak height,
57 FWHM. The background is NOT fit, because I find this is generally
58 unreliable, rather a median is determined in the 'outer' 10% of
59 pixels (i.e. those at the start/end of the data ndarray). The initial
60 estimate of the FWHM is the length of the ndarray/3, so it might fail
61 if the ndarray is too long. (This is non-robust anyway). Most data
62 does just fine and this is a good default gaussian fitter.
63
64 The values of the error code $err correspond to:
65
66 =over 4
67
68 =item 0: successful fit
69
70 =item 1: internal problem with memory allocation
71
72 =item 2: insufficient number of data points
73
74 =item 3: fit did not converge
75
76 =back
77
78 SEE ALSO: fitgauss1dr() for fitting radial gaussians
79
80 =head2 fitgauss1dr
81
82 =for ref
83
84 Fit 1D Gassian to radial data ndarray
85
86 =for example
87
88 ($pk, $fwhm2, $back, $err, $fit) = fitgauss1dr($r, $data);
89
90 =for usage
91
92 ($pk, $fwhm2, $back, $err, $fit) = fitgauss1dr($r, $data);
93
94 =for signature
95
96 xval(n); data(n); [o]peak_ht(); [o]fwhm();
97 [o]background();int [o]err(); [o]datafit(n);
98 [t]sig(n); [t]ytmp(n); [t]yytmp(n); [t]rtmp(n);
99
100 Fits a 1D radial Gaussian robustly free parameters are the peak height,
101 FWHM. Centre is assumed to be X=0 (i.e. start of ndarray).
102 The background is NOT fit, because I find this is generally
103 unreliable, rather a median is determined in the 'outer' 10% of
104 pixels (i.e. those at the end of the data ndarray). The initial
105 estimate of the FWHM is the length of the ndarray/3, so it might fail
106 if the ndarray is too long. (This is non-robust anyway). Most data
107 does just fine and this is a good default gaussian fitter.
108
109 The values of the error code $err correspond to:
110
111 =over 4
112
113 =item 0: successful fit
114
115 =item 1: internal problem with memory allocation
116
117 =item 2: insufficient number of data points
118
119 =item 3: fit did not converge
120
121 =back
122
123 SEE ALSO: fitgauss1d() to fit centre as well.
124
125 =cut
126
127 use strict;
128 use warnings;
129
130 EOD
131
132 pp_addhdr(<<'EOF');
133 #define NPAR 3
134 void lqsortD (double* xx, int a, int b);
135 int marquardt (int npoints, int npar, double*x, double *y,
136 double* sig, double par[NPAR], double* r,
137 double a[NPAR][NPAR]);
138 EOF
139
140 for my $name ('fitgauss1d','fitgauss1dr') {
141 pp_def($name,
142 Pars => 'xval(n); data(n); '.($name eq 'fitgauss1dr' ? '' : '[o]xcentre();').
143 '[o]peak_ht(); [o]fwhm();
144 [o]background();int [o]err(); [o]datafit(n);
145 [t]sig(n); [t]ytmp(n); [t]yytmp(n); [t]rtmp(n);',
146 GenericTypes => ['D'],
147 Code => '
148 int i, nb;
149 double ymax, xmax, xmin, val, xval, xcenguess, bkg, par[NPAR], a[NPAR][NPAR];
150 ymax = -1e-30; xmax = -1e-30; xmin = 1e30;
151 $err() = 0;
152 loop(n) %{
153 val = $data();
154 xval = $xval();
155 $ytmp() = val;
156 $sig() = 1.0; /* Room for expansion */
157
158 if (val>ymax) /* Various max and mins */
159 ymax = val;
160 if (xval>xmax)
161 xmax = xval;
162 if (xval<xmin)
163 xmin = xval;
164 %}
165
166 /* Find background points - outer 10% */
167 nb = 0;
168 loop(n) %{
169 if ( fabs($xval()-xmin) > 0.9*fabs(xmax-xmin) ) {
170 $yytmp(n=>nb) = $ytmp();
171 nb++;
172 }
173 %}
174
175 /* Estimate background and xcentroid */
176 bkg = 0;
177 xcenguess = 0.0;
178 if (nb>0) {
179 lqsortD( $P(yytmp), 0, nb-1 );
180 i = (nb-1)/2;
181 bkg = $yytmp( n=>i ); /* Median */
182 }
183 val = 0.0; xcenguess = 0.0;
184 loop(n) %{
185 $ytmp() -= bkg;
186 xcenguess += $ytmp() * $xval();
187 val += $ytmp();
188 %}
189 xcenguess /= val;
190
191 par[2] = xcenguess;
192 par[1] = ymax-bkg;
193 par[0] = (xmax-xmin)/3; /* 1/3 of given box */
194
195 /* fprintf (stderr, "gauss...1 %f %f %f\n", par[0], par[1], par[2]); */
196
197 /* Do the fit */
198
199 '.($name eq 'fitgauss1dr' ? '
200
201 par[2] = 0.0;
202 $err() = marquardt ($SIZE(n), 2, $P(xval), $P(ytmp), $P(sig), par, $P(rtmp), a);
203
204 ' : '
205 $err() = marquardt ($SIZE(n), 3, $P(xval), $P(ytmp), $P(sig), par, $P(rtmp), a);
206 $xcentre() = par[2];
207 ') .'
208
209 $fwhm() = (fabs(par[0]))*2.0*sqrt(log(2.0)); /* Ret Values */
210
211 $peak_ht() = par[1];
212 $background() = bkg;
213
214 loop(n) %{
215 val = ( (double) $xval() - par[2] ) / par[0];
216 $datafit() = par[1] * exp (- val * val) + bkg;
217 %}
218
219 ',
220 Doc=>undef
221 );
222 }
223
224 pp_addpm(<<'EOD');
225 =head1 BUGS
226
227 May not converge for weird data, still pretty good!
228
229 =head1 AUTHOR
230
231 This file copyright (C) 1999, Karl Glazebrook ([email protected]),
232 Gaussian fitting code by Alison Offer
233 ([email protected]). All rights reserved. There
234 is no warranty. You are allowed to redistribute this software /
235 documentation under certain conditions. For details, see the file
236 COPYING in the PDL distribution. If this file is separated from the
237 PDL distribution, the copyright notice should be included in the file.
238
239 =cut
240 EOD
241
242 pp_done();
243