function perm = fmstset(law)
% Function to generate setup information for fast m-sequence
% transform (fmst) function.
%
% law     sequence law written as if it were octal.  For to
%         specify the law 2033 written in octal use the value
%         2033 decimal.
%
% p       A structure containing the number of transform layers
%         and two arrays of permuation vectors.  The vectors 
%         are used by fmt to unscramble and rescample indices
%         so that processing can be performed using the fast
%         Hadamard transform.
%
%         The basic m-sequence transform technique is described
%         in the paper "On Fast M-Sequence Transforms" by
%         M.Cohn and A.Lempel, IEEE Transactions on Information
%         Theory, January 1977.
%
%         22May2000 initial production version...K.Metzger

% convert law written in decimal to binary

value = 0; weight = 1; temp = law;
while temp ~= 0
  value = value+weight*rem(temp,10);
  temp = floor(temp/10);
  weight = weight*8;
end

% determine the number of transform layers
%   and the period length, L

layers = 0; v = floor(value/2);
while v ~= 0
  layers = layers+1;
  v = floor(v/2);
end
 
L = 2^layers-1;
end_bit = (L+1)/2;
ms_c = 1;
ss_c = 1;
scrm = zeros(1,L+1);
unsc = zeros(1,L+1);
scrm(1) = 1;
unsc(1) = 1;

% generate the permutaion index arrays

for idx = 2:L+1
  scrm(idx) = ss_c+1;
  unsc(idx) = ms_c+1;
  temp = bitand(ss_c, value);
  ss_c = floor(ss_c/2);
  while temp ~= 0
    if bitand(temp, 1)
      ss_c = bitxor(ss_c, end_bit);
    end
    temp = floor(temp/2);
  end
  if bitand(ms_c, 1)
    ms_c = bitxor(ms_c, value);
  end
  ms_c = floor(ms_c/2);
end

perm.layers = layers;  % number of transform layers to be used
perm.in = scrm;        % permutation to be applied to input
perm.out = unsc;       % permutation used to order the output

return;