/usr/share/octave/packages/communications-1.2.0/awgn.m is in octave-communications-common 1.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | ## Copyright (C) 2002 David Bateman
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{y} =} awgn (@var{x}, @var{snr})
## @deftypefnx {Function File} {@var{y} =} awgn (@var{x}, @var{snr}, @var{pwr})
## @deftypefnx {Function File} {@var{y} =} awgn (@var{x}, @var{snr}, @var{pwr}, @var{seed})
## @deftypefnx {Function File} {@var{y} =} awgn (@dots{}, @var{type})
##
## Add white Gaussian noise to a voltage signal.
##
## The input @var{x} is assumed to be a real or complex voltage signal. The
## returned value @var{y} will be the same form and size as @var{x} but with
## Gaussian noise added. Unless the power is specified in @var{pwr}, the
## signal power is assumed to be 0dBW, and the noise of @var{snr} dB will be
## added with respect to this. If @var{pwr} is a numeric value then the signal
## @var{x} is assumed to be @var{pwr} dBW, otherwise if @var{pwr} is
## "measured", then the power in the signal will be measured and the noise
## added relative to this measured power.
##
## If @var{seed} is specified, then the random number generator seed is
## initialized with this value
##
## By default the @var{snr} and @var{pwr} are assumed to be in dB and dBW
## respectively. This default behavior can be chosen with @var{type}
## set to "dB". In the case where @var{type} is set to "linear", @var{pwr}
## is assumed to be in Watts and @var{snr} is a ratio.
## @seealso{randn, wgn}
## @end deftypefn
function y = awgn (x, snr, varargin)
if (nargin < 2 || nargin > 5)
print_usage ();
endif
[m, n] = size (x);
if (isreal (x))
out = "real";
else
out = "complex";
endif
p = 0;
seed = [];
type = "dB";
meas = 0;
narg = 0;
for i = 1:length (varargin)
arg = varargin{i};
if (ischar (arg))
if (strcmp (arg, "measured"))
meas = 1;
elseif (strcmp (arg, "dB"))
type = "dB";
elseif (strcmp (arg, "linear"))
type = "linear";
else
error ("awgn: invalid argument '%s'", arg);
endif
else
narg++;
switch (narg)
case 1
p = arg;
case 2
seed = arg;
otherwise
error ("awgn: too many arguments");
endswitch
endif
endfor
if (isempty (p))
p = 0;
endif
if (!isempty (seed))
if (! (isscalar (seed) && isreal (seed) && seed >= 0 && seed == fix (seed)))
error ("awgn: random SEED must be an integer");
endif
endif
if (!isscalar (p) || !isreal (p))
error ("awgn: PWR must be a scalar");
endif
if (strcmp (type, "linear") && p < 0)
error ("awgn: PWR must be a non-negative scalar for TYPE \"linear\"");
endif
if (!isscalar (snr) || !isreal (snr))
error ("awgn: SNR must be a scalar");
endif
if (strcmp (type, "linear") && snr < 0)
error ("awgn: SNR must be a non-negative scalar for TYPE \"linear\"");
endif
if (!isempty (seed))
randn ("state", seed);
endif
if (meas == 1)
p = sum (abs (x(:)) .^ 2) / length (x(:));
if (strcmp (type, "dB"))
p = 10 * log10 (p);
endif
endif
if (strcmp (type, "linear"))
np = p / snr;
else
np = p - snr;
endif
y = x + wgn (m, n, np, 1, seed, type, out);
endfunction
%!shared x, y, noisy
%! x = [0:0.01:2*pi]; y = sin (x);
%! noisy = awgn (y, 20, "dB", "measured");
## Test of noisy is pretty arbitrary, but should pickup most errors
%!assert (isreal (noisy));
%!assert (iscomplex (awgn (y + 1i, 20, "dB", "measured")));
%!assert (size (y) == size (noisy))
%!assert (abs (10*log10 (mean (y.^2)/mean ((y-noisy).^ 2)) - 20) < 1);
%% Test input validation
%!error awgn ();
%!error awgn (1);
%!error awgn (1, 1, 1, 1, 1);
|