Mercurial > hg > beaglert
comparison projects/d-box/PinkNoise.h @ 0:8a575ba3ab52
Initial commit.
author | andrewm |
---|---|
date | Fri, 31 Oct 2014 19:10:17 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a575ba3ab52 |
---|---|
1 #ifndef _PinkNoise_H | |
2 #define _PinkNoise_H | |
3 | |
4 // Technique by Larry "RidgeRat" Trammell 3/2006 | |
5 // http://home.earthlink.net/~ltrammell/tech/pinkalg.htm | |
6 // implementation and optimization by David Lowenfels | |
7 | |
8 #include <cstdlib> | |
9 #include <ctime> | |
10 #include <stdlib.h> | |
11 | |
12 #define PINK_NOISE_NUM_STAGES 3 | |
13 | |
14 class PinkNoise { | |
15 public: | |
16 PinkNoise() { | |
17 srand ( time(NULL) ); // initialize random generator | |
18 clear(); | |
19 } | |
20 | |
21 void clear() { | |
22 for( size_t i=0; i< PINK_NOISE_NUM_STAGES; i++ ) | |
23 state[ i ] = 0.0; | |
24 } | |
25 | |
26 float tick() { | |
27 static const float RMI2 = 2.0 / float(RAND_MAX); // + 1.0; // change for range [0,1) | |
28 static const float offset = A[0] + A[1] + A[2]; | |
29 | |
30 // unrolled loop | |
31 float temp = float( rand() ); | |
32 state[0] = P[0] * (state[0] - temp) + temp; | |
33 temp = float( rand() ); | |
34 state[1] = P[1] * (state[1] - temp) + temp; | |
35 temp = float( rand() ); | |
36 state[2] = P[2] * (state[2] - temp) + temp; | |
37 return ( A[0]*state[0] + A[1]*state[1] + A[2]*state[2] )*RMI2 - offset; | |
38 } | |
39 | |
40 protected: | |
41 float state[ PINK_NOISE_NUM_STAGES ]; | |
42 static const float A[ PINK_NOISE_NUM_STAGES ]; | |
43 static const float P[ PINK_NOISE_NUM_STAGES ]; | |
44 }; | |
45 | |
46 //const float PinkNoise::A[] = { 0.02109238, 0.07113478, 0.68873558 }; // rescaled by (1+P)/(1-P) | |
47 //const float PinkNoise::P[] = { 0.3190, 0.7756, 0.9613 }; | |
48 | |
49 #endif |