In [1]:
import os
import wave

In [2]:
# add project-specific src directories
import sys
sys.path.insert(2, '../src/')
# project imports
import fft

In [3]:
%load_ext autoreload
%autoreload 2

In [4]:
data_dir = '../data/'


In [20]:
stream_01, sample_rate_01 = read_wav.mono_channel(os.path.join(data_dir, 'z01.wav'))

In [21]:
stream_02, sample_rate_02 = read_wav.mono_channel(os.path.join(data_dir, 'z02.wav'))

In [22]:
stream_01[:10]


Out[22]:
[10136.0,
 9761.0,
 10277.0,
 9907.0,
 10029.0,
 9079.0,
 9717.0,
 7967.0,
 9239.0,
 6424.0]

In [23]:
stream_02[:10]


Out[23]:
[10139.0,
 9762.0,
 10277.0,
 9907.0,
 10028.0,
 9078.0,
 9718.0,
 7964.0,
 9238.0,
 6425.0]

In [24]:
wr01 = wave.open(os.path.join(data_dir, 'z01.wav'))
wr01.readframes(10)


Out[24]:
"\x98'!&%(\xb3&-'w#\xf5%\x1f\x1f\x17$\x18\x19\xf2\x1f\x0e\x11\n\x1b\xe4\n\x07\x1aB\x08x\x1e\x1a\x0c\xf5$\xd5\x16"

In [25]:
wr01 = wave.open(os.path.join(data_dir, 'z02.wav'))
wr01.readframes(10)


Out[25]:
'\x9b\'"&%(\xb3&,\'v#\xf6%\x1c\x1f\x16$\x19\x19\xf2\x1f\x0e\x11\t\x1b\xe7\n\x06\x1aC\x08{\x1e\x19\x0c\xf5$\xd5\x16'

In [9]:
len(res)


Out[9]:
882000

In [10]:
len(stream)


Out[10]:
882000

In [19]:
wr = wave.open(os.path.join(data_dir, 'z01.wav'))

In [17]:
wr.getparams()


Out[17]:
(2, 2, 44100, 441000, 'NONE', 'not compressed')

In [ ]:
../data/

In [3]:
import match
import numpy

In [ ]:
match.get_mono

In [19]:
import numpy as np
from scipy.io import wavfile

def get_mono(fpath):
    """ Converts the given wav file to 5512Hz PCM Mono
    """
    samplerate, channels = wavfile.read(fpath)
    return np.mean(channels, axis=1)

In [ ]:
def mse(A, B):
    #convert from complex to real vectors
    A = map(convert, A)
    B = map(convert, B)
    #caluclate euclidean distance between A and B
    dist = [distance.euclidean(a,b) ** 2 for a, b in zip(A, B)]
    return np.mean(dist)

In [27]:
z01 = get_mono('../data/z01.wav')
z02 = get_mono('../data/z05.wav')

In [36]:
np.dstack([z01, z02])[0]


Out[36]:
array([[  9948.5,  11150.5],
       [ 10092. ,   9677. ],
       [  9554. ,   8229.5],
       ..., 
       [ -5257. ,  -5854. ],
       [ -4042.5,  -3997. ],
       [ -2156.5,  -1994.5]])

In [46]:
np.mean([abs(a - b) ** 2 for a, b in  np.dstack([z01, z02])[0]])


Out[46]:
158891459.84634298

In [37]:
match.fft('../data/z01.wav')


Out[37]:
array([ 1292984.50000000      +0.j        ,
        1087538.68086148 -236766.76432563j,
       -1546519.78285878+1033734.65951854j, ...,
        -905800.25706673-1520717.55708944j,
       -1546519.78285878-1033734.65951852j,
        1087538.68086146 +236766.76432563j])

In [40]:
t = map(match.convert, match.fft('../data/z01.wav'))

In [42]:
np.real(match.fft('../data/z01.wav'))


Out[42]:
array([ 1292984.5       ,  1087538.68086148, -1546519.78285878, ...,
        -905800.25706673, -1546519.78285878,  1087538.68086146])

In [55]:
def downsample(stream):
    """ Downsample from 44100 -> 5512Hz
    """
    return decimate(stream, 8)

def get_mono(fpath):
    """ Converts the given wav file to 5512Hz PCM Mono
    """
    samplerate, channels = wavfile.read(fpath)
    return np.mean(channels, axis=1)

def fft(fpath):
    mono_stream = get_mono(fpath)
    return np.fft.fft(mono_stream)

def mse(A, B):
    return ((np.real(A) - np.real(B)) ** 2).mean()

def similarity(f1, f2):
    ffts = map(fft, [f1, f2])
    return mse(ffts[0], ffts[1])

In [56]:
similarity('../data/z01.wav', '../data/z02.wav')


Out[56]:
247141.99999999971

In [57]:
similarity('../data/z01.wav', '../data/z06.wav')


Out[57]:
36780463382812.633

In [59]:
similarity('../data/z03.wav', '../data/z04.wav')


Out[59]:
148965953915.5

In [60]:
148965953915
150000000000


Out[60]:
False