function phi = itd2angle(itd,lookup)
% ITD2ANGLE converts the given ITD to an angle using a lookup table
% Usage: phi = itd2angle(itd,lookup)
%
% Input parameters:
% itd : ITDs to convert to angles
% lookup : a struct containing the polinomial fitting entries p,MU,S.
% This struct can be generated by ITD2ANGLE_lookuptable
%
% Output parameters:
% phi : angles for the corresponding ITD values / deg
%
% ITD2ANGLE(itd,lookup) converts the given ITD values to azimuth angles phi.
% Therefore a lookup table containing the polynomial fitting parameters is
% used. This lookup table is created from a set of HRTFs with known azimuth
% angles and stores the corresponding ITD values. itd2angle works with
% DIETZ2011 and LINDEMANN1986. Corresponding lookup tables can be created
% by ITD2ANGLE_LOOKUPTABLE.
%
% Url: http://amtoolbox.org/amt-1.5.0/doc/common/itd2angle.php
% #Author: Mathias Dietz
% #Author: Hagen Wierstorf
% This file is licensed unter the GNU General Public License (GPL) either
% version 3 of the license, or any later version as published by the Free Software
% Foundation. Details of the GPLv3 can be found in the AMT directory "licences" and
% at <https://www.gnu.org/licenses/gpl-3.0.html>.
% You can redistribute this file and/or modify it under the terms of the GPLv3.
% This file is distributed without any warranty; without even the implied warranty
% of merchantability or fitness for a particular purpose.
%% ===== Checking of input parameters ====================================
nargmin = 2;
nargmax = 2;
narginchk(nargmin,nargmax);
%% ===== Computation =====================================================
phi = zeros(size(itd));
for n = 1:size(itd,2)
% by calling the output S and MU, phi is z-scored, thus improving the fitting
phi(:,n)=polyval(lookup.p(:,n),itd(:,n),lookup.S{n},lookup.MU(:,n));
end
% neglect angles > 95°. Warning => maybe systematic underestimation for azi ~ 90°
phi(abs(phi)>95) = NaN;