簡體   English   中英

MATLAB xcorr 不同大小的信號

[英]MATLAB xcorr different size of signals

我正在嘗試制作像 shazam 這樣的項目,但我卡住了。 我想加載一個模板信號(這是歌曲),當我加載歌曲的某些部分時,它應該比較模板信號並說這是你的歌曲。我認為使用 xcorr function 但是當我使用它時 matlab 給出了向量大小之類的錯誤應該是一樣的。所以很明顯我的模板歌曲和我試圖與之比較的信號大小不一樣。那我該怎么辦? 我應該使用其他功能然后 xcorr 還是我可以做些什么不同的事情? 這是我的代碼塊:

[y,fs] = audioread('sound.wav');
[y2,fs2] = audioread('sound4.mp3');

y = y(:,1);
y2 = y2(:,1);
dt = 1/fs;
t = 0:dt:(length(y)*dt)-dt;
figure(1);
subplot(2,1,1);
plot(t,y); xlabel('Seconds'); ylabel('Amplitude');         %time domain
subplot(2,1,2);
plot(psd(spectrum.periodogram,y,'Fs',fs,'NFFT',length(y)));%frequency domain

figure(2);
subplot(2,1,1);
plot(t,y2); xlabel('Seconds'); ylabel('Amplitude');         %time domain
subplot(2,1,2);
plot(psd(spectrum.periodogram,y2,'Fs',fs,'NFFT',length(y2)));%frequency domain

[C1,lag1] = xcorr(y,y2);
C1_new = C1./max(abs(C1(:)));

figure(3);
ax(1) = subplot(211);
plot(lag1/fs,C1_new,'k');
ylabel('Normalized Amplitude');
grid on;
title('cross corre')
xlabel('time (second)');

快速 mocking 上一個不同長度的yy2信號表明這不是xcorr的錯誤。 相反,您收到的錯誤是Error using plot Vectors must be the same length. 這源於這里plot(t,y2);

如果y2的長度與y不同,則不能對y2使用t到 plot

建議

創建第二個t向量t2 ,並將其y2的長度

[y,fs] = audioread('sound.wav');
[y2,fs2] = audioread('sound4.mp3');

y = y(:,1);
y2 = y2(:,1);
dt = 1/fs;
t = 0:dt:(length(y)*dt)-dt;
t2 = 0:dt:(length(y2)*dt)-dt;

figure(1);
subplot(2,1,1);
plot(t,y); xlabel('Seconds'); ylabel('Amplitude');         %time domain
subplot(2,1,2);
plot(psd(spectrum.periodogram,y,'Fs',fs,'NFFT',length(y)));%frequency domain

figure(2);
subplot(2,1,1);
plot(t2,y2); xlabel('Seconds'); ylabel('Amplitude');         %time domain
subplot(2,1,2);
plot(psd(spectrum.periodogram,y2,'Fs',fs,'NFFT',length(y2)));%frequency domain

[C1,lag1] = xcorr(y,y2);
C1_new = C1./max(abs(C1(:)));

figure(3);
ax(1) = subplot(211);
plot(lag1/fs,C1_new,'k');
ylabel('Normalized Amplitude');
grid on;
title('cross corre')
xlabel('time (second)');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM