DSPRelated.com
Forums

gaussian white noise generation

Started by sita January 18, 2006
Steve Underwood wrote:


> ... Of course, if anyone complains you are short > changing them on precision, you have the option of baking 13 values > instead of 12. :-)
So 6.5 numbers would be half baked? :-) Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
On 2006-01-18 11:03:27 -0400, Jerry Avins <jya@ieee.org> said:

> Andor wrote: >> Naebad wrote: >> >> >>>> can any one please suggest me how to generate guassian white noise with >>>> zero mean and variance 1 in matlab? >>>> >>>> thanks. >>>> >>> >>> If you have uniform distributed noise you can take say 12 samples ... >> >> >> There's that number again. There must be something about it that I >> don't know about :-). > > Six works pretty well too. 13 is better; you have to stop somewhere.
The variance of the uniform is 1/12 so 12 leads to a variance of 1 for the sum. Adequate as an excuse but a bit weak as a reason. The resulting approximate Gaussian should only be used until you have 10 minutes to do it better.
> Jerry
Gordon Sande wrote:
> On 2006-01-18 11:03:27 -0400, Jerry Avins <jya@ieee.org> said: > >> Andor wrote: >> >>> Naebad wrote: >>> >>> >>>>> can any one please suggest me how to generate guassian white noise >>>>> with >>>>> zero mean and variance 1 in matlab? >>>>> >>>>> thanks. >>>>> >>>> >>>> If you have uniform distributed noise you can take say 12 samples ... >>> >>> >>> >>> There's that number again. There must be something about it that I >>> don't know about :-). >> >> >> Six works pretty well too. 13 is better; you have to stop somewhere. > > > The variance of the uniform is 1/12 so 12 leads to a variance of 1 > for the sum. Adequate as an excuse but a bit weak as a reason. > The resulting approximate Gaussian should only be used until you > have 10 minutes to do it better.
Ten minutes and the necessary smarts. What's a better way? Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
On 2006-01-18 12:15:43 -0400, Jerry Avins <jya@ieee.org> said:

> Gordon Sande wrote: >> On 2006-01-18 11:03:27 -0400, Jerry Avins <jya@ieee.org> said: >> >>> Andor wrote: >>> >>>> Naebad wrote: >>>> >>>> >>>>>> can any one please suggest me how to generate guassian white noise with >>>>>> zero mean and variance 1 in matlab? >>>>>> >>>>>> thanks. >>>>>> >>>>> >>>>> If you have uniform distributed noise you can take say 12 samples ... >>>> >>>> >>>> >>>> There's that number again. There must be something about it that I >>>> don't know about :-). >>> >>> >>> Six works pretty well too. 13 is better; you have to stop somewhere. >> >> >> The variance of the uniform is 1/12 so 12 leads to a variance of 1 >> for the sum. Adequate as an excuse but a bit weak as a reason. >> The resulting approximate Gaussian should only be used until you >> have 10 minutes to do it better. > > Ten minutes and the necessary smarts. What's a better way?
Ask google about Box-Muller transformation. Turns uniforms into Gaussians exactly. There ae a couple related transforms that are also possible. Then there are the various schemes put forward by Marsaglia.
> Jerry
Jerry Avins wrote:
> Gordon Sande wrote: > > On 2006-01-18 11:03:27 -0400, Jerry Avins <jya@ieee.org> said: > > > >> Andor wrote: > >> > >>> Naebad wrote: > >>> > >>> > >>>>> can any one please suggest me how to generate guassian white noise > >>>>> with > >>>>> zero mean and variance 1 in matlab? > >>>>> > >>>>> thanks. > >>>>> > >>>> > >>>> If you have uniform distributed noise you can take say 12 samples ... > >>> > >>> > >>> > >>> There's that number again. There must be something about it that I > >>> don't know about :-). > >> > >> > >> Six works pretty well too. 13 is better; you have to stop somewhere. > > > > > > The variance of the uniform is 1/12 so 12 leads to a variance of 1 > > for the sum. Adequate as an excuse but a bit weak as a reason. > > The resulting approximate Gaussian should only be used until you > > have 10 minutes to do it better. > > Ten minutes and the necessary smarts. What's a better way?
"Central limit theorem" based methods just keep getting better (and slower) as you add in more numbers. 12 eliminates the division, but the division (really just a multiplication by a constant) is extremely cheap wrt to generation of the 12 rvs. If you add N rvs (let's assume the individual rvs are rectangular) then the p-p value of the gaussian approximation will only be N times the p-p value of the individual rvs. The tails won't be particularly accurate past about N/3 SDs away from the mean. Note that the N rvs must be independent. Many simple (particularly the fast ones) rectangular number generators aren't really good enough. The other approach (that I think Jerry was alluding to) involves a transformation: Start with two independent rectangular on [0,1] rvs, R1 and R2. Calculate Z1 = sqrt(-2 ln (R1)) cos(2 pi R2) Z2 = sqrt(-2 ln (R1)) sin(2 pi R2) Z1 and Z2 are gaussian rvs, with zero mean and sd of 1. This of course uses nasty logs and trig. However, on a modern FPU, they should be pretty quick, perhaps much faster than generating and summing 12 good rvs. Accuracy in the tails still sucks somewhat. This is determined by the precision of R1. The 'tails' come from really small R1 values (since sin/cos are limited to +/-1, the log is the only thing that can generate large numbers, and this happens when its argument is close to zero). More bits in R1 help. The last time I played with these things, I found that 32 bits wasn't anywhere near enough. I think I might have been doing BER simulations on a modem, and the tails mattered a lot! Regards, Allan
Gordon Sande wrote:
> On 2006-01-18 12:15:43 -0400, Jerry Avins <jya@ieee.org> said: > > > Gordon Sande wrote: > >> On 2006-01-18 11:03:27 -0400, Jerry Avins <jya@ieee.org> said: > >> > >>> Andor wrote: > >>> > >>>> Naebad wrote: > >>>> > >>>> > >>>>>> can any one please suggest me how to generate guassian white noise with > >>>>>> zero mean and variance 1 in matlab? > >>>>>> > >>>>>> thanks. > >>>>>> > >>>>> > >>>>> If you have uniform distributed noise you can take say 12 samples ... > >>>> > >>>> > >>>> > >>>> There's that number again. There must be something about it that I > >>>> don't know about :-). > >>> > >>> > >>> Six works pretty well too. 13 is better; you have to stop somewhere. > >> > >> > >> The variance of the uniform is 1/12 so 12 leads to a variance of 1 > >> for the sum. Adequate as an excuse but a bit weak as a reason. > >> The resulting approximate Gaussian should only be used until you > >> have 10 minutes to do it better. > > > > Ten minutes and the necessary smarts. What's a better way? > > Ask google about Box-Muller transformation. Turns uniforms into > Gaussians exactly. There ae a couple related transforms that > are also possible. Then there are the various schemes put forward > by Marsaglia.
Ah yes. Box-Muller was the name I was trying to remember.
> Turns uniforms into Gaussians exactly.
I argue that your statement is only true for infinite precision arithmetic. Practical precisions may result in a poor approximation. I suggest that the user works out what precision they actually need. Regards, Allan
allanherriman@hotmail.com writes:
> [...] > Gordon Sande wrote: >> Turns uniforms into Gaussians exactly. > > I argue that your statement is only true for infinite precision > arithmetic. Practical precisions may result in a poor approximation. > I suggest that the user works out what precision they actually need.
Ayup. B-M "only" requires ln(), sqrt(), and sin/cos. What a fixed-point nightmare! Summing a few uniforms is MUCH easier (and probably just as or more accurate for a given computation time). -- % Randy Yates % "Bird, on the wing, %% Fuquay-Varina, NC % goes floating by %%% 919-577-9882 % but there's a teardrop in his eye..." %%%% <yates@ieee.org> % 'One Summer Dream', *Face The Music*, ELO http://home.earthlink.net/~yatescr

allanherriman@hotmail.com wrote:


> Start with two independent rectangular on [0,1] rvs, R1 and R2. > > Calculate > Z1 = sqrt(-2 ln (R1)) cos(2 pi R2) > Z2 = sqrt(-2 ln (R1)) sin(2 pi R2) > > Z1 and Z2 are gaussian rvs, with zero mean and sd of 1. > > This of course uses nasty logs and trig. However, on a modern FPU, > they should be pretty quick, perhaps much faster than generating and > summing 12 good rvs. > > Accuracy in the tails still sucks somewhat. This is determined by the > precision of R1. The 'tails' come from really small R1 values (since > sin/cos are limited to +/-1, the log is the only thing that can > generate large numbers, and this happens when its argument is close to > zero). More bits in R1 help. > > > The last time I played with these things, I found that 32 bits wasn't > anywhere near enough. I think I might have been doing BER simulations > on a modem, and the tails mattered a lot!
I fully agree with your notes on the tails of the generated gaussian distribution, especially if it is critical to have good statistics at the range over 4x standard deviation. The double precision accuracy is required for R1, and the good 64bit random number generator is required also. Indeed it is faster to generate two gaussian numbers using single precision float and rand(), and add those to make better distribution. The single precision works well up to 3xSD, so the sum should be good enough for up to 6xSD. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
"Jerry Avins" <jya@ieee.org> wrote in message
news:wdqdnQYS9OboxVPeRVn-qg@rcn.net...
> Naebad wrote:
> What is the DC level of a single average? Assuming that the RNG is > itself zero mean, you can omit the subtraction step. > > Jerry > -- > Engineering is the art of making what you want from things you can get. >
I think it's 6 but it's so long since I did this... Naebad
y = randn(1);

(The "1" means "1x1" array, i.e., a scalar).

--RY