how can i compare two audio files?
Show older comments
Basically i am trying to compare a given transformed audio input with the original audio present in the database and to determine how much similar it will be with the original audio.for this i will calculate the spectral features(centroid,energy,roll_off,flux) of the two audios and will store them in the file called spectral1.doc(i/p) and file1,file2,file3.....so on(for original audios) and then i will calculate the eucledian distance b/w the i/p file matrix and all the files generated for my original audios and the minimum distance will give me the most similar audio for the given input..is my approach for solving my problem rite? and i am stuck in b/w for achieving it!!!
1 Comment
RAHUL KM
on 9 Jul 2014
Answers (1)
Brian Hemmat
on 30 Dec 2019
Euclidean distance between feature vectors is a good start for analyzing the difference between audio files. However, there are much better (and more complicated) methods. Audio Toolbox has a number of classification examples for more specific applications.
If you want to go that first route of analyzing Euclidean distance between feature vectors, here's some code to get you started. The example uses an audioDatastore object to manage a dataset and create a pre-processing pipeline, and an audioFeatureExtractor to extract common audio features. It requires Audio Toolbox R2019a or later.
% Create an audioDatastore that points to some audio samples included with
% Audio Toolbox.
folder = fullfile(matlabroot,'toolbox','audio','samples');
files = {'\Ambiance-16-44p1-mono-12secs.wav', ...
'\Rainbow-16-8-mono-114secs.wav', ...
'\Engine-16-44p1-stereo-20sec.wav', ...
'\FemaleSpeech-16-8-mono-3secs.wav', ...
'\FunkyDrums-44p1-stereo-25secs.mp3', ...
'\JetAirplane-16-11p025-mono-16secs.wav', ...
'\MainStreetOne-24-96-stereo-63secs.wav', ...
'\RockDrums-44p1-stereo-11secs.mp3', ...
'\Turbine-16-44p1-mono-22secs.wav', ...
'\RockGuitar-16-44p1-stereo-72secs.wav', ...
'\SpeechDFT-16-8-mono-5secs.wav'};
ads = audioDatastore(strcat(folder,files));
numFiles = numel(ads.Files);
% Create a preprocessing pipeline so that all of the audio is mono, 16
% kHz, and normalized such that the max absolute value is 1.
desiredFs = 16e3;
adsMono = transform(ads,@(x)mean(x,2));
adsMono16 = transform(adsMono,@(x,info)resample(x,desiredFs,info.SampleRate),'IncludeInfo',true);
adsMono16Normalized = transform(adsMono16,@(x)x/max(abs(x)));
% Create an audioFeatureExtractor object to extract the spectral centroid,
% rolloff, and flux for 30 ms windows and 10 ms hops.
afe = audioFeatureExtractor('SampleRate',desiredFs, ...
'Window',hann(round(desiredFs*0.03),'periodic'), ...
'OverlapLength',round(desiredFs*0.02), ...
'spectralCentroid',true, ...
'spectralRolloffPoint',true, ...
'spectralFlux',true);
% In a loop, read a file from the datastore, extract features, and then
% average the features across the analyzed file.
featuresPerFile = zeros(numFiles,3);
for ii = 1:numFiles
audioIn = read(adsMono16Normalized);
features = extract(afe,audioIn);
featuresPerFile(ii,:) = mean(movmedian(features,10),1,'omitnan');
end
% Normalize each feature across the dataset so no one feature dominates
% the distance measurement.
featuresPerFile = (featuresPerFile - mean(featuresPerFile,1))./std(featuresPerFile);
% Calculate the Euclidean distance between each pair.
distanceMatrix = zeros(size(featuresPerFile,1));
for ii = 1:size(featuresPerFile,1)
distanceMatrix(:,ii) = sqrt(sum((featuresPerFile(ii,:) - featuresPerFile).^2,2));
end
% Create a heatmap to inspect simalarity.
filenames = extractBetween(ads.Files,'samples\','-');
heatmap(filenames,filenames,distanceMatrix)
title('Euclidean Distance of Spectral Features')

1 Comment
JIkrul Sayeed
on 26 Feb 2020
Brian Hemmat, can you compare two songs except samples ?
Categories
Find more on Feature Extraction in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!