Correlation Lab

Solution

ali

## Read audio files

close all; clear all; clc;
[R Fs]=audioread('Right.wav');
[L Fs]=audioread('Left.wav');


## Determine size of each chunk

Since we're dealing with 200ms chunks, number of samples in each chunk will be .

Chunk_size=Fs*0.2;
azimuth=[];     % Define vector for storing azimuth


## Determining azimuth

Evaluating azimuth for each 200ms chunk. There are 20 of them

for i=0:19
%Extract appropriate samples as a sub-vector
Rch=R((i*Chunk_size)+1:(i*Chunk_size)+Chunk_size);
Lch=L((i*Chunk_size)+1:(i*Chunk_size)+Chunk_size);
%Use cross-correlation.
[X lag]=xcorr(Rch,Lch);
[gr gr_index]=max(X);       % Find index position of maximum superimposition in the correlation vector.
disparity=gr_index-8820;    % Find its disparity in samples with reference to the start of the reference channel
t=abs(disparity)/Fs;        % Use sampling frequency to find out the time difference in arrival between the different microphones

theta=asind((t/0.8)*343);   % Use speed of sound, distance b/w microphones and trigonometric ratios to find angle.
theta2 = theta * (-1*(disparity/abs(disparity)));  % Determine whether angle is clockwise or CCW based on time lag or lead using sign of disparity.
azimuth(i+1,1)=theta2;                             % Store as azimuth for that time chunk.
end


## Output the vector of azimuth values

azimuth

azimuth =

25.3266
25.3266
25.3266
25.3266
25.3266
6.1392
6.1392
6.1392
6.1392
6.1392
6.1392
6.1392
6.1392
74.2595
74.2595
74.2595
74.2595
74.2595
-52.8657
-52.8657



## Plot path.

figure;
hold on;
xd=0; yd=0; sum_angle=0;
for j=1:20
sum_angle=azimuth(j)+sum_angle;
aa=plotline(xd,yd,1,sum_angle);
text(xd,yd,num2str(j));
xd=aa.XData(2);
yd=aa.YData(2);
end
title('Path of Robot');
grid on;
xlabel('X_{meters}');
ylabel('Y_{meters}'); 