Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / core / gauss.c @ 1114

History | View | Annotate | Download (652 Bytes)

1 998 nparis
/**
2
 * @file gauss.c
3
 * @author Colony Project
4
 *
5
 * @brief Generates noise.
6
 *
7
 * Uses a Gaussian curve to approximate noise
8
 * for robots in the simulator.
9
 **/
10
11
12
#include <stdlib.h>
13
#include <math.h>
14 1000 bpoole
#include "gauss.h"
15 999 nparis
16 1002 bpoole
double chaos_scalar;
17 1000 bpoole
18 1002 bpoole
void gauss_init(double chaos, unsigned int seed)
19 998 nparis
{
20 1002 bpoole
    chaos_scalar = chaos;
21
    srand(seed);
22
}
23 998 nparis
24 1002 bpoole
double gauss_noise(void)
25
{
26
    double x1, x2, w, y1, y2;
27
28 1000 bpoole
    do
29
    {
30
        x1 = 2.0 * rand()/RAND_MAX - 1.0;
31
        x2 = 2.0 * rand()/RAND_MAX - 1.0;
32
        w = x1 * x1 + x2 * x2;
33
    } while ( w >= 1.0 );
34 1002 bpoole
    w = sqrtf( (-2.0 * logf( w ) ) / w );
35
36
    return chaos_scalar*x1*w;
37 998 nparis
}
38
39