1 ## Copyright (C) 2007 Sylvain Pelissier <sylvain.pelissier@gmail.com>
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
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
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/>.
17 ## @deftypefn {Function File} {[@var{y} @var{i}] =} bitrevorder(@var{x})
18 ## Reorder x in the bit reversed order
22 function [y i] = bitrevorder(x)
24 if(nargin < 1 || nargin >1)
26 elseif(log2(length(x)) ~= floor(log2(length(x))))
27 error('x must have a length equal to a power of 2');
30 old_ind = 0:length(x)-1;
31 new_ind = bi2de(fliplr(de2bi(old_ind)));
38 ## The following functions, de2bi and bi2de, are from the communications package.
39 ## However, the communications package is already dependent on the signal
40 ## package and to avoid circular dependencies their code was copied here. Anyway,
41 ## in the future bitrevorder should be rewritten as to not use this functions
42 ## at all (and pkg can be fixed to support circular dependencies on pkg load
43 ## as it already does for pkg install).
45 ## note that aside copying the code from the communication package, their input
46 ## check was removed since in this context they were always being called with
49 function b = de2bi (d, n, p, f)
52 n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
56 if ( any (d < 0) || any (d != floor (d)) )
57 error ("de2bi: d must only contain non-negative integers");
61 n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
64 power = ones (length (d), 1) * (p .^ [0 : n-1] );
66 b = floor (rem (d, p*power) ./ power);
68 if (strcmp (f, 'left-msb'))
69 b = b(:,columns(b):-1:1);
70 elseif (!strcmp (f, 'right-msb'))
71 error ("de2bi: unrecognized flag");
77 function d = bi2de (b, p, f)
82 if ( any (b(:) < 0) || any (b(:) != floor (b(:))) || any (b(:) > p - 1) )
83 error ("bi2de: d must only contain integers in the range [0, p-1]");
86 if (strcmp (f, 'left-msb'))
87 b = b(:,size(b,2):-1:1);
88 elseif (!strcmp (f, 'right-msb'))
89 error ("bi2de: unrecognized flag");
95 d = b * ( p .^ [ 0 : (columns(b)-1) ]' );