Hi, i try to program a white noise generator with gaussian distribution. When i first used the rand() function i connected the 12-Bit DA-converter to a speaker. A periodic timer interrupt (about 400kHz) gets a rand number and puts it to the DA-converter.
I expected white noise, but wasn't satiesfied, i could hear a hum inside the white noise. The random numbers seemed to have some periodic pattern with a high repeat rate inside. I think with a spectrum-analyzer there would be some extra lines inside the spectrum. Now i wrote my own rand() function.
//*************************************************************************** int my_rand(int init_rand) { static unsigned int Z; if (init_rand) Z = init_rand; if (Z & 0x80000000) { Z<<=1; Z^=0x04C11DB7;//i saw it in a dream ;-) } else { Z<<=1; } return Z; } //***************************************************************************
After once beeing initialised with a value not zero it sounds great.
But is it mathematical correct?
After how many cycles does the initial value return?
Can i get a pseudo gaussian distribution with the following?
Z = my_rand(0); K = ((Z>>11 ^ Z>>22) & 0x3FF) +((Z>>11 ^ Z ) & 0x3FF) +((Z>>22 ^ Z>>5 ) & 0x3FF) +((Z ^ Z>>17) & 0x3FF); DAC0DAT = K<<16;//the 12bit-ADuC7026-DA-converter
There must be a more elegant method to have gaussian distribution!
Thanks for advice.
I beleive that the rand() function is supposed to model a uniform distribution, not a Gaussian one. Tell us what you are trying to achieve and we might be able to help. If you are interested in the subject, read up on it. You could start with a Wikipedia article:
en.wikipedia.org/.../White_noise
- mike