Main Archive Specials Wiki | FAQ Links Submit Forum


C++ gaussian noise generation

Type : gaussian noise generation
References : Posted by paul[at]expdigital[dot]co[dot]uk

Notes :
References :
Tobybears delphi noise generator was the basis. Simply converted it to C++.
Link for original is:
http://www.musicdsp.org/archive.php?classid=0#129
The output is in noise.


Code :
/* Include requisits */
#include
#include

/* Generate a new random seed from system time - do this once in your constructor */
srand(time(0));

/* Setup constants */
const static int q = 15;
const static float c1 = (1 << q) - 1;
const static float c2 = ((int)(c1 / 3)) + 1;
const static float c3 = 1.f / c1;

/* random number in range 0 - 1 not including 1 */
float random = 0.f;

/* the white noise */
float noise = 0.f;

for (int i = 0; i < numSamples; i++)
{
random = ((float)rand() / (float)(RAND_MAX + 1));
noise = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3;
}



Comments


Added on : 10/07/09 by none[ AT ]none[ DOT ]com
Comment :
What's the difference between the much simpler noise generator:

randSeed = (randSeed * 196314165) + 907633515;    out=((int)randSeed)*0.0000000004656612873077392578125f;

and this one? they both sound the same to my ears...




Added on : 22/07/11 by unknown[ AT ]unknown[ DOT ]com
Comment :
How can you change the variance (sigma)?



Added on : 12/06/13 by bebul[ AT ]none[ DOT ]com
Comment :
This is NOT a good code to generate Gaussian Noice. Look into:
  (random * c2) + (random * c2) + (random * c2)
It is all nonsense! The reason of adding three numbers it the Central Limit Theorem to aproximate Gaussian distribution. But the random numbers inside must differ, which is not the case. The code on original link http://www.musicdsp.org/archive.php?classid=0#129 is correct.




Add your own comment
Comments are displayed in fixed width, no HTML code allowed!
Email:

Comment:

Are you human?



Site created and maintained by Bram
Graphic design by line.out | Server sponsered by fxpansion