PX4 / PX4-SITL_gazebo-classic

Set of plugins, models and worlds to use with OSRF Gazebo Simulator in SITL and HITL.
http://dev.px4.io/simulation-gazebo.html
360 stars 781 forks source link

Incorrectly using Box-Muller transform for barometer noise #817

Open robin-shaun opened 2 years ago

robin-shaun commented 2 years ago

Hello, in the gazebo_barometer_plugin.cpp, the noise is generated using polar form of Box-Muller transformation

 // generate Gaussian noise sequence using polar form of Box-Muller transformation
double y1;
    {
      double x1, x2, w;
      if (!baro_rnd_use_last_) {
        do {
          x1 = 2.0 * standard_normal_distribution_(random_generator_) - 1.0;
          x2 = 2.0 * standard_normal_distribution_(random_generator_) - 1.0;
          w = x1 * x1 + x2 * x2;
        } while ( w >= 1.0 );
        w = sqrt( (-2.0 * log( w ) ) / w );
        // calculate two values - the second value can be used next time because it is uncorrelated
        y1 = x1 * w;
        baro_rnd_y2_ = x2 * w;
        baro_rnd_use_last_ = true;
      } else {
        // no need to repeat the calculation - use the second value from last update
        y1 = baro_rnd_y2_;
        baro_rnd_use_last_ = false;
      }
    }

However, as Wikipedia says, The Box–Muller transform is a random number sampling method for generating pairs of independent, standard, normally distributed given a source of uniformly distributed random numbers. But this code uses standard_normal_distribution_(random_generator_). So I think using standard_normal_distribution_(random_generator_) without using Box–Muller transform is OK.

Jaeyoung-Lim commented 2 years ago

@robin-shaun Thank you for your insights.

If you think the noise generation can be improved, it would be great if you can create a pull request

Jaeyoung-Lim commented 2 years ago

@robin-shaun Any updates?