3 * Copyright 2002 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
24 * Copyright 1997 Massachusetts Institute of Technology
26 * Permission to use, copy, modify, distribute, and sell this software and its
27 * documentation for any purpose is hereby granted without fee, provided that
28 * the above copyright notice appear in all copies and that both that
29 * copyright notice and this permission notice appear in supporting
30 * documentation, and that the name of M.I.T. not be used in advertising or
31 * publicity pertaining to distribution of the software without specific,
32 * written prior permission. M.I.T. makes no representations about the
33 * suitability of this software for any purpose. It is provided "as is"
34 * without express or implied warranty.
39 #include <gr_random.h>
46 #define NDIV (1+(IM-1)/NTAB)
48 #define RNMX (1.0-EPS)
51 gr_random::gr_random (long seed)
57 gr_random::reseed (long seed)
61 for (int i = 0; i < NTAB; i++)
68 * This looks like it returns a uniform random deviate between 0.0 and 1.0
69 * It looks similar to code from "Numerical Recipes in C".
71 float gr_random::ran1()
77 if (d_seed <= 0 || !d_iy) {
82 for (j=NTAB+7;j>=0;j--) {
84 d_seed=IA*(d_seed-k*IQ)-IR*k;
93 d_seed=IA*(d_seed-k*IQ)-IR*k;
106 * Returns a normally distributed deviate with zero mean and variance 1.
107 * Also looks like it's from "Numerical Recipes in C".
109 float gr_random::gasdev()
118 } while (rsq >= 1.0 || rsq == 0.0);
119 fac= sqrt(-2.0*log(rsq)/rsq);
127 * Copied from The KC7WW / OH2BNS Channel Simulator
128 * FIXME Need to check how good this is at some point
131 float gr_random::laplacian()
135 return log(2.0 * z) / M_SQRT2;
137 return -log(2.0 * (1.0 - z)) / M_SQRT2;
141 * Copied from The KC7WW / OH2BNS Channel Simulator
142 * FIXME Need to check how good this is at some point
145 // 5 => scratchy, 8 => Geiger
147 float gr_random::impulse(float factor = 5)
149 float z = -M_SQRT2 * log(ran1());
150 if (fabsf(z) <= factor)
157 * Complex rayleigh is really gaussian I and gaussian Q
158 * It can also be generated by real rayleigh magnitude and
159 * uniform random angle
160 * Adapted from The KC7WW / OH2BNS Channel Simulator
161 * FIXME Need to check how good this is at some point
164 gr_complex gr_random::rayleigh_complex()
166 return gr_complex(gasdev(),gasdev());
171 ang = 2.0 * M_PI * RNG();
177 float gr_random::rayleigh()
179 return sqrt(-2.0 * log(ran1()));