1 ## Copyright (C) 2002 David Bateman
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{y} =} wgn (@var{m},@var{n},@var{p})
18 ## @deftypefnx {Function File} {@var{y} =} wgn (@var{m},@var{n},@var{p},@var{imp})
19 ## @deftypefnx {Function File} {@var{y} =} wgn (@var{m},@var{n},@var{p},@var{imp},@var{seed},)
20 ## @deftypefnx {Function File} {@var{y} =} wgn (@var{...},'@var{type}')
21 ## @deftypefnx {Function File} {@var{y} =} wgn (@var{...},'@var{output}')
23 ## Returns a M-by-N matrix @var{y} of white Gaussian noise. @var{p} specifies
24 ## the power of the output noise, which is assumed to be referenced to an
25 ## impedance of 1 Ohm, unless @var{imp} explicitly defines the impedance.
27 ## If @var{seed} is defined then the randn function is seeded with this
30 ## The arguments @var{type} and @var{output} must follow the above numerial
31 ## arguments, but can be specified in any order. @var{type} specifies the
32 ## units of @var{p}, and can be 'dB', 'dBW', 'dBm' or 'linear'. 'dB' is
33 ## in fact the same as 'dBW' and is keep as a misnomer of Matlab. The
34 ## units of 'linear' are in Watts.
36 ## The @var{output} variable should be either 'real' or 'complex'. If the
37 ## output is complex then the power @var{p} is divided equally betwen the
38 ## real and imaginary parts.
40 ## @seealso{randn,awgn}
46 function y = wgn (m, n, p, varargin)
48 if ((nargin < 3) || (nargin > 7))
49 error ("usage: wgn(m, n, p, imp, seed, type, output)");
52 if (!isscalar(m) || !isreal(m) || (m < 0) || !isscalar(n) || ...
54 error ("wgn: matrix dimension error");
63 for i=1:length(varargin)
66 if (strcmp(arg,"real"))
68 elseif (strcmp(arg,"complex"))
70 elseif (strcmp(arg,"dB"))
72 elseif (strcmp(arg,"dBW"))
74 elseif (strcmp(arg,"dBm"))
76 elseif (strcmp(arg,"linear"))
79 error ("wgn: invalid argument");
89 error ("wgn: too many arguments");
96 elseif (!isscalar(imp) || !isreal(imp) || (imp < 0))
97 error ("wgn: impedance value illegal");
101 if (!isscalar(seed) || !isreal(seed) || (seed < 0) ||
102 ((seed-floor(seed)) != 0))
103 error ("wgn: random seed must be integer");
107 if (!isscalar(p) || !isreal(p))
108 error("wgn: invalid power");
110 if (strcmp(type,"linear") && (p < 0))
111 error("wgn: invalid power");
114 if (strcmp(type,"dBW"))
116 elseif (strcmp(type,"dBm"))
117 np = 10 ^((p - 30)/10);
118 elseif (strcmp(type,"linear"))
126 if (strcmp(out,"complex"))
127 y = (sqrt(imp*np/2))*(randn(m,n)+1i*randn(m,n));
129 y = (sqrt(imp*np))*randn(m,n);
134 ## Allow 30% error in standard deviation, due to randomness
138 %!error wgn (1,1,1,1,1,1);
139 %!assert (isreal(wgn(10,10,30,1,"dBm","real")));
140 %!assert (iscomplex(wgn(10,10,30,1,"dBm","complex")));
141 %!assert (abs(std(wgn(10000,1,30,1,"dBm")) - 1) < 0.3);
142 %!assert (abs(std(wgn(10000,1,0,1,"dBW")) - 1) < 0.3);
143 %!assert (abs(std(wgn(10000,1,1,1,"linear")) - 1) < 0.3);