Gaussian Noise

Post Reply
User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Gaussian Noise

Post 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
Attachments
Screenshot
Screenshot
screen.PNG (120.46 KiB) Viewed 3648 times
Last edited by Dutchman on Mon Jan 12, 2015 12:46 pm, edited 4 times in total.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Gaussian Noise

Post 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?

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Gaussian Noise

Post 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(). ;)

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Gaussian Noise

Post by Mr. Kibernetik »

I see! You needed Gaussian distribution :!:

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Gaussian Noise

Post by Dutchman »

Yes, that gives more 'natural' distribution. I explain that in the introduction. :D

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Gaussian Noise

Post 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

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Gaussian Noise

Post by Mr. Kibernetik »

Why not to be inside [0;1] ?

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Gaussian Noise

Post 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.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Gaussian Noise

Post by Mr. Kibernetik »

[-1;1]?

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Gaussian Noise

Post 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.

Post Reply