SIGNAL PROCESSING
Laboratory #6

Signal filtering in Python

M. Kociński, P. Strumiłło, A. Borowska-Terka

Medical Electronics Division
Institute of Electronics


In [1]:
import funkcje_do_lab6 as f6
f6.hide()
Out[1]:
The raw code for this IPython notebook is by default hidden for easier reading. To toggle on/off the raw code, click here.

PURPOSE:

Getting acquainted with basic properties of Finite Impulse Response (FIR) filters and Infinite Impulse Response (IIR) filters, their design in Python environment and their practical applications.

TASKS:

INTRODUCTION:

In [2]:
f6.fig('introduction.png')
Out[2]:

TASK 1:
Write difference equations and plot block diagrams of the following filters (by default we assume a[0]=1, if a[0]≠1 we need to divide all coefficients values by a[0] ≠0):
b[0] =4, b[1]=2;
b[0] =2, a[1]=-0.5;
b[0] =0.5, a[3]=1;
b[0] =1, b[1]=2, a[1]=0.5, a[3]=-1.

I. FIR filters
Pros:
1) available simple, linear methods for designing filters,
2) linear phase can be easily achieved (symmetry of coefficients),
3) are always stable,
4) feature short transient states (finite impulse response),

Cons: 1) a high number of filter coefficients are required (i.e. high filter order) to obtain steep frequency characteristics.

TASK 2:
A FIR filter is given b[0] =0.5, b[1]=0.5. Write a script showing that impulse response of this filter is equal to its moving average coefficients.
Hint: define signal x=[1, 0, 0, ….,0] of length N=100 and sampled at a frequency rate of fs=11400 Hz, and use the lfilter function from the scipy.signal package to obtain the impulse response.

EXPECTED RESULTS:

In [3]:
%pylab inline
pylab.rcParams['figure.figsize'] = (14.0, 7.0)
f6.task2()
Populating the interactive namespace from numpy and matplotlib

TASK 3:
Compute frequency characteristic of the filter defined in Task 2 by using the two following methods:
a. by computing the Fourier transform of the impulse response obtained in Task 2; plot the amplitude and phase spectrum of the filter,
b. by using the freqz command from the scipy.signal package. What kind of filter frequency characteristic you have obtained? Is the phase linear? What is the practical consequence of phase linearity?
c. From within the Python environment play the voice_noise.wav wave file (see lab. 3). From the lecture signal database load the wave file voice_noise.wav into the Python workspace (the signal is sampled with a rate of fs=11.4 kHz. Filter the voice signal using the filter from Task 2. Store the filtering result in file voice_filtered.wav. Finally, play the voice_filtered.wav wave file.

TASK 3a - EXPECTED RESULTS:

In [4]:
f6.task3a()

TASK 3b - EXPECTED RESULTS:

In [5]:
f6.task3b()

TASK 3c - EXPECTED RESULTS:

The voice_noise.wav wave file:

In [6]:
from IPython.display import Audio
sampling_rate, data=f6.sound(f6.noise_voice)
Audio(data=data, rate=sampling_rate)
Out[6]:
In [7]:
f6.task3c()

The filtered result in file voice_filtered.wav:

In [8]:
from IPython.display import Audio
sampling_rate, data=f6.sound(f6.noise_voice_filtered)
Audio(data=data, rate=sampling_rate)
Out[8]:

TASK 4:
Repeat Task 2 for the FIR filter with coefficients b[0] =0.5, b[1]=-0.5.

EXPECTED RESULTS:

In [9]:
f6.task4()

TASK 5:
Compute the impulse response of a filter which is a cascade connection of the filters studied in Task 2 and Task 4. What is the frequency characteristic of this combined filter.

II. IIR filters
Pros:
- steep frequency characteristics can be achieved for low filter orders.

Cons:
- difficulty in keeping the filter phase linear, - can be unstable.

TASK 6:

In [10]:
f6.fig('task6.png')
Out[10]:

EXPECTED RESULTS:

In [11]:
f6.task6()

TASK 7:
Load into the Pylab environment an audio signal male_voice.wav sampled at a rate of fs=11.4kHz. Use the following IIR filter coefficients b[0]=1, a[0]=1, a[1000]=-0.7 to filter this signal. Save the result in a wave file. Play the file and comment the obtained audio effect.

The male_voice.wav wave file:

In [12]:
f6.task7()
sampling_rate, data=f6.sound(f6.male_voice)
Audio(data=data, rate=sampling_rate)
Out[12]:

EXPECTED RESULTS:

The filtered result in file male_voice_filtered.wav:

In [13]:
sampling_rate, data=f6.sound(f6.male_voice_filtered)
Audio(data=data, rate=sampling_rate)
Out[13]:

TASK 8:
Go to scipy.signal reference guide available from the webpage:
http://docs.scipy.org/doc/scipy/reference/signal.html

and find the Filter design section. Then select the firwin function for designing finite impulse response filter for specified frequency characteristics, e.g. the command b=firwin(40,0.7) will compute M=40 autoregressive filter coefficients defining a low-pass filter with a cut-off frequency at fc=0.7*(fs/2). Use freqz command to plot the frequency characteristic of this filter.

Follow the examples in the scipy.signal.firwin section and define: high-pass, band-pass and band-stop filters. Plot frequency characteristics of these filters.

EXPECTED RESULTS: