Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Introduction to Digital Signal

Processing (DSP)
Part 3 of 4 (“Implementing Discrete Filters”)

James R. (Jim) Beaty, PhD - NASA Langley Research Center


Vehicle Analysis Branch, Systems Analysis & Concepts Directorate
Bldg 1209, Room 128B, M/S 451
1 North Dryden Street
Hampton, VA 23681
757-864-1322
James.R.Beaty@nasa.gov

Learning from the Past, Looking to the Future Page: 1


Overview
• Part 1, “Mathematical Concepts”
– Definitions, mathematical pre-requisites, and application techniques
• Fourier transforms and convolution
• Discrete Fourier transforms (DFT) and Fast Fourier transforms (FFT)
• Shannon Sampling Theorem and data aliasing

• Part 2, “Power Spectral Density Basics”


– Power Spectral Density (PSD) to estimate spectral characteristics of waveforms
– PSD Demonstration
• Part 3 “Implementing Discrete Filters”
– Converting from continuous Laplace domain filters to discrete Z-domain filters (Tustin’s
rule, or bilinear transformation)
– Recursive filter implementations for discrete filters
• Part 4 “Typical GN&C Applications”
– Normalized frequency
– Methods to verify transfer function magnitude/phase of discrete filters using DFT (or FFT)
– Example use of discrete filter to implement a notch filter

Learning from the Past, Looking to the Future Page: 2


Implementing Continuous or
Discrete Filters
• Filters are used to condition signals, either to remove unwanted content at
certain frequencies or to change the stability characteristics of a control
loop
• Filters can be implemented as continuous filters (using Laplace transforms)
or discrete filters (using Z Transforms and recursive equations)
• Most modern control systems utilize digital controllers, so discrete filters
are more commonly used in the GN&C applications
– But, more often the specifications of filters or stability analyses of a control
loop will be performed in the continuous, or Laplace domain
– Thus, there is a need to convert between the two forms

Learning from the Past, Looking to the Future Page: 3


Laplace Transform to Z Transform
– Tustin’s rule
• Tustin’s rule (or bilinear transformation) can be used for converting from
continuous Laplace domain filters to discrete Z domain filters
• Laplace transform and its inverse are defined as:
 1   j
 2 j   j
 st
F (s)  e f (t ) dt & f (t )  e  st F ( s ) ds
0

• Z transform and its inverse are defined as:



1
X ( z)   xn z
n  
n
& xn 
2 j C
X ( z ) z n1 dz

where C denotes a counterclockwise closed path encircling the origin and entirely in
the function’s region of convergence, and containing all the poles of X(z)
• Z transform is a generalization of the discrete-time Fourier transform
Learning from the Past, Looking to the Future Page: 4
Derivation of Tustin’s Rule
• From the definitions of the Laplace transform and the Z transform, it can be shown
that the Laplace operator, s, is related to the Z transform operator, z, by
z = e sT or s = 1/T loge(z)
where T is the sample time of the discrete system, and loge is natural logarithm
• Now, since | z | < 1 (the domain of z is entirely within the unit circle around the
origin), the natural log of z can be approximated by
 z 1   1  z 1 
log e ( z )  2    2  1

 z  1   1 z 
• This results in the bilinear transformation, or Tustin’s rule, for substituting s for z-1
in a transfer function, G(s)

2  1  z 1 
s   
1 
T  1 z 

• Note also that z -1 = e -sT represents a delay of one sample of a discrete sequence at T
seconds per sample
Learning from the Past, Looking to the Future Page: 5
Implementation of Continuous
Filters
• Consider a Laplace domain transfer function, G(s), which defines a filter with a lead
frequency, wl, and a lag frequency, wn:

( s / wl  1 )
x( s )  G ( s )  y ( s ) or x( s )   y(s)
( s / wn  1 )

• To implement this filter in a time-domain simulation, first break the transfer


function into two parts, with the lag part first, introduce a temporary state, w, re-
arrange terms, substitute the time derivative for the Laplace operator, s, and solve
for the highest derivative of the output, y: E.G. –

1 ( s / wl  1 )
x( s )   w( s )   y(s)
( s / wn  1 ) 1

Learning from the Past, Looking to the Future Page: 6


First Order Continuous Domain
Filter Implementation
• To implement this transfer function, define a state, w, to be integrated, and compute
its derivative from eq (1), and then the filter output at any time is the value of, y,
from eq (2):
w 1
( s)   w( s ) ( s / wn  1 )  x( s )
x ( s / wn  1)
or
s w( s ) / wn  w( s )  x( s )  s w( s )  wn [ x( s )  w( s ) ]
or

w  wn ( x  w ) (1)
and
y ( s / wl  1 )
( s)   y ( s )  s w( s ) / wl  w( s )
w 1
or

y  ( w / wl  w ) ( 2)

Learning from the Past, Looking to the Future Page: 7


