Unverified Commit f3b4fc54 authored by pfeatherstone's avatar pfeatherstone Committed by GitHub
Browse files

Added "get_random_complex_gaussian" to dlib::rand (#2149)



* Added a function for computing a gaussian distributed complex number. The real version is adapted to use the complex version

* Missing header

* missed std:: I was too quick
Co-authored-by: default avatarpf <pf@pf-ubuntu-dev>
parent f55a1a51
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#define DLIB_RAND_KERNEl_1_ #define DLIB_RAND_KERNEl_1_
#include <string> #include <string>
#include <complex>
#include "../algs.h" #include "../algs.h"
#include "rand_kernel_abstract.h" #include "rand_kernel_abstract.h"
#include "mersenne_twister.h" #include "mersenne_twister.h"
...@@ -227,15 +228,9 @@ namespace dlib ...@@ -227,15 +228,9 @@ namespace dlib
} }
} }
double get_random_gaussian ( std::complex<double> get_random_complex_gaussian (
) )
{ {
if (has_gaussian)
{
has_gaussian = false;
return next_gaussian;
}
double x1, x2, w; double x1, x2, w;
const double rndmax = std::numeric_limits<dlib::uint32>::max(); const double rndmax = std::numeric_limits<dlib::uint32>::max();
...@@ -252,9 +247,22 @@ namespace dlib ...@@ -252,9 +247,22 @@ namespace dlib
} while ( w >= 1.0 ); } while ( w >= 1.0 );
w = std::sqrt( (-2.0 * std::log( w ) ) / w ); w = std::sqrt( (-2.0 * std::log( w ) ) / w );
next_gaussian = x2 * w; return std::complex<double>(x1 * w, x2 * w);
}
double get_random_gaussian (
)
{
if (has_gaussian)
{
has_gaussian = false;
return next_gaussian;
}
std::complex<double> r = get_random_complex_gaussian();
next_gaussian = r.imag();
has_gaussian = true; has_gaussian = true;
return x1 * w; return r.real();
} }
void swap ( void swap (
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#ifdef DLIB_RAND_KERNEl_ABSTRACT_ #ifdef DLIB_RAND_KERNEl_ABSTRACT_
#include <string> #include <string>
#include <complex>
#include "../uintn.h" #include "../uintn.h"
namespace dlib namespace dlib
...@@ -180,6 +181,14 @@ namespace dlib ...@@ -180,6 +181,14 @@ namespace dlib
with mean 0 and standard deviation 1. with mean 0 and standard deviation 1.
!*/ !*/
std::complex<double> get_random_complex_gaussian (
);
/*!
ensures
- returns a random complex number sampled from a Gaussian distribution
with mean 0 and standard deviation 1.
!*/
void swap ( void swap (
rand& item rand& item
); );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment