Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bspline_fit.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <math.h>
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>
67 
68 #include "extern.h"
69 
70 
71 int doBspline()
72 {
73  struct LP_IMAGE_INFO *image = h0;
74 
75  image->pinfo.nspline = env->nspline;
76 
77  if(isSet(PF_S_FLAG))
78  {
79  st_report("Performing a bspline fit on baseline data using: nspline = %d", image->pinfo.nspline);
80  bspline_smooth(image);
81  }
82  else
83  {
84  st_report("Performing a bspline fit on raw data using: nspline = %d", image->pinfo.nspline);
85  bspline_fit(image);
86  }
87 
88  return(0);
89 }
90 
91 
92 int bspline_fit(image)
93 struct LP_IMAGE_INFO *image;
94 {
95  const size_t n = image->head.noint;
96  const size_t ncoeffs = image->pinfo.nspline;
97  const size_t nbreak = ncoeffs-2;
98  size_t i, j;
99  gsl_bspline_workspace *bw;
100  gsl_vector *B;
101  gsl_rng *r;
102  gsl_vector *c, *w;
103  gsl_vector *x, *y;
104  gsl_matrix *X, *cov;
105  gsl_multifit_linear_workspace *mw;
106  double chisq, Rsq, dof, tss;
107 
108  if(n == 0 || ncoeffs < 4 || nbreak < 2)
109  {
110  st_fail("Incorrect nspline value: %d %d %d", n, ncoeffs, nbreak);
111  return(1);
112  }
113 
114  if(ncoeffs >= n)
115  {
116  st_fail("ncoeffs(%d) > number of channels(%d)", ncoeffs, n);
117  return(1);
118  }
119 
120  gsl_rng_env_setup();
121 
122  r = gsl_rng_alloc(gsl_rng_default);
123 
124 /* allocate a cubic bspline workspace (k = 4) */
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);
134 
135 /* this is the data to be fitted */
136  for (i=0;i<n;++i)
137  {
138  double sigma;
139 
140  double xi = (double)i;
141  double yi = image->yplot1[i];;
142 
143  sigma = 0.1 * yi;
144 
145  gsl_vector_set(x, i, xi);
146  gsl_vector_set(y, i, yi);
147  gsl_vector_set(w, i, 1.0 / (sigma * sigma));
148  }
149 
150 /* use uniform breakpoints on [0, 15] */
151  gsl_bspline_knots_uniform(0.0, image->head.noint, bw);
152 
153 /* construct the fit matrix X */
154  for(i=0;i<n;++i)
155  {
156  double xi = gsl_vector_get(x, i);
157 
158 /* compute B_j(xi) for all j */
159  gsl_bspline_eval(xi, B, bw);
160 
161 /* fill in row i of X */
162  for(j=0;j<ncoeffs;++j)
163  {
164  double Bj = gsl_vector_get(B, j);
165 
166  gsl_matrix_set(X, i, j, Bj);
167  }
168  }
169 
170 /* do the fit */
171  gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);
172 
173  dof = n - ncoeffs;
174 
175  tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
176 
177  Rsq = 1.0 - chisq / tss;
178 
179  if(isSet(PF_V_FLAG))
180  {
181  st_report("chisq/dof = %e, Rsq = %f", chisq / dof, Rsq);
182  }
183 
184 /* output the smoothed curve */
185  {
186  int i;
187  double xi, yi, yerr;
188 
189  for(i=0;i<n;i++)
190  {
191  xi = (double)i;
192  gsl_bspline_eval(xi, B, bw);
193  gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
194 
195  image->bplot[i] = yi; /* Stick results in the gauss fit array for plotting */
196  }
197  }
198 
199  image->pinfo.bplot = 1; /* Flag the xgrapher that there is data here */
200 
201  gsl_rng_free(r);
202  gsl_bspline_free(bw);
203  gsl_vector_free(B);
204  gsl_vector_free(x);
205  gsl_vector_free(y);
206  gsl_matrix_free(X);
207  gsl_vector_free(c);
208  gsl_vector_free(w);
209  gsl_matrix_free(cov);
210  gsl_multifit_linear_free(mw);
211 
212  return(0);
213 }
214 
215 
216 int bspline_smooth(image)
217 struct LP_IMAGE_INFO *image;
218 {
219  const size_t n = image->head.noint;
220  const size_t ncoeffs = image->pinfo.nspline;
221  const size_t nbreak = ncoeffs-2;
222  size_t i, j;
223  gsl_bspline_workspace *bw;
224  gsl_vector *B;
225  gsl_rng *r;
226  gsl_vector *c, *w;
227  gsl_vector *x, *y;
228  gsl_matrix *X, *cov;
229  gsl_multifit_linear_workspace *mw;
230  float yplot[MAX_CHANS];
231  double chisq, Rsq, dof, tss;
232 
233  if(n == 0 || ncoeffs < 4 || nbreak < 2)
234  {
235  st_fail("Incorrect nspline value: %d %d %d", n, ncoeffs, nbreak);
236  return(1);
237  }
238 
239  if(ncoeffs >= n)
240  {
241  st_fail("ncoeffs(%d) > number of channels(%d)", ncoeffs, n);
242  return(1);
243  }
244 
245  if(image->pinfo.bplot == 0)
246  {
247  st_fail("No Baseline profile to smooth");
248  return(1);
249  }
250 
251  /* Copy the prviously computed bplot into the local array for now */
252  bcopy((char *)&image->bplot[0], (char *)&yplot[0], sizeof(yplot));
253 
254  gsl_rng_env_setup();
255 
256  r = gsl_rng_alloc(gsl_rng_default);
257 
258 /* allocate a cubic bspline workspace (k = 4) */
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);
268 
269 /* this is the data to be fitted' now smooth the bplot data */
270  for (i=0;i<n;++i)
271  {
272  double sigma;
273 
274  double xi = (double)i;
275  double yi = yplot[i];;
276 
277  sigma = 0.1 * yi;
278 
279  gsl_vector_set(x, i, xi);
280  gsl_vector_set(y, i, yi);
281  gsl_vector_set(w, i, 1.0 / (sigma * sigma));
282  }
283 
284 /* use uniform breakpoints on [0, 15] */
285  gsl_bspline_knots_uniform(0.0, image->head.noint, bw);
286 
287 /* construct the fit matrix X */
288  for(i=0;i<n;++i)
289  {
290  double xi = gsl_vector_get(x, i);
291 
292 /* compute B_j(xi) for all j */
293  gsl_bspline_eval(xi, B, bw);
294 
295 /* fill in row i of X */
296  for(j=0;j<ncoeffs;++j)
297  {
298  double Bj = gsl_vector_get(B, j);
299 
300  gsl_matrix_set(X, i, j, Bj);
301  }
302  }
303 
304 /* do the fit */
305  gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);
306 
307  dof = n - ncoeffs;
308 
309  tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
310 
311  Rsq = 1.0 - chisq / tss;
312 
313  if(isSet(PF_V_FLAG))
314  {
315  st_report("chisq/dof = %e, Rsq = %f", chisq / dof, Rsq);
316  }
317 
318 /* output the smoothed curve */
319  {
320  int i;
321  double xi, yi, yerr;
322 
323  for(i=0;i<n;i++)
324  {
325  xi = (double)i;
326  gsl_bspline_eval(xi, B, bw);
327  gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
328 
329  image->bplot[i] = yi; /* Stick results in the gauss fit array for plotting */
330  }
331  }
332 
333  image->pinfo.bplot = 1; /* Flag the xgrapher that there is data here */
334 
335  gsl_rng_free(r);
336  gsl_bspline_free(bw);
337  gsl_vector_free(B);
338  gsl_vector_free(x);
339  gsl_vector_free(y);
340  gsl_matrix_free(X);
341  gsl_vector_free(c);
342  gsl_vector_free(w);
343  gsl_matrix_free(cov);
344  gsl_multifit_linear_free(mw);
345 
346  return(0);
347 }
struct LP_IMAGE_INFO * h0
Definition: image.c:74
int nspline
Definition: image.h:36
double noint
Definition: header.h:211
#define MAX_CHANS
Definition: defines.h:38
int nspline
Definition: env.h:84
int bspline_smooth(struct LP_IMAGE_INFO *image)
Definition: bspline_fit.c:216
struct PLOT_INFO pinfo
Definition: image.h:102
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int isSet(int flag)
Definition: parse.c:646
struct HEADER head
Definition: image.h:104
void st_fail()
int doBspline()
Definition: bspline_fit.c:71
void st_report()
int bspline_fit(struct LP_IMAGE_INFO *image)
Definition: bspline_fit.c:92