import funkcje_do_lab6 as f6 f6.hide()
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.
Write difference equations and plot block diagrams of the following filters (by default we assume a=1, if a≠1 we need to divide all coefficients values by a ≠0):
b =4, b=2;
b =2, a=-0.5;
b =0.5, a=1;
b =1, b=2, a=0.5, a=-1.
I. FIR filters
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.
A FIR filter is given b =0.5, b=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.
%pylab inline pylab.rcParams['figure.figsize'] = (14.0, 7.0) f6.task2()
Populating the interactive namespace from numpy and matplotlib
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:
TASK 3b - EXPECTED RESULTS:
TASK 3c - EXPECTED RESULTS:
The voice_noise.wav wave file:
from IPython.display import Audio sampling_rate, data=f6.sound(f6.noise_voice) Audio(data=data, rate=sampling_rate)
The filtered result in file voice_filtered.wav:
from IPython.display import Audio sampling_rate, data=f6.sound(f6.noise_voice_filtered) Audio(data=data, rate=sampling_rate)
Repeat Task 2 for the FIR filter with coefficients b =0.5, b=-0.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
- steep frequency characteristics can be achieved for low filter orders.
- difficulty in keeping the filter phase linear, - can be unstable.
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=1, a=1, a=-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:
f6.task7() sampling_rate, data=f6.sound(f6.male_voice) Audio(data=data, rate=sampling_rate)
The filtered result in file male_voice_filtered.wav:
sampling_rate, data=f6.sound(f6.male_voice_filtered) Audio(data=data, rate=sampling_rate)
Go to scipy.signal reference guide available from the webpage:
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.