import numpy as np
import matplotlib.pyplot as plt

from scipy.io import wavfile
from scipy.fftpack import fft

#The frequency in Hz at which the sound file was recorded 
freq = 44100.0

#The number of samples to examine the spectrum of
N = 8128

#How much of the spectrum to look at (5 means the first 1/5th)
frac = 5.0

def myPlot(myFFT):

    #This makes the x points be in units of Hz
    x = np.linspace(0.0, ((N/2)*(44100.0/N))/frac, (N//2)//frac)
    y = np.abs(myFFT[0:(N//2)//frac])

    plt.plot(x, y)
    plt.grid()
    plt.show()


def main():
    (rate,sound) = wavfile.read("foo.wav")
    print("Sound")
    print("\tsound is =\n",sound)
    print("\tSize of sound is sound.size =",sound.size)
    print("\tShape of sound is sound.shape =",sound.shape)
    print("\tNumber of dimensions of sound is sound.ndim =",sound.ndim)
    print("\tType of sound is sound.dtype.name =",sound.dtype.name)
    print("\tSize of sound is sound.size =",sound.size)
    start = len(sound)/2
    end = start + N
    myFFT = fft(sound[start:end])
    print("FFT")
    print("\tmyFFT is =\n",myFFT)
    print("\tSize of myFFT is myFFT.size =",myFFT.size)
    print("\tShape of myFFT is myFFT.shape =",myFFT.shape)
    print("\tNumber of dimensions of myFFT is myFFT.ndim =",myFFT.ndim)
    print("\tType of myFFT is myFFT.dtype.name =",myFFT.dtype.name)
    print("\tSize of myFFT is myFFT.size =",myFFT.size)

    myPlot(myFFT)


main()
