X-shooter Pipeline Reference Manual 3.8.15
Macros | Functions
Fitting functions

Macros

#define CONCAT(a, b)   a ## _ ## b
 
#define CONCAT2X(a, b)   CONCAT(a,b)
 
#define CPL_TYPE   double
 
#define CPL_TYPE   float
 
#define CPL_TYPE   int
 

Functions

static double irplib_tools_ipow (double x, int p)
 Compute x to the power of p.
 
static cpl_vector * irplib_vector_transform_mean (const cpl_vector *x, double *pm)
 Transform: xhat = x - mean(x)
 
static cpl_matrix * irplib_matrix_product_normal_create (const cpl_matrix *self)
 Create and compute A = B * transpose(B)
 
static cpl_error_code irplib_matrix_product_transpose (cpl_matrix *self, const cpl_matrix *ma, const cpl_matrix *mb)
 Fill a matrix with the product of A * B'.
 
static cpl_error_code irplib_matrix_solve_chol_transpose (const cpl_matrix *self, cpl_matrix *rhs)
 Solve a L*transpose(L)-system with a transposed Right Hand Side.
 
static void irplib_fit_imagelist_polynomial_double (cpl_imagelist *, const cpl_matrix *, const cpl_matrix *, const cpl_vector *, const cpl_imagelist *, const cpl_vector *, double, int, int, cpl_image *)
 
static void irplib_fit_imagelist_polynomial_float (cpl_imagelist *, const cpl_matrix *, const cpl_matrix *, const cpl_vector *, const cpl_imagelist *, const cpl_vector *, double, int, int, cpl_image *)
 
static void irplib_fit_imagelist_polynomial_int (cpl_imagelist *, const cpl_matrix *, const cpl_matrix *, const cpl_vector *, const cpl_imagelist *, const cpl_vector *, double, int, int, cpl_image *)
 
static void irplib_fit_imagelist_residual_double (cpl_image *, int, const cpl_vector *, const cpl_vector *, const cpl_matrix *, const cpl_matrix *)
 
static void irplib_fit_imagelist_residual_float (cpl_image *, int, const cpl_vector *, const cpl_vector *, const cpl_matrix *, const cpl_matrix *)
 
static void irplib_fit_imagelist_residual_int (cpl_image *, int, const cpl_vector *, const cpl_vector *, const cpl_matrix *, const cpl_matrix *)
 
static void irplib_polynomial_shift_double (double *coeffs, int n, double u)
 Given p and u, modify the polynomial to p(x) := p(x+u)
 
cpl_imagelist * xsh_fit_imagelist_polynomial (const cpl_vector *x_pos, const cpl_imagelist *values, int mindeg, int maxdeg, cpl_boolean is_eqdist, cpl_image *fiterror)
 Fit a polynomial to each pixel in a list of images.
 
cpl_error_code xsh_image_find_barycenter (const cpl_image *im, int xpos, int ypos, int size, double *norm, double *xcen, double *ycen, double *sig_x, double *sig_y, double *fwhm_x, double *fwhm_y)
 Apply a gaussian fit on an image sub window.
 

Detailed Description

Macro Definition Documentation

◆ CONCAT

#define CONCAT (   a,
 
)    a ## _ ## b

Definition at line 837 of file xsh_fit.c.

◆ CONCAT2X

#define CONCAT2X (   a,
 
)    CONCAT(a,b)

Definition at line 838 of file xsh_fit.c.

◆ CPL_TYPE [1/3]

#define CPL_TYPE   double

Definition at line 848 of file xsh_fit.c.

◆ CPL_TYPE [2/3]

#define CPL_TYPE   float

Definition at line 848 of file xsh_fit.c.

◆ CPL_TYPE [3/3]

#define CPL_TYPE   int

Definition at line 848 of file xsh_fit.c.

Function Documentation

◆ irplib_fit_imagelist_polynomial_double()

static void irplib_fit_imagelist_polynomial_double ( cpl_imagelist *  ,
const cpl_matrix *  ,
const cpl_matrix *  ,
const cpl_vector *  ,
const cpl_imagelist *  ,
const cpl_vector *  ,
double  ,
int  ,
int  ,
cpl_image *   
)
static

◆ irplib_fit_imagelist_polynomial_float()

static void irplib_fit_imagelist_polynomial_float ( cpl_imagelist *  ,
const cpl_matrix *  ,
const cpl_matrix *  ,
const cpl_vector *  ,
const cpl_imagelist *  ,
const cpl_vector *  ,
double  ,
int  ,
int  ,
cpl_image *   
)
static

◆ irplib_fit_imagelist_polynomial_int()

static void irplib_fit_imagelist_polynomial_int ( cpl_imagelist *  ,
const cpl_matrix *  ,
const cpl_matrix *  ,
const cpl_vector *  ,
const cpl_imagelist *  ,
const cpl_vector *  ,
double  ,
int  ,
int  ,
cpl_image *   
)
static

◆ irplib_fit_imagelist_residual_double()

