% Test Matrix Approximation by Gabor Mulitpliers and circulant matrices
% Script-File
% see GMAPPirr.m for more information
%
% see also TestBestApprMult.m
reg = 1;
single = 1;
testcirc = 1;
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);
else
    %T = eye(n);
    T = randc(n,n);
    xpo = rand(n) > (1-2/n); % red = 2
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

% Compare to standard Approximation by Frame Multiplier
% [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');
 
TAC = bestapprcircl(T); % approximation by circular matrix = time-variant system.
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,2);surf(TAC); title('approximation by circulant matrix (time invariant system)');
 subplot(3,1,3);
 %subplot(2,1,2);
 surf(TAI); title('approximation (irregular algorithm)');
 compnorm(TA,TAI);
end
 
if testcirc == 1
 TAC = bestapprcircl(T); % approximation by circular matrix = time-variant system.
 figure(5)
 subplot(3,1,1);
 % subplot(2,1,1);
 surf(T);title('original');
 subplot(3,1,2);surf(TAC); title('approximation by circulant matrix (time invariant system)');
 subplot(3,1,3);
 %subplot(2,1,2);
 surf(TAI); title('approximation (irregular algorithm)');
 compnorm(T,TAC);
 compnorm(T,TAI);

figure(6);
TS = spread(T);
TAIS = spread(TAI);
TACS = spread(TAC);
subplot(3,1,1);
 surf(TS);title('TF spread of original spread');
 subplot(3,1,2);
 surf(TACS); title('TF spread of circular approximation');
 subplot(3,1,3);
 surf(TAIS); title('TF spread of approximation (irregular algorithm)');
end