In [20]:
import warnings
warnings.filterwarnings('ignore')
import os
import sys
import string
import glob
import re
import itertools
import hashlib
from collections import Counter

import scipy as sp
import numpy as np
import pandas as pd
import scipy.io.wavfile as siow

import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
import cufflinks as cf
cf.set_config_file(offline=False, world_readable=True, theme='pearl')
tls.set_credentials_file(username=os.environ.get('PLOTLY_USERNAME'),
                         api_key=os.environ.get('PLOTLY_APIKEY'))

import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline

import seaborn as sns
sns.set_context("notebook",
                font_scale=1.5,
                rc={"lines.linewidth": 2.5})

In [7]:
Fs = 150.0;  # sampling rate
Ts = 1.0/Fs; # sampling interval
t = np.arange(0, 1, Ts) # time vector
ff = 5; # frequency of the signal
y1 = np.sin(2 * np.pi * ff * t)
y2 = np.sin(3.75 * np.pi * ff * t)
y = y1 + y2

n = len(y) # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range

Y = np.fft.fft(y) # fft computing and normalization
Y = Y[range(n/2)]
Y = 10*np.log10(abs(Y)) # put into db scale

In [8]:
fig, ax = plt.subplots(2, 1)
ax[0].plot(t, y)
ax[0].set_xlabel('Time')
ax[0].set_ylabel('Amplitude')
ax[1].plot(frq, Y, 'r')
ax[1].set_xlabel('Freq (Hz)')
ax[1].set_ylabel('|Y(freq)|')
py.iplot_mpl(fig)


Out[8]:

Two signals, two peaks in the FFT, this is correct. Let's see the spectrum


In [12]:
from pylab import specgram
pyplot.clf()
sgram = specgram(y)
pyplot.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-4056df4546b5> in <module>()
      1 from pylab import specgram
----> 2 pyplot.clf()
      3 sgram = specgram(y)
      4 pyplot.show()

NameError: name 'pyplot' is not defined

In [21]:
infile = '../data/kendra.wav'
sample, audio = siow.read(infile)

In [24]:
print type(audio[0])
print sample


<type 'numpy.ndarray'>
44100

In [27]:
outfile = '../data/kendra_fast.wav'
samplerate = sample*1.25
sp.io.wavfile.write(outfile, samplerate, audio)

In [28]:
outfile = '../data/kendra_slow.wav'
samplerate = sample/1.25
sp.io.wavfile.write(outfile, samplerate, audio)

In [102]:
audiofft = np.fft.rfft(audio)
audiofft = 10*log10(abs(audiofft))

In [ ]:
pyplot.clf()
sgram = specgram(audio)

In [ ]: