You are on page 1of 14

DSP MatLab: Assessed exercise IIR and FIR filter design

IIR low-pass filter Butterworth

signal sampled frequency = 40kHz


cut-off frequency = 8kHz.
transition band = 4kHz.
gain in pass-band = 0dB.
attenuation in stop-band = 40dB.

This difference equation defines how the input signal is related to the output signal

where P is the forward filter order, bi are the forward filter coefficients, Q is the feedback
filter order, ai, are the feedback filter coefficients, x(n) is the input signal and y(n) is the
output signal.

Filter order

n=8

n Factors of Polynomial Bn(s)


8º = (s2 + 0.3902s + 1)(s2 + 1.1111s + 1)(s2 + 1.6629s + 1)(s2 + 1.9616s + 1)

LP PreWarping

Wd = 2 pi 8000

2 Wd delta(t)
Walfa = --------- tan ---------------
delta(t) 2
2 pi 8000 (1/40000)
Walfa = 2(40 000) tan -------------------------- = 58123.40 = 2 pi 58123
2
1
H(s) = -------------------------------------------------------------------------------------
(s2 + 0.3902s + 1)(s2 + 1.1111s + 1)(s2 + 1.6629s + 1)(s2 + 1.9616s + 1)

s = s / 2 pi 58123

Bilinear transformation

2 (1 – Z -)
s = ----------------------------
delta (t) (1 + Z -)

b= 0.0001 0.0008 0.0028 0.0056 0.0070 0.0056 0.0028 0.0008 0.0001


a= 1.0000 -1.5906 2.0838 -1.5326 0.8694 -0.3192 0.0821 -0.0122 0.0009

0.0001+0.0008Z-+0.0028Z-+0.0056Z-+0.0070Z-+0.0056Z-+0.0028Z-
6+0.0008Z+0.0001Z-
HLP(z) = --------------------------------------------------------------------------------------------------------------------
1-1.5906Z-+2.0838Z--1.5326Z-+0.8694Z--0.3192Z-+0.0821Z-6-0.0122Z-+0.0009Z-
1
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

IIR low-pass filter Butterworth


Butterworth Lowpass Filter, Magnitude,order=8 % Butterworth Filter
0
[n,fn] =
-100
buttord(frequency_pass_band,frequency_stop_
|H(f)| dB

-200 band,ripple_pass_band,ripple_stop_band)
-300

-400
[b,a] = butter(n,fn)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
frequency 4
x 10
Butterworth Lowpass Filter, Phase,order=8 [H,w] = freqz(b,a,512,40000);
4

2 figure;
angle H(f) rad

