Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GrNoiseSource.h

Go to the documentation of this file.
00001 /* -*- Mode: c++ -*- */
00002 /*
00003  * Copyright 2001 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio 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, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 /*
00023  *  Copyright 1997 Massachusetts Institute of Technology
00024  * 
00025  *  Permission to use, copy, modify, distribute, and sell this software and its
00026  *  documentation for any purpose is hereby granted without fee, provided that
00027  *  the above copyright notice appear in all copies and that both that
00028  *  copyright notice and this permission notice appear in supporting
00029  *  documentation, and that the name of M.I.T. not be used in advertising or
00030  *  publicity pertaining to distribution of the software without specific,
00031  *  written prior permission.  M.I.T. makes no representations about the
00032  *  suitability of this software for any purpose.  It is provided "as is"
00033  *  without express or implied warranty.
00034  * 
00035  */
00036 
00037 
00038 #ifndef _GRNOISESOURCE_H_
00039 #define _GRNOISESOURCE_H_
00040 
00041 #include <VrSource.h>
00042 #include <gr_random.h>
00043 
00044 enum { GR_GAUSSIAN, GR_LAPLACIAN, GR_IMPULSE };
00045 
00046 template<class oType> 
00047 class GrNoiseSource : public VrSource<oType> {
00048 protected:
00049   int type;
00050   float amp;
00051   gr_random RNG;
00052 public:
00053   virtual const char *name() { return "GrNoiseSource"; }
00054   virtual int work2(VrSampleRange output, void *o[]);
00055   
00056   GrNoiseSource(double sampfreq, int type, float amp, int seed = 3021)  
00057     :type(type),amp(amp) {
00058     setSamplingFrequency(sampfreq);
00059     RNG.reseed(seed);
00060   }
00061 };
00062 
00063 // Specialization for Complex Output
00064 /*
00065   template<>
00066 class GrNoiseSource<VrComplex> : public VrSource<VrComplex> {
00067 protected:
00068   int type;
00069   float amp;
00070   gr_random RNG;
00071 public:
00072   virtual const char *name() { return "GrNoiseSource"; }
00073   virtual int work2(VrSampleRange output, void *o[]);
00074 
00075   GrNoiseSource(double sampfreq, int type, float amp, int seed = 3021)
00076     :type(type),amp(amp) {
00077     setSamplingFrequency(sampfreq);
00078     RNG.reseed(seed);
00079   }
00080 };
00081 */
00082 
00083 template<class oType> int
00084 GrNoiseSource<oType>::work2(VrSampleRange output, void *ao[])
00085 { 
00086   oType **o = (oType **)ao;
00087   unsigned int size = output.size;
00088   switch(type)
00089     {
00090     case GR_GAUSSIAN:
00091       for (unsigned int i=0; i<size; i++) 
00092         *o[0]++ = (oType)(amp * RNG.gasdev());
00093       break;
00094     case GR_LAPLACIAN:
00095       for (unsigned int i=0; i<size; i++) 
00096         *o[0]++ = (oType)(amp * RNG.laplacian());
00097       break;
00098     case GR_IMPULSE:    // FIXME changeable impulse settings
00099       for (unsigned int i=0; i<size; i++) 
00100         *o[0]++ = (oType)(amp * RNG.impulse(9));
00101       break;
00102     default:
00103       cerr << "Unsupported Noise Type: " << type << endl;
00104       exit(-1);
00105     }
00106   return output.size;              // Others should use the slower mod method
00107 }
00108 
00109 template<> int
00110 GrNoiseSource<VrComplex>::work2(VrSampleRange output, void *ao[])
00111 {
00112   VrComplex **o = (VrComplex **)ao;
00113   unsigned int size = output.size;
00114   assert(type == GR_GAUSSIAN);
00115   for(unsigned int i=0; i<size; i++)
00116     *o[0]++ = (amp * RNG.rayleigh_complex());
00117   return size;
00118 }
00119 
00120 #endif 

Generated on Tue Mar 15 23:46:36 2005 for GNU Radio by  doxygen 1.4.0