Checking order of two audio clips with a delay
3 views (last 30 days)
Show older comments
Suppose I have two audio arrays recorded from two microphones a distance d apart. I am trying to figure out which array is a delayed version of the other; I want to know which microphone started receiving the sound before the other one. I am trying to write a code to figure out where the sound is coming from. This must be done using correlation as the sounds obviously are not exact copies.
Thank you.
Answers (1)
Yash
on 25 Apr 2023
Hi Habib,
To determine which microphone received the sound first, you can use cross-correlation. The basic idea is to shift one of the audio signals by a certain time delay and then calculate the correlation between the two signals at each time delay. The time delay that results in the maximum correlation value indicates the time delay between the two signals, and thus which microphone received the sound first.
Here's some example code in MATLAB that demonstrates how to perform cross-correlation:
Note that this code assumes that the two signals have the same sample rate and are synchronized in time.
% Load the two audio signals
mic1 = audioread('mic1.wav');
mic2 = audioread('mic2.wav');
% Determine the length of the signals
n1 = length(mic1);
n2 = length(mic2);
% Determine the maximum possible time delay between the signals
maxdelay = abs(n1 - n2);
% Perform cross-correlation between the two signals
corr = xcorr(mic1, mic2);
% Find the time delay corresponding to the maximum correlation value
[~, delay] = max(corr);
delay = delay - (n1 + n2 - 1);
% Determine which microphone received the sound first
if delay > 0
disp('Microphone 1 received the sound first');
else
disp('Microphone 2 received the sound first');
end
In this code, mic1 and mic2 are the two audio signals recorded from the two microphones. The xcorr function is used to perform cross-correlation between the two signals, and the max function is used to find the maximum correlation value and its corresponding time delay. Finally, the time delay is compared to zero to determine which microphone received the sound first.
I hope this helps :)
0 Comments
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!