Tuesday, February 24, 2009

[SW] GSL -- GNU Scientific Library

God, the open source community always make me surprised!

About 2 or 3 days ago, when I was searching for some open source code to add Gaussian random noise to my artificial data, I found the GSL (GNU Scientific Library). It's amazing. Although I am not very good at programming, but after some trials and with the help of examples, I produced Gaussian distributed random noise successfully! And there is pdf (probability distribution function) function for me to check the Gaussian distribution data.

Big thanks to all the people contributing to the open source world!

Here is the source code I used to generate Gaussian distributed random noise (first image), and also the pdf of the generated random noise (second image).

/*
* gcc -Wall rngGussian.c -o rngGussian -lgsl -lgslcblas -lm
*/

#include <stdio.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

int main(void){
const gsl_rng_type * T;
gsl_rng * r;

double randomNum;
double randomPdf;
int i;

gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);

gsl_rng_set (r, time(NULL)); /* use time() to generate the seed */
for(i=0; i<1000; i++){
randomNum = gsl_ran_gaussian(r, 1.0);
randomPdf = gsl_ran_gaussian_pdf(randomNum, 1.0);
printf("%lf %lf\n", randomNum, randomPdf);
}

gsl_rng_free (r);
return 0;
}

No comments:

Post a Comment