Second Order Continuous Domain
Filter Implementation
• Now, consider a 2nd order transfer function
• To implement this transfer function, define two states, y and w, to be integrated,
and compute their derivatives from eqs (1) & (2), and the filter output at any time is
the integrated value of the state, y
y 1
(s)   y ( s ) [ ( s / wn ) 2  2  s / wn  1]  x( s)
x ( s / wn )  ( 2  / wn ) s  1
2

or
s 2 y ( s ) / wn  (2  / wn ) s y ( s )  y ( s )  x( s )  s 2 y ( s )  wn [ x( s)  y ( s ) ]  2  wn s y ( s )
2 2

or
 
y  wn ( x  y )  2 wn y
2

now define w so that :



yw (1)
so

w  wn ( x  y )  2 wn w
2
( 2)

Learning from the Past, Looking to the Future Page: 8


Tustin’s Rule Implementation
• Now, to implement the previous first order lead/lag example filter as a discrete
filter, with an update rate of T seconds, substitute the Laplace operator, s, with the z-
transform operator, z , using Tustin’s rule:
( s / wl  1 )
G ( s) 
( s / wn  1)
2 ( 1  z 1 )
Now, with Tustin ' s rule , s 
T ( 1  z 1 )
 1  z 1 
2 2 2 2
  1 ( 1  z 1 )  ( 1  z 1) (1  )  (1  ) z 1
 1  z 1 
wl T wl T wl T wl T
 G( z)   
2  1  z 1  2
( 1  z 1 )  ( 1  z 1) (1 
2
)  (1 
2
) z 1
  1
wnT  1  z 1  wnT wnT wnT
or
( a0  a1 z 1 ) This is the recursive equation for implementing
G( z) 
( 1  b1 z 1 ) this discrete filter
where
wl T wl T  wT 
w  1 2 w  1 2  1 n 
a0   n  , a1    n  , b1   2 
 wl  1  w nT  wl  1  wnT w
 1  nT



2 2  2 

Learning from the Past, Looking to the Future Page: 9


Implementation of Discrete Filter
With Recursive Equation
• Now that we have defined the transfer function, G(z), to implement the discrete
form of the original Laplace filter, how do we implement it?
( a0  a1 z 1 )
xn  G ( z )  y n or xn   yn
(1  b1 z 1 )
or
yn (1  b1 z 1 )  xn ( a0  a1 z 1 )
• Since z -1 = e -sT, it is a delay of one sample, T, and the equation above can be re-
written in terms of current and previous samples of the input signal x[n], and the
output y[n]:
yn  a0 xn  z 1 ( a1 xn  b1 yn )
but , z 1 xn  xn 1 and z 1 yn  yn 1 , so that
yn  a0 xn  a1 xn 1  b1 yn 1 , n  1,2, ... , size ( x)
• Note that implementation of the discrete filter does not require integration of any
states, just algebraic equations, which are easier to implement in digital controllers

Learning from the Past, Looking to the Future Page: 10


Another Form for G(z)
 1 x / 2 
Note that for x  1, e  x   , so the b1 coefficient is often approximated by
 1  x / 2 
wT
1 n
b1  2  e wnT
wT
1 n
2
so that G ( z ) can also be written as :
( a0  a1 z 1)
G( z) 
1  e wnT z 1
also note that with the same approximations a0 and a1 can be written as
(1  e wn T )
a0  wl T
, and a1   a0 e wl T
(1  e )
so that G ( z ) can also be written as :
a0 (1  e wl T z 1)
G( z) 
1  e wnT z 1

Learning from the Past, Looking to the Future Page: 11


Recursive Equation for
Implementing Discrete Filters
• One general form† of recursive filter implementation is:
Y ( a0  a1 z 1  a2 z 2  a3 z 3  ...  a p z  p )
G( z)  ( z) 
X ( 1  b1 z 1  b2 z  2  b3 z 3  ...  br z  r )
which results in the recursive equation
yn  a0 xn  a1 xn 1  a2 xn  2  a3 xn 3 ...  a p xn  p
 b1 yn 1  b2 yn  2  b3 yn 3  b4 yn  4 ...  br yn  r

