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} =} awgn (@var{x},@var{snr})
18 ## @deftypefnx {Function File} {@var{y} =} awgn (@var{x},@var{snr},@var{pwr})
19 ## @deftypefnx {Function File} {@var{y} =} awgn (@var{x},@var{snr}, @var{pwr},@var{seed})
20 ## @deftypefnx {Function File} {@var{y} =} awgn (@var{...}, '@var{type}')
22 ## Add white Gaussian noise to a voltage signal.
24 ## The input @var{x} is assumed to be a real or complex voltage signal. The
25 ## returned value @var{y} will be the same form and size as @var{x} but with
26 ## Gaussian noise added. Unless the power is specified in @var{pwr}, the
27 ## signal power is assumed to be 0dBW, and the noise of @var{snr} dB will be
28 ## added with respect to this. If @var{pwr} is a numeric value then the signal
29 ## @var{x} is assumed to be @var{pwr} dBW, otherwise if @var{pwr} is
30 ## 'measured', then the power in the signal will be measured and the noise
31 ## added relative to this measured power.
33 ## If @var{seed} is specified, then the random number generator seed is
34 ## initialized with this value
36 ## By default the @var{snr} and @var{pwr} are assumed to be in dB and dBW
37 ## respectively. This default behaviour can be chosen with @var{type}
38 ## set to 'dB'. In the case where @var{type} is set to 'linear', @var{pwr}
39 ## is assumed to be in Watts and @var{snr} is a ratio.
41 ## @seealso{randn,wgn}
46 function y = awgn (x, snr, varargin)
48 if ((nargin < 2) || (nargin > 5))
49 error ("usage: awgn(x, snr, p, seed, type");
65 for i=1:length(varargin)
68 if (strcmp(arg,"measured"))
70 elseif (strcmp(arg,"dB"))
72 elseif (strcmp(arg,"linear"))
75 error ("awgn: invalid argument");
85 error ("wgn: too many arguments");
95 if (!isscalar(seed) || !isreal(seed) || (seed < 0) ||
96 ((seed-floor(seed)) != 0))
97 error ("awgn: random seed must be integer");
101 if (!isscalar(p) || !isreal(p))
102 error("awgn: invalid power");
104 if (strcmp(type,"linear") && (p < 0))
105 error("awgn: invalid power");
108 if (!isscalar(snr) || !isreal(snr))
109 error("awgn: invalid snr");
111 if (strcmp(type,"linear") && (snr < 0))
112 error("awgn: invalid snr");
120 p = sum( abs( x(:)) .^ 2) / length(x(:));
121 if (strcmp(type,"dB"))
126 if (strcmp(type,"linear"))
132 y = x + wgn (m, n, np, 1, seed, type, out);
137 %! x = [0:0.01:2*pi]; y = sin (x);
138 %! noisy = awgn (y, 20, "dB", "measured");
140 ## Test of noisy is pretty arbitrary, but should pickup most errors
143 %!error awgn (1,1,1,1,1);
144 %!assert (isreal(noisy));
145 %!assert (iscomplex(awgn(y+1i,20,"dB","measured")));
146 %!assert (size(y) == size(noisy))
147 %!assert (abs(10*log10(mean(y.^2)/mean((y-noisy).^ 2)) - 20) < 1);