Thread: Gaussian Noise

  1. #1
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59

    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:
    Gaussian Noise-codecogseqn-2-gif

    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:
    Gaussian Noise-codecogseqn-gif

    so I need to find new Npower but I need to get it in a linear scale, so:
    Gaussian Noise-codecogseqn-1-gif

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would probably be easier to use an approach that is often used for this purpose, such as the Box-Muller transform.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    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. #4
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    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. #5
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Quote Originally Posted by qny View Post
    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?
    Last edited by DeliriumCordia; 09-23-2012 at 03:48 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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 .....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    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. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gaussian Noise Generator
    By trido in forum C Programming
    Replies: 4
    Last Post: 06-29-2009, 05:35 AM
  2. Ubuntu fan noise
    By Stonehambey in forum Tech Board
    Replies: 10
    Last Post: 09-06-2008, 12:28 PM
  3. Noise problems
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 12-04-2005, 01:07 AM
  4. Making noise
    By Unregiste-6 in forum C++ Programming
    Replies: 8
    Last Post: 10-09-2002, 12:24 AM
  5. Noise pollution
    By lockpatrick in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-07-2002, 02:56 AM