Read and compare multiple audio files

7 views (last 30 days)
MATTHEW JONES
MATTHEW JONES on 19 Feb 2021
Edited: jibrahim on 24 Feb 2021
I'm trying to write a function 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:
  1. Read an audio file
  2. See if my sample is from current audio file
  3. If it is I need the file number (out of 39), as well as where in the file my sample is from
  4. If not I need to move on to the next file
Any help with this would be greatly appreciated

Answers (1)

jibrahim
jibrahim on 24 Feb 2021
Edited: jibrahim on 24 Feb 2021
% Use an audioDatastore to point to all audio files in a folder
% Set IncludeSubFolders to true to read subfolders too
ads = audioDatastore(pwd,'IncludeSubfolders',true);
% Read the first 5 seconds of a file of your choice
% This is example code - change it to your use case
index = 39;
[sig,Fs] = audioread(ads.Files{39});
sig = sig(1:Fs*5);
% Read all the files
% If you have too many files, and the signals don't all fit in memory, use
% a for loop instead (use read(ads) to read the datastore one file at a
% time)
allData = readall(ads);
% Find the max correlation of each signal with sig
m = cellfun(@(x)max(xcorr(x, sig)), allData, 'UniformOutput', false);
% Pick the index of max of max value
[~,index2] = max(cell2mat(m));
fprintf('Actual: %d\n',index) %39
fprintf('Expected: %d\n',index2) %39

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!