# Thread: Gaussian Noise

1. ## Gaussian Noise

Hi guys,

I start saying that this is not properly a "technical" question, but it requires a little bit knowledge about communications concepts like gaussian noise and SNR.

I'm coding a scientific software, and obviously it has some problem. It's not giving the result I'm expecting, so I triple checked everything and the one I'm proposing seems me to be the weakest part of the software.

Ok, I created an array of complex elementes, but img part is always 0.
I need to create a gaussian noise vector to add to this array to simulate noise conditions.

I've not found source files to generate gaussian noise, so I decided to try by myself: I tought i could generate 1000 arrays of pseudorandom elements between -1 and 1 and sum them time by time. At the end I divide each element by 1000, so I have a "random" array of values in the range [-1,1].

This is the code.

Main part:
Code:
```for (j = 0; j < NVARS; j++) {                     init_genrand(rand());
for (i = 0; i < LENGTH; i++) {
uniformVar[i].real = (genrand_real1()*2 - 1);
uniformVar[i].img = 0.0;

}
cpxvecsum(noisevector, uniformVar, LENGTH);
}```
Now that I have a random array of noise, I need to make it gaussian noise, obviously depending on my SNR value.

First of all I find the power of this array, so I can divide each element for a certain number to make its power=1.

Snippet:
Code:
```float DigitalSignalPower(complex* vettore, int size) {    int i;
float absol;

float temp = 0.0;
for (i = 0; i < size; i++) {
absol = fabs(vettore[i].real);

temp += pow(absol, 2);
}
return temp / size;
}```
what I did is:

and at the end divide by size to normalize the result on the length of the array.

And in the main I divide each element by the square of this number. Now the power of the vector is 1, but I don't know if the "algorithm" is right.

By the way, I know that:

so I need to find new Npower but I need to get it in a linear scale, so:

I have a fixed SNRdB, and I compute Signal power as I computed noise power at the start of the topic:

Code:
```float DigitalSignalPower(complex* vettore, int size) {    int i;
float absol;

float temp = 0.0;
for (i = 0; i < size; i++) {
absol = fabs(vettore[i].real);

temp += pow(absol, 2);
}
return temp / size;
}```
and at the end I multiply my noise vector element by element with the square of this number.

Seems you right?

Maybe you know a faster way to implement gaussian noise?

2. It would probably be easier to use an approach that is often used for this purpose, such as the Box-Muller transform.

3. I didn't check the math.
Just noticed something very wrong at the top of the "Main part"
Code:
`for (j = 0; j < NVARS; j++) {                     init_genrand(rand());`
You initialize the random number generator inside a loop. Don't do that! Initialize it once only.

4. Oh ok, so I just need to fill my noise vector with numbers generated by this transform? I should always normalize it by the SNR value as the function does not get SNR as input... I was looking for something similar to awgn function in Matlab, but I often find it implemented in c++ or other languages.

5. Originally Posted by qny
I didn't check the math.
Just noticed something very wrong at the top of the "Main part"
Code:
`for (j = 0; j < NVARS; j++) {                     init_genrand(rand());`
You initialize the random number generator inside a loop. Don't do that! Initialize it once only.
Ok thanks, it already seems pretty faster!

But results still are wrong. It seems nice the solution proposed by using Box Muller, but it doesn't take SNR as input, and I really don't know how to normalize it... I should use it instead of generating 1000 vars?

6. Box-Muller generates pairs of normally distributed numbers with mean zero and unit variance (assuming a statistically significant number of samples taken, which is sort of the point). It is trivial to do a transformation on such values to get any specified mean and variance.

You already gave formulae relating mean noise level to SNR and signal level .....

7. Ok I think I got it, but if I simulate a vector of 1000 elements, generated with that algorithm, I get a mean that is in the range [-0.05,0.05], isn't it a little big?

8. You are aware of the statistical notion of confidence intervals?

To answer your question, however, for a thousand elements that variability of the mean is not particularly significant.

Popular pages Recent additions