function [A02, b_totalbase] = getHaarA(X,dimx,dimy,b_fromlevel,b_tolevel,useSparse)

row=dimx*dimy;
col=0;
for temp=b_fromlevel:b_tolevel
    col=col+(dimx/(2^temp))*(dimy/(2^temp))*3; % 3 for H,V,D; 2^temp is support length
end
col=col+(dimx/(2^temp))*(dimy/(2^temp)); % don't miss the approx for last level
%use sparse if big; but slower
if useSparse==1
   A02=sparse(row,col);
else
%use non-sparse if small
   A02=zeros(row,col);
end
current=1;
for i=b_fromlevel:b_tolevel
   length=2^i; %non-zero elements
   row_basenum=dimx/length;
   col_basenum=dimy/length;
   x=[repmat(1,[1,length]) repmat(0,[1,dimx-length])];
   y=[repmat(1,[1,length/2]) repmat(-1,[1,length/2]) repmat(0,[1,dimx-length])];
   vw=kron(x,y')/length;%horizontal
   wv=kron(y,x')/length;%vertical
   ww=kron(y,y')/length;%diagonal
   for j=row_basenum:-1:1
       for k=col_basenum:-1:1
           if size(X,1)==0
               tempww=circshift(ww,[length*(k-1),length*(j-1)]);
           else
               tempww=circshift(ww,[length*(k-1),length*(j-1)]).*X;
           end
           A02(:,current)=reshape(tempww,dimx*dimy,1);
           current=current+1;          
       end
   end
   for j=row_basenum:-1:1
       for k=col_basenum:-1:1
           if size(X,1)==0
               tempwv=circshift(wv,[length*(k-1),length*(j-1)]);
           else
               tempwv=circshift(wv,[length*(k-1),length*(j-1)]).*X;
           end
           A02(:,current)=reshape(tempwv,dimx*dimy,1);
           current=current+1;          
       end
    end  
   for j=row_basenum:-1:1
       for k=col_basenum:-1:1
           if size(X,1)==0
               tempvw=circshift(vw,[length*(k-1),length*(j-1)]);
           else
               tempvw=circshift(vw,[length*(k-1),length*(j-1)]).*X;               
           end
           A02(:,current)=reshape(tempvw,dimx*dimy,1);
           current=current+1;          
       end
   end   
end
%add approx coef. for the last level only
vv=kron(x,x')/length;
for j=row_basenum:-1:1
    for k=col_basenum:-1:1
        if size(X,1)==0
            tempvv=circshift(vv,[length*(k-1),length*(j-1)]);
        else
            tempvv=circshift(vv,[length*(k-1),length*(j-1)]).*X;
        end
        A02(:,current)=reshape(tempvv,dimx*dimy,1);
        current=current+1;          
    end
end
b_totalbase=current-1;