function y = spatial(signal)
%Humans can localize sound sources in a 3D space with good accuracy using
%several cues. Because the HRTF (Head Related Transfer Function) would
%vary with elevation and azimuth, this would create a unique HRTF for each
%sound source location. HRTF was important to researcher to get information
%on the ability of humans to use sonic cues to estimate the spatial location
%of a target.This matlab script will use the concept of HRTF. The major
%concept is that a signal (either it is a noise or music signal) will be
%convolve with the Head Related Impulse Response (HRIR) that has several
%directivity information in terms of azimuth and elevation. The lpc function
%also being use in to generates filter coefficients that approximate the
%magnitude response of a signal. It is expected that when the output sound
%is played over headphones, it should sound as if it were coming from a
%specific spatial location which give the impression of spatial audio sources.
%The function will first read the HRIR wave file. The lpc will generates
%filter coefficients that approximate the magnitude response of the signal.
%Lpc for each right stereo/channel and left stereo/channel HRIR are done
%separately. The number of pole will dictate how many poles will be used to
%model the resonance. An impz function will be then generating impulse
%responses from the pole coefficients obtained from the lpc function. The
%impulses responses are then convolve with the signal file to each signal
%channel. The convolved signals are then turned into stereo format. These
%steps will be done six times. The entire six stereo convolved signals are
%then summed to produce the stereo spatial output.The parameters that a user
%should have are:
%a)Signal – a signal type can be a noise signal such as white noise or sine
%tone or a music type file. The format of the file must in wav format. A
%wav file type data need to be assign as a ‘signal’ at the workspace.
%b)HRIR – a stereo HRIR file need to have by the user. The HRIR file must
%be in wav format. The HRIR can have any kind of azimuth and elevation
%information depending on the user interest. Users should have six types of
%files, three are right ear HRIR and the other three are left ear HRIR.
%Users need to key in the parameters as below:
%a)A wav file type need to be assign as a ‘signal’ at the workspace.
%b)Users need to put in the HRIR wav file at each dataR and dataL.
%c)Users also can change the number of poles from 10 to another value to
%check either it will give the differences in the output sound.
fs = 44100;
dataR1 = wavread ('R_195_15.wav'); %import HRIR of right ear, the name of the wav file here are only example
dataL1 = wavread ('L_15_15.wav'); %import HRIR of left ear, the name of the wav file here are only example
dataR2 = wavread ('R_210_30.wav'); %import HRIR of right ear, the name of the wav file here are only example
dataL2 = wavread ('L_30_30.wav'); % import HRIR of left ear, the name of the wav file here are only example
dataR3 = wavread ('R_240_45.wav'); %import HRIR of right ear, the name of the wav file here are only example
dataL3 = wavread ('L_60_45.wav'); % import HRIR of left ear, the name of the wav file here are only example
% 1st impulse response
polecoeffR1 = lpc((dataR1(:,1)),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffR1);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
polecoeffR2 = lpc(dataR1(:,2),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffR2);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
hrirR1 = polecoeffR1;
hrirR2 = polecoeffR2;
lind1 = conv(hrirR1(:), signal(:,1)); %convolving with HRIR
rind1 = conv(hrirR2(:), signal(:,2)); %convolving with HRIR
stereosig1 = [lind1 rind1]; %making stereo
% 2nd impulse response
polecoeffL1 = lpc(dataL1(:,1),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffL1);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
polecoeffL2 = lpc(dataL1(:,2),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffL2);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
hrirL1 = polecoeffL1;
hrirL2 = polecoeffL2;
lind2 = conv(hrirL1(:), signal(:,1)); %convolving with HRIR
rind2 = conv(hrirL2(:), signal(:,2)); %convolving with HRIR
stereosig2 = [lind2 rind2]; %making stereo
% 3rd impulse response
polecoeffR3 = lpc(dataR2(:,1),10);
%This is where the lcp function is called using just one channel from
%the HRIR.
impz (polecoeffR3);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
polecoeffR4 = lpc(dataR2(:,2),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffR4);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
hrirR3 = polecoeffR3;
hrirR4 = polecoeffR4;
lind3 = conv(hrirR3(:), signal(:,1)); %convolving with HRIR
rind3 = conv(hrirR4(:), signal(:,2)); %convolving with HRIR
stereosig3 = [lind3 rind3]; %making stereo
% 4th impulse response
polecoeffL3 = lpc(dataL2(:,1),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffL3);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
polecoeffL4 = lpc(dataL2(:,2),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffL4);
% An impz function is used to generate impulse responses from the pole
% coefficients obtained from the lpc function.
hrirL3 = polecoeffL3;
hrirL4 = polecoeffL4;
lind4 = conv(hrirL3(:), signal(:,1));
rind4 = conv(hrirL4(:), signal(:,2));
stereosig4 = [lind4 rind4]; %making stereo
% 5th impulse response
polecoeffR5 = lpc(dataR3(:,1),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffR5);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
polecoeffR6 = lpc(dataR3(:,2),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffR6);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
hrirR5 = polecoeffR5;
hrirR6 = polecoeffR6;
lind5 = conv(hrirR5(:), signal(:,1)); %convolving with HRIR
rind5 = conv(hrirR6(:), signal(:,2)); %convolving with HRIR
stereosig5 = [lind5 rind5]; %making stereo
% 6th impulse response
polecoeffL5 = lpc(dataL3(:,1),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffL5);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
polecoeffL6 = lpc(dataL3(:,2),10);
%This is where the lcp function is called using just one channel from
%the HRIR. The number of pole here is 10.This will dictate how many poles
%will be used to model the resonance.
impz (polecoeffL6);
%An impz function is used to generate impulse responses from the pole
%coefficients obtained from the lpc function.
hrirL5 = polecoeffL5;
hrirL6 = polecoeffL6;
lind6 = conv(hrirL5(:), signal(:,1)); %convolving with HRIR
rind6 = conv(hrirL6(:), signal(:,2)); %convolving with HRIR
stereosig6 = [lind6 rind6]; %making stereo
y = stereosig1 + stereosig2 + stereosig3 + stereosig4 + stereosig5 + stereosig6; % summing all the input
sound(y, fs); %play the sound output
end