I'm trying to write a function in MATLAB that can match a ten second audio sample to the audio file that it came from, out of 39 audio files. I need to search through a directory of audio files to find the file that my sample came from. I know I need to use the xcorr function in some way but I can't figure out how to read through the directory to make the comparison. Basicaly I need to:
Read an audio file
See if my sample is from the current audio file
If it is, I need the file number (out of 39), as well as where in the file my sample occurs
If not I need to move on to the next file
Any help with this would be greatly appreciated
A broad overview of this process involves doing the cross-correlation between each signal and the ten second sample. After doing the cross-correlation we find take the maximum, max()
which will give us a factor of "best fit" (most correlation) for each of the comparisons. We then take the maximum result of the peak-correlations to find which of the signals fits the best. The script below uses a structure, struct
to hold the significant data such as the audio signals, cross-correlation and peak-correlation. I've only tested my sample against 3 audio files, so the results might differ when attempting 39, but it should work to the best of my knowledge. To stop playing the sound simply type clear sound
in the command window.
• Read the audio file names from a folder/directory using dir()
.
• Read the ten second audio sample and the files in the folder using the audioread()
function.
• Compare the signals using the xcorr()
function and compute the peak-correlation for the comparison using the max()
function.
• Find which comparison had the highest peak-correlation by using the max()
function on the peak correlation array.
• Keep in mind I only used the left channel so you might have to test against both channels if your audio is not the same for both channel or mono.
%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);