annotate projects/d-box/PinkNoise.h @ 0:8a575ba3ab52

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