]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/randsrc.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / randsrc.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} = } randsrc (@var{n})
18 ## @deftypefnx {Function File} {@var{b} = } randsrc (@var{n},@var{m})
19 ## @deftypefnx {Function File} {@var{b} = } randsrc (@var{n},@var{m},@var{alphabet})
20 ## @deftypefnx {Function File} {@var{b} = } randsrc (@var{n},@var{m},@var{alphabet},@var{seed})
21 ##
22 ## Generate a matrix of random symbols. The size of the matrix is
23 ## @var{n} rows by @var{m} columns. By default @var{m} is equal to @var{n}.
24 ##
25 ## The variable @var{alphabet} can be either a row vector or a matrix with 
26 ## two rows. When @var{alphabet} is a row vector the symbols returned in
27 ## @var{b} are chosen with equal probability from @var{alphabet}. When
28 ## @var{alphabet} has two rows, the second row determines the probabilty
29 ## with which each of the symbols is chosen. The sum of the probabilities
30 ## must equal 1. By default @var{alphabet} is [-1 1].
31 ##
32 ## The variable @var{seed} allows the random number generator to be seeded
33 ## with a fixed value. The initial seed will be restored when returning.
34 ## @end deftypefn
35
36 ## 2003 FEB 13
37 ##   initial release
38
39 function b = randsrc (n, m, alphabet, seed)
40
41   switch (nargin)
42     case 0,
43       m = 1;
44       n = 1;
45       alphabet = [-1,1];
46       seed = Inf;
47     case 1,
48       m = n;
49       alphabet = [-1,1];
50       seed = Inf;
51     case 2,
52       alphabet = [-1,1];
53       seed = Inf;
54     case 3,
55       seed = Inf;      
56     case 4,
57     otherwise
58       usage ("b = randsrc (n, [m, [alphabet, [seed]]])");
59   endswitch
60
61   ## Check alphabet
62   [ar,ac] = size (alphabet);
63   if (ac == 1)
64     b = alphabet (1, 1) * ones (n, m);
65     return;
66   endif
67
68   if (ar == 1)
69     prob = [1:ac] / ac;
70   elseif (ar == 2)
71     prob = alphabet(2,:);
72     alphabet = alphabet(1,:);
73     if (abs(1-sum(prob)) > eps)
74       error ("randsrc: probabilities must added up to one");
75     endif
76     prob = cumsum(prob);
77   else
78     error ("randsrc: alphabet must have 1 or 2 rows");
79   endif
80   
81   ## Check seed;
82   if (!isinf (seed))
83     old_seed = rand ("seed");
84     rand ("seed", seed);
85   endif
86   
87   ## Create indexes with the right probabilities
88   tmp = rand (n, m);
89   b = ones (n, m);
90   for i = 1:ac-1 
91     b = b + (tmp > prob(i));
92   end
93
94   ## Map the indexes to the symbols
95   b = alphabet(b);
96   
97   ## BUG: the above gives a row vector for some reason. Force what we want
98   b = reshape(b, n, m);
99     
100   ## Get back to the old
101   if (!isinf (seed))
102     rand ("seed", old_seed);
103   endif
104
105 endfunction
106
107 %!shared n, alph1, alph2, seed, a1, a2, a3, a4, a5, a6
108 %!    n = 10; alph1 = [0,1;0.3,0.7]; alph2 = ['a','b']; seed = 1; 
109 %!    a1 = randsrc(n); a2 = randsrc(n,n);
110 %!    a3 = randsrc(n,n,alph1); a4 = randsrc(n,n,alph2); 
111 %!    a5 = randsrc(n,n,alph1,seed); a6 = randsrc(n,n,alph1,seed);
112
113 %!error randsrc (n,n,n,n,n);
114 %!assert (size(a1) == [n, n] && size(a2) == [n, n]);
115 %!assert (max ([a1(:); a2(:)]) <= 1 && min([a1(:); a2(:)]) >= -1);
116 %!assert (size(a3) == [n, n] && size(a4) == [n, n]);
117 %!assert (max (a3(:)) <= 1 && min(a3(:)) >= 0);
118 %!assert (max(toascii(a4(:))) <= toascii('b'))
119 %!assert (max(toascii(a4(:))) >= toascii('a'))
120 %!assert (a5(:) == a6(:));