% HRTF data
function hrir_signal = conv_hrir (signal, x, y, z, index)
% 1. Open the selected HRTF data
% 2. Find the specific Impulse response from the HRTF data
global hrtf_data earsize
global sig1 sig2 sig3
global az_all el_all r_all
global num_channel_sig1 num_channel_sig2 num_channel_sig3

% global hrir_sig1 hrir_sig2 hrir_sig3
% global sig1 sig2 sig3

hrtf_data = ['hrtf_data/hrir_',earsize,'.mat'];

%load location_data;
% By using readhrit_cipic function, acquire data for each location

[az, el, r] = ctos(x, y, z);
if num_channel_sig1 == 1    
    sig1(:,1) = sig1;
    sig1(:,2) = sig1;
elseif num_channel_sig2 == 1    
    sig2(:,1) = sig2;
    sig2(:,2) = sig2;
elseif num_channel_sig3 == 1    
    sig3(:,1) = sig3;
    sig3(:,2) = sig3;
end

%Matrix for HRIR Data
az_data = [-80 -65 -55 -45:5:45 55 65 80];
el_data = [-45:5.625:230.625];

[az_closest_value, az_index] = closest_value(az, az_data);
[el_closest_value, el_index] = closest_value(el, el_data);

az_all(:,index) = az;
el_all(:,index) = el;
r_all(:,index) = r;

[hl, hr] = readhrir_cipic(hrtf_data, az_index, el_index);

%Convolution with HRIR
left = conv(signal(:,1), hl);
right = conv(signal(:,2), hr);

hrir_signal = [left right];




