[7e536c]: inst / awgn.m Maximize Restore History

Download this file

awgn.m    147 lines (128 with data), 4.2 kB

  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
146
## 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 (@var{...}, '@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 behaviour 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");
endif
else
narg++;
switch (narg)
case 1
p = arg;
case 2
seed = arg;
otherwise
error ("wgn: too many arguments");
endswitch
endif
endfor
if (isempty(p))
p = 0;
endif
if (!isempty(seed))
if (!isscalar(seed) || !isreal(seed) || (seed < 0) ||
((seed-floor(seed)) != 0))
error ("awgn: random seed must be integer");
endif
endif
if (!isscalar(p) || !isreal(p))
error("awgn: invalid power");
endif
if (strcmp(type,"linear") && (p < 0))
error("awgn: invalid power");
endif
if (!isscalar(snr) || !isreal(snr))
error("awgn: invalid snr");
endif
if (strcmp(type,"linear") && (snr < 0))
error("awgn: invalid snr");
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);