Digital_Filter_EE392_S14

ELE 392 Analog Circuits Laboratory
Constructing a Digital Filter using MATLAB as a design aid
Digital filters are used in a system with an A/D converter and D/A converteras shown
below. The A/D converter changes the voltage signal to numbers. Therefore, the digitial filter is
simply a math ptoblem. As the numbers come in as a function of time, do math on them and put
them back out to the D/A converter. If you did the right math, the output can be a filtered
version of the input. Of course if you do no math and just put the number back out, the same
signal will appear at the input and output. The output will just be a discrete time/discrete voltage
version of the input.
2R
5V
R
2R
+
Vout
R
A/D
B7
2R
B6
V1
B5
PIC
18F1220
R
2R
B4
B3
R
2R
B2
B1
R
2R
B0
R
2R
1 Vpk
VCC
1kΩ
1kΩ
R15
1kΩ
1kΩ
R12
R14
R16
C1
R2
R8
R10
R4
1kHz
1µF
1kΩ
R1
R3
R5
R11
R13
1kΩ
1kΩ
R7
R9
R6
0°
R
2R
2R
-
The Filter Transfer Function
A filter can be described by the location of the poles of the transfer function in the
complex frequency plane or s-plane. You may remember that a Butterworth filter has the poles
equally spaced around a semi-circle in the left-half side of the s-plane. For a low-pass filter, all
the zeros are at s→∞ while for a high-pass filter all the zeros are at the origin of the s-plane.
You could simply mark the location of the poles on the s-plane for the particular filter
you want to design. Because you know that each pole corresponds to a root of the polynomial
that comprises the denominator of the transfer function, you can derive the denominator of the
transfer function from the product
(𝑠 − 𝑝1 )(𝑠 − 𝑝2 ) … (𝑠 − 𝑝3 ).
An arbitrary transfer function is of the form
𝐻(𝑠) =
𝑏1 𝑠 𝑛 + 𝑏2 𝑠 𝑛−1 + ⋯ + 𝑏𝑛+1
,
𝑎1 𝑠 𝑛 + 𝑎2 𝑠 𝑛−1 + ⋯ + 𝑎𝑛+1
and is written as [b,a] in MATLAB.
Transforming the continuous time transfer function to discrete time
The action of the A/D will be to “chop up” the signal into discrete numbers at discrete
moments in time. As you know, when these numbers are converted back to voltages at discrete
moments in time, not all frequencies can be reproduced by the system. Frequencies of up to ½
the sampling rate, fs, can be reproduced, but if a higher frequency is entered into the system it is
“aliased” back to frequency between 0 and ½fs. In other words, if a frequency of 7/8fs is
presented at the input, a signal of 1/8fs will come out of the output.
In order to handle the effect of the discrete sampling time, a discrete time complex plane
similar the continuous time s-plane has been invented. The discrete time complex plane is called
the z-plane. The y-axis of the s-plane represents the imaginary component of the complex
frequency. The “jω” axis extends to infinity as any Cartesian coordinate system does. Points on
this axis represent sine waves with any frequency, even approaching infinity. Because discrete
time signals can only go up to frequencies of ½fs, the “jω” axis must be wrapped around a circle.
This allows ever increasing frequencies to alias back around to smaller frequencies. The
transformation that describes this effect is
𝑧 = 𝑒 𝑠𝑇 ,
where T is sampling period equal to 1/fs. Notice that a DC signal, s=0, is transformed to z=1. If
s=jω and ω=¼ (2πfs), then z=0+1j. If the frequency is increased to ω=½ (2πfs), then z=-1+0j and
if ω=3/2 (2πfs), then z=0-1j. Finally when ω=2πfs, z loops back around a circle to equal 1. The
frequency can be increased forever in the continuous time domain, while in discrete time domain
it will simply loop around the unit circle returning to 1 at every integer multiple of the sampling
frequency.
Not only can the jω axis be transformed to the z-plane, but any point in the s-plane can be
transformed to a point in the z-plane. For example poles and zeros in the s-plane can be
transformed to poles and zeros in the z-plane. As an example, consider a filter with three poles
s=-5 and s=-3±j4. If a sampling rate of 10 samples per second is used, these are transformed to
the z-plane as z=0.61 and z=0.68±j.29. The figures below show the poles and zeros for this
example filter on the s-plane and z-plane respectively.
Transforming points from the s-plane to the z-plane is fine, however, we really want to
transform the entire transfer function from H(s) to a new transfer function H(z). To do this, we
can rewrite the s and z relationship as
𝑠=
ln(𝑧)
.
𝑇
Using substitution, the third order z-domain transfer function can be written as
ln⁡(𝑧)⁄ 3
ln⁡(𝑧)⁄ 2
ln⁡(𝑧)⁄
)
+
𝑏
(
2
𝑇
𝑇) + 𝑏3 (
𝑇) + 𝑏4
𝐻(𝑧) =
.
ln⁡(𝑧)⁄ 3
ln⁡(𝑧)⁄ 2
ln⁡(𝑧)⁄
𝑎1 (
𝑇 ) + 𝑎2 (
𝑇 ) + 𝑎3 (
𝑇 ) + 𝑎4
𝑏1 (
In order to implement a digital filter, one must solve the following equation
𝑉𝑜𝑢𝑡 = 𝐻(𝑧)⁡𝑉𝑖𝑛 .
Of course Vout and Vin are simply numbers presented to and from the D/A and A/D converters,
respectively. As it turns out, this transfer function is too difficult for any modern DSP processor
to calculate in real time at any respectable sampling rate. This means approximations are used
for H(z). Often, the Taylor series expansion of the exponential is used to form the approximation
as shown in the equations below.
𝑧 = 𝑒 𝑠𝑇 = 1 + 𝑠𝑇 +
𝑧=
1
𝑒 −𝑠𝑇
(𝑠𝑇)2⁄
(𝑠𝑇)3⁄
2! +
3! + ⋯ 𝑜𝑟
1
=
1 − 𝑠𝑇 +
(𝑠𝑇)2⁄
(𝑠𝑇)3⁄
2! −
3! + ⋯
.
If one takes the first order approximations of the equations above, the methods known as
the forward Euler and backward Euler result.
𝑧 ≈ 1 + 𝑠𝑇 → 𝑠 ≈
𝑧≈
𝑧−1
⁡𝑓𝑜𝑟𝑤𝑎𝑟𝑑⁡𝐸𝑢𝑙𝑒𝑟
𝑇
1
𝑧−1
→𝑠≈
⁡𝑏𝑎𝑐𝑘𝑤𝑎𝑟𝑑⁡𝐸𝑢𝑙𝑒𝑟
1 − 𝑠𝑇
𝑧𝑇
Many different approximations to the exponential may be used in order to form a discrete
time system. MATLAB does not use the forward or backward Euler method but a different one
called Tustin’s method. The exponential is approximated with the following equation
(𝑠𝑇⁄2)2⁄
(𝑠𝑇⁄2)3⁄
𝑠𝑇
1 + ⁄2 +
𝑠𝑇
𝑠𝑇
2! +
3! + ⋯.
𝑧 = 𝑒 ⁄2 𝑒 ⁄2 = −𝑠𝑇 =
(𝑠𝑇⁄2)2⁄
(𝑠𝑇⁄2)3⁄
𝑒 ⁄2
1 − 𝑠𝑇⁄2 +
−
2!
3! + ⋯
Keeping only the first order terms yields the bilinear transform
𝑠𝑇
𝑒 ⁄2
𝑧≈
1 + 𝑠𝑇⁄2
2 (𝑧 − 1)
→𝑠≈
⁡𝑇𝑢𝑠𝑡𝑖𝑛′ 𝑠⁡𝑚𝑒𝑡ℎ𝑜𝑑
𝑠𝑇
(𝑧
𝑇
+
1)
1 − ⁄2
The discrete time transfer function can now be solved more easily. Substituting for s
results in
3
2
2 (𝑧 − 1)
2 (𝑧 − 1)
2 (𝑧 − 1)
𝑏1 (𝑇
) + 𝑏2 (𝑇
) + 𝑏3 (𝑇
) + 𝑏4
(𝑧 + 1)
(𝑧 + 1)
(𝑧 + 1)
𝐻(𝑧) =
.
3
2
2 (𝑧 − 1)
2 (𝑧 − 1)
2 (𝑧 − 1)
𝑎1 (𝑇
) + 𝑎2 (𝑇
) + 𝑎3 ( 𝑇
) + 𝑎4
(𝑧 + 1)
(𝑧 + 1)
(𝑧 + 1)
After considerable algebra, this equation can be simplified to
𝐻(𝑧) =
𝑏d1 + 𝑏d2 z −1 + 𝑏d3 z −2 + 𝑏d4 z −3
.
𝑎d1 + 𝑎d2 z −1 + 𝑎d3 z −2 + 𝑎d3 z −3
Given an s-plane transfer function [b,a], MATLAB performs this algebra for you with the
function
[bd,ad]=bilinear(b, a, fs).
One further complication occurs when the transfer function is changed from continuous
time to discrete time. The complication is made obvious by the location of the zeros for the lowpass filter. In the discrete time domain they are located at z=-1+j0 while in the continuous time
domain they are located at s→∞. The mapping of jω axis wraps around and around the unit
circle and in the limit of s→∞ it ends up at the same place as the frequency πfs. The result is that
the entire continuous frequency range from 0 to ∞ becomes compressed into the discrete
frequency range from 0 to πfs. A plot of the values of H(z) as z moves half way around the unit
circle will exactly match H(s) as s moves all the way up the y-axis to infinity. The effect of
compressing the frequencies into this finite range is called frequency warping. One can
compensate for frequency warping by moving the z-plane poles into a slightly new location that
forces H(z) to equal H(s) at one particular frequency, usually the cut-off frequency of the filter.
In MATLAB, the bilinear function call has a parameter that allows the introduction of “prewarping” as shown below
[bd,ad]=bilinear(b, a, fs, fc).
Writing difference equations from the discrete time transfer function
MATLAB will calculate the transfer function in the form
𝑏d1 + 𝑏d2 z −1 + 𝑏d3 z −2 + 𝑏d4 z −3 + ⋯
𝐻(𝑧) =
.
𝑎d1 + 𝑎d2 z −1 + 𝑎d3 z −2 + 𝑎d3 z −3 + ⋯
If one defines X as the input signal and Y as the output signal, the transfer function can be
written as H(z) = Y/X. If you cross multiply the transfer function, you get
⁡Y𝑎d1 + Y𝑎d2 z −1 + Y𝑎d3 z −2 + Y𝑎d3 z −3 + ⋯ = X𝑏d1 + X𝑏d2 z −1 + X𝑏d3 z −2 + X𝑏d4 z −3 + ⋯.
The term z-1 can be thought of as a unit delay, or “the previous sample”. With this
consideration, the transfer function can be written as
⁡Y(n)𝑎d1 + Y(n − 1)𝑎d2 + Y(n − 2)𝑎d3 + Y(n − 3)𝑎d3 + ⋯ =⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡
⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡X(n)𝑏d1 + X(n − 1)𝑏d2 + X(n − 2)𝑏d3 + X(n − 3)𝑏d4 + ⋯.
The output can be calculated from
Y(n) =
−Y(n − 1)𝑎d2 − Y(n − 2)𝑎d3 − ⋯ + ⁡X(n)𝑏d1 + X(n − 1)𝑏d2 + X(n − 2)𝑏d3 + ⋯
.
⁡𝑎d1
When MATLAB calculates the transfer function, it always makes the coefficient ad1=1. The
final difference equation is written as
Y(n) = −Y(n − 1)𝑎d2 − Y(n − 2)𝑎d3 − ⋯ + ⁡X(n)𝑏d1 + X(n − 1)𝑏d2 + X(n − 2)𝑏d3 + ⋯.