]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/de2bi.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / de2bi.m
1 ## Copyright (C) 2001 Laurent Mazet
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} = } de2bi (@var{d})
18 ## @deftypefnx {Function File} {@var{b} = } de2bi (@var{d},@var{n})
19 ## @deftypefnx {Function File} {@var{b} = } de2bi (@var{d},@var{n},@var{p})
20 ## @deftypefnx {Function File} {@var{b} = } de2bi (@var{d},@var{n},@var{p},@var{f})
21 ##
22 ## Convert a non-negative integer to bit vector.
23 ##
24 ## The variable @var{d} must be a vector of non-negative integers. @dfn{de2bi}
25 ## then returns a matrix where each row represents the binary representation
26 ## of elements of @var{d}. If @var{n} is defined then the returned matrix
27 ## will have @var{n} columns. This number of columns can be either larger
28 ## than the minimum needed and zeros will be added to the msb of the
29 ## binary representation or smaller than the minimum in which case the
30 ## least-significant part of the element is returned.
31 ##
32 ## If @var{p} is defined then it is used as the base for the decomposition
33 ## of the returned values. That is the elements of the returned value are
34 ## between '0' and 'p-1'.
35 ##
36 ## The variable @var{f} defines whether the first or last element of @var{b}
37 ## is considered to be the most-significant. Valid values of @var{f} are
38 ## 'right-msb' or 'left-msb'. By default @var{f} is 'right-msb'.
39 ##
40 ## @seealso{bi2de}
41 ## @end deftypefn
42
43 function b = de2bi (d, n, p, f)
44
45   if (nargin == 1)
46     p = 2;
47     n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
48     f = 'right-msb';
49   elseif (nargin == 2)
50     p = 2;
51     f = 'right-msb';
52   elseif (nargin == 3)
53     if (ischar (p))
54       f = p;
55       p = 2;
56     else
57       f = 'right-msb';
58     endif
59   elseif (nargin == 4)
60     if (ischar (p))
61       tmp = f;
62       f = p;
63       p = tmp;
64     endif
65   else
66     print_usage ();
67   endif
68
69   d = d(:);
70   if ( any (d < 0) || any (d != floor (d)) )
71     error ("de2bi: d must only contain non-negative integers");
72   endif
73
74   if (isempty (n))
75     n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
76   endif
77
78   power = ones (length (d), 1) * (p .^ [0 : n-1] );
79   d = d * ones (1, n);
80   b = floor (rem (d, p*power) ./ power);
81
82   if (strcmp (f, 'left-msb'))
83     b = b(:,columns(b):-1:1);
84   elseif (!strcmp (f, 'right-msb'))
85     error ("de2bi: unrecognized flag");
86   endif
87
88 endfunction
89
90                                 %!shared x
91                                 %! x = randi ([0 2^16-1], 100, 1);
92                                 %!assert (de2bi (0), 0)
93                                 %!assert (de2bi (1), 1)
94                                 %!assert (de2bi (255), ones (1, 8))
95                                 %!assert (de2bi (255, [], 256), 255)
96                                 %!assert (de2bi (1023, 8, 8), [7 7 7 1 0 0 0 0])
97                                 %!assert (size (de2bi (x, 16)), [100 16])
98                                 %!assert (de2bi (x, 16, "right-msb"), de2bi (x, 16))
99                                 %!assert (de2bi (x, 16, "left-msb"), fliplr (de2bi (x, 16)))
100
101 %% Test input validation
102                                 %!error de2bi ()
103                                 %!error de2bi (1, 2, 3, 4, 5)
104                                 %!error de2bi (1, 2, 3, 4)
105                                 %!error de2bi (1, 2, 3, "invalid")
106                                 %!error de2bi (0.1)
107                                 %!error de2bi (-1)