% Test Approximation of Matrices by irreg. and reg. Gabor Mulitpliers 
% Script-File
% see GMAPPirr.m for more information
reg = 1;
single = 1;
randommat = 1;
startgabmul = 0;

n = 32;
g = gaussnk(n);
if single == 1
    gamma = g;
else 
    gamma = hamming(n/2).';
    gamma= [gamma((n/4+1):(n/2)) zeros(1,n/2) gamma(1:n/4)];
    gamma = gamma/norm(gamma);
    figure(5); subplot(1,2,1); plotc(g); title('Analysis Atom:');
    subplot(1,2,2); plotc(gamma); title('Synthesis Atom:');
end;
%gamma= [gamma(1:n/4) zeros(1,n/2) gamma((n/4+1):(n/2))];
%imagesc(abs(stft(g,g)));
%pause;
%imagesc(abs(stft(gamma,g)));

if reg == 1 
    a = 4;
    b = 4;
    xpo = zeros(n,n);
    xpo(1:b:n,1:a:n) = 1;
    T = randc(n,n);
    % T = rot(eye(n),6);
    K = n^2/(a*b); % number of TF points
else
    %T = eye(n);
    T = randc(n,n);
    xpo = rand(n) > (1-2/n); % red = 2
    K = sum(sum(xpo)); % number of TF points
end;

% Choose Matrix
if randommat == 1
    % random matrix
    T = randc(n,n);
elseif startgabmul == 1
   % Gabor Multiplier
   G = gabbaspirr(g,xpo);
   Ga = gabbaspirr(gamma,xpo);
   sym = randc(K,1);
   T = zeros(n,n);
   for i = 1:K
       P = G(i,:)'*Ga(i,:);
       T= T + sym(i)*P;
    end;  
else
    % identity
    T = eye(n);
end

[TAI,COEFFI] = GMAPPir(T,xpo,g);

G = gabbaspirr(g,xpo);
% Ga = gabbaspirr(gamma,xpo);
S = G'*G;
rank(S)
e = eig(S);

if reg == 1 
    disp(sprintf(...  % continued on the next line for printout
    '(g,%g,%g) : Lower Frame Bound for g : A = %g Upper Frame Bound: B = %g',...
    a,b,min(e),max(e)));
else
    disp(sprintf(...  % continued on the next line for printout
    '(g,xpo) : Lower Frame Bound for g : A = %g Upper Frame Bound: B = %g',...
    min(e),max(e)));
end

[IA1 coeff1] = ApprFramMult(T,G.');
compnorm(TAI,IA1);

figure(6);  subplot(1,2,2); surf(TAI); hold; spy(xpo); title('approximation'); 
 subplot(1,2,1); surf(T); hold; spy(xpo); title('original');

figure(1);
subplot(1,2,1);surf(T);title('original');
subplot(1,2,2);
surf(TAI); title('approximation');
figure(2); 
subplot(1,2,1); spy(xpo); hold; contour(T); title('original');
subplot(1,2,2); spy(xpo); hold; contour(TAI); title('approximation');
 
if reg == 1 & single == 1
 TA = GMAPPMH(T,g,a,b); % regular version
 figure(4)
 subplot(3,1,1);
 % subplot(2,1,1);
 surf(T);title('original');
 subplot(3,1,2);surf(TA); title('approximation (regular by MH)');
 subplot(3,1,3);
 % subplot(2,1,2);
 surf(TAI); title('approximation (irregular algorithm)');
 disp('Differences of regular and irregular version:');
 compnorm(TA,TAI);
end

figure(5);
TS = mat2spr(T);
TAIS = mat2spr(TAI);
subplot(2,1,1);
 surf(TS);title('spread of original spread');
 subplot(2,1,2);
 surf(TAIS); title('spread of approximation (irregular algorithm)');
