% Larsen-Algorithmus fuer die FWT
% liefert Sequenzordnung
% Enrico May, 2007
function s = larsen(s)
  N = length(s);
  n = log2(N);
  d = 1;
  b = 0;
  e = N / 4;
  while n ~= 0
    k = 0;
    while k < d
      i = k;
      while i < N
        j = i+d;
        if b >= 32768
          a = band(b, e);
          if a ~= 0
            s(j+1) = -s(j+1);
          end
        end
        s(i+1) = (s(i+1)+s(j+1)) / 2;
        s(j+1) = s(i+1)-s(j+1);
        b = b+1;
        i = i+d+d;
      end
      k = k+1;
    end
    d = d+d;
    b = 32768;
    n = n-1;
  end
s = bitrevorder(s);

% bitweises UND
function result = band(x, y)
  x = fliplr(dec2bin(x) - 48);
  y = fliplr(dec2bin(y) - 48);
  x(20) = 0;
  y(20) = 0;
result = sum(x & y);
