
function wcoef = RWT_1step(x,j,gt,sgt)

% RWT_1step -- 1 step of Biorthogonal 2D, tensor-product redundant WT
% Usage
%   wcoef = RWT_1step(x,j,gt,sgt)
%   or
%   wcoef = RWT_1step(x,j,ht,sht)
% Inputs
%   x        signal (can be an array)
%   j        level
%   gt, sgt  hp filter + shift
% Outputs
%   wcoef    RWT, one step of signal
% Description
%   filters (the rows of) x to go from level j to level j+1
%   j = 1 is the finesr resolution
%   filters only one subband at a time (lowpass or highpass)


N = size(x,2);
Nj = N;
pgt = length(gt);
sgt = -sgt - pgt + 1;
   
% filters must be flipped in forward wavelet transform
% flipping causes change in shifts sht and sgt (see above)
gt = gt(pgt:-1:1);
p = pgt + mod(pgt,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

wcoef = zeros(size(x));

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