mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
The Gaussian Function

Utilities for working with the Gaussian function

Functions

template<typename floatT >
constexpr floatT mx::math::func::twosqrt2log2 ()
 Constant to convert between the Gaussian width parameter and FWHM. More...
 
template<typename floatT >
floatT mx::math::func::fwhm2sigma (floatT fw)
 Convert from FWHM to the Gaussian width parameter. More...
 
template<typename floatT >
floatT mx::math::func::sigma2fwhm (floatT sig)
 Convert from Gaussian width parameter to FWHM. More...
 
template<typename realT >
realT mx::math::func::gaussian (const realT x, const realT G0, const realT G, const realT x0, const realT sigma)
 Find value at position (x) of the 1D arbitrarily-centered symmetric Gaussian. More...
 
template<typename realT >
realT mx::math::func::gaussian2D (const realT x, const realT y, const realT G0, const realT G, const realT x0, const realT y0, const realT sigma)
 Find value at position (x,y) of the 2D arbitrarily-centered symmetric Gaussian. More...
 
template<typename realT >
void mx::math::func::gaussian2D (realT *arr, size_t nx, size_t ny, const realT G0, const realT G, const realT x0, const realT y0, const realT sigma)
 Fill in an array with the 2D arbitrarily-centered symmetric Gaussian. More...
 
template<typename realT >
realT mx::math::func::gaussian2D (const realT x, const realT y, const realT G0, const realT G, const realT x0, const realT y0, const realT a, const realT b, const realT c)
 Find value at position (x,y) of the 2D general elliptical Gaussian. More...
 
template<typename realT >
void mx::math::func::gaussian2D_gen2rot (realT &sigma_x, realT &sigma_y, realT &theta, const realT a, const realT b, const realT c)
 Convert from (a,b,c) to ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) for the elliptical Gaussian. More...
 
template<typename realT >
void mx::math::func::gaussian2D_rot2gen (realT &a, realT &b, realT &c, const realT sigma_x, const realT sigma_y, const realT theta)
 Convert from ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) to (a,b,c) for the elliptical Gaussian. More...
 
template<typename realT >
realT mx::math::func::gaussian2D_ang (const realT x, const realT y, const realT G0, const realT G, const realT x0, const realT y0, const realT sigma_x, const realT sigma_y, const realT theta)
 Find value at position (x,y) of the 2D rotated elliptical Gaussian. More...
 
template<typename realT >
void mx::math::func::gaussian2D (realT *arr, size_t nx, size_t ny, const realT G0, const realT G, const realT x0, const realT y0, const realT a, const realT b, const realT c)
 Fill in an array with the 2D general elliptical Gaussian. More...
 
template<typename realT >
void mx::math::func::gaussian2D_ang (realT *arr, size_t nx, size_t ny, const realT G0, const realT G, const realT x0, const realT y0, const realT sigma_x, const realT sigma_y, const realT theta)
 Fill in an array with the 2D general elliptical Gaussian. More...
 
template<typename realT >
void mx::math::func::gaussian2D_jacobian (realT *j, const realT x, const realT y, const realT G0, const realT G, const realT x0, const realT y0, const realT a, const realT b, const realT c)
 Calculate the Jacobian at position (x,y) for the 2D general elliptical Gaussian. More...
 

Function Documentation

◆ fwhm2sigma()

template<typename floatT >
floatT mx::math::func::fwhm2sigma ( floatT  fw)

Convert from FWHM to the Gaussian width parameter.

Performs the conversion:

\( \sigma = 2\sqrt{2\log2}FWHM \)

Returns
the converted value of the Gaussian width parameter
Parameters
[in]fwthe full-width at half maximum

Definition at line 65 of file gaussian.hpp.

Referenced by mx::AO::influenceFunctionsGaussian().

◆ gaussian()

template<typename realT >
realT mx::math::func::gaussian ( const realT  x,
const realT  G0,
const realT  G,
const realT  x0,
const realT  sigma 
)

Find value at position (x) of the 1D arbitrarily-centered symmetric Gaussian.

Computes: \( G(x) = G_0 + G\exp[-(0.5/\sigma^2)((x-x_0)^2)]\)

Returns
the value of the 1D arbitrarily-centered symmetric Gaussian at (x)
Template Parameters
realTis the type to use for arithmetic
Parameters
[in]xis the x-position at which to evaluate the Gaussian
[in]G0is the constant to add to the Gaussian
[in]Gis the scaling factor (peak height = G-G0)
[in]x0is the x-coordinate of the center
[in]sigmais the width of the Gaussian.

Definition at line 100 of file gaussian.hpp.

References mx::astro::constants::G(), and mx::astro::constants::sigma().

◆ gaussian2D() [1/4]

template<typename realT >
realT mx::math::func::gaussian2D ( const realT  x,
const realT  y,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  a,
const realT  b,
const realT  c 
)

Find value at position (x,y) of the 2D general elliptical Gaussian.

Computes:

\( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

where, for a counter-clockwise rotation \( \theta \), we have

\( a = \frac{\cos^2 \theta}{\sigma_x^2} + \frac{\sin^2\theta}{\sigma_y^2}\)

\( b = \frac{\sin 2\theta}{2} \left(\frac{1}{\sigma_x^2} - \frac{1}{\sigma_y^2} \right)\)

\( c = \frac{\sin^2 \theta}{\sigma_x^2} + \frac{\cos^2\theta}{\sigma_y^2}\)

In this version the parameters are specified directly as (a,b,c), in particular avoiding the trig function calls. This should be much more efficient, and so this version should be used inside fitting routines, etc. However note that the matrix {a b}{b c} must be positive-definite otherwise infinities can result from the argument of the exponent being positive.

The functions gaussian2D_gen2rot() and gaussian2D_rot2gen() provide conversions from (a,b,c) to ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) and back. The function gaussian2D_ang() is a wrapper for this function, which instead accepts ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) as inputs.

Returns
the value of the 2D elliptical Gaussian at (x,y)
Template Parameters
realTis the type to use for arithmetic
Parameters
[in]xthe x-position at which to evaluate the Gaussian
[in]ythe y-positoin at which to evaluate the Gaussian
[in]G0the constant to add to the Gaussian
[in]Gthe scaling factor (peak = G-G0)
[in]x0the x-coordinate of the center
[in]y0the y-coordinate of the center
[in]athe first rotation and scaling factor
[in]bthe second rotation and scaling factor
[in]cthe third rotation and scaling factor

Definition at line 203 of file gaussian.hpp.

References mx::astro::constants::c(), and mx::astro::constants::G().

◆ gaussian2D() [2/4]

template<typename realT >
realT mx::math::func::gaussian2D ( const realT  x,
const realT  y,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  sigma 
)

Find value at position (x,y) of the 2D arbitrarily-centered symmetric Gaussian.

Computes:

\[ f(x,y) = G_0 + G \exp[-(0.5/\sigma^2)((x-x_0)^2+(y-y_0)^2)] \]

Returns
the value of the 2D arbitrarily-centered symmetric Gaussian at (x,y)
Template Parameters
realTis the type to use for arithmetic
Test:
Scenario: Verify direction and accuracy of various image shifts [test doc]
Parameters
[in]xthe x-position at which to evaluate the Gaussian
[in]ythe y-positoin at which to evaluate the Gaussian
[in]G0the constant to add to the Gaussian
[in]Gthe scaling factor (peak height is G-G0)
[in]x0the x-coordinate of the center
[in]y0the y-coordinate of the center
[in]sigmathe width of the Gaussian.

Definition at line 127 of file gaussian.hpp.

References mx::astro::constants::G(), and mx::astro::constants::sigma().

◆ gaussian2D() [3/4]

template<typename realT >
void mx::math::func::gaussian2D ( realT *  arr,
size_t  nx,
size_t  ny,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  a,
const realT  b,
const realT  c 
)

Fill in an array with the 2D general elliptical Gaussian.

At each pixel (x,y) of the array this computes:

\( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

where, for a counter-clockwise rotation \( \theta \), we have

\( a = \frac{\cos^2 \theta}{\sigma_x^2} + \frac{\sin^2\theta}{\sigma_y^2}\)

\( b = \frac{\sin 2\theta}{2} \left(\frac{1}{\sigma_x^2} - \frac{1}{\sigma_y^2} \right)\)

\( c = \frac{\sin^2 \theta}{\sigma_x^2} + \frac{\cos^2\theta}{\sigma_y^2}\)

In this version the parameters are specified directly as (a,b,c), in particular avoiding the trig function calls. This should be much more efficient, and so this version should be used inside fitting routines, etc.

The functions gaussian2D_gen2rot() and gaussian2D_rot2gen() provide conversions from (a,b,c) to ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) and back. The function gaussian2D_ang() is a wrapper for this function, which instead accepts ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) as inputs.

Template Parameters
realTis the type to use for arithmetic
Parameters
[out]arrthe native array to populate with the Gaussian function
[in]nxthe x-size of the array, in pixels
[in]nythe y-size of the array, in pixels
[in]G0the constant to add to the Gaussian
[in]Gthe scaling factor (peak = G)
[in]x0the x-coordinate of the center, in pixels
[in]y0the y-coordinate of the center, in pixels
[in]ais the first rotation and scaling factor
[in]bis the second rotation and scaling factor
[in]cis the third rotation and scaling factor

Definition at line 420 of file gaussian.hpp.

References mx::astro::constants::c(), and mx::astro::constants::G().

Referenced by mx::math::func::gaussian2D(), and mx::math::func::gaussian2D_ang().

◆ gaussian2D() [4/4]

template<typename realT >
void mx::math::func::gaussian2D ( realT *  arr,
size_t  nx,
size_t  ny,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  sigma 
)

Fill in an array with the 2D arbitrarily-centered symmetric Gaussian.

At each pixel (x,y) of the array this computes:

\( f(x,y) = G_0 + G\exp[-(0.5/\sigma^2)((x-x_0)^2+(y-y_0)^2)] \)

Template Parameters
realTis the type to use for arithmetic
Parameters
[out]arris the allocated array to fill in
[in]nxis the size of the x dimension of the array
[in]nyis the size of the y dimension of the array
[in]G0is the constant to add to the Gaussian
[in]Gis the scaling factor (peak height = G-G0)
[in]x0is the x-coordinate of the center
[in]y0is the y-coordinate of the center
[in]sigmais the third rotation and scaling factor

Definition at line 150 of file gaussian.hpp.

References mx::astro::constants::G(), mx::math::func::gaussian2D(), and mx::astro::constants::sigma().

◆ gaussian2D_ang() [1/2]

template<typename realT >
realT mx::math::func::gaussian2D_ang ( const realT  x,
const realT  y,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  sigma_x,
const realT  sigma_y,
const realT  theta 
)

Find value at position (x,y) of the 2D rotated elliptical Gaussian.

Computes: \( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

where, for a counter-clockwise rotation \( \theta \), we have

\( a = \frac{\cos^2 \theta}{\sigma_x^2} + \frac{\sin^2\theta}{\sigma_y^2}\)

\( b = \frac{\sin 2\theta}{2} \left(\frac{1}{\sigma_x^2} - \frac{1}{\sigma_y^2} \right)\)

\( c = \frac{\sin^2 \theta}{\sigma_x^2} + \frac{\cos^2\theta}{\sigma_y^2}\)

This is a convenience wrapper for the general elliptical Gaussian function gaussian2D(), where here (a,b,c) are first calculated from ( \(\sigma_x\), \(\sigma_y\), \(\theta\)). This will in general be slower due to the trig function calls, so the (a,b,c) version should be used most of the time.

The functions gaussian2D_gen2rot() and gaussian2D_rot2gen() provide conversions from (a,b,c) to ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) and back. The function gaussian2D_rot() is a wrapper for this function, which instead accepts ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) as inputs.

Returns
the value of the 2D elliptical Gaussian at (x,y)
Template Parameters
realTis the type to use for arithmetic
Parameters
[in]xthe x-position at which to evaluate the Gaussian
[in]ythe y-positoin at which to evaluate the Gaussian
[in]G0the constant to add to the Gaussian
[in]Gthe scaling factor (peak height = G-G0)
[in]x0the x-coordinate of the center
[in]y0the y-coordinate of the center
[in]sigma_xthe width in the rotated x direction
[in]sigma_ythe width in the rotated y direction
[in]thetathe counter-clockwise rotation angle

Definition at line 376 of file gaussian.hpp.

References mx::astro::constants::c(), mx::astro::constants::G(), mx::math::func::gaussian2D(), and mx::math::func::gaussian2D_rot2gen().

◆ gaussian2D_ang() [2/2]

template<typename realT >
void mx::math::func::gaussian2D_ang ( realT *  arr,
size_t  nx,
size_t  ny,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  sigma_x,
const realT  sigma_y,
const realT  theta 
)

Fill in an array with the 2D general elliptical Gaussian.

At each pixel (x,y) of the array this computes:

\( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

where, for a counter-clockwise rotation \( \theta \), we have

\( a = \frac{\cos^2 \theta}{\sigma_x^2} + \frac{\sin^2\theta}{\sigma_y^2}\)

\( b = \frac{\sin 2\theta}{2} \left(\frac{1}{\sigma_x^2} - \frac{1}{\sigma_y^2} \right)\)

\( c = \frac{\sin^2 \theta}{\sigma_x^2} + \frac{\cos^2\theta}{\sigma_y^2}\)

This is a convenience wrapper for the general elliptical Gaussian function gaussian2D( realT *, size_t, size_t, const realT, const realT, const realT, const realT, const realT, const realT, const realT) , where here (a,b,c) are first calculated from ( \(\sigma_x\), \(\sigma_y\), \(\theta\)).

The functions gaussian2D_gen2rot() and gaussian2D_rot2gen() provide conversions from (a,b,c) to ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) and back.

Template Parameters
realTis the type to use for arithmetic
Parameters
[out]arrthe native array to populate with the Gaussian function
[in]nxthe x-size of the array, in pixels
[in]nythe y-size of the array, in pixels
[in]G0the constant to add to the Gaussian
[in]Gthe scaling factor (peak = G)
[in]x0the x-coordinate of the center, in pixels
[in]y0the y-coordinate of the center, in pixels
[in]sigma_xthe width in the rotated x direction, in pixels
[in]sigma_ythe width in the rotated y direction, in pixels
[in]thetathe counter-clockwise rotation angle, in radians

Definition at line 473 of file gaussian.hpp.

References mx::astro::constants::c(), mx::astro::constants::G(), mx::math::func::gaussian2D(), and mx::math::func::gaussian2D_rot2gen().

◆ gaussian2D_gen2rot()

template<typename realT >
void mx::math::func::gaussian2D_gen2rot ( realT &  sigma_x,
realT &  sigma_y,
realT &  theta,
const realT  a,
const realT  b,
const realT  c 
)

Convert from (a,b,c) to ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) for the elliptical Gaussian.

The general 2D elliptical Gaussian

\( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

can be expressed in terms of widths and a rotation angle using:

\( a = \frac{\cos^2 \theta}{\sigma_x^2} + \frac{\sin^2\theta}{\sigma_y^2}\)

\( b = \frac{\sin 2\theta}{2} \left(\frac{1}{\sigma_x^2} - \frac{1}{\sigma_y^2} \right)\)

\( c = \frac{\sin^2 \theta}{\sigma_x^2} + \frac{\cos^2\theta}{\sigma_y^2}\)

where \( \theta \) specifies a counter-clockwise rotation.

This function calculates ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) from inputs (a,b,c). It adopts the convention that the long axis of the ellipse is \(\sigma_x\), and \(\theta\) is chosen appropriately. Note that \(-\frac{\pi}{2} < \theta < \frac{\pi}{2}\).

Template Parameters
realTis the type to use for arithmetic
Parameters
[out]sigma_xthe width parameter in the rotated x-direction
[out]sigma_ythe width parameter in the rotated y-direction
[out]thetathe c.c.w rotation
[in]athe first rotation and scaling parameter
[in]bthe second rotation and scaling parameter
[in]cthe the third rotation and scaling parameter

Definition at line 245 of file gaussian.hpp.

References mx::astro::constants::c().

◆ gaussian2D_jacobian()

template<typename realT >
void mx::math::func::gaussian2D_jacobian ( realT *  j,
const realT  x,
const realT  y,
const realT  G0,
const realT  G,
const realT  x0,
const realT  y0,
const realT  a,
const realT  b,
const realT  c 
)

Calculate the Jacobian at position (x,y) for the 2D general elliptical Gaussian.

Note
this has not been verified and may be incorrect.

Given:

\( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

we calculate

\( \frac{\partial G}{\partial G_0} = 1 \)

\( \frac{\partial G}{\partial G} = (G - G_0)/A \)

\( \frac{\partial G}{\partial x_0} = 0.5(G-G_0)( 2a(x-x_0) + b(y-y_0)) \)

\( \frac{\partial G}{\partial y_0} = 0.5(G-G_0) ( b(x-x_0) + 2c(y-y_0)) \)

\( \frac{\partial G}{\partial a} = -0.5(G-G_0) ( (x-x_0)^2 ) \)

\( \frac{\partial G}{\partial b} = -0.5(G-G_0) ( (x-x_0)(y-y_0) ) \)

\( \frac{\partial G}{\partial c} = -0.5(G-G_0) ( (y-y_0)^2 )\)

Parameters
jis a 7 element vector which is populated with the derivatives
xis the x-position at which to evaluate the Gaussian
yis the y-positoin at which to evaluate the Gaussian
G0is the constant to add to the Gaussian
Gis the scaling factor (peak = A)
x0is the x-coordinate of the center
y0is the y-coordinate of the center
ais the first rotation and scaling factor
bis the second rotation and scaling factor
cis the third rotation and scaling factor
Template Parameters
realTis the type to use for arithmetic

Definition at line 535 of file gaussian.hpp.

References mx::astro::constants::c(), and mx::astro::constants::G().

◆ gaussian2D_rot2gen()

template<typename realT >
void mx::math::func::gaussian2D_rot2gen ( realT &  a,
realT &  b,
realT &  c,
const realT  sigma_x,
const realT  sigma_y,
const realT  theta 
)

Convert from ( \(\sigma_x\), \(\sigma_y\), \(\theta\)) to (a,b,c) for the elliptical Gaussian.

The general 2D elliptical Gaussian

\( f(x,y) = G_0 + G\exp [-0.5( a(x-x_0)^2 + b(x-x_0)(y-y_0) + c(y-y_0)^2 ]\)

can be expressed in terms of widths and a rotation angle using:

\( a = \frac{\cos^2 \theta}{\sigma_x^2} + \frac{\sin^2\theta}{\sigma_y^2}\)

\( b = \frac{\sin 2\theta}{2} \left(\frac{1}{\sigma_x^2} - \frac{1}{\sigma_y^2} \right)\)

\( c = \frac{\sin^2 \theta}{\sigma_x^2} + \frac{\cos^2\theta}{\sigma_y^2}\)

where \( \theta \) specifies a counter-clockwise rotation.

This function calculates (a,b,c) from inputs ( \(\sigma_x\), \(\sigma_y\), \(\theta\)).

Template Parameters
realTis the type to use for arithmetic
Parameters
[out]athe first rotation and scaling parameter
[out]bthe second rotation and scaling parameter
[out]cthe the third rotation and scaling parameter
[in]sigma_xthe width parameter in the rotated x-direction
[in]sigma_ythe width parameter in the rotated y-direction
[in]thetathe c.c.w rotation

Definition at line 328 of file gaussian.hpp.

References mx::astro::constants::c().

Referenced by mx::math::func::gaussian2D_ang().

◆ sigma2fwhm()

template<typename floatT >
floatT mx::math::func::sigma2fwhm ( floatT  sig)

Convert from Gaussian width parameter to FWHM.

Performs the conversion:

\( FWHM = 2\sqrt{2\log2}\sigma \)

Returns
the converted value of the full-width at half maximum
Parameters
[in]sigthe Gaussian width parameter

Definition at line 80 of file gaussian.hpp.

Referenced by mx::improc::imCenterCircleSym< realT >::dumpResults(), and mx::math::fit::fitGaussian2D< fitterT >::fwhm().

◆ twosqrt2log2()

template<typename floatT >
constexpr floatT mx::math::func::twosqrt2log2 ( )
constexpr

Constant to convert between the Gaussian width parameter and FWHM.

Used for

\( FWHM = 2\sqrt{2\log2}\sigma \)

This was calculated in long double precision.

Definition at line 50 of file gaussian.hpp.