]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/randerr.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / randerr.m
1 ## Copyright (C) 2003 David Bateman
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{b} = } randerr (@var{n})
18 ## @deftypefnx {Function File} {@var{b} = } randerr (@var{n},@var{m})
19 ## @deftypefnx {Function File} {@var{b} = } randerr (@var{n},@var{m},@var{err})
20 ## @deftypefnx {Function File} {@var{b} = } randerr (@var{n},@var{m},@var{err},@var{seed})
21 ##
22 ## Generate a matrix of random bit errors. The size of the matrix is
23 ## @var{n} rows by @var{m} columns. By default @var{m} is equal to @var{n}.
24 ## Bit errors in the matrix are indicated by a 1.
25 ##
26 ## The variable @var{err} determines the number of errors per row. By
27 ## default the return matrix @var{b} has exactly one bit error per row.
28 ## If @var{err} is a scalar, there each row of @var{b} has exactly this
29 ## number of errors per row. If @var{err} is a vector then each row has
30 ## a number of errors that is in this vector. Each number of errors has    
31 ## an equal probability. If @var{err} is a matrix with two rows, then 
32 ## the first row determines the number of errors and the second their
33 ## probabilities.
34 ##
35 ## The variable @var{seed} allows the random number generator to be seeded
36 ## with a fixed value. The initial seed will be restored when returning.
37 ## @end deftypefn
38
39 ## 2003 FEB 13
40 ##   initial release
41
42 function b = randerr (n, m, err, seed)
43
44   switch (nargin)
45     case 0,
46       m = 1;
47       n = 1;
48       err = 1;
49       seed = Inf;
50     case 1,
51       m = n;
52       err = 1;
53       seed = Inf;
54     case 2,
55       err = 1;
56       seed = Inf;
57     case 3,
58       seed = Inf;      
59     case 4,
60     otherwise
61       usage ("b = randerr (n, [m, [err, [seed]]])");
62   endswitch
63
64   ## Check error vector
65   [ar,ac] = size (err);
66   if (ac == 1) 
67     if (ar > 1)
68       err = err';
69     endif
70   elseif ((ac > 1) && (ar != 1) && (ar != 2))
71     error ("randerr: err must be a scalar, vector or two row matrix");
72   endif
73   for i=1:ac
74     if (err(1,i) > m)
75       error ("randerr: illegal number of errors per row");
76     endif
77   end
78     
79   # Use randsrc to calculate the number of errors per row
80   nerrs = randsrc (n, 1, err, seed);
81   
82   # Now put to errors into place in the return matrix
83   b = zeros (n, m);
84   for i=1:n
85     if (nerrs(i) > 0)
86       if (nerrs(i) == 1)
87         indx = sort(randint(1,nerrs(i),m,seed));
88       else
89         do
90           indx = sort(randint(1,nerrs(i),m,seed));
91         until (! any(indx(1:nerrs(i)-1) == indx(2:nerrs(i))))
92       endif
93       b(i,indx+1) = ones(1,nerrs(i)); 
94     endif
95   end
96 endfunction
97
98
99 %!shared n, err1, err2, seed, a1, a2, a3, a4, a5, a6
100 %!    n = 10; err1 = 2; err2 = [1,2;0.7,0.3] ; seed = 1; 
101 %!    a1 = randerr(n); a2 = randerr(n,n);
102 %!    a3 = randerr(n,n,err1); a4 = randerr(n,n,err2); 
103 %!    a5 = randerr(n,n,err1,seed); a6 = randerr(n,n,err1,seed);
104
105 %!error randerr (n,n,n,n,n);
106 %!assert (size(a1) == [n, n] && size(a2) == [n, n]);
107 %!assert (all (sum (a1.') == 1) && all (sum (a2.') == 1))
108 %!assert (all((a1(:) == 1 | a1(:) == 0)) &&all((a2(:) == 1 | a2(:) == 0)))
109 %!assert (size(a3) == [n, n] && size(a4) == [n, n]);
110 %!assert (all (sum (a3.') == err1))
111 %!assert (all((a3(:) == 1 | a3(:) == 0)))
112 %!assert (all ((sum (a4.') == err2(1,1)) | (sum (a4.') == err2(1,2))))
113 %!assert (all((a4(:) == 1 | a4(:) == 0)))
114 %!assert (a5(:) == a6(:));