0 % Magnitude Response
-2
plot(w, 20*log10(abs(H)));
title(['Butterworth Lowpass Filter,
-4
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 Magnitude,order=',num2str(n)]);
frequency 4
x 10

% Plotting the phase response


Pole-Zero, Butterworth Low Pass Filter plot(w,angle(H));
1 title(['Butterworth Lowpass Filter,
0.8 Phase,order=',num2str(n)]);
0.6 grid on
0.4
% Pole-Zero Plot
Imaginary Part

0.2
z = roots(b);
0
p = roots(a);
-0.2
figure;
-0.4
zplane(z,p);
-0.6
title('Pole-Zero, Butterworth Low Pass Filter');
-0.8

-1

-1 -0.5 0 0.5 1
Real Part

b= 0.0001 0.0008 0.0028 0.0056 0.0070 0.0056 0.0028 0.0008 0.0001


a= 1.0000 -1.5906 2.0838 -1.5326 0.8694 -0.3192 0.0821 -0.0122 0.0009

2
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

IIR high-pass filter Butterworth

signal sampled frequency = 40kHz


cut-off frequency = 12kHz.
transition band = 4kHz.
gain in pass-band = 0dB.
attenuation in stop-band = 40dB.

Filter order
Is the same formula used in the low-pass filter, we use the value obtained in the low-pass
filter

n=8

n Factors of Polynomial Bn(s)

8º = (s2 + 0.3902s + 1)(s2 + 1.1111s + 1)(s2 + 1.6629s + 1)(s2 + 1.9616s + 1)

Low-Pass to High-Pass Filter

Since the High-Pass filter has a cut-off frequency of 8KHz, the filter transfer function may be
obtained from the Low-Pass filter, HLP(Z) by substitution Z to -Z.

From the HLP(Z):

b= 0.0001 0.0008 0.0028 0.0056 0.0070 0.0056 0.0028 0.0008 0.0001


a= 1.0000 -1.5906 2.0838 -1.5326 0.8694 -0.3192 0.0821 -0.0122 0.0009

0.0001+0.0008Z-+0.0028Z-+0.0056Z-+0.0070Z-+0.0056Z-+0.0028Z-
6+0.0008Z+0.0001Z-
HLP(z) = --------------------------------------------------------------------------------------------------------------------
1-1.5906Z-+2.0838Z--1.5326Z-+0.8694Z--0.3192Z-+0.0821Z-6-0.0122Z-+0.0009Z-

This yields the High-Pass filter transfer function HHP(Z):

b_HP = 0.0001 -0.0008 0.0028 -0.0056 0.0070 -0.0056 0.0028 - 0.0008 0.0001
a_HP = 1.0000 1.5906 2.0838 1.5326 0.8694 0.3192 0.0821 0.0122 0.0009

0.0001-0.0008Z-+0.0028Z--0.0056Z-+0.0070Z--0.0056Z-+0.0028Z-6-0.0008Z+0.0001Z-

HLP(z) = -------------------------------------------------------------------------------------------------------------------
1+1.5906Z-+2.0838Z-+1.5326Z-+0.8694Z-+0.3192Z-+0.0821Z-6+0.0122Z-
+0.0009Z-

3
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

IIR high-pass filter Butterworth


Butter Highpass Filter, Magnitude ,order=8 [n,fn] =
200
buttord(frequency_pass_band,frequency_stop_
0 band,ripple_pass_band,ripple_stop_band);
|H(f)| dB

-200
[z,p,k] = buttap(n);
-400
N = n;
0 0.2 0.4 0.6 0.8 1
f
1.2 1.4 1.6 1.8
4
2
b = k*poly(z); % Numerator polynomial
x 10
Butter Highpass Filter, Phase ,order=8 a = poly(p); % Demnominator
4
[b1,a1] = lp2hp(b,a,2*pi*12000); % Low
2 Pass to High Passtransformation
angle H(f) rad

-2
[b2,a2] = bilinear(b1,a1,40000, 12000); %
Bilinear Transformation , frequency_pass_band
-4
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 =Pre-warping freq.
f 4
x 10
%Or trying the Z -> -Z
%a2 = [1.0000 1.5906 2.0838 1.5326
Pole-Zero Butter HighPass Filter 0.8694 0.3192 0.0821 0.0122 0.0009];
1 %b2 = [0.0001 -0.0008 0.0028 -0.0056
0.8 0.0070 -0.0056 0.0028 - 0.0008 0.0001];
0.6

0.4
[H3,w3] = freqz(b2,a2,512,40000);
figure;
Imaginary Part

0.2

0
% Pole-Zero Plot
-0.2
z = roots(b2);
-0.4
p = roots(a2);
-0.6
figure;
-0.8 zplane(z,p);
-1 title('Pole-Zero, Butterworth HighPass Filter');
-1 -0.5 0 0.5 1
Real Part

b_HP = 0.0001 -0.0008 0.0028 -0.0056 0.0070 -0.0056 0.0028 - 0.0008 0.0001
a_HP = 1.0000 1.5906 2.0838 1.5326 0.8694 0.3192 0.0821 0.0122 0.0009

4
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

IIR band-pass filter Butterworth

signal sampled frequency = 40kHz


cut-off frequency = 12kHz.
transition band = 8kHz.
gain in pass-band = 0dB.
attenuation in stop-band = 40dB.

Low-Pass to Band-Pass Filter

For the Band-Pass filter, the prototype Low-Pass filter should have cut-off frequency of

WCLP = 2 pi (12 000 – 4 000)

WCLP = 2 pi 8000

Thus the Low-Pass filter HLP(Z) may also be used to design the Band-Pass filter. The other
parameter that is required is alfa, which may be found:

pi(2 pi 12000) + 2 pi 8000


Cos --------------------------------
2 pi 40000
alfa = -----------------------------------------------------
pi(2 pi 12000) - 2 pi 8000
Cos --------------------------------
2 pi 40000

The Low-Pass to Band-Pass transformation then simplifies to Z- to -Z-

From the HLP(Z):

b= 0.0001 0.0008 0.0028 0.0056 0.0070 0.0056 0.0028 0.0008 0.0001


a= 1.0000 -1.5906 2.0838 -1.5326 0.8694 -0.3192 0.0821 -0.0122 0.0009

5
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

0.0001+0.0008Z-+0.0028Z-+0.0056Z-+0.0070Z-+0.0056Z-+0.0028Z-
6+0.0008Z+0.0001Z-
HLP(z) = --------------------------------------------------------------------------------------------------------------------
1-1.5906Z-+2.0838Z--1.5326Z-+0.8694Z--0.3192Z-+0.0821Z-6-0.0122Z-+0.0009Z-

This yields the Band-Pass filter transfer function HBP(Z):

From matlab:

HBP(Z)

a = [1.0000 -0.8085 3.2043 -1.8508 3.7585 -1.4247 1.9031 -0.3663 0.3486];


b = [0.0010 -0.0000 -0.0040 -0.0000 0.0060 -0.0000 -0.0040 -0.0000 0.0010];

IIR band-pass filter Butterworth


Butterworth BandPass Filter, Magnitude response,order=8
200
frequency_sample = 40000;
0 frequency_pass_band1 = 8000;
|H(f)| dB

frequency_pass_band2= 12000;
-200

-400
wo =
0 0.2 0.4 0.6 0.8 1
f
1.2 1.4 1.6 1.8
4
2
2*pi*sqrt(frequency_pass_band1*frequency_p
x 10
Butterworth BandPass Filter, Phase response,order=8 ass_band2);%Centre Frequency rad/s wo =
4
2*pi*sqrt(Fpb1*Fpb2);
2
angle H(f) rad

0 bw = (frequency_pass_band2-
-2
frequency_pass_band1)*2*pi; %Bandwidth
rad/s bw = (Fpb2-Fpb1)*2*pi;
-4
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 N =4;
f 4
x 10
[z,p,k] = buttap(N);
b = k*poly(z); % Numerator polynomial
a = poly(p); % Demnominator

[bt,at] = lp2bp(b,a,wo,bw); % Low Pass to


Bandpass Transformation
[b2,a2] = bilinear(bt,at,frequency_sample,
frequency_pass_band1); % Bilinear
Transformation matchinglower pass frequency

%Butterworth Filter
[H,w] = freqz(b2,a2,512,40000);

% Pole-Zero Plot
z = roots(b2);
p = roots(a2);
figure;
zplane(z,p);
title('Pole-Zero, Butterworth BandPass Filte

6
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

a = 1.0000 -0.8085 3.2043 -1.8508 3.7585 -1.4247 1.9031 -0.3663 0.3486


b = 0.0010 -0.0000 -0.0040 -0.0000 0.0060 -0.0000 -0.0040 -0.0000 0.0010

FIR low-pass filter Hanning

signal sampled = 40kHz


cut-off frequency = 8kHz.
transition band = 4kHz.
gain in pass-band = 0dB.
attenuation in stop-band = 40dB.

This difference equation defines how the input signal is related to the output signal

where P is the filter order, bi are the forward filter coefficients, x(n) is the input signal
and y(n) is the output signal.

HD(W) = 0 {2 pi /5 < w < pi / delta (t)}


HD(W) = 1 {0 <= w <= 2 pi /5 }

Step 1. If a stop-band rejection = 40dB then any Hanning window could be employed.
Transition band = 4000Hz

Step 2. For Hanning the overall length or complexity to achieve the required transition band.
Hanning = 3.1 / delta t = 3.1 x 40000 / 4000 = 31

Step 3. Calculate the window coefficient values for N-tap filter, only M + 1 calculation are required to
obtain the 2M + 1.

N = 2M + 1 for Wn
Half sampling frequency is 20kHz
Pi / delta t = 20 kHz
Stop-band = 8000 Hz
Stop-band = 8 pi / 20 delta t = 2 pi / 5 delta t

M = (N – 1) / 2 = 15
7
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

Wn = ½ (1 + cos (n pi / M))
Wn = ½ (1 + cos (n pi / 15)

Step 4. To obtain the frequency response HD(w), calculate N = 2M+1 coeficients .

Step 5. Cn’ = Wn Cn

Step 6.

FIR low-pass filter Butterworth

50

% 1 Design a low pass filter FIR Filter type


Magnitude (dB)

-50 % Filter Specs


-100 % Hanning = 3.1 / delta t = 3.1 x 40000 /
-150
4000 = 31
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample)
%15 1 15
0
n=[-15:1:15];
Phase (degrees)

-500
%Stop-band = 8000 Hz
-1000 %Stop-band = 8 pi / 20 delta t = 2 pi / 5
delta(t)
-1500
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample)
y = 2/5*sinc(2/5.*n);

FIR % Wn = ½ (1 + cos (31 pi / 15)


0.4
w=0.5*(1+cos(n.*pi/15));
0.35

0.3 FIR=y.*w
0.25 figure;
0.2 plot(FIR)
grid on;
0.15
title(['FIR']);
0.1
xlabel('N / 100');
0.05 ylabel('');
0 % plot the magnitud and phase
-0.05
freqz(FIR)
-0.1
0 50 100 150 200 250 300 350 % help to plot all
N / 100
fvtool(FIR);

[a values]
Columns 1 through 7 0 -0.0002 -0.0006 0.0015 0.0046 -0.0000 -0.0116

Columns 8 through 14 -0.0105 0.0148 0.0330 -0.0000 -0.0632 -0.0564 0.0895

8
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

Columns 15 through 21 0.2994 0.4000 0.2994 0.0895 -0.0564 -0.0632 -0.0000

Columns 22 through 28 0.0330 0.0148 -0.0105 -0.0116 -0.0000 0.0046 0.0015

Columns 29 through 31 -0.0006 -0.0002 0

FIR high-pass filter Hanning

signal sampled frequency = 40kHz


cut-off frequency = 12kHz.
transition band = 4kHz.
gain in pass-band = 0dB.
attenuation in stop-band = 40dB.

This formula defines how the input signal is related to the output signal

where P is the filter order, bi are the forward filter coefficients, x(n) is the input signal
and y(n) is the output signal.

HD(W) = 0 {0 < w < pi /5 }


HD(W) = 1 {pi /5 <= w <= pi / delta (t)}

Step 1. If a stop-band rejection = 40dB then any Hanning window could be employed.
Transition band = 4000Hz

Step 2. For Hanning the overall length or complexity to achieve the required transition band.
Hanning = 3.1 / delta t = 3.1 x 40000 / 4000 = 31

Step 3. Calculate the window coefficient values for N-tap filter, only M + 1 calculation are required to
obtain the 2M + 1.

N = 2M + 1 for Wn
Half sampling frequency is 20kHz
Pi / delta t = 20 kHz
Stop-band = 12000 Hz
Stop-band = 12 pi / 20 delta t = 3 pi / 5 delta t

9
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

M = (N – 1) / 2 = 15

Wn = ½ (1 + cos (n pi / M))
Wn = ½ (1 + cos (n pi / 15)

Step 4. To obtain the frequency response HD(w), calculate N = 2M+1 coeficients .

Step 5. Cn’ = Wn Cn

Step 6.
FIR high-pass filter Butterworth

50

% 1 Design a high pass filter FIR Filter type


Magnitude (dB)

-50 % Filter Specs


-100 % Hanning = 3.1 / delta t = 3.1 x 40000 /
-150
4000 = 31
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
Normalized Frequency ( rad/sample)
0.8 0.9 1
n=[-15:1:15]
500
%Stop-band = 12000 Hz
Phase (degrees)

0
%Stop-band = 12 pi / 20 delta t = 3 pi / 5
-500 delta t
-1000
const=2/5;
-1500
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 part1=sinc(n/5)
Normalized Frequency ( rad/sample)
part2=cos(4*pi*n/5)
y=const.*part1.*part2
FIR
0.4
%y = 2/5*sinc(2/5.*n);
0.3

figure
0.2
plot (y)
0.1 grid on;
title(['FIR']);
0
xlabel('N / 100');
-0.1 ylabel('');
-0.2
% Wn = ½ (1 + cos (31 pi / 15)
-0.3 w=0.5*(1+cos(n.*pi/15));
-0.4
0 50 100 150 200 250 300 350
N / 100

[a values]
Columns 1 through 7 0 -0.0002 0.0006 0.0015 -0.0046 -0.0000 0.0116

Columns 8 through 14 -0.0105 -0.0148 0.0330 0.0000 -0.0632 0.0564 0.0895

10
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

Columns 15 through 21 -0.2994 0.4000 -0.2994 0.0895 0.0564 -0.0632 0.0000

Columns 22 through 28 0.0330 -0.0148 -0.0105 0.0116 -0.0000 -0.0046 0.0015

Columns 29 through 31 0.0006 -0.0002 0

FIR band-pass filter Hanning

signal sampled frequency = 40kHz


cut-off frequency = 12kHz.
transition band = 8kHz.
gain in pass-band = 0dB.
attenuation in stop-band = 40dB.

HD(w) = 0 {0 < w < pi /10,


4 pi /10 < w <= delta (t)}
HD(w) = 1 {pi /10 <= w <= pi / 4 pi /10 }

Step 1. If a stop-band rejection = 40dB then any Hanning window could be employed.
Transition band = 4000Hz

Step 2. For Hanning the overall length or complexity to achieve the required transition band.
Hanning = 3.1 / delta t = 3.1 x 40000 / 4000 = 31

Step 3. Calculate the window coefficient values for N-tap filter, only M + 1 calculations are required
to obtain the 2M + 1.

frequency is 40kHz
Pi / delta t = 40 kHz

Stop-band_upper = 12000 Hz
Stop-band_lower = 4000 Hz
Stop-band_upper = 12 pi / 40 delta t = 3 pi / 10 delta t
Stop-band_lower = 4 pi / 40 delta t = pi / 10 delta t

Transition band = 8000 Hz


Const = 8000 pi / 40000 = pi / 5

M = (N – 1) / 2 = 15

Wn = ½ (1 + cos (n pi / M))
Wn = ½ (1 + cos (31 pi / 15)

11
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

Step 4. To obtain the frequency response HD(w), calculate N = 2M+1 coeficients .

Step 5. Cn’ = Wn Cn

Step 6.

FIR band-pass filter Butterworth

% 1 Design a band pass filter FIR Filter type


Magnitude (dB)

-50

% Filter Specs
-100
% Hanning = 3.1 / delta t = 3.1 x 40000 /
-150
4000 = 31
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
Normalized Frequency ( rad/sample)
0.8 0.9 1
n=[-15:1:15]
500
%Stop-band1 = 12000 Hz and
Phase (degrees)

0 %Stop-band2 = 4000Hz
%Stop-band1 = 12 pi / 40 delta t = 3 pi / 10
-500 %Stop/band2 = 1 pi / 10 delta t
-1000
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 const=1/5;
Normalized Frequency ( rad/sample)
part1=sinc(n/10)
part2=cos(pi*n/2)
FIR y=const.*part1.*part2
0.4

0.3 figure
plot (y)
0.2
grid on;
0.1 title(['FIR']);
xlabel('N / 100');
0
ylabel('');
-0.1

% Wn = ½ (1 + cos (31 pi / 15)


-0.2
w=0.5*(1+cos(n.*pi/15));
-0.3 FIR=y.*w
-0.4
0 50 100 150 200 250 300 350
N / 100

[a values]
Columns 1 through 7 0 0.0005 0.0000 -0.0030 0.0000 -0.0000 0.0000

Columns 8 through 14 0.0209 -0.0000 -0.0660 0.0000 0.1263 -0.0000 -0.1790

Columns 15 through 21 0.0000 0.2000 0.0000 -0.1790 -0.0000 0.1263 0.0000

12
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

Columns 22 through 28 -0.0660 -0.0000 0.0209 0.0000 -0.0000 0.0000 -0.0030

Columns 29 through 31 0.0000 0.0005 0

Bonus work.
Analysis of finite precision/quantization effects. Find for each filter and each
implementation (IIR and FIR) the minimum required accuracy (in terms of bits) to
represent the coefficients of the filters so that the filters are stable.

It is often the case that a guaranteed SNR is required for a signal which may be any
amplitude within a given dynamic range. In this case, it is necessary to determine
how many ADC bits are required to achieve the SNR for the smallest signal, and
then to have additional bits to ensure that the ADC does not saturate for the largest
signal.

It is common practice to express the SNR of a signal in terms of decibels (dB).


This is achieved by taking the 10log10 of the SNR.

40dB = 6.02N + 1.76


N1 = 6.35 = 7 bits

An additional N2 bits are also required to maintain a SNR of 40dB for the 6V part
of the waveform.

2 exp(N2) = 6 / 0.05
Log10 (12)
N2 = log2 (12) = --------------- = 6.9 = 7 bits
Log10(2)

13
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design

Hence, the total number of bits required to produce a SNR of 40dB for a sine wave
acquisition that varies between 0.5V and 6V is given by:
Total bits = N1 + N2
Total bits = 7 + 7 = 14 bits

It is important to realize that the bits must be rounded up to become a real integer
as we are using binary. Since the number of quantization bits is 14, the
quantization step size q can now be calculated.

q = 6 / 2 exp 9 = 0.0117 volts

I found another approach that say:

A good rule of thumb [1] is to assume 5dB/bit for the dynamic range

Note: that the usual 6db/bit rule doesn’t apply because quantization error for the filter
coefficients tends to be correlated, especially at the extremes of the impulse response.

In this case 40dB we may need 8 bits

[1] f. harris, “Multirate Signal Processing in Transmit-ter


& Receiver Designs,” UCLA Extension course,
November, 2000.

14
Luis Alfredo Mateos Guzman

You might also like