summaryrefslogtreecommitdiff
path: root/macros/digitrevorder.sci
blob: 886c43fdef5401a11e77de9a31c0bc840ffe7e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Returns input data in digit-reversed order

// Calling Sequence
//[y,i] = digitrevorder(x,r)
//y = digitrevorder(x,r)

// Parameters
//x: Vector of real or complex values
//r: radix / base
//y: input vector in digit reverse order
//i: indices

// Description
//This function returns the input data after reversing the digits of the indices and reordering the elements of the input array.

// Examples
//x = [%i,1,3,6*%i] ;
//r = 2 ;
//[y i]=digitrevorder(x, r)
//Output :
// i  =
//
//    1.    3.    2.    4.
// y  =
//
//    i      3.    1.    6.i
function [y, i] = digitrevorder (x, r)
    funcprot(0);
    [nargout, nargin] = argn() ;

  if (nargin > 2 | nargin <= 1)
    error("digitrevorder : invalid number of inputs")
  elseif (~ isvector (x))
    error ("digitrevorder : X must be a vector");
  elseif (~ (isscalar (r) & r == fix (r) & r >= 2 & r <= 36))
    error ("digitrevorder : R must be an integer between 2 and 36");
  else
    tmp = log (length(x)) / log (r);
    if (fix (tmp) ~= tmp)
      error ("digitrevorder: X must have length equal to an integer power of radix");
    end
  end

  old_ind = 0:length(x) - 1;

  //new_ind = base2dec(mtlb_fliplr(dec2base(old_ind, r)), r); //it works only on octave
  old_ind_base = dec2base(old_ind, r) ;
  new_ind_base = [] ;
  b = [] ;
  for i=1:length(x)
      new_ind_base = [new_ind_base mtlb_fliplr(old_ind_base(i))];
  end
  new_ind = base2dec(new_ind_base,r) ;
  //end of index conversion

  i = new_ind + 1;
  y(old_ind + 1) = x(i);

  [rows_x columns_x] = size(x) ;

  if (columns_x == 1)
    y = y';
  else
    i = i;
  end

endfunction