Skip to content

Instantly share code, notes, and snippets.

@ronggong
Created February 16, 2017 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronggong/c6f81a1f727f1543ee5adccfc156c751 to your computer and use it in GitHub Desktop.
Save ronggong/c6f81a1f727f1543ee5adccfc156c751 to your computer and use it in GitHub Desktop.
QBSH-code-snippets
# DTW distance
# Change dtwPath
# open terminal, go to your/path/to/sankalp/dtw, then compile: python setup.py build_ext --inplace
# use function dtw1d_std, inputs are two pitch contour lists, use udist as distance
import os,sys
fileDir = os.path.dirname(os.path.realpath('__file__'))
dtwPath = os.path.join(fileDir, 'your/path/to/sankalp/dtw')
dtwPath = os.path.abspath(os.path.realpath(dtwPath))
sys.path.append(dtwPath)
import dtw
def dtw1d_std(x,y):
"""
1D standard DTW
input: x, y, two lists
output: udist, unnormalized distance
"""
configuration = {}
configuration['Output'] = 3
configuration['Ldistance'] = {}
configuration['Ldistance']['type'] = 0
udist,_,path = dtw.dtw1d(x,y,configuration)
return udist,path
# Evaluation functions
def MRR(list_rank):
'''
mean reciprocal rank
:param list_rank: [rank_0,rank_1,...], each one starting from 1
:return:
'''
return np.sum(1.0/np.array(list_rank))/len(list_rank)
def topXhit(X,list_rank):
'''
:param X: if you want to calculate top 5 hit, assign X=5
:param list_rank:
:return:
'''
counter = 0
for rank in list_rank:
if rank <= X:
counter+=1
return counter/float(len(list_rank))
# pitch contour extraction
# install pyin https://code.soundsoftware.ac.uk/projects/pyin/files
# pyin is a vamp plug-in, so you need to do http://www.vamp-plugins.org/download.html#install
# to use pyin in python, you need to install https://pypi.python.org/pypi/vamp/1.0.0
# to load audio, you need Essentia
def hz2cents(pitchInHz, tonic=261.626):
"""
convert hz to cents
input: an array in hz
"""
cents = 1200*np.log2(1.0*pitchInHz/tonic)
return cents
# essentia load audio
loader = ess.MonoLoader(filename=filename_wav, downmix = 'left', sampleRate = sr)
audio = loader()
# extract pitch contour, by a fixed sample rate "sr"
# hopsize = 1024 frames
# framesize = 2048 frames
data = vamp.collect(audio, sr, "pyin:pyin", output='smoothedpitchtrack')
pitch = data['vector'][1]
# convert Hz to cents
pitchInCents = hz2cents(pitch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment