Question 2
close all;
clear all;
Fs = 44100;
TotalTime = 4;
t = -TotalTime/2+1/Fs:1/Fs:TotalTime/2;
c = 345;
offset = 10;
freq = 300;
freq2 = freq*5/4;
freq3 = freq*3/2;
vSource = 20;
distance = vSource*t;
newDistance = sqrt(offset.^2 + distance.^2);
envelope = 1.0./newDistance;
theta = asin(offset./newDistance);
newVSource = vSource*cos(theta);
f = zeros(1,length(t));
f2 = f;
f3 = f;
for i = 1:length(t)
if t(i)<=0
f(i) = freq*c/(c-newVSource(i));
f2(i) = freq2*c/(c-newVSource(i));
f3(i) = freq3*c/(c-newVSource(i));
else
f(i) = freq*c/(c+newVSource(i));
f2(i) = freq2*c/(c+newVSource(i));
f3(i) = freq3*c/(c+newVSource(i));
end
end
x = envelope.*(cos(2*pi*f.*t) + cos(2*pi*f2.*t) + cos(2*pi*f3.*t));
h = readhrtf(0,15,'H');
h = h';
M = length(h);
azimuth = theta;
azimuth = azimuth*180/pi;
azimuth = round(azimuth/5);
azimuth = 90 - azimuth*5;
x = x/max(abs(x));
x = x';
x = [x x];
N = 5*128;
iterations = ceil(length(x)/N);
y = zeros(length(x),2);
for r=0:(iterations-2)/2
h = readhrtf(0,azimuth(N*r+1),'H')';
y((r*N+1):(r*N+1 + N+M-2),1) = y((r*N+1):(r*N+1 + N+M-2),1) + conv(x((r*N+1):(r*N+N),1),h(:,2));
y((r*N+1):(r*N+1 +N+M-2),2) = y((r*N+1):(r*N+1 +N+M-2),2) + conv(x((r*N+1):(r*N+N),2),h(:,1));
end
for r = (iterations-2)/2+1:(iterations-2)
h = readhrtf(0,azimuth(N*r+1),'H')';
y((r*N+1):(r*N+1 + N+M-2),1) = y((r*N+1):(r*N+1 + N+M-2),1) + conv(x((r*N+1):(r*N+N),1),h(:,1));
y((r*N+1):(r*N+1 +N+M-2),2) = y((r*N+1):(r*N+1 +N+M-2),2) + conv(x((r*N+1):(r*N+N),2),h(:,2));
end
y = 0.97*y/max(max(abs(y)));
soundsc(y,Fs);
wavwrite(y,Fs,'Prob2');