Skip to content

Instantly share code, notes, and snippets.

@bwhitman
Last active December 15, 2015 13:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwhitman/e2f90c9024f6897fb593 to your computer and use it in GitHub Desktop.
Save bwhitman/e2f90c9024f6897fb593 to your computer and use it in GitHub Desktop.
% 2 microphone TDOA using CPS
% http://www.academia.edu/5280022/Direction_of_arrival_estimation_by_cross-
% power_spectrum_phase_analysis_using_prior_distributions_and_voice_activit
% y_detection_information
function [sample_delay, theta_deg]=tdoa(sig, chunk_size, Fs, dist)
% sig should be samples x 2, and a multiple of chunk_size
% chunk_size is the size of each window
% Fs is Fs
% dist is distance mics are apart in mm
v = 343.2 * 1000; % mm / s speed of sound
[samples, channels] = size(sig);
chunks = samples/chunk_size;
sample_delay = zeros(chunks, 1);
theta_deg = zeros(chunks, 1);
for i=0:chunks-1
chunk_i = sig( (i*chunk_size)+1:(i+1)*chunk_size, 1);
chunk_j = sig( (i*chunk_size)+1:(i+1)*chunk_size, 2);
top = fft(chunk_i) .* conj(fft(chunk_j));
bottom = abs(fft(chunk_i)) .* abs(fft(chunk_j));
csp = ifft(top ./ bottom);
[y, idx] = max(csp);
theta = abs(asind( (idx * v) / (dist * Fs) ));
sample_delay(i+1, 1) = idx;
theta_deg(i+1, 1) = theta;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment