noisegenerator.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  *                                                                         *
00022  *   $Log: noisegenerator.h,v $
00023  *   Revision 1.10.6.1  2005/11/22 14:55:21  martius
00024  *   added math.h
00025  *
00026  *   Revision 1.10  2005/10/06 17:07:16  martius
00027  *   removed MAXINT
00028  *
00029  *   Revision 1.9  2005/09/11 11:20:21  martius
00030  *   virtual destructors
00031  *
00032  *   Revision 1.8  2005/08/22 20:32:29  martius
00033  *   sine noise has phaseShift
00034  *
00035  *   Revision 1.7  2005/08/06 20:47:54  martius
00036  *   Commented
00037  *
00038  *   Revision 1.6  2005/08/03 20:31:40  martius
00039  *   sinenoise
00040  *   no random initialisation anymore
00041  *
00042  *   Revision 1.5  2005/07/21 15:11:19  martius
00043  *   normalised noise strength for colored noise
00044  *
00045  *   Revision 1.4  2005/07/14 16:07:12  fhesse
00046  *   cmath included
00047  *
00048  *   Revision 1.3  2005/07/06 16:05:34  martius
00049  *   noise generator is splitted into sub classes with a common interface
00050  *
00051  *   Revision 1.2  2005/06/15 16:04:56  martius
00052  *   de-templatified
00053  *                                                                 *
00054  ***************************************************************************/
00055 #ifndef __NOISEGENERATOR_H
00056 #define __NOISEGENERATOR_H
00057 
00058 #include <stdlib.h>
00059 #include <time.h>
00060 #include <math.h>
00061 
00062 /** Interface and basic class for noise generator.
00063     It is suitable for single noise channels but also multidimensional noise. 
00064  */
00065 class NoiseGenerator{
00066 public:
00067   NoiseGenerator() {
00068     dimension=0;
00069   };    
00070 
00071   virtual ~NoiseGenerator(){}
00072 
00073   /** initialization with the the given dimension for multidimensional noise
00074       @see add()
00075    */
00076   virtual void init(unsigned int dimension) {
00077     this->dimension = dimension;
00078   };
00079 
00080   /** generate somehow distributed random number between parameterized with p1 and p2
00081       valid only for ONE random number, use add(...) for 
00082       adding this kind of noise to several channels
00083    */
00084   virtual double generate(double p1, double p2) = 0; 
00085 
00086   /** adds multidimensional noise to the value field.
00087       Generic implementation calls generate for each channel.
00088       Overload this if you need different behavior.
00089       @param value field where noise is added. Must have length dimension (see init())
00090       @param p1 first parameter for random number distribution
00091       @param p2 second parameter for random number distribution
00092    */
00093   virtual void add(double *value, double p1, double p2){
00094     for (unsigned int i = 0; i < dimension; i++){
00095       value[i]+=generate(p1,p2);
00096     }    
00097   }
00098 
00099 protected:
00100   //generates white (no averaging) uniformly distributed random number between "min" and "max"  
00101   double uniform(double min=-0.1, double max=0.1){
00102     return( (double(rand())/RAND_MAX)*(max-min)+min);
00103   }
00104   unsigned int dimension;
00105 };
00106 
00107 /// generates white (no averaging) uniformly distributed random number between "min" and "max"  
00108 class WhiteUniformNoise : public NoiseGenerator{
00109 public:
00110   WhiteUniformNoise(){}
00111   virtual ~WhiteUniformNoise(){}
00112   virtual double generate(double min, double max) {
00113     return uniform(min,max);
00114   }; 
00115 };
00116 
00117 /// generates white and normal distributed random numbers. p1: mean, p2: variance
00118 class WhiteNormalNoise : public NoiseGenerator{
00119 public:
00120   WhiteNormalNoise(){}
00121   virtual ~WhiteNormalNoise(){}
00122   virtual double generate(double mean, double variance) {
00123     double x1=uniform(0, 1);
00124     double x2=uniform(0, 1);
00125     return( (sqrt(-2*log(x1)) *cos(2*M_PI*x2))  * variance + mean) ; 
00126   };
00127 };
00128 
00129 /// generated colored noise. This is obtained by using time average of uniform distributed noise.
00130 class ColorUniformNoise : public NoiseGenerator{
00131 public:
00132   /** @param tau time averaging factor (1/window)
00133       (1: smoothing (white) 0.1: strong color, 0 no noise anymore
00134   */
00135   ColorUniformNoise(double tau=0.3)
00136     : tau(tau){
00137     mean1channel=0.0;
00138     mean=0;
00139     // we can consider the distribution as a sum of individual distributions.
00140     // following the central limit theorem the variance is 1/sqrt(n)*V 
00141     // we assume n=1/tau
00142     factor=sqrt(1/(tau+0.01)); 
00143   }
00144   virtual ~ColorUniformNoise(){}
00145   virtual void init(unsigned int dimension){
00146     NoiseGenerator::init(dimension);
00147     mean = (double*)malloc(sizeof(double) * dimension);    
00148     for (unsigned int i=0; i<dimension; i++){
00149         mean[i]=0.0;
00150     }   
00151   }
00152 
00153   virtual double generate(double min, double max) {
00154     
00155     mean1channel+=tau * (uniform(min*factor,  max*factor) - mean1channel);
00156     return(mean1channel);
00157   } 
00158   virtual void add(double *value, double min, double max){
00159     for (unsigned int i = 0; i < dimension; i++){     
00160       mean[i]+=tau * (uniform(min*factor,  max*factor) - mean[i]);
00161       value[i]+=mean[i];
00162     }    
00163   }   
00164 
00165 protected:
00166   double tau; // smoothing paramter
00167   double* mean;  
00168   double mean1channel;
00169   double factor;
00170 };
00171 
00172 /// like ColorUniformNoise but averaging over normal distributed noise instead.
00173 class ColorNormalNoise : public WhiteNormalNoise{
00174 public:
00175   ColorNormalNoise(double tau=0.3)
00176     : tau(tau){
00177     mean = 0;
00178     mean1channel=0.0;
00179     // we can consider the distribution as a sum of individual distributions.
00180     // following the central limit theorem the variance is 1/sqrt(n)*V 
00181     // we assume n=1/tau
00182     factor=sqrt(1/(tau+0.01)); 
00183   }
00184 
00185   virtual ~ColorNormalNoise(){}
00186 
00187   virtual void init(unsigned int dimension){
00188     WhiteNormalNoise::init(dimension);
00189     mean = (double*)malloc(sizeof(double) * dimension);    
00190     for (unsigned int i=0; i<dimension; i++){
00191         mean[i]=0.0;
00192     }   
00193   }
00194 
00195   virtual double generate(double variance, double meanvalue) {
00196     mean1channel += tau * WhiteNormalNoise::generate(variance*factor, meanvalue) - mean1channel;
00197     return(mean1channel);
00198   } 
00199   virtual void add(double *value, double variance, double meanvalue){
00200     for (unsigned int i = 0; i < dimension; i++){     
00201       mean[i]+=tau * (WhiteNormalNoise::generate(variance*factor, meanvalue) - mean[i]);
00202       value[i]+=mean[i];
00203     }    
00204   }   
00205 
00206 protected:
00207   double tau; // smoothing paramter
00208   double* mean;  
00209   double mean1channel;
00210   double factor;
00211 };
00212 
00213 /// Sine wave noise. Produces a 90 degree phase shifted sine wave or white noise
00214 class SineWhiteNoise : public NoiseGenerator{
00215 public:
00216   /** @param omega anglerate
00217       @param amplitude amplitude of the sine wave in respect to the noise strength
00218       @param channels number of channel for sine noise (and the rest get white noise)
00219    */
00220   SineWhiteNoise(double omega, double amplitude, double phaseShift = M_PI/2, 
00221                  unsigned int channels = 0xFFFF)
00222     : omega(omega), amplitude(amplitude)
00223     , channels(channels), phaseShift(phaseShift){
00224     t=0;
00225   }
00226 
00227   virtual ~SineWhiteNoise(){}
00228 
00229   virtual double generate(double min, double max) {        
00230     t ++;
00231     return uniform(min,  max) + sin(t*omega)*amplitude*(max-min);
00232   } 
00233   virtual void add(double *value, double min, double max){
00234 
00235     for (unsigned int i = 0; i < dimension; i++){     
00236       if(i < channels)
00237         value[i]+=sin(t*omega+i*phaseShift)*amplitude*(max-min);
00238       value[i]+=uniform(min,  max);
00239     }    
00240     t ++;
00241   }   
00242   void setOmega(double omega){
00243     this->omega=omega;
00244   }
00245   void setPhaseShift(double phaseShift){
00246     this->phaseShift=phaseShift;
00247   }
00248   
00249 protected:
00250   long int t;        // time
00251   double omega;     // angle velocity
00252   double amplitude; // factor for noise strength  
00253   unsigned int channels;     // number of channels with sine
00254   double phaseShift; // phase shift
00255 
00256 };
00257 
00258 
00259 #endif
00260 

Generated on Tue Apr 4 19:05:04 2006 for Robotsystem from Robot Group Leipzig by  doxygen 1.4.5