Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
noisegenerator.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __NOISEGENERATOR_H
25 #define __NOISEGENERATOR_H
26 
27 #include <stdlib.h>
28 #include <time.h>
29 #include <cmath>
30 #include <assert.h>
31 
32 #include "randomgenerator.h"
33 
34 /** Interface and basic class for noise generator.
35  It is suitable for single noise channels but also multidimensional noise.
36  */
38 public:
40  {
41  dimension = 0;
42  randGen = 0;
43  ownRandGen = false;
44  };
45 
46  virtual ~NoiseGenerator()
47  {
48  if (this->ownRandGen && this->randGen)
49  {
50  delete this->randGen;
51  this->randGen = 0;
52  this->ownRandGen = false;
53  }
54  }
55 
56  /** initialization with the the given dimension for multidimensional noise
57  @param dimension dimensionality of vectors to be used by add
58  @param randGen pointer to a random generator. If zero a new one generated internally
59  @see add()
60  */
61  virtual void init(unsigned int dimension, RandGen* randGen=0)
62  {
63  this->dimension = dimension;
64  if(randGen)
65  this->randGen=randGen;
66 
67  else {
68  this->randGen=new RandGen();
69  this->randGen->init(rand());
70  this->ownRandGen=true;
71  }
72  };
73 
74  /** generate somehow distributed random number parameterized with min and max.
75  valid only for ONE random number, use \ref add() for
76  adding this kind of noise to several channels
77  */
78  virtual double generate() = 0;
79 
80  /** adds multidimensional noise to the value field.
81  Generic implementation calls generate for each channel.
82  Overload this if you need different behavior.
83  @param value field where noise is added. Must have length dimension (\ref init())
84  */
85  virtual void add(double *value, double noiseStrength){
86  for (unsigned int i = 0; i < dimension; i++){
87  value[i]+=generate()*noiseStrength;
88  }
89  }
90 
91  virtual unsigned int getDimension() const {
92  return dimension;
93  }
94 
95  virtual void setDimension(unsigned int dim){
96  if(dimension!=dim){
97  init(dim, randGen);
98  }
99  }
100 
101 protected:
102  //generates white (no averaging) uniformly distributed random number between "min" and "max"
103  double uniform(double min=-0.1, double max=0.1){
104  assert(randGen);
105  return( randGen->rand()*(max-min)+min);
106  }
107  //generates white uniformly distributed random number between in [0,1)
108  double uniform01(){
109  assert(randGen);
110  return randGen->rand();
111  }
112  unsigned int dimension;
115 };
116 
117 /// generates no noise
118 class NoNoise : public NoiseGenerator{
119 public:
120  NoNoise() {}
121  virtual ~NoNoise(){}
122  virtual double generate() {
123  return 0;
124  };
125 };
126 
127 
128 /// generates white (no averaging) uniformly distributed random number between "min" and "max"
130 public:
132  virtual ~WhiteUniformNoise(){}
133  virtual double generate() {
134  return uniform(-1,1);
135  };
136 
137 };
138 
139 /// generates white and normal distributed random numbers. p1: mean, p2: standard deviation
140 /// new parameter definition: p1: min, p2: max. the mean and standard deviation are calculated by this values
142 public:
144  virtual ~WhiteNormalNoise(){}
145  virtual double generate() {
146  double x1=uniform01();
147  double x2=uniform01();
148  return( (sqrt(-2*log(x1)) *cos(2*M_PI*x2)));
149  };
150  // original version
151  // virtual double generate(double mean, double stddev) {
152  // double x1=uniform(0, 1);
153  // double x2=uniform(0, 1);
154  // return( (sqrt(-2*log(x1)) *cos(2*M_PI*x2)) * stddev + mean) ;
155  // }
156 
157 };
158 
159 /// generated colored noise. This is obtained by using time average of uniform distributed noise.
161 public:
162  /** @param tau time averaging factor (1/window)
163  (1: smoothing (white) 0.1: strong color, 0 no noise anymore
164  */
165  ColorUniformNoise(double tau=0.05)
166  : tau(tau){
167  sqrttau = sqrt(tau);
168  mean1channel=0.0;
169  mean=0;
170  }
171  virtual ~ColorUniformNoise(){ if(mean) free(mean);}
172  virtual void init(unsigned int dimension, RandGen* randGen=0){
173  NoiseGenerator::init(dimension, randGen);
174  mean = (double*)malloc(sizeof(double) * dimension);
175  for (unsigned int i=0; i<dimension; i++){
176  mean[i]=0.0;
177  }
178  }
179 
180  virtual double generate() {
181  mean1channel+= sqrttau * uniform(-1, +1) - tau * mean1channel;
182  return(mean1channel);
183  }
184 
185  /** adds multidimensional noise to the value field.
186  @param value field where noise is added. Must have length dimension (\ref init())
187  @param min lower bound of interval
188  @param max upper bound of interval
189  */
190  virtual void add(double *value, double noiseStrength){
191  for (unsigned int i = 0; i < dimension; i++){
192  mean[i]+= sqrttau * uniform(-1, +1)*noiseStrength - tau * mean[i];
193  value[i]+=mean[i];
194  }
195  }
196 
197  virtual double getTau(){ return tau;}
198  virtual void setTau(double newTau){
199  if(newTau >=0 && newTau <= 1){
200  tau=newTau;
201  sqrttau = sqrt(tau);
202  }
203  }
204 
205 protected:
206  double tau; // smoothing paramter
207  double sqrttau; // square root of smoothing parameter
208  double* mean;
209  double mean1channel;
210 };
211 
212 /// like ColorUniformNoise but averaging over normal distributed noise instead.
214 public:
215  ColorNormalNoise(double tau=0.05)
216  : tau(tau){
217  sqrttau = sqrt(tau);
218  mean = 0;
219  mean1channel=0.0;
220  }
221 
222  virtual ~ColorNormalNoise(){if(mean) free(mean);}
223 
224  virtual void init(unsigned int dimension, RandGen* randGen=0){
225  WhiteNormalNoise::init(dimension, randGen);
226  mean = (double*)malloc(sizeof(double) * dimension);
227  for (unsigned int i=0; i<dimension; i++){
228  mean[i]=0.0;
229  }
230  }
231 
232  virtual double generate() { //double stddev, double meanvalue) {
234  return(mean1channel);
235  }
236 
237  virtual void add(double *value, double noiseStrength) {
238  for (unsigned int i = 0; i < dimension; i++){
239  mean[i]+=sqrttau * WhiteNormalNoise::generate()*noiseStrength - tau*mean[i];
240  value[i]+=mean[i];
241  }
242  }
243 
244  virtual double getTau(){ return tau;}
245  virtual void setTau(double newTau){
246  if(newTau >=0 && newTau <= 1){
247  tau=newTau;
248  sqrttau = sqrt(tau);
249  }
250  }
251 
252 protected:
253  double tau; // smoothing paramter
254  double sqrttau; // square root of smoothing parameter
255  double* mean;
256  double mean1channel;
257  double factor;
258 };
259 
260 /// Sine wave noise. Produces a 90 degree phase shifted sine wave or white noise
262 public:
263  /** @param omega anglerate
264  @param amplitude weighting of sine wave against noise strength
265  @param phaseShift phase shift between channels in rad
266  @param channels number of channel for sine noise (and the rest get white noise)
267  */
268  SineWhiteNoise(double omega, double amplitude, double phaseShift = M_PI/2,
269  unsigned int channels = 0xFFFF)
270  : omega(omega), amplitude(amplitude)
272  t=0;
273  }
274 
275  virtual ~SineWhiteNoise(){}
276 
277  virtual double generate() {
278  t ++;
279  return (1-amplitude)*uniform(-1, 1) + sin(t*omega)*amplitude;
280  }
281 
282  /** adds multidimensional noise to the value field.
283  @param value field where noise is added. Must have length dimension (\ref init())
284  */
285  virtual void add(double *value, double noiseStrength) { // min, double max){
286 
287  for (unsigned int i = 0; i < dimension; i++){
288  if(i < channels){
289  value[i]+=sin(t*omega+i*phaseShift)*amplitude*noiseStrength;
290  value[i]+=(1-amplitude)*uniform(-1, 1)*noiseStrength;
291  }else{
292  value[i]+=uniform(-1, 1)*noiseStrength;
293  }
294  }
295  t ++;
296  }
297  void setOmega(double omega){
298  this->omega=omega;
299  }
300  void setPhaseShift(double phaseShift){
301  this->phaseShift=phaseShift;
302  }
303 
304 protected:
305  long int t; // time
306  double omega; // angle velocity
307  double amplitude; // factor for noise strength
308  unsigned int channels; // number of channels with sine
309  double phaseShift; // phase shift
310 
311 };
312 
313 
314 #endif
315 
unsigned int channels
Definition: noisegenerator.h:308
virtual double generate()
generate somehow distributed random number parameterized with min and max.
Definition: noisegenerator.h:133
RandGen * randGen
Definition: noisegenerator.h:113
generates white (no averaging) uniformly distributed random number between "min" and "max" ...
Definition: noisegenerator.h:129
double amplitude
Definition: noisegenerator.h:307
generates no noise
Definition: noisegenerator.h:118
virtual void setTau(double newTau)
Definition: noisegenerator.h:245
virtual ~WhiteUniformNoise()
Definition: noisegenerator.h:132
void setOmega(double omega)
Definition: noisegenerator.h:297
double rand()
returns a value in [0,1)
Definition: randomgenerator.h:42
virtual void add(double *value, double noiseStrength)
adds multidimensional noise to the value field.
Definition: noisegenerator.h:85
virtual ~WhiteNormalNoise()
Definition: noisegenerator.h:144
WhiteNormalNoise()
Definition: noisegenerator.h:143
double sqrttau
Definition: noisegenerator.h:207
ColorUniformNoise(double tau=0.05)
Definition: noisegenerator.h:165
bool ownRandGen
Definition: noisegenerator.h:114
virtual double generate()
generate somehow distributed random number parameterized with min and max.
Definition: noisegenerator.h:277
virtual void setDimension(unsigned int dim)
Definition: noisegenerator.h:95
like ColorUniformNoise but averaging over normal distributed noise instead.
Definition: noisegenerator.h:213
unsigned int dimension
Definition: noisegenerator.h:112
virtual double getTau()
Definition: noisegenerator.h:197
virtual ~ColorNormalNoise()
Definition: noisegenerator.h:222
double * mean
Definition: noisegenerator.h:208
virtual void add(double *value, double noiseStrength)
adds multidimensional noise to the value field.
Definition: noisegenerator.h:237
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
double sqrttau
Definition: noisegenerator.h:254
virtual void add(double *value, double noiseStrength)
adds multidimensional noise to the value field.
Definition: noisegenerator.h:285
struct _RandGen RandGen
random generator with 48bit integer arithmentic
long int t
Definition: noisegenerator.h:305
double omega
Definition: noisegenerator.h:306
virtual double generate()
generate somehow distributed random number parameterized with min and max.
Definition: noisegenerator.h:180
NoNoise()
Definition: noisegenerator.h:120
virtual double generate()
generate somehow distributed random number parameterized with min and max.
Definition: noisegenerator.h:122
void init(long int seedval)
Definition: randomgenerator.h:38
virtual ~NoNoise()
Definition: noisegenerator.h:121
virtual unsigned int getDimension() const
Definition: noisegenerator.h:91
ColorNormalNoise(double tau=0.05)
Definition: noisegenerator.h:215
virtual ~ColorUniformNoise()
Definition: noisegenerator.h:171
double phaseShift
Definition: noisegenerator.h:309
WhiteUniformNoise()
Definition: noisegenerator.h:131
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
virtual ~NoiseGenerator()
Definition: noisegenerator.h:46
virtual void init(unsigned int dimension, RandGen *randGen=0)
initialization with the the given dimension for multidimensional noise
Definition: noisegenerator.h:61
virtual void init(unsigned int dimension, RandGen *randGen=0)
initialization with the the given dimension for multidimensional noise
Definition: noisegenerator.h:172
virtual ~SineWhiteNoise()
Definition: noisegenerator.h:275
void setPhaseShift(double phaseShift)
Definition: noisegenerator.h:300
virtual double generate()
generate somehow distributed random number parameterized with min and max.
Definition: noisegenerator.h:232
virtual double generate()
generate somehow distributed random number parameterized with min and max.
Definition: noisegenerator.h:145
double uniform(double min=-0.1, double max=0.1)
Definition: noisegenerator.h:103
double * mean
Definition: noisegenerator.h:255
virtual void setTau(double newTau)
Definition: noisegenerator.h:198
NoiseGenerator()
Definition: noisegenerator.h:39
double factor
Definition: noisegenerator.h:257
double tau
Definition: noisegenerator.h:253
double uniform01()
Definition: noisegenerator.h:108
double mean1channel
Definition: noisegenerator.h:209
virtual void init(unsigned int dimension, RandGen *randGen=0)
initialization with the the given dimension for multidimensional noise
Definition: noisegenerator.h:224
SineWhiteNoise(double omega, double amplitude, double phaseShift=M_PI/2, unsigned int channels=0xFFFF)
Definition: noisegenerator.h:268
generates white and normal distributed random numbers.
Definition: noisegenerator.h:141
generated colored noise. This is obtained by using time average of uniform distributed noise...
Definition: noisegenerator.h:160
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
virtual double getTau()
Definition: noisegenerator.h:244
Sine wave noise. Produces a 90 degree phase shifted sine wave or white noise.
Definition: noisegenerator.h:261
double mean1channel
Definition: noisegenerator.h:256
virtual void add(double *value, double noiseStrength)
adds multidimensional noise to the value field.
Definition: noisegenerator.h:190
virtual double generate()=0
generate somehow distributed random number parameterized with min and max.
double tau
Definition: noisegenerator.h:206
Interface and basic class for noise generator.
Definition: noisegenerator.h:37