Practical Digital Signal Processing by Dogan Ibrahim
Practical Digital Signal Processing by Dogan Ibrahim
Practical Digital Signal Processing by Dogan Ibrahim
Microcontrollers
ACKNOWLEDGEMENTS
The following material is reproduced in this book with the kind permission of the
respective copyright holders and may not be reprinted, or reproduced in any way, without
their prior consent.
or
Figures A.1 A.11 are taken from Microchip Technology Inc. Data Sheet
PIC18(L)F2X/4XK22 (DS41412F). Figures 3.1 3.3, 3.9, 12.4 are taken from the web site
of Microchip Technology Inc.
kt
Figure 3.4 is taken from the web site of Custom Computer Services Inc.
El
e
Figures 3.6 3.8, 12.2, 12.4 are taken from the web site of mikroElektronica.
rig
ht
Figures 3.10 - 3.12, 14.1 14.3 are taken from the web site of Analog Devices Inc.
op
y
Figure 3.13 is taken from the web site of Texas Instruments Inc.
Figure 3.14 is taken from the web site of Freescale Semiconductors Inc.
PIC, PICSTART, and MPLAB are all trademarks of Microchip Technology Inc.
Contents
CHAPTER 1 1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
INTRODUCTION ........................................................................... 17
BACKGROUND ....................................................................................................... 17
A TYPICAL DSP APPLICATION ................................................................................... 18
THE ANTI-ALIASING ANALOG FILTER ........................................................................... 18
SAMPLE AND HOLD CIRCUIT ..................................................................................... 21
A/D CONVERTER ................................................................................................... 21
DSP ................................................................................................................... 24
D/A CONVERTER ................................................................................................... 24
EXERCISES ............................................................................................................ 25
CHAPTER 2 -
2.1
FIXED POINT NUMBERS ........................................................................................... 27
2.1.1
Decimal Number System............................................................................ 28
2.1.2
Binary Number System .............................................................................. 28
2.1.3
Octal Number System ................................................................................ 29
2.1.4
Hexadecimal Number System .................................................................... 29
2.1.5
Converting Binary Numbers into Decimal .................................................. 30
2.1.6
Converting Decimal Numbers into Binary .................................................. 31
2.1.7
Converting Binary Numbers into Hexadecimal ........................................... 33
2.1.8
Converting Hexadecimal Numbers into Binary ........................................... 34
2.1.9
Converting Hexadecimal Numbers into Decimal ........................................ 35
2.1.10 Converting Decimal Numbers into Hexadecimal ........................................ 36
2.1.11 Converting Octal Numbers into Decimal .................................................... 37
2.1.12 Converting Decimal Numbers into Octal .................................................... 38
2.1.13 Converting Octal Numbers into Binary ....................................................... 39
2.1.14 Converting Binary Numbers Into Octal....................................................... 40
2.1.15 Negative Numbers ..................................................................................... 40
2.1.16 Adding Binary Numbers ............................................................................. 42
2.1.17 Subtracting Binary Numbers ...................................................................... 43
2.1.18 Multiplication of Binary Numbers .............................................................. 43
2.1.19 Division of Binary Numbers ........................................................................ 45
2.1.20 Signed Magnitude Representation............................................................. 45
2.1.21 Offset Binary Representation ..................................................................... 46
4.2.7
Constants .................................................................................................. 95
4.2.8
Escape Sequences ...................................................................................... 96
4.2.9
Accessing Bits of a Variable ....................................................................... 96
4.2.10 sbit Type .................................................................................................... 97
4.2.11 bit Type ..................................................................................................... 97
4.2.12 Arrays ........................................................................................................ 97
4.2.13 Pointers ................................................................................................... 100
4.2.14 Structures ................................................................................................ 102
4.2.15 Operators in mikroC Pro for PIC ............................................................... 105
4.2.16 The Flow of Control ................................................................................. 109
4.3
FUNCTIONS IN MIKROC PRO FOR PIC ....................................................................... 118
4.3.1
void Functions ......................................................................................... 119
4.3.2
Passing Parameters to Functions ............................................................. 119
4.3.3
Passing Arrays to Functions ..................................................................... 120
4.3.4
Interrupt Processing ................................................................................ 121
4.4
MIKROC PRO FOR PIC BUILT-IN FUNCTIONS............................................................... 122
4.5
MIKROC PRO FOR PIC LIBRARIES ............................................................................. 123
4.6
USING THE MIKROC PRO FOR PIC COMPILER.............................................................. 124
4.6.1
mikroC Pro for PIC IDE ............................................................................. 125
4.6.2
Creating a New Source File ...................................................................... 127
4.6.3
Compiling the Source File......................................................................... 130
4.7
SUMMARY ......................................................................................................... 131
4.8
EXERCISES .......................................................................................................... 132
CHAPTER 5 -
5.1
GETTING STARTED ............................................................................................... 135
5.1.1
disp Command ........................................................................................ 137
5.1.2
format Command .................................................................................... 138
5.1.3
input Command....................................................................................... 139
5.1.4
Comment lines......................................................................................... 139
5.1.5
Programming in MATLAB ........................................................................ 139
5.1.6
The Working Environment ....................................................................... 149
5.1.7
Vectors in MATLAB .................................................................................. 149
5.1.8
Matrices in MATLAB ................................................................................ 154
5.1.9
Graphics Plotting Using MATLAB ............................................................. 157
5.2
SUMMARY ......................................................................................................... 170
5.3
EXERCISES .......................................................................................................... 170
CHAPTER 6 -
6.1
THE SAMPLING PROCESS ....................................................................................... 173
6.2
SOME DIGITAL SIGNAL TYPES.................................................................................. 174
6.2.1
Unit Step Function ................................................................................... 174
6.2.2
Unit Impulse Function.............................................................................. 175
6.2.3
Sinusoidal Signal...................................................................................... 175
6.2.4
Exponential Signal ................................................................................... 177
6.2.5
Complex Exponential Signal ..................................................................... 177
6.2.6
Exponential Sinusoidal Decay Signal ........................................................ 178
6.3
OPERATIONS ON DISCRETE TIME SIGNALS.................................................................. 179
6.3.1
Delay or Shift ........................................................................................... 179
6.3.2
Vector Addition and Subtraction .............................................................. 181
6.3.3
Scalar Addition and Subtraction .............................................................. 182
6.3.4
Scalar Multiplication and Division ............................................................ 182
6.3.5
Vector Multiplication and Division ........................................................... 183
6.4
BLOCK DIAGRAM REPRESENTATION.......................................................................... 184
6.4.1
Block Diagram Manipulation ................................................................... 184
6.5
DISCRETE SYSTEM CLASSIFICATION ........................................................................... 186
6.5.1
Linear and Non-linear .............................................................................. 186
6.5.2
Time-variant and Time-invariant ............................................................. 186
6.5.3
Causal and Non-causal ............................................................................ 186
6.5.4
Stable and Unstable ................................................................................ 187
6.5.5
Linear Time-invariant Systems ................................................................. 187
6.6
THE CONVOLUTION PROCESS.................................................................................. 187
6.7
EXERCISES .......................................................................................................... 192
CHAPTER 7 -
7.1
UNIT STEP FUNCTION ........................................................................................... 195
7.2
UNIT RAMP FUNCTION.......................................................................................... 196
7.3
EXPONENTIAL FUNCTION ....................................................................................... 197
7.4
SINE FUNCTION ................................................................................................... 198
7.5
DISCRETE IMPULSE FUNCTION................................................................................. 199
7.6
TABLES OF Z-TRANSFORMS .................................................................................... 199
7.7
THE Z-TRANSFORM OF A FUNCTION EXPRESSED AS A LAPLACE TRANSFORM ....................... 200
7.8
PROPERTIES OF THE Z-TRANSFORM .......................................................................... 201
7.9
INVERSE Z-TRANSFORMS ....................................................................................... 204
7.9.1
Coefficients of Partial Fraction Expansion ................................................ 211
7.10
CHAPTER 8 8.1
8.2
8.3
8.4
CHAPTER 9 9.1
9.2
CHAPTER 10 -
10
APPENDIX A
APPENDIX B
APPENDIX C
C.1
THE PIC18F45K22 MICROCONTROLLER .................................................................. 377
C.2
PIC18F45K22 ARCHITECTURE............................................................................... 378
C.2.1
The Program Memory ............................................................................. 380
C.2.2
The Data Memory ................................................................................... 381
C.2.3
Oscillator Configurations ......................................................................... 382
C.2.4
The Reset................................................................................................. 382
C.2.5
Parallel I/O Ports ..................................................................................... 383
C.2.6
Timer Modules ........................................................................................ 384
C.2.7
Analog-to-Digital Converter Module ........................................................ 387
C.2.8
Interrupts ................................................................................................ 391
APPENDIX D
APPENDIX E
APPENDIX F
APPENDIX G
APPENDIX H
APPENDIX I
11
PREFACE
Digital Signal Processing (DSP) is the process of capturing, analysing, and manipulation of
an analog signal by a digital processor, e.g. a digital computer.
The theory of DSP is quite complex and requires good understanding of high level of
mathematics. Students new to DSP are usually taught the theory in great detail with very
little or no practical applications. For example, in many cases a student can derive complex
equations for digital filters, but is unable to implement a digital filter in real life. Some
institutions use tools such as MATLAB to derive the coefficients of digital filters and then
to simulate the behaviour of these filters on a PC. Although simulation can be an invaluable
tool in teaching, it is never the same as real-time and real-life implementations.
rig
ht
El
e
kt
or
The aim of this book is to teach the basic principles of DSP and to introduce DSP from a
practical point of view using minimum of mathematics. Only the basic level of theory is
given just enough to implement DSP applications in real-time. The emphasis of the book is
on practical aspects of DSP, such as design issues and real-time implementation issues. The
practical implementation is described using the widely available low-cost general purpose
microcontrollers so that the readers can very easily and quickly design and implement DSP
applications in real-time. The architectures of dedicated DSP processors are complex and
this adds additional difficulty to students who may want to implement DSP applications
using such tools.
op
y
In this book the popular and highly powerful PIC18F45K22 microcontroller is used as the
processor to implement DSP applications in real-time. Using this microcontroller the
readers should be able to implement DSP applications with sampling frequencies within the
audio range. Programming is done using the popular and powerful mikroC PRO for PIC,
which is a C language and compiler developed specifically for the PIC family of
microcontrollers. The DSP projects given in the book are based on using the EasyPIC 7
microcontroller development kit, although any other development kit could also be used.
The DSP development environment used in the book consists of:
This book is primarily intended for students and practising engineers who may want to
learn the practical implementation of DSPs, such as digital filters in real-time.
13
or
El
e
kt
Chapter 6 provides the basic principles of discrete time systems required for the
understanding of DSP systems in general.
rig
ht
Chapter 7 is about the z-transforms and covers the principles of this important topic,
including the inverse transformations.
op
y
Chapter 8 is an introduction to the important topic of Discrete Fourier Transforms and gives
examples of finding the transform of some common functions.
Chapter 9 is about the features of digital filters and provides a comparison of various types
of filters used in practice.
Chapter 10 provides the theory of designing FIR digital filters. The design of low-pass and
high-pass filters is given with examples. In addition, the ScopeFIR digital filter design
software package is described with examples.
Chapter 11 provides the theory of designing IIR digital filters. The design of Butterworth
and Chebyshev type digital filters are given with examples. In addition, the ScopeIIR
digital filter design software package is described with examples.
Chapter 12 is about the implementation of FIR digital filters using general purpose
microcontrollers. Various tested and working example projects are given in this chapter,
including the implementation of several different windowed designs.
Chapter 13 is about the implementation of IIR digital filters using general purpose
microcontrollers. Various tested and working examples are given in this chapter, including
the implementation of Butterworth and Chebyshev type filters.
14
op
y
rig
ht
El
e
kt
or
15
op
y
Figure 6-14
rig
ht
El
e
kt
or
184
2.
[()] = [()] = []
[] = [] ()
=0
If the initial conditions are all zero, i.e. x(i) = 0, i = 0,1,2,.,m-1, then,
[] ( )
=0
kt
[ ] =
or
El
e
3.
[( + )] = []
If the initial conditions are all zero, i.e. x(i) = 0, i = 0,1,2,.,m-1, then,
rig
ht
Attenuation property
Suppose that the z-transform of x[n] is X[z], then,
op
y
4.
[( )] = []
[ ()] = [ ]
5.
6.
202
rig
ht
Figure 10-2
El
e
kt
or
op
y
10.2
As we have seen in Example 10.1, the impulse response of an ideal filter has
infinite number of coefficients and is not realizable. A finite length filter is obtained
from the ideal response by setting all its coefficients to zero outside the range of
M n M , thus giving a filter with a length of N = 2 M + 1 . If we then
249
b0 + b1 z 1 + b2 z 2
H ( z) =
1 + a1 z 1 + a 2 z 2
(13.1)
Figure 13-4
El
e
kt
or
Using equation (13.1), the second order section is implemented as shown in Figure
13-4, requiring 2 delays, 5 multiplications, and 2 summations.
G (b0 + b1 z 1 + b2 z 2 )
1 + a1 z 1 + a 2 z 2
op
y
H ( z) =
rig
ht
In the second method, the transfer function is written as in equation (13.2) below.
(13.2)
H ( z) =
G (1 + b1 z 1 + b2 z 2 )
1 + a1 z 1 + a 2 z 2
(13.3)
Figure 13-5 shows a second order filter implemented using the second method. The
ScopeIIR digital filter design program gives the filter parameters as shown in Figure
13-5.
325
Index
Index
Butterworth, 19, 271, 365
Butterworth Filters, 273
Butterworth Polynomials, 275
8
8-Bit Mode, 384
C language, 87
Cascade FIR, 269
Cascade Implementation, 324
Cascade Structure, 290
Causal, 186
CCS C compiler, 88
Character Arrays, 99
Character Constants, 95
Chebyshev, 19, 271, 371
Chebyshev Filters, 282
Code Assistant, 125
Code Composer, 86
Code Editor Window, 125
Code Explorer Window, 126
Code Template, 125, 126
Comment lines, 139
Comments, 90
Complex Exponential Signal, 177
Connector locations, 350
const, 95
Constant Strings, 100
Constants, 95
Converting Binary, 30, 33, 40
Converting Decimal, 31, 36, 38
Converting Hexadecimal, 34, 35
Converting Octal, 37, 39
Convolution Process, 187
Cost and system complexity, 68
CrossCore, 85
kt
El
e
rig
ht
op
y
or
D
D/A Converter, 24
423
kt
424
or
El
e
rig
ht
op
y
Index
LED connections, 361
LEDs, 361
LF398, 21
Library Manager Window, 127
Linear, 186
Linear Time-invariant, 187
Low-Pass Filter, 295, 323, 332
G
General purpose processors, 66
goto Statement, 118
Graphics Plotting, 157
mantissa, 55
MATLAB, 135
matrix operations, 154
MCP4921, 299
Memory, 67
Message Window, 126
Microcontroller, 173, 377
Microcontrollers, 295, 323
mikroC PRO, 83, 84
MPLAB C18, 84
MSC8156ADS StarCore DSP, 82
MT090, 21
Multi-channel, 23
Multi-dimensional array, 99
Multiple order roots, 212
Multiplication of Binary Numbers, 43
El
e
kt
or
rig
ht
op
y
L
Laplace transform, 195, 200
N
Negative Numbers, 40
New project window, 128
Non-causal, 186
Non-linear, 186
non-recursive filters, 238
Normalizing, 51
Number Systems, 27
O
Octal equivalent, 38
Octal Number System, 29
425
Offset Binary, 46
Operators, 105
Arithmetic Operators, 105
Bitwise, 107
Conditional, 107
Logical, 107
Preprocessor, 108
Relational, 107
Oscillator Configurations, 382
random noise, 23
Rectangular Window, 250
Remez Exchange, 260
Repetition Statements, 113
Reserved names, 92
Reserved Names, 92
Reset, 382
op
y
Q
Q-Format, 47
quantization error, 23
426
kt
El
e
rig
ht
Parallel converters, 24
Parameter Assistant, 125
Passing Arrays, 120
Passing Parameters, 119
PCSGU250, 300
Periodic Signals, 217
PIC Compiler, 124
PIC IDE, 125
PIC Libraries, 123
PIC Program, 89
PIC variable types, 93
PIC18 Explorer Board, 69
PIC18F45K22, 295, 297
PIC18F45K22 Development Kit, 72
PIC18F4XK20 Starter Kit, 70
PICDEM 4 Demo Board, 71
PIC-USB-STK Development Board, 73
Pointer Arithmetic, 101
Pointers, 100
Power consumption, 68
Program Memory, 380
Project Hardware, 295
Project Manager Window, 127
or
Index
Vector Addition, 181
Vector arithmetic operators, 153
Vector division, 183
Vector multiplication, 183
Vector Subtraction, 181
Vectors in MATLAB, 149
Velleman PCSGU250, 295
VisualDSP++, 356
void, 90
void Functions, 119
Symphony Studio, 86
Syntax, 89
System Classification, 186
T
Time-invariant, 186
Timer Modules, 384
Time-variant, 186
TMS320C5416 DSP, 81
Transfer Function, 271
Truncation, 249
Types
Derived, 93
Fundamental types, 93
or
kt
Variable Names, 92
Variable Types, 93
El
e
rig
ht
op
y
z-Transform
Attentuation property, 202
Final value theorem, 202
Initial value theorem, 202
iztrans, 204
Left shift property, 202
Linearity property, 201
Right shift property, 202
Z-transform, 195
Z-transforms tables, 199
427