
function x = IRT_1step(s,w,j,h,g,sh,sg,alfa)

% IRT_1step -- 1 step of Inverse Redundant WT
%  Usage
%    x = IRT_1step(s,w,j,h,g,sh,sg,alfa)
%  Inputs
%    s      scaling coefficients
%    w      wavelet coefficients
%    j      input resolution level (j > 0)
%           j = 0 corresponds to resolution of non-transformed data
%    h,sh   lp filter + shift
%    g,sg   hp filter + shift
%    alfa   
% Outputs
%    x      back transformed signal
%
%  Description
%    1 step of Inverse Redundant WT

N = size(s,2);
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

skip = 2^(j-1);
s = shift(s,-skip*sh);
wj = shift(w,-skip*sg);
% periodic exetension
while (p*2*skip > size(s,2))
    s = [s s(:,1:N)];
    wj = [wj wj(:,1:N)];
end
Nj = size(s,2);
s = [s s(:,1:2*skip*p)];
wj = [wj wj(:,1:2*skip*p)];
x1 = s;
x2 = s;
Njp = size(wj,2);
for k = 1:skip
    sk = s(:,k:2*skip:Njp);
    wjk = wj(:,k:2*skip:Njp);
    x1(:,k:2*skip:Njp) = filter(heven,1,sk')' + filter(geven,1,wjk')';
    x1(:,k+skip:2*skip:Njp) = filter(hodd,1,sk')' + filter(godd,1,wjk')';
    sk = s(:,skip+k:2*skip:Njp);
    wjk = wj(:,skip+k:2*skip:Njp);
    x2(:,k:2*skip:Njp) = filter(heven,1,sk')' + filter(geven,1,wjk')';
    x2(:,k+skip:2*skip:Njp) = filter(hodd,1,sk')' + filter(godd,1,wjk')';
end
x1(:,1:skip*p) = x1(:,Nj+1:Nj+skip*p);
x2(:,1:skip*p) = x2(:,Nj+1:Nj+skip*p);
x1 = x1(:,1:N);
x2 = shift(x2(:,1:N),-skip);
x = alfa*x1 + (1-alfa)*x2;
