How to judge the similarity between two pieces of audio?

Viewed 25

I want to achieve a similar singing scoring function to determine the similarity of two audio, but I do not know how a simple implementation, look at a lot of github projects, more mention is simhash, but I feel similar to the audio may not be very good, so here to ask for advice.

1 Answers

one approach would be to find the frequencies that are present in a audio segment by using auto correlation.

there are many implementations of this. e.g. librosa, scipy, numpy and so on.

a very loose and sloppy implementation to give you an understanding of the algorithm without libs:

import matplotlib.pyplot as plt
import math

'''
create a test signal
'''

sr = 44100 #hz
freq = 200 #hz
duration = 0.1 #sec
signal = [math.sin(x * (math.pi * 2 * freq) * (1 / sr)) for x in range(0, int(sr * duration))]

'''
compute the auto correlation at a given frequency
'''
def auto_correlation(signal, freq, sr):
  sample_delay = int (sr / freq) 
  score = 0

  for i in range(0, len(signal) - sample_delay):
    score += signal[i] * signal[i + sample_delay]

  return score / len(signal)

'''
itterate thru frequency spectrum and test the autocorrelation
'''
start_freq = 150
end_freq = 1000

scores = []
for freq in range(start_freq, end_freq):
  scores.append(auto_correlation(signal, freq, sr))
  
max_index = scores.index(max(scores))
print("estimated frequency : {} hz".format(max_index + start_freq))

plt.ylabel("correlation")
plt.xlabel("frequency Hz")
plt.plot([x + start_freq for x in range(len(scores))], scores)

enter image description here

you could then iterate thru the audio files, test segments for the dominating frequency and compare the scores.

another possibility is to do this by computing FFT for the audio files and compare those. librosa is a great lib if youre in python territory.

Related