I remember learning in the Analog Electronics class about all types of filters. Then we actually constructed these filters. As you can imagine, it's much easier to make a filter in software than in hardware.
We will build a filter and apply it to an audio fragment that we will download. We have done some of these steps before in this chapter, so we will leave out those parts.
The
iirdesign
function, as its name suggests, allows us to construct several types of analog and digital filters. It can be found in the scipy.signal
module. This module contains a comprehensive list of signal processing functions.
Design the filter with iirdesign
function of the scipy.signal
module.
IIR stands for infinite impulse response; for more information visit Wikipedia at http://en.wikipedia.org/wiki/Infinite_impulse_response. We are not going to go into all the details of the iirdesign
function. Have a look at the documentation at http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.iirdesign.html
, if necessary.
In short, we will set the following parameters:
b,a = scipy.signal.iirdesign(wp=0.2, ws=0.1, gstop=60, gpass=1, ftype='butter')
The configuration of this filter corresponds to a Butterworth bandpass filter (for more information on Butterworth bandpass filter visit http://en.wikipedia.org/wiki/Butterworth_filter).
We can apply the filter with the scipy.signal.lfilter
function. It accepts as arguments the values from the previous step and of course, the data array to filter:
filtered = scipy.signal.lfilter(b, a, data)
When writing the new audio file, we need to make sure that it is of the same data type as the original data array:
scipy.io.wavfile.write('filtered.wav', sample_rate, filtered.astype(data.dtype))
After plotting the original and filtered data, we get the following plot:
The code for the audio filter is as follows:
import scipy.io.wavfile import matplotlib.pyplot import urllib2 import scipy.signal response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav') print response.info() WAV_FILE = 'smashingbaby.wav' filehandle = open(WAV_FILE, 'w') filehandle.write(response.read()) filehandle.close() sample_rate, data = scipy.io.wavfile.read(WAV_FILE) print "Data type", data.dtype, "Shape", data.shape matplotlib.pyplot.subplot(2, 1, 1) matplotlib.pyplot.title("Original") matplotlib.pyplot.plot(data) # Design the filter b,a = scipy.signal.iirdesign(wp=0.2, ws=0.1, gstop=60, gpass=1, ftype='butter') # Filter filtered = scipy.signal.lfilter(b, a, data) # Plot filtered data matplotlib.pyplot.subplot(2, 1, 2) matplotlib.pyplot.title("Filtered") matplotlib.pyplot.plot(filtered) scipy.io.wavfile.write('filtered.wav', sample_rate, filtered.astype(data.dtype)) matplotlib.pyplot.show()
We created and applied a Butterworth bandpass filter. The following functions were introduced to create the filter:
Function |
Description |
---|---|
Creates an IIR digital or analog filter. This function has an extensive parameter list, which is documented at http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.iirdesign.html. | |
Filters an array given a digital filter. |