% Create a full (Gabor-type) matrix out of a block matrix
%
% Blo2WalXXL.M   Block-to-Walnut-matrix
% by XXL
%
% This program creates a matrix that looks like a Gabor frame
% matrix, a so called Gabor-like or Walnut matrix,  meaning only every 
% M=L/b sidediagonal is not zero and the side diagonals are a-periodic.
% So the matrices created have b*b diagonal blocks and are 
% a-block circulant. 
%
% Attention: it is important how the gabor like matrix is built.
%            With this program both possibilities are supported.
%
% inputs : A ........ block Matrix with the non trivial data
%          n ......... (minimum) size of result
%          method .... 0 - b x a block matrix 
%                      1 - a x b block matrix
%
%  output: the n x n matrix having only the data of A as non trivial 
%          values. The rest is defined by being a "Gabor frame like"
%          matrix, see above.
%
%  usage:  B = Blo2WalXXL(A,n)
%
%  notes : method == 0 corresponds to the algorithm first used and 
%          implemented by Qiu and Feichtinger, where the block matrix is
%          the condensed left b x n submatrix of the frame operator
%          method == 1 uses the a x b matrix, which is derived from the
%          upper submatrix a x n submatrix
%
% Author:   XXL .. Peter Balazs, contact: a8927259@unet.univie.ac.at
%
% Literature : [Qiu] S. Qiu, H.G. Feichtinger, Gabor-Type Matrices And 
%                    Discrete Huge Gabor Transforms 
%              [Xxl] P. Balazs, Gabor frame multipliers with application to
%                    psychoacoustical masking 
%                    (Ph.D. thesis, in preparation) 
%
% See also: blockxxl.m - which builds a block matrix for a gabor system
%           bm2fm.m    - version of Mario Hampjes
%
% Copyright :  (c) NUHAG, Faculty of Math., University of Vienna, AUSTRIA
%              http://nuhag.mat.univie.ac.at/
%              (c) Acoustics Research Institute, Austrian Academy of
%              Science
%              http://www.kfs.oeaw.ac.at
%
%              Permission is granted to modify and re-distribute this
%              code in any manner as long as this notice is preserved.
%              All standard disclaimers apply.

% news (12/01/2005) both kind of block matrices possible.

function U = Blo2Walxxl(A,n,method);

if nargin < 1 
    disp('At least one input (A) is needed');
    return;
elseif nargin < 3 | method ~= 1
    method = 0;
end;

[b a] = size(A);

if rem(n, lcm(a,b)) ~=0 ; 
    sprintf(...
    'n has to be a multiple of the number of rows (a= %i) and columns (b= %i) of A'...
    ,a,b)
    sprintf('Setting it to the smallest common multiple bigger than n=%i',n)
    n = exp(floor(log(n)/log(mult))+1)    
end;

U = zeros(n,n);
rr = 0:(b-1);

if method == 0
    M = n/b;
    for ii = 0:(n-1)
        p = mod(ii + rr*M,n)+1;
        U(p,ii+1) = A(mod(rr,b)+1,mod(ii,a)+1);
    end
else
   [a b] = size(A);
    M = n/b;
    for ii = 0:(n-1)
        p = mod(ii + rr*M,n)+1;
        U(ii+1,p) = A(mod(ii,a)+1,mod(rr,a)+1);
    end
end;
