Professional Documents
Culture Documents
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
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 -)
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
-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
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
2
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design
Filter order
Is the same formula used in the low-pass filter, we use the value obtained in the low-pass
filter
n=8
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.
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-
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
-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
For the Band-Pass filter, the prototype Low-Pass filter should have cut-off frequency of
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:
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-
From matlab:
HBP(Z)
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
%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
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.
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 5. Cn’ = Wn Cn
Step 6.
50
-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);
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
8
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design
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.
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 5. Cn’ = Wn Cn
Step 6.
FIR high-pass filter Butterworth
50
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
10
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design
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
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 5. Cn’ = Wn Cn
Step 6.
-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
[a values]
Columns 1 through 7 0 0.0005 0.0000 -0.0030 0.0000 -0.0000 0.0000
12
Luis Alfredo Mateos Guzman
DSP MatLab: Assessed exercise IIR and FIR filter design
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.
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.
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.
14
Luis Alfredo Mateos Guzman