Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Gaussian noise

Tags:

c++

matlab

I have a .arff file which contains a list of float numbers. I need to add to every number a gaussian noise, which in MATLAB would be:

m = m+k*randn(size(m)

where m is one of the numbers in the list and k is a standard deviation and has value 0.1. What is the C++ equivalent to randn()?

Could you please provide an example?

like image 544
Guvi Avatar asked Oct 01 '15 13:10

Guvi


People also ask

How do you add a Gaussian noise to a signal?

y = awgn( x , snr ) adds white Gaussian noise to the vector signal x . This syntax assumes that the power of x is 0 dBW. For more information about additive white Gaussian noise, see What is AWGN? y = awgn( x , snr , signalpower ) accepts an input signal power value in dBW.

Where do you put Gaussian noise?

We can add a GaussianNoise layer as the input layer. The amount of noise must be small. Given that the input values are within the range [0, 1], we will add Gaussian noise with a mean of 0.0 and a standard deviation of 0.01, chosen arbitrarily.


1 Answers

Use std::normal_distribution with an appropriate generator (std::default_random_engine will usually work). See http://en.cppreference.com/w/cpp/numeric/random for details on all of the random number generation facilities of the C++ standard library.

(live example)

#include <iostream>
#include <iterator>
#include <random>

int main() {
    // Example data
    std::vector<double> data = {1., 2., 3., 4., 5., 6.};

    // Define random generator with Gaussian distribution
    const double mean = 0.0;
    const double stddev = 0.1;
    std::default_random_engine generator;
    std::normal_distribution<double> dist(mean, stddev);

    // Add Gaussian noise
    for (auto& x : data) {
        x = x + dist(generator);
    }

    // Output the result, for demonstration purposes
    std::copy(begin(data), end(data), std::ostream_iterator<double>(std::cout, " "));
    std::cout << "\n";

    return 0;
}

Output:

0.987803 1.89132 3.06843 3.89248 5.00333 6.07448 

Further considerations

For decent statistical properties, you'll probably want to choose the std::mersenne_twister_engine generator (or, for convenience, the std::mt19937 predefined version), and seed it using std::random_device:

std::mt19937 generator(std::random_device{}()); 

[Note: Seeding from std::random_device is a good practice to get into; if you use the current time as a seed, you can end up with the same seed value across multiple generators (e.g. when initialising several generators in a very short period of time). std::random_device will obtain entropy from the system, if available.]

In order to avoid passing the generator to the distribution every time, you can do something like:

auto dist = std::bind(std::normal_distribution<double>{mean, stddev},
                      std::mt19937(std::random_device{}()));

which you can then use as follows:

double val = dist();

(Live example with these modifications)

like image 76
Andrew Avatar answered Oct 15 '22 15:10

Andrew