
function wcoef = RWT_BIO(x,M,ht,gt,sht,sgt)

% RWT_BIO -- Redundant biorthogonal wavelet transform
% Usage
%   wcoef = RWT_BIO(x,M,ht,gt,sht,sgt)
% Inputs
%   x        signal (possibly array)
%   M        number of levels to be transformed
%   ht, sht  lp filter + shift
%   gt, sgt  hp filter + shift
% Outputs
%   wcoefs   RWT on the rows
% Description
%   filter the rows of x

N = length(x);
Nj = N;
pht = length(ht);
pgt = length(gt);
sht = -sht - pht + 1;
sgt = -sgt - pgt + 1;
   
% filters must be flipped in forward wavelet transform
% flipping causes change in shifts sht and sgt (see above)
ht = ht(pht:-1:1);
gt = gt(pgt:-1:1);
p = floor((max(pht,pgt)+1)/2)*2;
% p must be even for two reasons:
% otherwise vectors of nonsimilar lengths are being added)
% and also we are adding a shift of p/2

w = zeros(M+1,N);
% w(M+1,:,:) = x;
w(M+1,:) = row(x);
% the last row of w will contain the scaling coefficients
% x is used as working space

skip = 1;
for j = 1:M
  s = shift(w(M+1,:),-skip*sgt);
  % periodic exetension
  while (p*skip > size(s,2))
      for tel = 1:skip
         s = [s s(:,1:N)];
      end
  Nj = size(s,2);
  end
  s = [s s(:,1:skip*p)];
  wj = zeros(size(s));
  Njp = size(wj,2);
  for k = 1:skip
      sk = s(:,k:skip:Njp);
      wj(k:skip:Njp) = filter(gt,1,sk);
  end
  s = shift(w(M+1,:),-skip*sht);
  % periodic exetension
  while (p*skip > size(s,2))
      for tel = 1:skip
         s = [s s(:,1:N)];
      end
  Nj = size(s,2);
  end
  s = [s s(:,1:skip*p)];
  x = zeros(size(s));
  Njp = size(x,2);
  for k = 1:skip
      sk = s(:,k:skip:Njp);
      x(k:skip:Njp) = filter(ht,1,sk);
  end
  wj(:,1:skip*p) = wj(:,Nj+1:Nj+skip*p);
  x(:,1:skip*p) = x(:,Nj+1:Nj+skip*p);
  w(j,:) = wj(:,1:N);
  w(M+1,:) = x(:,1:N);
  skip = skip*2;
end
wcoef = w;
