how can i compare two audio files?

50 views (last 30 days)
RAHUL KM
RAHUL KM on 9 Jul 2014
Commented: JIkrul Sayeed on 26 Feb 2020
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
RAHUL KM on 9 Jul 2014
THIS IS MY CODE!!!
filelist = dir('*.wav');
result = zeros(size(filelist));
for index = 1 : length(filelist) fprintf('Processing %s\n', filelist(index).name); result = zeros(size(filelist)); for index = 1 : length(filelist) fprintf('Processing %s\n', filelist(index).name); [y, fs] = wavread(filelist(index).name);
[signal] =y; fs=44100; windowLength=512; frameRate = 100; step=441; signal = signal / max(abs(signal)); curPos = 1; L = length(signal); numOfFrames = floor((L-windowLength)/step) + 1; c=0.8; % threshold cen_present=1; energy_present=1; roll_Pos=1; fluxpos=1; H = hamming(windowLength); m = ((fs/(2*windowLength))*[1:windowLength])'; Centroid = zeros(1,numOfFrames); Energy=zeros(1,numOfFrames); Rolloff=zeros(1,numOfFrames); Flux=zeros(1,numOfFrames); for i=1:numOfFrames cen_window = H.*(signal(cen_present:cen_present+windowLength-1)); FFT = (abs(fft(cen_window,2*windowLength))); FFT = FFT(1:windowLength); FFT = FFT / max(FFT); Centroid(i) = sum(m.*FFT)/sum(FFT); if (sum(cen_window.^2)<0.010) Centroid(i) = 0.0; end cen_present = cen_present + step;
% short time energy of signal is calculated
energy_window = (signal(energy_present:energy_present+windowLength-1));
Energy(i) = (1/(windowLength)) * sum(abs(energy_window.^2));
energy_present = energy_present + step;
% spectral roll off calculation
roll_window = (signal(roll_Pos:roll_Pos+windowLength-1));
FFT1 = (abs(fft(roll_window,512)));
FFT1 = FFT1(1:255);
totalEnergy = sum(FFT1);
curEnergy = 0.0;
countFFT1 = 1;
while ((curEnergy<=c*totalEnergy) && (countFFT1<=255))
curEnergy = curEnergy + FFT(countFFT1);
countFFT1 = countFFT1 + 1;
end
Rolloff(i) = ((countFFT1-1))/(fs/2);
roll_Pos = roll_Pos + step;
% procedure to calculate Spectral flux begins here
flux_window = H.*(signal(fluxpos:fluxpos+windowLength-1));
FFT2 = (abs(fft(flux_window,2*windowLength)));
FFT2 = FFT2(1:windowLength);
FFT2 = FFT2 / max(FFT2);
if (i>1)
Flux(i) = sum((FFT2-FFTprev).^2);
else
Flux(i) = 0;
end
if (isnan(Flux(i)))
Flux(i)=0;
end
fluxpos = fluxpos + step;
FFTprev = FFT2;
end Centroid = Centroid / (fs/2); nam=sprintf('file%d',index); fid=fopen(nam,'w'); [r col]=size(Centroid); for ii=1:1:col %fprintf(fid,'\n%f',Centroid(ii)); fprintf(fid,'\n%f\t%f\t%f\t%f',Centroid(ii),Energy(ii),Rolloff(ii),Flux(ii)); end fclose(fid);

Sign in to comment.

Answers (1)

Brian Hemmat
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')
euclidean.png
  1 Comment
JIkrul Sayeed
JIkrul Sayeed on 26 Feb 2020
Brian Hemmat, can you compare two songs except samples ?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!