MATLAB: How to cross correlate two audio files

audiocorrelationsignal

Hello, I'm trying to cross correlate of two audio files and plot them but the problem is; it doesn't show the 3rd signals when I plot it which is the correlation of the audio files.
[x,fs]=audioread("Word14.wav");
[y,fs]=audioread("Word16.wav");
subplot(3,1,1), plot (x);
subplot(3,1,2), plot (y);
[C1, lag1] = xcorr(x,y);
subplot(3,1,3), plot(lag1/fs,C1);
ylabel("Amplitude"); grid on
title("Cross-correlation ")
it shows me this error:
Second argument must be a vector.
both audio files are 8 seconds duration and have the same sampling frequency.

Best Answer

hello
you forgot to check that x and y must have exactly the same length to use xcorr
so this is a little improvement to avoid this :
[x,fs]=audioread("Word14.wav");
[y,fs]=audioread("Word16.wav");
lx = length(x);
ly = length(y);
samples = 1:min(lx,ly);
subplot(3,1,1), plot (x(samples));
subplot(3,1,2), plot (y(samples));
[C1, lag1] = xcorr(x(samples),y(samples));
subplot(3,1,3), plot(lag1/fs,C1);
ylabel("Amplitude"); grid on
title("Cross-correlation ")
Related Question