Plotting Example

Load the 'cssigps' Python modules and test a sample plotting method.


In [9]:
import os
import sys

import matplotlib.pyplot as plt
import numpy as np

def add_project_module_path():
    """
    Simple method for adding our module to the sys path
    """
    path_items=(os.getcwd()).split(os.sep)
    path_items.pop()
    final_path=""
    for item in path_items:
        final_path+=item+os.sep
    final_path=final_path+'cssigps'
    sys.path.append(final_path)

# method for adding module to sys path
add_project_module_path()
from utils import *
%matplotlib inline

def _extract_single_channel(audio):
    """
    Extract a single channel from the audio input.
    """
    if len(audio.shape) > 1:
        return audio[:,0]
    return audio

wf = '/Users/evanbowling/Dropbox/School/CS598_ML_SignalProcessing/cs598proj/templates/template_Aa2.wav'

def load_spectrogram(wavfile):
    """
    Utility method for loading and displaying wav file content in a
    spectrogram.

    Parameters:
    -----------
    wavfile : string
        absolute path to the wavfile
    """
    (fs,x) = wav.read(wavfile)
    xs = _extract_single_channel(x)
    plt.figure()
    #ax1 = plt.subplot(211)
    #plt.plot(t, xs)
    plt.subplot(212) #, sharex=ax1)
    nfft=512 #1024
    overlap=400 #900
    Pxx, freqs, bins, im = plt.specgram(xs, NFFT=nfft, Fs=fs, noverlap=overlap,
                                    cmap=plt.cm.gist_heat)
    plt.show()

load_spectrogram(wf)



In [ ]: