pf_pdf.c
Go to the documentation of this file.
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000 Brian Gerkey & Kasper Stoy
4  * gerkey@usc.edu kaspers@robotics.usc.edu
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 /**************************************************************************
22  * Desc: Useful pdf functions
23  * Author: Andrew Howard
24  * Date: 10 Dec 2002
25  * CVS: $Id: pf_pdf.c 6348 2008-04-17 02:53:17Z gerkey $
26  *************************************************************************/
27 
28 #include <assert.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32 //#include <gsl/gsl_rng.h>
33 //#include <gsl/gsl_randist.h>
34 
35 #include "amcl/pf/pf_pdf.h"
36 #include "portable_utils.hpp"
37 
38 // Random number generator seed value
39 static unsigned int pf_pdf_seed;
40 
41 
42 /**************************************************************************
43  * Gaussian
44  *************************************************************************/
45 
46 // Create a gaussian pdf
48 {
49  pf_matrix_t cd;
50  pf_pdf_gaussian_t *pdf;
51 
52  pdf = calloc(1, sizeof(pf_pdf_gaussian_t));
53 
54  pdf->x = x;
55  pdf->cx = cx;
56  //pdf->cxi = pf_matrix_inverse(cx, &pdf->cxdet);
57 
58  // Decompose the convariance matrix into a rotation
59  // matrix and a diagonal matrix.
60  pf_matrix_unitary(&pdf->cr, &cd, pdf->cx);
61  pdf->cd.v[0] = sqrt(cd.m[0][0]);
62  pdf->cd.v[1] = sqrt(cd.m[1][1]);
63  pdf->cd.v[2] = sqrt(cd.m[2][2]);
64 
65  // Initialize the random number generator
66  //pdf->rng = gsl_rng_alloc(gsl_rng_taus);
67  //gsl_rng_set(pdf->rng, ++pf_pdf_seed);
69 
70  return pdf;
71 }
72 
73 
74 // Destroy the pdf
76 {
77  //gsl_rng_free(pdf->rng);
78  free(pdf);
79  return;
80 }
81 
82 
83 /*
84 // Compute the value of the pdf at some point [x].
85 double pf_pdf_gaussian_value(pf_pdf_gaussian_t *pdf, pf_vector_t x)
86 {
87  int i, j;
88  pf_vector_t z;
89  double zz, p;
90 
91  z = pf_vector_sub(x, pdf->x);
92 
93  zz = 0;
94  for (i = 0; i < 3; i++)
95  for (j = 0; j < 3; j++)
96  zz += z.v[i] * pdf->cxi.m[i][j] * z.v[j];
97 
98  p = 1 / (2 * M_PI * pdf->cxdet) * exp(-zz / 2);
99 
100  return p;
101 }
102 */
103 
104 
105 // Generate a sample from the pdf.
107 {
108  int i, j;
109  pf_vector_t r;
110  pf_vector_t x;
111 
112  // Generate a random vector
113  for (i = 0; i < 3; i++)
114  {
115  //r.v[i] = gsl_ran_gaussian(pdf->rng, pdf->cd.v[i]);
116  r.v[i] = pf_ran_gaussian(pdf->cd.v[i]);
117  }
118 
119  for (i = 0; i < 3; i++)
120  {
121  x.v[i] = pdf->x.v[i];
122  for (j = 0; j < 3; j++)
123  x.v[i] += pdf->cr.m[i][j] * r.v[j];
124  }
125 
126  return x;
127 }
128 
129 // Draw randomly from a zero-mean Gaussian distribution, with standard
130 // deviation sigma.
131 // We use the polar form of the Box-Muller transformation, explained here:
132 // http://www.taygeta.com/random/gaussian.html
133 double pf_ran_gaussian(double sigma)
134 {
135  double x1, x2, w, r;
136 
137  do
138  {
139  do { r = drand48(); } while (r==0.0);
140  x1 = 2.0 * r - 1.0;
141  do { r = drand48(); } while (r==0.0);
142  x2 = 2.0 * r - 1.0;
143  w = x1*x1 + x2*x2;
144  } while(w > 1.0 || w==0.0);
145 
146  return(sigma * x2 * sqrt(-2.0*log(w)/w));
147 }
pf_pdf_gaussian_free
void pf_pdf_gaussian_free(pf_pdf_gaussian_t *pdf)
Definition: pf_pdf.c:75
pf_ran_gaussian
double pf_ran_gaussian(double sigma)
Definition: pf_pdf.c:133
pf_vector_t
Definition: pf_vector.h:38
pf_pdf_gaussian_sample
pf_vector_t pf_pdf_gaussian_sample(pf_pdf_gaussian_t *pdf)
Definition: pf_pdf.c:106
pf_pdf_gaussian_t::cx
pf_matrix_t cx
Definition: pf_pdf.h:56
pf_pdf_gaussian_t::x
pf_vector_t x
Definition: pf_pdf.h:55
pf_pdf_gaussian_alloc
pf_pdf_gaussian_t * pf_pdf_gaussian_alloc(pf_vector_t x, pf_matrix_t cx)
Definition: pf_pdf.c:47
drand48
static double drand48(void)
Definition: portable_utils.hpp:13
pf_pdf_gaussian_t
Definition: pf_pdf.h:45
srand48
static void srand48(long int seedval)
Definition: portable_utils.hpp:18
pf_pdf_seed
static unsigned int pf_pdf_seed
Definition: pf_pdf.c:39
pf_matrix_t::m
double m[3][3]
Definition: pf_vector.h:47
pf_matrix_unitary
void pf_matrix_unitary(pf_matrix_t *r, pf_matrix_t *d, pf_matrix_t a)
Definition: pf_vector.c:222
pf_pdf.h
portable_utils.hpp
pf_matrix_t
Definition: pf_vector.h:45
pf_pdf_gaussian_t::cr
pf_matrix_t cr
Definition: pf_pdf.h:61
assert.h
pf_vector_t::v
double v[3]
Definition: pf_vector.h:45
pf_pdf_gaussian_t::cd
pf_vector_t cd
Definition: pf_pdf.h:62


amcl
Author(s): Brian P. Gerkey, contradict@gmail.com
autogenerated on Mon Mar 6 2023 03:50:13