# In[2]: get_ipython().magic(u'pylab') # Out[2]: # Using matplotlib backend: TkAgg # Populating the interactive namespace from numpy and matplotlib # # In[3]: import numpy as np import scipy as sp import matplotlib.pyplot as plt get_ipython().magic(u'matplotlib inline') # In[4]: # Graphing helper function def setup_graph(title='', x_label='', y_label='', fig_size=None): fig = plt.figure() if fig_size != None: fig.set_size_inches(fig_size[0], fig_size[1]) ax = fig.add_subplot(111) ax.set_title(title) ax.set_xlabel(x_label) ax.set_ylabel(y_label) # Import audio file. # In[8]: from scipy.io import wavfile rate1,data1= wavfile.read('beethoven.wav') print 'rate1 =', rate1 print 'data1 =', data1 length = np.size(data1) print 'length = ', length # zero pad to power of 2 data2 = np.concatenate( (data1, 128*np.ones(2**18 - length))) - [128] t1 = np.linspace(0, float(length)/float(rate1), 2**18) _ = plt.plot(t1, data2) # now write zero padded data file wavfile.write('beethoven-pad.wav', rate1, (data2+[128]).astype(uint8)) # print 'data2:', (data2[0:8]+[128]).astype(uint8) setup_graph(title='$x[n]$', x_label='$n$', y_label='$ real x[n]$', fig_size=(6,3)) _ = plt.plot(t1[30000:32000],data2[30000:32000].real) # Out[8]: # rate1 = 22050 # data1 = [127 128 128 ..., 128 128 128] # length = 216032 # # image file: # image file: # Now calculate FFT # In[6]: Y = np.fft.fft(data2) n1 = np.linspace(0,2**18, 2**18) setup_graph(title='$Y$', x_label='$k$', y_label='real Y[k]', fig_size=(6,3)) _ = plt.plot(n1,Y.real) # print Y[0:4].real setup_graph(title='$Y$', x_label='$k$', y_label='$|Y[k]|$', fig_size=(6,3)) _ = plt.plot(n1[0:50000],np.abs(Y[0:50000])) # Out[6]: # image file: # image file: # Now apply ``ideal'' low pass filter to Y[k] to obtain Z[k], and plot z[n]. # In[10]: ## your code here to calculate Z[k] ## ## z = np.fft.ifft(Z) setup_graph(title='$z[n]$', x_label='$n$', y_label='real z[n]', fig_size=(6,3)) _ = plt.plot(n1,z.real) setup_graph(title='$z[n]$', x_label='$n$', y_label='$real z[n]$', fig_size=(6,3)) _ = plt.plot(n1[30000:32000],z[30000:32000].real) setup_graph(title='$z[n]$', x_label='$n$', y_label='$imag z[n]$', fig_size=(6,3)) _ = plt.plot(n1[30000:32000],z[30000:32000].imag) wavfile.write('beethoven-lpf.wav', rate1, (z.real +[128]).astype(uint8)) # Out[10]: --------------------------------------------------------------------------- NameError Traceback (most recent call last) in () 2 ## 3 ## ----> 4 z = np.fft.ifft(Z) 5 setup_graph(title='$z[n]$', x_label='$n$', y_label='real z[n]', fig_size=(6,3)) 6 _ = plt.plot(n1,z.real) NameError: name 'Z' is not defined # In[ ]: