Page 1 of 1

Gaussian Noise

Posted: Sat Jan 10, 2015 6:50 pm
by Dutchman
The standard random generators in computer-languages have a uniform distribution.
That means that each number has the same chance to occur.
In natural processes the distribution curve however has often a bell shape.
That means that the frequency of occurrence of extreme numbers is far lower than the mid-values.
A special bell-shape is the Gaussian distribution curve.
It is also called the normal distribution because it is often a characteristic of the variation in normal natural processes.
In order to simulate natural processes, I developed a Gaussian number generator according to the "Marsaglia polar method".
See http://en.wikipedia.org/wiki/Marsaglia_polar_method
By using the complex random number generator of SB, it was rather easy to do.
The following code is a program in which the distribution curve of that function 'GRAND()' is tested.

Code: Select all

'Gaussian Noise: function GRANDC() with testprogram
'by Dutchman, january 2015
twidth=3 'total width: sigma's
count=1E5 'total of random numbers
mid=12 'mid size of distribution
FontSize=FONT_SIZE()
sh=SCREEN_HEIGHT()
lines=sh/(1.2*fontsize)-5 ' number of lines to print
size=2*mid+1 'array-size
OPTION BASE 0
DIM number(size)
'---- fill array with distribution
PRINT "Calculating:"
sw=SCREEN_WIDTH()
SLIDER "run" VALUE 0 AT 0,30 SIZE sw
FOR i=1 TO count/2 'two numbers per cycle
  n=GRANDC()
  u=REAL(n) ! i1=mid+INT(mid/twidth*u)
  v=IMAG(n) ! i2=mid+INT(mid/twidth*v)
  IF i1<size AND i1>0 THEN number(i1)+=1
  IF i2<size AND i2>0 THEN number(i2)+=1
  IF i%100=0 THEN SLIDER "run" SET VALUE 2*i/count
NEXT i
SLIDER "run" DELETE ! TEXT CLEAR
'---- show distribution
PRINT "Distributioin within total width of";twidth;"sigma"
PRINT "Width (%)","Total (%)"
n=number(mid) ! i=0
repeat:
PRINT "####.#":100*(2*i+1)/size,"####.#":100*n/count
i+=1
IF i<=MIN(mid,lines) THEN
  n=n+number(mid+i)+number(mid-i)
  GOTO repeat
ENDIF
'---------- peak calculation
peak:
p=200
DO
  p-=1
  n=10^-p+1i*10^-p
UNTIL ABS(n)^2>1E-300
p-=1
GRANDC.preset=1
GRANDC.n=10^-p+1i*10^-p
GRANDC.s=ABS(GRANDC.n)^2
PRINT
PRINT "Peak=";GRANDC();" at seed: n=";GRANDC.n
END


DEF GRANDC()
'Generates random complex number
'with Gaussian distribution
'according to the Marsaglia polar method
'r'Remove tinted lines: is only for peak testing
IF preset THEN Transform
''Start with random complex number within limits
DO ! n=RNDC(1) ! s=ABS(n)^2
UNTIL s<1 AND s>1E-320
Transform:
m=SQR(-2*LN(s)/s)
u=SIGN(RND(1)-0.5)*REAL(n)*m
v=SIGN(RND(1)-0.5)*IMAG(n)*m
RETURN u+1i*v
END DEF

Re: Gaussian Noise

Posted: Sun Jan 11, 2015 4:40 pm
by Mr. Kibernetik
So, what do you think about comparison with default RNDC() function distribution? It seems to me that it gives better result. Not?

Re: Gaussian Noise

Posted: Sun Jan 11, 2015 4:54 pm
by Dutchman
Mr. Kibernetik wrote:So, what do you think about comparison with default RNDC() function distribution? It seems to me that it gives better result. Not?
RNDC() function gives a flat distribution. Therefore I could use that to make GRAND.
Why compare? The application is different. In fact it is an addition to RNDC(). It generates random complex numbers with a gaussian distribution.
I will change the name from GRAND() to GRANDC(). ;)

Re: Gaussian Noise

Posted: Sun Jan 11, 2015 4:56 pm
by Mr. Kibernetik
I see! You needed Gaussian distribution :!:

Re: Gaussian Noise

Posted: Sun Jan 11, 2015 4:58 pm
by Dutchman
Yes, that gives more 'natural' distribution. I explain that in the introduction. :D

Re: Gaussian Noise

Posted: Mon Jan 12, 2015 12:50 pm
by Dutchman
The original GRANDC() function gave 'NAN+INFi' as maximum output.
NAN however means 'Not A Number', so I modified the function to generate representable peak-values.
The peak value is now 26+26i, but the probability that this peakvalue is generated is very very very low :D

Re: Gaussian Noise

Posted: Mon Jan 12, 2015 12:52 pm
by Mr. Kibernetik
Why not to be inside [0;1] ?

Re: Gaussian Noise

Posted: Mon Jan 12, 2015 12:57 pm
by Dutchman
Mr. Kibernetik wrote:Why not to be inside [0;1] ?
Gaussian noise in principle has very large peaks, positive AND negative, although with a low probability. :!:
So it is not 'natural' to limit it in the range 0…1.

Re: Gaussian Noise

Posted: Mon Jan 12, 2015 12:59 pm
by Mr. Kibernetik
[-1;1]?

Re: Gaussian Noise

Posted: Mon Jan 12, 2015 2:39 pm
by Dutchman
Why should it be limited at all?
Tangent of 90° is in the app limited to 1.6E+16. For the same reason GRANDC() is limited now to 26.18… although it is incorrect.
The user can limit or scale the output to its own use.