static void irplib_fit_imagelist_residual_double ( cpl_image *  ,
int  ,
const cpl_vector *  ,
const cpl_vector *  ,
const cpl_matrix *  ,
const cpl_matrix *   
)
static

◆ irplib_fit_imagelist_residual_float()

static void irplib_fit_imagelist_residual_float ( cpl_image *  ,
int  ,
const cpl_vector *  ,
const cpl_vector *  ,
const cpl_matrix *  ,
const cpl_matrix *   
)
static

◆ irplib_fit_imagelist_residual_int()

static void irplib_fit_imagelist_residual_int ( cpl_image *  ,
int  ,
const cpl_vector *  ,
const cpl_vector *  ,
const cpl_matrix *  ,
const cpl_matrix *   
)
static

◆ irplib_matrix_product_normal_create()

static cpl_matrix * irplib_matrix_product_normal_create ( const cpl_matrix *  self)
static

Create and compute A = B * transpose(B)

Parameters
selfM x N Matrix
Returns
Pointer to created M x M product matrix, or NULL on error.
Note
Only the upper triangle of A is computed, while the elements below the main diagonal have undefined values.
See also
cpl_matrix_product_create()

@error

CPL_ERROR_NULL_INPUT Any input matrix is a NULL pointer.

@enderror

To destroy the new matrix the function cpl_matrix_delete() should be used.

FIXME: Improved from cpl_matrix_product_normal_create().

Definition at line 519 of file xsh_fit.c.

References m, and n.

Referenced by xsh_fit_imagelist_polynomial().

◆ irplib_matrix_product_transpose()

static cpl_error_code irplib_matrix_product_transpose ( cpl_matrix *  self,
const cpl_matrix *  ma,
const cpl_matrix *  mb 
)
static

Fill a matrix with the product of A * B'.

Parameters
selfThe matrix to fill, is or else will be set to size M x N
maThe matrix A, of size M x K
mbThe matrix B, of size N x K
Returns
CPL_ERROR_NONE or the relevant CPL error code on error
Note
The use of the transpose of B causes a more efficient memory access
Changing the order of A and B is allowed, it transposes the result
See also
cpl_matrix_product_create()

Definition at line 384 of file xsh_fit.c.

Referenced by irplib_fit_imagelist_polynomial().

◆ irplib_matrix_solve_chol_transpose()

static cpl_error_code irplib_matrix_solve_chol_transpose ( const cpl_matrix *  self,
cpl_matrix *  rhs 
)
static

Solve a L*transpose(L)-system with a transposed Right Hand Side.

Parameters
selfN by N L*transpose(L)-matrix from cpl_matrix_decomp_chol()
rhsM right-hand-sides to be replaced by their solution
Returns
CPL_ERROR_NONE on success, or the relevant CPL error code
See also
cpl_matrix_solve_chol()
Note
Only the lower triangle of self is accessed
The use of the transpose of rhe causes a more efficient memory access

@error

CPL_ERROR_NULL_INPUT An input pointer is NULL.
CPL_ERROR_ILLEGAL_INPUT self is not an n by n matrix.
CPL_ERROR_INCOMPATIBLE_INPUT Selfs number of rows differs from rhs' number of columns.
CPL_ERROR_DIVISION_BY_ZERO The main diagonal of L contains a zero. This error can only occur if the L*transpose(L)-matrix does not come from a successful call to cpl_matrix_decomp_chol().

@enderror

Definition at line 600 of file xsh_fit.c.

References n.

Referenced by irplib_fit_imagelist_polynomial().

◆ irplib_polynomial_shift_double()

static void irplib_polynomial_shift_double ( double *  coeffs,
int  n,
double  u 
)
static

Given p and u, modify the polynomial to p(x) := p(x+u)

Parameters
coeffsThe polynomial coefficients to be modified in place
nThe number of coefficients
uThe shift
Returns
void
See also
cpl_polynomial_shift_1d
Note
The function will assert() on NULL input.

FIXME: Duplicated from cpl_polynomial_shift_double().

Definition at line 445 of file xsh_fit.c.

References n.

Referenced by irplib_fit_imagelist_polynomial().

◆ irplib_tools_ipow()

static double irplib_tools_ipow ( double  x,
int  p 
)
static

Compute x to the power of p.

Parameters
xThe base
pThe non-negative power
Returns
x to the power of p

Apart from a possible difference in round-off the result equals pow(x, p).

Definition at line 348 of file xsh_fit.c.

References x.

Referenced by xsh_fit_imagelist_polynomial().

◆ irplib_vector_transform_mean()

static cpl_vector * irplib_vector_transform_mean ( const cpl_vector *  x,
double *  pm 
)
static

Transform: xhat = x - mean(x)

Parameters
xThe vector to be transformed
pmOn return, *pm is the mean of x
Returns
The created transformed vector
Note
The function will assert() on NULL input.
See also
cpl_vector_transform_mean

FIXME: Duplicated from cpl_vector_transform_mean().

Definition at line 474 of file xsh_fit.c.

References x.

Referenced by xsh_fit_imagelist_polynomial().

◆ xsh_fit_imagelist_polynomial()

