% ITAKURA Itakura distance between two signals.
%
%   sig is the signal to compare with sig0.
%   sig0 (or a0,r0) is the reference signal.
%   p is the LPC order (e.g. 6).
%
%   d         = itakura(p, sig, sig0)
%   [d, a, r] = itakura(p, sig, sig0)
%   d         = itakura(p, sig, a0, r0)
%   [d, a, r] = itakura(p, sig, a0, r0)
%
%     where:
%   [x0,r0] = corrmtx(sig0,p);
%   a0 = levinson(r0(1,:));

function [d, a, r] = itakura(p, sig, sig0_or_a0, r0)

    if nargin < 3
        error('itakura() needs at least 3 arguments');
    end
    
    if nargin == 3
        sig0 = sig0_or_a0;
        [x0,r0] = corrmtx(sig0,p);
        a0 = levinson(r0(1,:));
    else
        a0 = sig0_or_a0;
    end
    
    [x,r] = corrmtx(sig,p);
    a = levinson(r(1,:));

    d = log(a * r0 * a'/(a0 * r0 * a0'));
 