function f = IWT_BIO(w,L,h,g,sh,sg)

%   Usage
%     f = IWT_BIO(w,L,h,g,sh,sg)
%   Inputs
%     w        wavelet coefficients
%     L        coarsest level of transform
%     h,sh     lp filter and shift
%     g,sg     hp filter and shift
%   Outputs
%     f        Inverse wavelet transform Biorthogonal filter 
%   Description
%      1D Inverse wavelet transform

% LET OP: LUIZEN MOGELIJK in regel
% x(:,1:2:2*Njp) = (1-sho)*x1 + (1-sgo)*x2 + shift(sho*x3 + sgo*x4,-1);
% (reden voor shift niet doorgrond, enkel vastgesteld bij ervaring)

N = size(w,2);
if (N == 1)
   N = size(w,1);
   w = w';
end
J = log2(N);
ph = length(h);
pg = length(g);

%%% SPLITTING THE FILTERS %%%
heven = h(1:2:ph);
hodd  = h(2:2:ph);
geven = g(1:2:pg);
godd  = g(2:2:pg);
   
p = floor((max(ph,pg)+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

% last row of w contains scale coefficients
% x is working space

sho = mod(sh,2);
sgo = mod(sh,2);
sh = floor(sh/2);
sg = floor(sg/2);
for j = L:J-1
  s = shift(w(:,1:2^j),-sh);
  wj = shift(w(:,2^j+1:2^(j+1)),-sg);
  % periodic exetension
  Nj = size(s,2);
  while (2*p > size(s,2))
      s = [s s(:,1:Nj)];
      wj = [wj wj(:,1:Nj)];
  end
  Nj = size(s,2);
  s = [s s(:,1:2*p)];
  wj = [wj wj(:,1:2*p)];
  Njp = size(wj,2);
  x = zeros(size(s,1),2*Njp);
  x1 =  filter(heven,1,s')';
  x2 = filter(geven,1,wj')';
  x3 = filter(hodd,1,s')';
  x4 = filter(godd,1,wj')';
  x(:,1:2:2*Njp) = (1-sho)*x1 + (1-sgo)*x2 + shift(sho*x3 + sgo*x4,-1);
  x(:,2:2:2*Njp) = (1-sho)*x3 + (1-sgo)*x4 + sho*x1 + sgo*x2;
  x(:,1:2*p) = x(:,2*Nj+1:2*Nj+2*p);
  w(:,1:2^(j+1)) = x(:,1:2^(j+1));
end
f = w;