cpl_imagelist * xsh_fit_imagelist_polynomial ( const cpl_vector *  x_pos,
const cpl_imagelist *  values,
int  mindeg,
int  maxdeg,
cpl_boolean  is_eqdist,
cpl_image *  fiterror 
)

Fit a polynomial to each pixel in a list of images.

Parameters
x_posThe vector of positions to fit
valuesThe list of images with values to fit
mindegThe smallest degree with a non-zero coefficient
maxdegThe polynomial degree of the fit, at least mindeg
is_eqdistTrue iff the x_pos values are known to be equidistant
fiterrorWhen non-NULL, the error of the fit
Note
values and x_pos must have the same number of elements.
The created imagelist must be deallocated with cpl_imagelist_delete().
x_pos must have at least 1 + (maxdeg - mindeg) distinct values.
Returns
The image list of the fitted polynomial coefficients or NULL on error.
See also
cpl_polynomial_fit_1d_create()

For each pixel, a polynomial representing the relation value = P(x) is computed where: P(x) = x^{mindeg} * (a_0 + a_1 * x + ... + a_{nc-1} * x^{nc-1}), where mindeg >= 0 and maxdeg >= mindeg, and nc is the number of polynomial coefficients to determine, nc = 1 + (maxdeg - mindeg).

The returned image list thus contains nc coefficient images, a_0, a_1, ..., a_{nc-1}.

np is the number of sample points, i.e. the number of elements in x_pos and number of images in the image list.

is_eqdist is ignored if mindeg is nonzero, otherwise is_eqdist may to be set to CPL_TRUE if and only if the values in x_pos are known a-priori to be equidistant when sorted, eg. (1,2,3,4) and (1,3,2,4), but not (1,2,4,6). Setting is_eqdist to CPL_TRUE is faster and eliminates certain round-off errors.

Even though it is not an error, it is hardly useful to use an image of pixel type integer for the fitting error. An image of pixel type float should on the other hand be sufficient for most fitting errors.

The call requires the following number of FLOPs, where nz is the number of pixels in any one image in the imagelist:

2 * nz * nc * (nc + np) + np * nc^2 + nc^3/3 + O(nc * (nc + np)).

If mindeg is zero an additional nz * nc^2 FLOPs are required.

If fiterror is non-NULL an additional 2 * nz * nc * np FLOPs are required.

Possible #_cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if an input const pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if mindeg is negative or maxdeg is less than mindeg.
  • CPL_ERROR_INCOMPATIBLE_INPUT if x_pos and values have different lengths, or if fiterror is non-NULL with a different size than that of values, or if the input images do not all have the same dimensions and pixel type.
  • CPL_ERROR_DATA_NOT_FOUND if x_pos contains less than nc values.
  • CPL_ERROR_SINGULAR_MATRIX if x_pos contains less than nc distinct values.

Definition at line 171 of file xsh_fit.c.

References irplib_fit_imagelist_polynomial_double(), irplib_fit_imagelist_polynomial_float(), irplib_fit_imagelist_polynomial_int(), irplib_matrix_product_normal_create(), irplib_tools_ipow(), irplib_vector_transform_mean(), nx, and ny.

Referenced by xsh_compute_linearity(), and xsh_fit_imagelist_polynomial_tests().

◆ xsh_image_find_barycenter()

cpl_error_code xsh_image_find_barycenter ( const cpl_image *  im,
int  xpos,
int  ypos,
int  size,
double *  norm,
double *  xcen,
double *  ycen,
double *  sig_x,
double *  sig_y,
double *  fwhm_x,
double *  fwhm_y 
)

Apply a gaussian fit on an image sub window.

Parameters
imthe input image
xposthe x position of the center (1 for the first pixel)
yposthe y position of the center (1 for the first pixel)
sizethe window size in pixels
normthe norm of the gaussian or NULL
xcenthe x center of the gaussian or NULL
ycenthe y center of the gaussian or NULL
sig_xthe sigma in x of the gaussian or NULL
sig_ythe sigma in y of the gaussian or NULL
fwhm_xthe FHHM in x or NULL
fwhm_ythe FHHM in y or NULL
Returns
the #_cpl_error_code_ or CPL_ERROR_NONE

The computed norm, xcen, ycen, sig_x, sig_y coefficients are defining the gaussian:

f(x, y) = (norm/(2*pi*sig_x*sig_y)) * exp(-(x-xcen)^2/(2*sig_x^2)) * exp(-(y-ycen)^2/(2*sig_y^2))

fwhm_x and fwhm_y are derived from sig_x and sig_y like: fwhm = 2 * sqrt(2*ln(2)) * sigma

Images can be CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.

Possible #_cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if the input image pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if xpos, ypos or size or illegal
  • CPL_ERROR_TYPE_MISMATCH if the passed image type is not supported
Note
: code extracted from CPL4.0

Definition at line 696 of file xsh_fit.c.

References llx, lly, nx, ny, size, urx, and ury.

Referenced by main(), xsh_detect_arclines(), xsh_detect_arclines_dan(), and xsh_gfit_tests().