vnl_gaussian_kernel_1d.cxx
Go to the documentation of this file.
1 // This is core/vnl/algo/vnl_gaussian_kernel_1d.cxx
2 //:
3 // \file
4 // \author Andrew W. Fitzgibbon, Oxford RRG
5 // \date 07 Aug 1997
6 //
7 //-----------------------------------------------------------------------------
8 
9 #include <cmath>
11 #include <vnl/vnl_math.h>
12 
13 // G(x) = 1/(sigma * sqrt(2*pi)) * exp(-0.5 * (x/sigma)^2)
14 // x(g) = sigma * sqrt(-2 * log(g * sigma * sqrt(2*pi) ) )
15 
16 // Compute the x value at which a Gaussian becomes lower than cutoff.
17 static inline
18 double compute_width(double sigma, double cutoff)
19 {
20  return sigma * std::sqrt(-2 * std::log(cutoff * sigma * vnl_math::sqrt2pi));
21 }
22 
23 //: Construct a sampled 1D gaussian of standard deviation sigma.
24 // The vector is normalized so that its sum is 0.5.
25 vnl_gaussian_kernel_1d::vnl_gaussian_kernel_1d(double sigma, double cutoff):
26  vec_((int)std::ceil(compute_width(sigma, cutoff)))
27 {
28  int wid = vec_.size();
29  inscale_ = 0.5/(sigma * sigma);
30  double area = 0;
31  for (int i = 0; i < wid; ++i) {
32  double v = G(i);
33  area += v;
34  vec_[i] = v;
35  }
36  vec_ *= (0.5/area);
37 }
38 
39 double vnl_gaussian_kernel_1d::G(double x) const
40 {
41  return std::exp(-x*x * inscale_);
42 }
double G(double x) const
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
Namespace with standard math functions.
vnl_decnum ceil(vnl_decnum const &x)
Definition: vnl_decnum.h:400
vnl_gaussian_kernel_1d(double sigma, double cutoff=0.5/256.0)
Construct a sampled 1D gaussian of standard deviation sigma.
#define v
Definition: vnl_vector.h:42
vnl_vector< double > vec_
Holds one half of a discretely sampled 1D gaussian distribution.