Computer Homework 2

In this computer homework you will design lowpass, bandpass and highpass IIR filters using matlab. After designing the filter, you will filter the given signal and listen to it.

Designing IIR filters in Matlab

Matlab has many commands for designing IIR filters. We can design Butterworth, Chebychev type I and II, and elliptical Filters using two commands:

1. buttord, cheb1ord, cheb2ord, or ellipord to find the order of the filter needed, and

2. butter, cheby1, cheby2, and ellip to find the filter coefficients.

These commands can be used to design both digital, H(z), and analog filters, Hc(s). We will use them to design digital filters here.

Assume we have the specifications. From this we get four variables, Wp, Ws, Rp, and Rs. Wp and Ws are the pass and stop frequencies in rad/sam. They must be normalized by dividing by pi. For example, if Wp is 0.2pi, use 0.2 in matlab. Rp is the difference between K and

K/√(1+ε2) in decibels, and Rs is the difference between K and K/A in decibels.

To design a butterworth lowpass filter, we use

[N, Wo] = BUTTORD(Wp, Ws, Rp, Rs)

to get the order of the filter, N, and the cutoff frequency, Wo. It is the same for the other types of filters.

To get the filter coefficients,

[b,a]=butter(N,Wo);

[b,a]=cheby1(N,Rp,Wo);

To design highpass filters, it is similar, but use

[b,a]=butter(N,Wo,'high');

[b,a]=cheby1(N,Rp,Wo,'high');

To design bandpass filters, Wp and Ws are vectors of length 2. Wp will have both pass frequencies, and Ws will have both stop frequencies.

After the filter is designed, we can use the command 'filter' to filter the signal. To load wave files use [x,fs]=wavread('filename'); x is the variable, and fs is the sampling frequency in Hz.

To listen to sounds use the 'sound' command. sound(x,fs) will play what is in x.

************************************************

Ex.

Design a LPF with a passband from 0 to 0.2pi, and a stopband from 0.4pi to pi. No more than 1 dB loss in the passband, and at least 20 dB loss in the stopband. Read the file input.wav, filter the signal using the designed filter, and play it over the speaker.

clear;

[x,fs]=wavread('c:\input.wav');

[n,wo]=buttord(.2,.4,1,20);

[b,a]=butter(n,wo);

y=filter(b,a,x);

sound(y,fs);

************************************************

Computer Homework 2:

Part 1

1. Download the sound from the web

2. Design a Butterworth LPF with a cutoff of 0.05pi. Transition region from 0.05pi to 0.07pi. Not more than 1dB loss in the passband, and at least 20 dB attenuation in the stopband.

3. Design 3 Cheychev type I BPFs with passbands 0.06pi to 0.15pi, 0.12pi to 0.2pi, 0.18pi to 0.3pi, all transition regions of 0.03pi.

4. Design a Butterworth HPF with a passband starting at 0.28pi, and a transition region of 0.03pi.

5. Plot magnitude response of all the filters.

6. Filter the signal using each filter, and listen to it. Do not hand this step in.

Part 2

1. Design a Chebychev type I LPF with pass band from 0 to .5pi, stopband from .7pi to pi. Not more than 1dB loss in the passband, and at least 20 dB attenuation in the stopband.

3. Get the group delay of the filter using the grpdelay function.

[gr,w]=grpdelay(b,a,n)

gr is the group delay in samples, and w is the frequency in rad/sam.

4. Plot the group delay (in samples)  vs. the frequency.

5. Input two cosines into the filter, one with frequency 0.05pi, the other frequency 0.5pi rad/sam.

6. Make 2 plots showing input and output on the same graph.

7. Show that the difference between the input and output cosines is predicted by the group delay plot. Make shure this is clearly visible in the plots.