Gaussian Noise

This is a discussion on Gaussian Noise within the C Programming forums, part of the General Programming Boards category; Hi guys, I start saying that this is not properly a "technical" question, but it requires a little bit knowledge ...

  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:
    Name:  CodeCogsEqn (2).gif
Views: 525
Size:  994 Bytes

    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:
    Name:  CodeCogsEqn.gif
Views: 529
Size:  823 Bytes

    so I need to find new Npower but I need to get it in a linear scale, so:
    Name:  CodeCogsEqn (1).gif
Views: 527
Size:  1.0 KB

    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,205
    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%.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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,205
    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%.

  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,205
    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%.

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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21