Created
December 17, 2017 02:07
-
-
Save anjiro/e148efe17c1e994981638b1a0c6d0954 to your computer and use it in GitHub Desktop.
Enhanced autocorrelation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Perform enhanced autocorrelation on a wave file. | |
Based very loosely on https://bitbucket.org/yeisoneng/python-eac | |
which is based on Audacity's implementation. This version uses | |
Numpy features to significantly speed up processing.""" | |
from __future__ import division | |
import numpy as np | |
from numpy.fft.fftpack import fft, rfft | |
from scipy.interpolate import interp1d | |
from scipy.signal import argrelextrema | |
def eac(sig, winsize=512, rate=44100): | |
"""Return the dominant frequency in a signal.""" | |
s = np.reshape(sig[:len(sig)//winsize*winsize], (-1, winsize)) | |
s = np.multiply(s, np.hanning(winsize)) | |
f = fft(s) | |
p = (f.real**2 + f.imag**2)**(1/3) | |
f = rfft(p).real | |
q = f.sum(0)/s.shape[1] | |
q[q < 0] = 0 | |
intpf = interp1d(np.arange(winsize//2), q[:winsize//2]) | |
intp = intpf(np.linspace(0, winsize//2-1, winsize)) | |
qs = q[:winsize//2] - intp[:winsize//2] | |
qs[qs < 0] = 0 | |
return rate/qs.argmax() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment