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

Download this file

wgn.m    143 lines (127 with data), 4.3 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
## 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} =} wgn (@var{m},@var{n},@var{p})
## @deftypefnx {Function File} {@var{y} =} wgn (@var{m},@var{n},@var{p},@var{imp})
## @deftypefnx {Function File} {@var{y} =} wgn (@var{m},@var{n},@var{p},@var{imp},@var{seed},)
## @deftypefnx {Function File} {@var{y} =} wgn (@var{...},'@var{type}')
## @deftypefnx {Function File} {@var{y} =} wgn (@var{...},'@var{output}')
##
## Returns a M-by-N matrix @var{y} of white Gaussian noise. @var{p} specifies
## the power of the output noise, which is assumed to be referenced to an
## impedance of 1 Ohm, unless @var{imp} explicitly defines the impedance.
##
## If @var{seed} is defined then the randn function is seeded with this
## value.
##
## The arguments @var{type} and @var{output} must follow the above numerial
## arguments, but can be specified in any order. @var{type} specifies the
## units of @var{p}, and can be 'dB', 'dBW', 'dBm' or 'linear'. 'dB' is
## in fact the same as 'dBW' and is keep as a misnomer of Matlab. The
## units of 'linear' are in Watts.
##
## The @var{output} variable should be either 'real' or 'complex'. If the
## output is complex then the power @var{p} is divided equally betwen the
## real and imaginary parts.
##
## @seealso{randn, awgn}
## @end deftypefn
function y = wgn (m, n, p, varargin)
if ((nargin < 3) || (nargin > 7))
print_usage ();
endif
if (!isscalar(m) || !isreal(m) || (m < 0) || !isscalar(n) || ...
!isreal(n) || (n<0))
error ("wgn: matrix dimension error");
endif
type = "dBW";
out = "real";
imp = 1;
seed = [];
narg = 0;
for i=1:length(varargin)
arg = varargin{i};
if (ischar(arg))
if (strcmp(arg,"real"))
out = "real";
elseif (strcmp(arg,"complex"))
out = "complex";
elseif (strcmp(arg,"dB"))
type = "dBW";
elseif (strcmp(arg,"dBW"))
type = "dBW";
elseif (strcmp(arg,"dBm"))
type = "dBm";
elseif (strcmp(arg,"linear"))
type = "linear";
else
error ("wgn: invalid argument");
endif
else
narg++;
switch (narg)
case 1
imp = arg;
case 2
seed = arg;
otherwise
error ("wgn: too many arguments");
endswitch
endif
endfor
if (isempty(imp))
imp = 1;
elseif (!isscalar(imp) || !isreal(imp) || (imp < 0))
error ("wgn: impedance value illegal");
endif
if (!isempty(seed))
if (!isscalar(seed) || !isreal(seed) || (seed < 0) ||
((seed-floor(seed)) != 0))
error ("wgn: random seed must be integer");
endif
endif
if (!isscalar(p) || !isreal(p))
error("wgn: invalid power");
endif
if (strcmp(type,"linear") && (p < 0))
error("wgn: invalid power");
endif
if (strcmp(type,"dBW"))
np = 10 ^ (p/10);
elseif (strcmp(type,"dBm"))
np = 10 ^((p - 30)/10);
elseif (strcmp(type,"linear"))
np = p;
endif
if(!isempty(seed))
randn("state",seed);
endif
if (strcmp(out,"complex"))
y = (sqrt(imp*np/2))*(randn(m,n)+1i*randn(m,n));
else
y = (sqrt(imp*np))*randn(m,n);
endif
endfunction
## Allow 30% error in standard deviation, due to randomness
%!assert (isreal(wgn(10,10,30,1,"dBm","real")));
%!assert (iscomplex(wgn(10,10,30,1,"dBm","complex")));
%!assert (abs(std(wgn(10000,1,30,1,"dBm")) - 1) < 0.3);
%!assert (abs(std(wgn(10000,1,0,1,"dBW")) - 1) < 0.3);
%!assert (abs(std(wgn(10000,1,1,1,"linear")) - 1) < 0.3);
%% Test input validation
%!error wgn ();
%!error wgn (1);
%!error wgn (1,1);
%!error wgn (1,1,1,1,1,1);