62 #include <gsl/gsl_bspline.h>
63 #include <gsl/gsl_multifit.h>
64 #include <gsl/gsl_rng.h>
65 #include <gsl/gsl_randist.h>
66 #include <gsl/gsl_statistics.h>
96 const size_t ncoeffs = image->pinfo.nspline;
97 const size_t nbreak = ncoeffs-2;
99 gsl_bspline_workspace *bw;
105 gsl_multifit_linear_workspace *mw;
106 double chisq, Rsq, dof, tss;
108 if(n == 0 || ncoeffs < 4 || nbreak < 2)
110 st_fail(
"Incorrect nspline value: %d %d %d", n, ncoeffs, nbreak);
116 st_fail(
"ncoeffs(%d) > number of channels(%d)", ncoeffs, n);
122 r = gsl_rng_alloc(gsl_rng_default);
125 bw = gsl_bspline_alloc(4, nbreak);
126 B = gsl_vector_alloc(ncoeffs);
127 x = gsl_vector_alloc(n);
128 y = gsl_vector_alloc(n);
129 X = gsl_matrix_alloc(n, ncoeffs);
130 c = gsl_vector_alloc(ncoeffs);
131 w = gsl_vector_alloc(n);
132 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
133 mw = gsl_multifit_linear_alloc(n, ncoeffs);
140 double xi = (double)i;
141 double yi = image->yplot1[i];;
145 gsl_vector_set(x, i, xi);
146 gsl_vector_set(y, i, yi);
147 gsl_vector_set(w, i, 1.0 / (sigma * sigma));
151 gsl_bspline_knots_uniform(0.0, image->head.noint, bw);
156 double xi = gsl_vector_get(x, i);
159 gsl_bspline_eval(xi, B, bw);
162 for(j=0;j<ncoeffs;++j)
164 double Bj = gsl_vector_get(B, j);
166 gsl_matrix_set(X, i, j, Bj);
171 gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);
175 tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
177 Rsq = 1.0 - chisq / tss;
181 st_report(
"chisq/dof = %e, Rsq = %f", chisq / dof, Rsq);
192 gsl_bspline_eval(xi, B, bw);
193 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
195 image->bplot[i] = yi;
199 image->pinfo.bplot = 1;
202 gsl_bspline_free(bw);
209 gsl_matrix_free(cov);
210 gsl_multifit_linear_free(mw);
220 const size_t ncoeffs = image->pinfo.nspline;
221 const size_t nbreak = ncoeffs-2;
223 gsl_bspline_workspace *bw;
229 gsl_multifit_linear_workspace *mw;
231 double chisq, Rsq, dof, tss;
233 if(n == 0 || ncoeffs < 4 || nbreak < 2)
235 st_fail(
"Incorrect nspline value: %d %d %d", n, ncoeffs, nbreak);
241 st_fail(
"ncoeffs(%d) > number of channels(%d)", ncoeffs, n);
245 if(image->pinfo.bplot == 0)
247 st_fail(
"No Baseline profile to smooth");
252 bcopy((
char *)&image->bplot[0], (
char *)&yplot[0],
sizeof(yplot));
256 r = gsl_rng_alloc(gsl_rng_default);
259 bw = gsl_bspline_alloc(4, nbreak);
260 B = gsl_vector_alloc(ncoeffs);
261 x = gsl_vector_alloc(n);
262 y = gsl_vector_alloc(n);
263 X = gsl_matrix_alloc(n, ncoeffs);
264 c = gsl_vector_alloc(ncoeffs);
265 w = gsl_vector_alloc(n);
266 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
267 mw = gsl_multifit_linear_alloc(n, ncoeffs);
274 double xi = (double)i;
275 double yi = yplot[i];;
279 gsl_vector_set(x, i, xi);
280 gsl_vector_set(y, i, yi);
281 gsl_vector_set(w, i, 1.0 / (sigma * sigma));
285 gsl_bspline_knots_uniform(0.0, image->head.noint, bw);
290 double xi = gsl_vector_get(x, i);
293 gsl_bspline_eval(xi, B, bw);
296 for(j=0;j<ncoeffs;++j)
298 double Bj = gsl_vector_get(B, j);
300 gsl_matrix_set(X, i, j, Bj);
305 gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);
309 tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
311 Rsq = 1.0 - chisq / tss;
315 st_report(
"chisq/dof = %e, Rsq = %f", chisq / dof, Rsq);
326 gsl_bspline_eval(xi, B, bw);
327 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
329 image->bplot[i] = yi;
333 image->pinfo.bplot = 1;
336 gsl_bspline_free(bw);
343 gsl_matrix_free(cov);
344 gsl_multifit_linear_free(mw);
struct LP_IMAGE_INFO * h0
int bspline_smooth(struct LP_IMAGE_INFO *image)
int bspline_fit(struct LP_IMAGE_INFO *image)