• The recursion coefficients a0, a1, ... and b1, b2, ... can be computed as described on the previous
slides using Tustin’s rule to convert a Laplace domain transfer function, G(s), to its discrete Z-
transform form, G(z)
• In these recursive filter implementations, the filter update time interval is defined as T in the
bilinear transformation used to define the coefficients; z-1 represents the previous value of the
parameters x or y, z-2 represents the previous value of the previous value (e.g. – second oldest),
etc.
† Care should be exercised when someone gives you recursive filter coefficients, as some
people define the polarity of the denominator coefficients, bi, as positive rather than
negative as shown above, and sometimes the leading factor of 1 in the denominator is
instead another constant, b0
Learning from the Past, Looking to the Future Page: 12
Continuous Filters vs. Discrete
Filters
• Advantages of recursive filters versus continuous filters:
– can be implemented as “stand-alone” algebraic functions which do not require numerical
integration and its interface with the controlling process
• The most common causes of problems with discrete filters are:
– Denominator polynomial coefficient polarities are not as expected (see note on previous
page)
– Discrete filter is not defined in the “standard form” shown in the example:
• the filter coefficients are not defined so that the denominator leading coefficient is 1
• the polarities of the denominator coefficients are positive instead of negative
– Recursive filter is called, or updated, at a rate inconsistent with the time interval “T” used
to define the recursion coefficients with Tustin’s rule
• This results in a frequency shift of the filter characteristics, so that the actual natural
frequency of the discrete filter is different than intended
– Past states for input (xn, xn-1, ...) and output (yn, yn-1, ...) are not properly initialized or
saved from update to update of the recursive filter
– Confusion over converting from “real” frequency to normalized frequency
Learning from the Past, Looking to the Future Page: 13
Numerical Example of Discrete
Lead/Lag Filter
• Consider the lead/lag filter example previously shown to illustrate Tustin’s rule
( s / wl  1 )
x( s )   y( s)
( s / wn  1 )
• Now, assign some values to the lead, wl, and lag, wn, frequencies and the discrete sample
interval, T:
– Lead frequency, wl = 2 * 10 hertz = 62.8318 rad/sec
– Lag frequency, wn = 2 * 1 hertz = 6.28318 rad/sec
– Discrete filter sample interval, T = 0.01 seconds (100 hertz)
(1  e 2 1 ( 0.01) )
 a0   2 10 ( 0.01)
 0.130540 , and a1   a0 e  2 10 ( 0.01)   0.069642
(1  e )
so that G ( z ) can also be written as :
a0 (1  e wl T z 1) 0.130540  0.069642 z 1
G( z)  
1  e wnT z 1 1  0.939101 z 1

Learning from the Past, Looking to the Future Page: 14


Matlab / Octave Commands For
Example
% define lag frequency of 1 hz, lead frequency of 10 hz, sample frequency of 100 hz
fn = 1; , wn = 2*pi*fn;
fl = 10.; , wl = 2*pi*fl;
fs=100; , T=1/fs;
% compute the continuous frequency response of the specified lead/lag filter
[magc,phasec,freqc]=mybode( tf2sys( [1/wl 1] , [1/wn 1] ) , logspace(-3,2,1000) );
% compute the recursive filter coefficients
b1 = exp(-wn*T);
a0 = ( 1 - b1 ) / ( 1 - exp(-wl*T) );
a1 = -a0 * exp(-wl*T);
a = [a0 a1];
% define 4096 point impulse response function ( 1 for first sample; 0 for all others )
impulse = zeros(size([1:4096])); , impulse(1) = 1;
% define the 4096 point impulse response of the lead-lag filter
x=recursive_filter(impulse,a,b1);
% compute the frequency response of the impulse response of the filter with discrete Fourier transform
(with FFT implementation)
[mag,phase,freq]=dft_fft(x,4096);

Learning from the Past, Looking to the Future Page: 15


Frequency Response of Example
Lead/Lag Discrete Filter
Note frequency
range from DFT Note last frequency
is “zero” to 1 computed is 0.5

(frequency is normalized
to the sample frequency)

Learning from the Past, Looking to the Future Page: 16


Compare Frequency Response of
Digital & Continuous Lead/Lag Filter

semilogx(freqc/2/pi,magc,'-rx;continuous;')
hold on
semilogx(freq*fs,mag,'-bo;discrete;')

semilogx(freqc/2/pi,phasec,'-rx;continuous;')
hold on
semilogx(freq*fs,phase,'-bo;discrete;')

Learning from the Past, Looking to the Future Page: 17


Introduction to DSP, Part 3 of 4
Summary
• In Part 1, “Mathematical Concepts” we covered the following:
– Definitions, mathematical pre-requisites, and application techniques
• Fourier transforms and convolution
• Discrete Fourier transforms (DFT) and Fast Fourier transforms (FFT)
• Shannon Sampling Theorem and data aliasing

• In Part 2, “Power Spectral Density Basics”, we covered the following:


– Power Spectral Density (PSD) to estimate spectral characteristics of waveforms
– PSD Demonstration
• In Part 3 “Implementing Discrete Filters” we have covered the following:
– Converting from continuous Laplace domain filters to discrete Z-domain filters (Tustin’s
rule, or bilinear transformation)
– Recursive filter implementations for discrete filters
• In Part 4 “Typical GN&C Applications” we will cover the following:
– Normalized frequency
– Methods to verify transfer function magnitude/phase of discrete filters using DFT (or FFT)
– Example use of discrete filter to implement a notch filter

Learning from the Past, Looking to the Future Page: 18

You might also like