Skip to content

Instantly share code, notes, and snippets.

@anjiro
Created December 17, 2017 02:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anjiro/e148efe17c1e994981638b1a0c6d0954 to your computer and use it in GitHub Desktop.
Save anjiro/e148efe17c1e994981638b1a0c6d0954 to your computer and use it in GitHub Desktop.
Enhanced autocorrelation
"""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