您如何交叉关联音频文件以查看样本是否与原始文件匹配?

问题描述

我正在尝试在 MATLAB 中编写一个函数,该函数可以将 39 个音频文件中的 10 秒音频样本与其来源的音频文件相匹配。我需要搜索音频文件目录以找到我的样本来自的文件。我知道我需要以某种方式使用 xcorr 函数,但我不知道如何通读目录进行比较。基本上我需要:

读取音频文件

看看我的样本是否来自当前的音频文件

如果是,我需要文件编号(共 39 个),以及我的样本在文件中的位置

如果不是,我需要转到下一个文件

对此的任何帮助将不胜感激

解决方法

此过程的广泛概述涉及在每个信号和十秒样本之间进行互相关。在进行互相关后,我们发现取最大值 max(),这将为我们提供每个比较的“最佳拟合”(最大相关性)因子。然后我们取峰值相关性的最大结果来找出哪个信号最适合。下面的脚本使用结构 struct 来保存重要数据,例如音频信号、互相关和峰值相关。我只针对 3 个音频文件测试了我的样本,因此尝试 39 时结果可能会有所不同,但据我所知,它应该可以工作。要停止播放声音,只需在命令窗口中输入 clear sound


步骤:

• 使用 dir() 从文件夹/目录中读取音频文件名。

• 使用 audioread() 函数读取十秒音频样本和文件夹中的文件。

• 使用 xcorr() 函数比较信号并使用 max() 函数计算比较的峰值相关性。

• 使用峰值相关性数组上的 max() 函数查找具有最高峰值相关性的比较。

• 请记住,我只使用了左声道,因此如果您的声道或单声道的音频不同,您可能需要针对两个声道进行测试。


完整脚本:

%Loading ten second audio sample%
Ten_Second_Sample = audioread("Sample.mp3");
Audio_Properties = audioinfo("Sample.mp3");
Sampling_Frequency = Audio_Properties.SampleRate;

%Grabbing all the mp3 and mp4 file names in a folder named "Audio Folder"%
Folder_Name = 'Audio Folder';
Audio_Files = [dir(fullfile(Folder_Name,'*mp3')); dir(fullfile(Folder_Name,'*m4a'))];

%Creating a structure to hold all the audio data and cross-correlation
%results%
Audio_Signals = struct("Signal",[],"Cross_Correlation","Peak_Correlation",[]);

%Evaluting the cross-correlation and peak-correlation between the audio and ten second sample%
for Audio_Index = 1: length(Audio_Files)
Audio_Signals(Audio_Index).Signal = audioread(fullfile('Audio Folder/',Audio_Files(Audio_Index).name));
Left_Channel = Audio_Signals(Audio_Index).Signal(:,1);
Audio_Signals(Audio_Index).Cross_Correlation = xcorr(Left_Channel,Ten_Second_Sample(:,1));
Audio_Signals(Audio_Index).Peak_Correlation = max(Audio_Signals(Audio_Index).Cross_Correlation);
end

%Evaluating the highest peak correlation among the compared signals%
Peak_Correlations = [Audio_Signals.Peak_Correlation].';
[~,Index] = max(Peak_Correlations);
disp(Audio_Files(Index).name);
sound(Audio_Signals(Index).Signal,Sampling_Frequency);