<< Chapter < Page Chapter >> Page >
This module gives a summary of the code we wrote to implement multilateration.

Program:

Here is the code for the program that we wrote to record and analyze the data received from the microphones:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Program constants

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nMics = 4; % The number of microphones

Fs = 44100; % The sample rate

nBits = 8; % The number of bits per sample, either {8,16,24}

nChannels = 1; % The number of channels recorded, either {0,1}

recordLengthSeconds = 3; % The length of each recording in seconds

recordLength = recordLengthSeconds*Fs; % The length of each recording in entries

v = 1130.90551;

maxLags = 0.5*Fs;

% Offset in seconds to add to each mic's tau to account for delay start in recording

mic1TauOffset = 0;

mic2TauOffset = 0.0279;

mic3TauOffset = 0.0596;

mic4TauOffset = 0.1095;

This first part demonstrates the variables that we created and our averages (the offsets) used in calibrating the microphones (we got these values with time delay tests before we tested the entire array). The variable v represents the speed of sound in the area that we tested, and the maxLags represents the maximum lag between the microphones.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Initialization

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pause on; % Enable pausing, which is used below

ids = zeros(1,nMics); % The device ID for each microphone

ids(1) = 1;

ids(2) = 2;

ids(3) = 3;

ids(4) = 4;

taus = zeros(1,nMics); % The TDOA in seconds between each mic and mic 1

As = zeros(1,nMics-2);

Bs = zeros(1,nMics-2);

Ds = zeros(1,nMics-2);

xs = zeros(1,nMics); % The x-position in feet of each mic relative to mic 1

ys = zeros(1,nMics); % The y-position in feet of each mic relative to mic 1

%{

xs(1) = 0; % Microphone #1 is at (0,0)

ys(1) = 0;

xs(2) = 0; % Microphone #2 is at (0,2)

ys(2) = 2;

xs(3) = 2; % Microphone #3 is at (2,0)

ys(3) = 0;

xs(4) = 2; % Microphone #4 is at (2,2)

ys(4) = 2;

%}

xs(1) = 0; % Microphone #1 is at (0,0)

ys(1) = 0;

xs(2) = -.5; % Microphone #2 is at (-.5,1)

ys(2) = 1;

xs(3) = -1; % Microphone #3 is at (-1,2)

ys(3) = 2;

xs(4) = -1; % Microphone #4 is at (-1,3)

ys(4) = 3;

A = zeros(nMics-2,2); %

B = zeros(nMics-2,1); % The matrices used to solve for point of origin, AX = B

This second part shows how we set up our coordinate grid. We used Matlab to identify each microphone and them input the position of each microphone (both array setups are shown). The last three lines identify the matrices used to find the point of origin, meaning that position was solved with respect to a certain microphone (the microphone at (0,0)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Record audio data

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

recorder1 = audiorecorder(Fs, nBits, nChannels, ids(1));

recorder2 = audiorecorder(Fs, nBits, nChannels, ids(2));

recorder3 = audiorecorder(Fs, nBits, nChannels, ids(3));

recorder4 = audiorecorder(Fs, nBits, nChannels, ids(4));

record(recorder1, recordLengthSeconds);

record(recorder2, recordLengthSeconds);

record(recorder3, recordLengthSeconds);

record(recorder4, recordLengthSeconds);

pause(recordLengthSeconds*1.5); % Pause for the length of the recording

% plus 50% to ensure recording finishes

temp = getaudiodata(recorder1);

record1 = temp;

temp = getaudiodata(recorder2);

record2 = temp;

temp = getaudiodata(recorder3);

record3 = temp;

temp = getaudiodata(recorder4);

record4 = temp;

%{

figure;

plot(record1);

figure;

plot(record2);

figure;

plot(record3);

figure;

plot(record4);

%}

The third part involves the recording the data from the microphones and then plotting the impulse. The pause function is used to ensure that all of the impulse is recorded and not cut off. The data is then plotted on the same graph. The plots all showed the impulse response of the clap.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Analyze audio data

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Find TDOA for each mic

taus(1) = 0; % No TDOA between mic 1 and itself

crosscorrelation = xcorr(record2,record1,maxLags); % TDOA for mic 2

[maxVal, maxIndex] = max(crosscorrelation);

taus(2) = (maxIndex-maxLags)/Fs + mic2TauOffset;

crosscorrelation = xcorr(record3,record1,maxLags); % TDOA for mic 3

[maxVal, maxIndex] = max(crosscorrelation);

taus(3) = (maxIndex-maxLags)/Fs + mic3TauOffset;

crosscorrelation = xcorr(record4,record1,maxLags); % TDOA for mic 4

[maxVal, maxIndex] = max(crosscorrelation);

taus(4) = (maxIndex-maxLags)/Fs + mic4TauOffset;

% Find A_m, B_m and D_m for each mic m

for i=3:nMics

As(i) = 2*xs(i)/(v*taus(i)) - 2*xs(2)/(v*taus(2));

Bs(i) = 2*ys(i)/(v*taus(i)) - 2*ys(2)/(v*taus(2));

Ds(i) = v*taus(i) - v*taus(2) - (xs(i)^2 + ys(i)^2)/(v*taus(i)) + (xs(2)^2 + ys(2)^2)/(v*taus(2));

end

% Generate matrices A, B to solve for Ax = B

for i=3:nMics

A(i-2,1) = As(i);

A(i-2,2) = Bs(i);

B(i-2,1) = -Ds(i);

end

X = linsolve(A,B);

A

B

X

taus

The final part involved finding the actual time differences between the sound recordings of the microphones. As seen at the top, the known data of the different time arrivals of the sound to each individual microphone was cross-correlated in order to find the time differences in arrival of the sound for any two microphones. This information was then used to create matrix points for each of the microphones. The matrix was then solved to find the position of the person clapping using the equation Ax = B. The values were then printed out.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Elec 301 projects fall 2011. OpenStax CNX. Jun 18, 2012 Download for free at http://cnx.org/content/col11431/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Elec 301 projects fall 2011' conversation and receive update notifications?

Ask