]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/bitrevorder.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / bitrevorder.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{y} @var{i}] =} bitrevorder(@var{x})
18 ## Reorder x in the bit reversed order
19 ## @seealso{fft,ifft}
20 ## @end deftypefn
21
22 function [y i] = bitrevorder(x)
23
24   if(nargin < 1 || nargin >1)
25     print_usage;
26   elseif(log2(length(x)) ~= floor(log2(length(x))))
27     error('x must have a length equal to a power of 2');
28   end
29
30   old_ind = 0:length(x)-1;
31   new_ind = bi2de(fliplr(de2bi(old_ind)));
32   i = new_ind + 1;
33
34   y(old_ind+1) = x(i);
35
36 endfunction
37
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).
44
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
47 ## nargin == 1
48
49 function b = de2bi (d, n, p, f)
50
51   p = 2;
52   n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
53   f = 'right-msb';
54
55   d = d(:);
56   if ( any (d < 0) || any (d != floor (d)) )
57     error ("de2bi: d must only contain non-negative integers");
58   endif
59
60   if (isempty (n))
61     n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
62   endif
63
64   power = ones (length (d), 1) * (p .^ [0 : n-1] );
65   d = d * ones (1, n);
66   b = floor (rem (d, p*power) ./ power);
67
68   if (strcmp (f, 'left-msb'))
69     b = b(:,columns(b):-1:1);
70   elseif (!strcmp (f, 'right-msb'))
71     error ("de2bi: unrecognized flag");
72   endif
73
74 endfunction
75
76
77 function d = bi2de (b, p, f)
78
79   p = 2;
80   f = 'right-msb';
81
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]");
84   endif
85
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");
90   endif
91
92   if (length (b) == 0)
93     d = [];
94   else
95     d = b * ( p .^ [ 0 : (columns(b)-1) ]' );
96   endif
97
98 endfunction