# v_distitpf

## PURPOSE

V_DISTITPF calculates the Itakura spectral distance between power spectra D=(PF1,PF2,MODE)

## SYNOPSIS

function d=v_distitpf(pf1,pf2,mode)

## DESCRIPTION

    V_DISTITPF calculates the Itakura spectral distance between power spectra D=(PF1,PF2,MODE)

     Inputs: PF1,PF2     Power spectra to be compared. Each row represents a power spectrum: the first
                         and last columns represent the DC and Nyquist terms respectively.
                         PF1 and PF2 must have the same number of columns.

             MODE        Character string selecting the following options:
                             'x'  Calculate the full distance matrix from every row of PF1 to every row of PF2
                             'd'  Calculate only the distance between corresponding rows of PF1 and PF2
                                  The default is 'd' if PF1 and PF2 have the same number of rows otherwise 'x'.

     Output: D           If MODE='d' then D is a column vector with the same number of rows as the shorter of PF1 and PF2.
                         If MODE='x' then D is a matrix with the same number of rows as PF1 and the same number of columns as PF2'.

     If ave() denotes the average over +ve and -ve frequency, the Itakura spectral distance is

                                   log(ave(pf1/pf2)) - ave(log(pf1/pf2))

     The Itakura distance is gain-independent, i.e. v_distitpf(g*pf1,pf2) is independent of g.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function d=v_distitpf(pf1,pf2,mode)
    0002 %V_DISTITPF calculates the Itakura spectral distance between power spectra D=(PF1,PF2,MODE)
    0003 %
    0004 % Inputs: PF1,PF2     Power spectra to be compared. Each row represents a power spectrum: the first
    0005 %                     and last columns represent the DC and Nyquist terms respectively.
    0006 %                     PF1 and PF2 must have the same number of columns.
    0007 %
    0008 %         MODE        Character string selecting the following options:
    0009 %                         'x'  Calculate the full distance matrix from every row of PF1 to every row of PF2
    0010 %                         'd'  Calculate only the distance between corresponding rows of PF1 and PF2
    0011 %                              The default is 'd' if PF1 and PF2 have the same number of rows otherwise 'x'.
    0012 %
    0013 % Output: D           If MODE='d' then D is a column vector with the same number of rows as the shorter of PF1 and PF2.
    0014 %                     If MODE='x' then D is a matrix with the same number of rows as PF1 and the same number of columns as PF2'.
    0015 %
    0016 % If ave() denotes the average over +ve and -ve frequency, the Itakura spectral distance is
    0017 %
    0018 %                               log(ave(pf1/pf2)) - ave(log(pf1/pf2))
    0019 %
    0020 % The Itakura distance is gain-independent, i.e. v_distitpf(g*pf1,pf2) is independent of g.
    0021
    0022 % The Itakura distance can also be calculated directly from AR coefficients; providing np is large
    0023 % enough, the values of d0 and d1 in the following will be very similar:
    0024 %
    0025 %         np=255; d0=v_distitar(ar1,ar2); d1=v_distitpf(v_lpcar2pf(ar1,np),v_lpcar2pf(ar2,np))
    0026 %
    0027
    0028 % Ref: A.H.Gray Jr and J.D.Markel, "Distance measures for speech processing", IEEE ASSP-24(5): 380-391, Oct 1976
    0029 %      L. Rabiner abd B-H Juang, "Fundamentals of Speech Recognition", Section 4.5, Prentice-Hall 1993, ISBN 0-13-015157-2
    0030 %      F. Itakura, "Minimum prediction residual principle applied to speech recognition", IEEE ASSP-23: 62-72, 1975
    0031
    0032
    0033 %      Copyright (C) Mike Brookes 1997
    0034 %      Version: $Id: v_distitpf.m 10865 2018-09-21 17:22:45Z dmb $
    0035 %
    0036 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0037 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0038 %
    0039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0040 %   This program is free software; you can redistribute it and/or modify
    0041 %   it under the terms of the GNU Lesser General Public License as published by
    0042 %   the Free Software Foundation; either version 3 of the License, or
    0043 %   (at your option) any later version.
    0044 %
    0045 %   This program is distributed in the hope that it will be useful,
    0046 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0047 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0048 %   GNU Lesser General Public License for more details.
    0049 %
    0050 %   You can obtain a copy of the GNU Lesser General Public License from
    0051 %   https://www.gnu.org/licenses/ .
    0052 %
    0053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0054
    0055 [nf1,p2]=size(pf1);
    0056 p1=p2-1;
    0057 nf2=size(pf2,1);
    0058 if nargin<3 | isempty(mode) mode='0'; end
    0059 if any(mode=='d') | (mode~='x' & nf1==nf2)
    0060    nx=min(nf1,nf2);
    0061    r=pf1(1:nx,:)./pf2(1:nx,:);
    0062    q=log(r);
    0063    d=log((sum(r(:,2:p1),2)+0.5*(r(:,1)+r(:,p2)))/p1)-(sum(q(:,2:p1),2)+0.5*(q(:,1)+q(:,p2)))/p1;
    0064 else
    0065    r=permute(pf1(:,:,ones(1,nf2)),[1 3 2])./permute(pf2(:,:,ones(1,nf1)),[3 1 2]);
    0066    q=log(r);
    0067    d=log((sum(r(:,:,2:p1),3)+0.5*(r(:,:,1)+r(:,:,p2)))/p1)-(sum(q(:,:,2:p1),3)+0.5*(q(:,:,1)+q(:,:,p2)))/p1;
    0068 end