% Find the best Approximation by circulant matrices 
% bestapprcirc.M,   29.06.2004 XXL
% 
% function   P  =  bestapprcirc(A,method); 
%
% Description : This function finds the best approximation of a 
%               matrix A by circulant matrices using different 
%               methods, depending on the second input. (Default = 1)
%
%               Methods: (0) .. best approximation in HS sense (direct)
%                        (1) .. -"- (fft method)  [faster]
%                        (2) .. mean value of rows
%                        (3) .. quadratic mean for diagonal on fft 
% 
% Input     :   A       =  matrix 
%               method  =  method
%
% Output    :   P = the circulant matrix approximating A
% 
% Comments  :   In the frobenius norm methods 0 & 1 are the best
%               approximation. For the operator norm this is no 
%               longer valid.
%
% Copyright :  (c) Acoustics Research Institute, Austrian Academy of
%              Science
%              http://www.kfs.oeaw.ac.at
function   C=bestapprcirc(A,method); 

if nargin < 1 
    disp('no input -> no output');
    return;
elseif nargin < 2
    method = 1;
end

[n1 n2] = size(A);
if n1 ~= n2
    disp('Non-quadratic matrices are at the moment not supported.');
    return
end

%profile on; %profiling

if method == 0
% direct
    c = zeros(1,n2);
    C = zeros(n1,n2);
    for k = 1:n2
        c(k) = (sum(A(find(transmatxxl(n1,k)))))/n1;
        % = inneres Produkt von A und Pi_L^k in HS
        C = C+c(k)*transmatxxl(n1,k);
    end
elseif method == 1
% using fft
    C = iFMFxxl(diag(diag(FMFxxl(A))));
elseif method == 2 
    v=mean(A.');
    C = zeros(n1,n2);
    for k = 1:n2
        C = C+v(k)*transmatxxl(n1,k);
    end
else
  C = iFMFxxl(diag(sqrt(sum(FMFxxl(A).^2))));
end
%profile viewer;
%profile off;

