Additive White Gaussian Noise Generator
This function generates an Additive White Gaussian Noise (AWGN) sample at every call. Its beauty lies in its simplicity! The generated sample set will have zero mean and a standard deviation of 1. Therefore, one can simply scale the output samples by a different standard deviation to generate different noise profiles. To do this scaling, simply multiply the sample with the standard deviation of your choice.
This is a very versatile function that can be used in communication system simulations and random number generation in various applications. I have used it in the past while working with channel encoder and decoder simulators.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926536
double AWGN_generator()
{/* Generates additive white Gaussian Noise samples with zero mean and a standard deviation of 1. */
double temp1;
double temp2;
double result;
int p;
p = 1;
while( p > 0 )
{
temp2 = ( rand() / ( (double)RAND_MAX ) ); /* rand() function generates an
integer between 0 and RAND_MAX,
which is defined in stdlib.h.
*/
if ( temp2 == 0 )
{// temp2 is >= (RAND_MAX / 2)
p = 1;
}// end if
else
{// temp2 is < (RAND_MAX / 2)
p = -1;
}// end else
}// end while()
temp1 = cos( ( 2.0 * (double)PI ) * rand() / ( (double)RAND_MAX ) );
result = sqrt( -2.0 * log( temp2 ) ) * temp1;
return result; // return the generated random sample to the caller
}// end AWGN_generator()