Read and compare multiple audio files

6 次查看(过去 30 天)
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

回答(1 个)

jibrahim
jibrahim 2021-2-24
编辑:jibrahim 2021-2-24
% 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

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by