]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/oct2dec.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / oct2dec.m
1 ## Copyright (C) 2007 Sylvain Pelissier <sylvain.pelissier@gmail.com>
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{d} = oct2dec (@var{c})
18 ##
19 ## Convert octal to decimal values.
20 ##
21 ## Each element of the octal matrix @var{c} is converted to a decimal value.
22 ##
23 ## @seealso{base2dec,bin2dec,dec2bin}
24 ## @end deftypefn
25
26 function d = oct2dec (c)
27
28   if (nargin != 1)
29     print_usage ();
30   endif
31
32   # Check for negative or non-integer values
33   if (any (c(:) < 0) || any (c(:) != floor (c(:))))
34     error ("oct2dec: c must be an octal matrix");
35   endif
36
37   d = zeros (size (c));
38   l = size (c, 2);
39   for k = 1:l
40     str = num2str (c(:,k), "%ld");
41     d(:,k) = base2dec (str, 8);
42     if (any (isnan (d(:,k))))
43       error ("oct2dec: c must be an octal matrix");
44     endif
45   endfor
46
47 endfunction
48
49 %!shared x,y
50 %! x = reshape ([0:79], 10, 8)(1:8,:);
51 %! y = reshape ([0:63], 8, 8);
52 %!assert (oct2dec (0), 0)
53 %!assert (oct2dec (77777777), 2^24 - 1)
54 %!assert (oct2dec (x), y)
55
56 %% Test input validation
57 %!error oct2dec ()
58 %!error oct2dec (0, 0)
59 %!error <octal matrix> oct2dec (0.1)
60 %!error <octal matrix> oct2dec (-1)
61 %!error <octal matrix> oct2dec (8)