Delay-and-Sum Beamformer

% Find the distances of each mic to the source, calculate the

% time-difference of arrival, and then add shifted waveforms. 

[xShift,wghts] = delaytracking(x, xPrev, fs, spos, mpos, c, iw);

b = sum(xShift, 2);  % add data across columns

function [XSF,wghts] = delaytracking(x, xPrev, fs, spos, mpos, c, iw)

if nargin == 6 %Set flag to no weighting
iw = 0;
end

%% Setup
% Initialize vars and get some information about the target audio
N = size(x,1); % total number of samples in each audio track
M = size(x,2); % number of microphones in the array
xShift = zeros(N, M); % initialize shifted track matrix

%% Beamform

% Find the distances of each mic to the source
% spos*ones: expend one spos to every mpos so as to subtract
dist = sqrt(sum((mpos-spos*ones(1,size(mpos,2))).^2, 1));

if any(dist==0)
error(‘Cant have a source on top of a mic’)
end
if iw == 1
wghts = arweights(dist); % inverse distance weighting
else
wghts = ones(size(dist)); % no weighting
end

% Divide distances by the speed of sound to find duration of travel
% for each signal. Take that time relative to the furthest mic,
% since all other tracks will be delayed to match the wave that
% took the longest to be recieved.
t = (max(dist)-dist)/c;

% Convert TDOA to integer delays
delays = round(t*fs);

% Shift each track.we’re recycling x into xPrev on the next iteration
% take delay amount from xPrev to new xShift windows

for k=1:size(x,2)
xShift(:,k) = wghts(k)*[xPrev(end-delays(k)+1:end, k);…
x(1:end-delays(k), k)];
end
% output
XSF=xShift;

end % function dsb