% function [dtf,ctf]=hrtf2dtfctf(hrtf)
nfft=size(hrtf,1);
ctff=mean(20*log10(fft(hrtf)),2);

% switch for 1) fu,fo  2) full  3) auto -> 60dB

% AUTO
idx=find(mean(abs(ctff),3) >= max(mean(abs(ctff),3))-60);
ctff=10.^(ctff(idx,:,:)/20);

% minimal phase
n=size(ctff,1);
for ch=1:size(ctff,3)
  for ii=1:size(ctff,2)
      % decompose signal
    amp1=abs(squeeze(ctff(:,ii,ch)));
      % transform
    amp2=amp1;
    an2u=-imag(hilbert(log(amp1))); % minimal phase

        % reconstruct signal from amp2 and an2u
          % build a symmetrical phase 
    an2u=an2u(1:floor(n/2)+1);
    an2u=[an2u; -flipud(an2u(2:end+mod(n,2)-1))];
    an2=an2u-round(an2u/2/pi)*2*pi;  % wrap around +/-pi: wrap(x)=x-round(x/2/pi)*2*pi
          % amplitude
    amp2=amp2(1:floor(n/2)+1);
    amp2=[amp2; flipud(amp2(2:end+mod(n,2)-1))];
          % back to time domain
    ctff(:,ii,ch)=amp2.*exp(1i*an2);
  end
end
% ctff=fft(AA_MinimalPhase(ifft(ctff)));

hrtff=fftreal(hrtf,nfft/2);
dtff=zeros(size(ctff,1),size(hrtff,2),size(hrtff,3));
for ch=1:2
    for ii=1:size(dtff,2)
        dtff(:,ii,ch)=hrtff(idx,ii,ch)./ctff(:,1,ch);
    end
end

dtf=ifft(dtff);
ctf=ifft(ctff);
