Skip to content

Instantly share code, notes, and snippets.

@zhuth
Created February 23, 2015 07:05
Show Gist options
  • Save zhuth/9762c043943be650514a to your computer and use it in GitHub Desktop.
Save zhuth/9762c043943be650514a to your computer and use it in GitHub Desktop.
wave file fft
#!/usr/bin/python
import wave
import numpy as np
import math
def dofft(filename, wlen = 16, sz = 44100):
wlen = wlen * sz / 1000
print 'wlen:', wlen
wr = wave.open(filename, 'r')
dfs = []
nframes = wr.getnframes()
freq = np.fft.fftfreq(wlen, d=1.0 / sz)
da = np.fromstring(wr.readframes(nframes), dtype=np.int16)
for offset in range(0, nframes, wlen):
nframes -= wlen
dfrag = da[offset : offset + wlen]
df = np.abs(np.fft.fft(dfrag))
dfs.append(abs(freq[np.argmax(df)]))
return dfs
ms = np.power(2, 1./12)
A = 440.0
q = dofft('dnll16.wav', 10, 44100)
print 'length:', len(q)
ds = []
for hz in q:
if abs(hz) > ms:
diff = math.log(hz/A, 2.) * 12
diff = int(round(diff))
ds.append(diff)
dl = -1000
for d in ds:
if dl != d:
print d,
dl = d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment