> Programming - adding noise to data?

Programming - adding noise to data?

Posted at: 2014-12-18 
I've been asked a question to plot a graph of sin(x), but also to add random noise to the graph. I'm using Labwindows CVI 2009 which is based on C++. Anyone know how i'd go about adding this noise?

"Random noise" usually means Gaussian random noise. If you have the header (not formally part of C++ until the 2011 revision, but implemented in many compilers as part of the "C++0x" run-up) then it's built-in. Include and use:

double amplitude = 10.0; // signal amplitude

double SNR = 0.1; // signal-to-noise ratio

default_random_engine engine;

normal_distribution noise(0.0, SNR);

Then just add the noise value to your sine output with:

output = amplitude*( sin(x) + noise(engine) );

I'd have to look up how to get a good Gaussian value from the C rand() function from . That's the only random generator officially part of C++ prior to the C++11 standard. You can fake it with the average of a few independent uniform variables, multiplied by the square root of the

Define a function

double urand()

{

.... return -1.0 + 2.0*rand()/RAND_MAX; // uniform: -1.0<=u1<=1.0

}

That's uniform on the interval [-1,1], with standard deviation 1/sqrt(3).

A sum of n variables with standard deviation s will have a standard deviation of s*sqrt(n), so adding 3 of them together will get a somewhat normal (Gaussian) distribution with standard deviaition of 1, so you can output:

amplitude*(sin(x) + SNR*(urand() + urand() + urand()))

...as your noisy sine value.