%%
clear
close all
SOFAstart;
%hrtf = SOFAload('FABIAN_HRTF_DATABASE_V2\1 HRIRs\SOFA\FABIAN_HRIR_measured_HATO_0.sofa');
hrtf = SOFAload('subject_003.sofa');
%SOFAinfo(hrtf);
fs_hrtf = hrtf.Data.SamplingRate;
hrtfData = hrtf.Data.IR;
sourcePosition = hrtf.SourcePosition;
%
SOFAplotGeometry(hrtf);
pos = [180,0];
[~,ind] = min( sum(  (bsxfun(@minus, sourcePosition(:,1:2) ,mod(pos,360))).^2,2  ) );
hrtf = squeeze(hrtfData(ind,:,:));
figure;
f = (0:size(hrtf,2)-1)/size(hrtf,2)*fs_hrtf;
semilogx(f,20*log10(abs(fft(hrtf.'))));
xlim([0,fs_hrtf/2])
%
[signal_in,fs_signal] = audioread('GitL.wav');
signal_in = signal_in(1:3*fs_signal);
signal_out = [conv(signal_in,hrtf(1,:)'),...
              conv(signal_in,hrtf(2,:)')]; 
%
soundsc(signal_out,fs_signal);
