Download as pdf or txt
Download as pdf or txt
You are on page 1of 198

DIGITAL SIGNAL PROCESSING

WITH MATLAB® APPLICATIONS

As.dr.ing. Bujor PĂVĂLOIU

2007
Recenzenţi ştiinţici:
Prof.dr.ing. Paul Dan CRISTEA, membru corespondent al Academiei Române
Prof.dr.ing.Rodica Aurora TUDUCE
Contents

Contents

Contents .........................................................................................................................................i

Foreword.......................................................................................................................................v

1 Introduction to DSP ...................................................................................................................1


1.1 What is DSP? ......................................................................................................................1
1.2 Signal Processing ................................................................................................................1
1.3 Digital Signal Processing....................................................................................................2
1.4 Digital versus Analog .........................................................................................................3
1.5 Concluding Exercises..........................................................................................................3

2 MATLAB Basic Functions and Operators ................................................................................5


2.1 Environment........................................................................................................................6
2.1.1 The Command Window ...............................................................................................6
2.1.2 The Help Window ........................................................................................................7
2.1.3 The Command History Window ..................................................................................7
2.1.4 The Workspace Browser ..............................................................................................8
2.2 Working with Scalars..........................................................................................................8
2.3 Working with Matrices .....................................................................................................10
2.3.1 Matrix Arithmetic Operations ....................................................................................10
2.3.2 Special Matrices and Functions of Matrices ..............................................................12
2.3.3 Slash Operator............................................................................................................13
2.3.4 Array Operations........................................................................................................14
2.3.5 Sum, Transpose and Diag ..........................................................................................14
2.3.6 Subscripts ...................................................................................................................15
2.3.7 The Colon Operator....................................................................................................16
2.3.8 Plot Vectors or Matrices.............................................................................................18
2.3.9 Subplot .......................................................................................................................20
2.4 Script M-files ....................................................................................................................21
2.5 Flow Control .....................................................................................................................22
2.5.1 Relational Operators...................................................................................................22
2.5.2 Logical Operators.......................................................................................................23
2.5.3 Selection Statements ..................................................................................................24
2.5.4 Loop Statements.........................................................................................................25
2.6 Function M-files................................................................................................................26
2.7 Working with Files ...........................................................................................................27
2.7.1 Importing an Existing File to MATLAB ...................................................................27
2.7.2 Specialized I/O Functions ..........................................................................................27
2.8 Efficiency..........................................................................................................................28
2.9 Concluding Exercises........................................................................................................29

i
Digital Signal Processing with Matlab® Applications

3 Digital Signals......................................................................................................................... 31
3.1 Discrete-Time Signals ...................................................................................................... 32
3.2 Classification of Discrete-Time Signals........................................................................... 34
3.2.1 Real Signals and Complex Signals............................................................................ 34
3.2.2 Finite Signals and Infinite Signals............................................................................. 35
3.2.3 Periodic Signals and Aperiodic Signals..................................................................... 35
3.2.4 Symmetric Signals and Antisymmetric Signals ........................................................ 37
3.2.5 Deterministic Signals and Random Signals............................................................... 38
3.3 Basic Waveforms ............................................................................................................. 39
3.3.1 The Sampling Process – Time Vectors...................................................................... 39
3.3.2 Common Periodic Waveforms .................................................................................. 41
3.3.3 Common Aperiodic Waveforms................................................................................ 43
3.3.4 User-defined Waveforms........................................................................................... 43
3.4 Saving (Recording) a signal ............................................................................................. 48
3.5 Sampling Theorem ........................................................................................................... 49
3.6 Aliasing ............................................................................................................................ 52
3.7 Binary Representation ...................................................................................................... 54
3.7.1 Floating Point Representation ................................................................................... 54
3.7.2 Fixed Point Representation........................................................................................ 56
3.8 Sample & Hold, Quantization .......................................................................................... 58
3.9 Concluding Exercises ....................................................................................................... 59

4 The Discrete Fourier Transform.............................................................................................. 61


4.1 Discrete Fourier Transform – DFT .................................................................................. 61
4.1.2. DFT Frequency Representation................................................................................ 67
4.1.3 DFT Symmetry .......................................................................................................... 67
4.1.4 DFT Linearity ............................................................................................................ 69
4.1.5 DFT Magnitude ......................................................................................................... 70
4.1.6 DFT Shifting.............................................................................................................. 71
4.2 The Fast Fourier Transform – FFT................................................................................... 71
4.4 Concluding Exercises ....................................................................................................... 73

5 Convolution............................................................................................................................. 75
5.1 Linear Convolution........................................................................................................... 75
5.2 Circular Convolution ........................................................................................................ 76
5.3 Convolution Properties..................................................................................................... 78
5.3.1 Commutative Property............................................................................................... 78
5.3.2 Associative Property.................................................................................................. 79
5.3.3 Distributive Property ................................................................................................. 79
5.3.4 Linear Convolution – Circular Convolution Equivalence......................................... 80
5.3.5 Time-Frequency Correspondence.............................................................................. 80
5.4 Fast Convolution .............................................................................................................. 81
5.4.1 FFT Convolution ....................................................................................................... 81
5.4.2 The Overlap-Save Method......................................................................................... 83

ii
Contents

5.4.3 The Overlap-Add Method ..........................................................................................84


5.4.4 Fast Convolution Exploits..........................................................................................86
5.5 Concluding Exercises........................................................................................................87

6 Digital Filters ...........................................................................................................................88


6.1 Digital Filter Properties.....................................................................................................88
6.1.1 Digital Systems ..........................................................................................................88
6.1.2 Types Of Digital Filters .............................................................................................93
6.1.3 Filter Characteristics ..................................................................................................93
6.2 FIR - Finite Impulse Response Filters ..............................................................................98
6.2.1 Time Domain Filtering...............................................................................................99
6.2.2 Window Method.......................................................................................................102
6.2.3 Frequency-Sampling Method...................................................................................115
6.2.4 Least-Square Linear-Phase Method .........................................................................117
6.2.5 Parks-McClellan Optimal Equiriple Method ...........................................................118
6.3 IIR - Infinite Impulse Response Filters...........................................................................119
6.3.1 The Bilinear Transform............................................................................................120
6.3.2 Butterworth Filters ...................................................................................................121
6.3.3 Chebyshev Filters.....................................................................................................125
6.3.4 Elliptic Filters...........................................................................................................128
6.3.5 Bessel Filters ............................................................................................................129
6.4 Filter Design and Analysis..............................................................................................130
6.4.1. General Steps for Filter Design...............................................................................130
6.4.2 Characteristics of FIR and IIR filters .......................................................................130
6.4.3 Filter Structure (Realization)....................................................................................131
6.4.5 Filter Design Functions in MATLAB ......................................................................135
6.5 Special Topics.................................................................................................................136
6.5.1 Nonlinear Filtering ...................................................................................................136
6.5.2 Deconvolution ..........................................................................................................137
6.5.3 Digital Restrictions...................................................................................................138
6.6 Concluding Exercises......................................................................................................139

7 Audio Signal Processing, Audio Effects, Noise Reduction...................................................142


7.1 Digital Audio ..................................................................................................................142
7.2 Audio Effects ..................................................................................................................148
7.2.1 Silence ......................................................................................................................148
7.2.2 Volume settings........................................................................................................148
7.2.3 Balance.....................................................................................................................148
7.2.4 Fading In ..................................................................................................................149
7.2.5 Fading Out................................................................................................................149
7.2.6 Echo..........................................................................................................................149
7.2.7 Reverberation ...........................................................................................................149
7.2.8 DC Blocker...............................................................................................................150
7.2.11 Equalizer ................................................................................................................150
7.3 Noise Reduction..............................................................................................................150
7.4 Concluding Exercises......................................................................................................153

iii
Digital Signal Processing with Matlab® Applications

8 Modulation and Encoding ..................................................................................................... 155


8.1 Analog Modulation......................................................................................................... 155
8.1.1 Modulation-Demodulation ...................................................................................... 155
8.1.2 Amplitude Modulation-AM..................................................................................... 156
8.1.3 Frequency Modulation-FM...................................................................................... 158
8.1.4 Phase Modulation-PM ............................................................................................. 158
8.2 Discrete Modulation ....................................................................................................... 160
8.2.1 Pulse-Code Modulation - PCM ............................................................................... 160
8.2.2 Differential Pulse-Code Modulation - DPCM......................................................... 161
8.2.3 Delta modulation - DM............................................................................................ 163
8.3 Concluding Exercises ..................................................................................................... 164

9 Data Compression ................................................................................................................. 166


9.1 Lossless Compression .................................................................................................... 166
9.1.1 Packing .................................................................................................................... 166
9.1.2 Huffman Encoding .................................................................................................. 169
9.1.3 Run Length Encoding.............................................................................................. 175
9.1.4 Lempel-Ziv coding .................................................................................................. 176
9.2 Lossy Compression ........................................................................................................ 178
9.2.1 Downsampling......................................................................................................... 178
9.2.2 Downquantization.................................................................................................... 179
9.2.3 Transform Compression .......................................................................................... 179
9.3 Concluding Exercises ..................................................................................................... 180

10 Real-Time Digital Signal Processing .................................................................................. 182


10.1 Real-Time Constrains................................................................................................... 182
10.2 The DAQ Toolbox........................................................................................................ 183
10.2.1 Data Input .............................................................................................................. 183
10.2.2 Data Output............................................................................................................ 186
10.3 Fast Algorithms ............................................................................................................ 187
10.3.1 Fast Fourier Transform.......................................................................................... 187
10.3.2 Fast Matrix Multiplication..................................................................................... 188
10.4 Concluding Exercises ................................................................................................... 189

Glossary.................................................................................................................................... 191

References ................................................................................................................................ 203

iv
Foreword

Foreword

This book is the expression of the author’s conducting Digital Signal Processing laboratory
activities. It is devised primarily as an exercise book for the students in the third year of study at the
Electrical Engineering and Computer Science Division of the Faculty of Engineering in Foreign
Languages from University “Politehnica” of Bucharest, but it is also a support material for any
scholar interested in taking the first steps in the DSP domain.
The author has assumed that the readers have the basic knowledge about mathematics,
electrical engineering, electronics and programming, although he tried to express the knowledge in
an independent and self contained form. He also has presumed that the readers have access to a
computer with MATLAB® installed on it. For a large part of the book the Student Version can be
used and the author encourage the reader to write his own code for the needed functions, but for the
special parts, toolboxes like Signal Processing, Data Acquision and casually Communications,
Symbolic Math and Filter Design toolboxes.
The book has ten chapters, as well as a glossary and a references part. After the opening
chapter 1 Introduction to DSP, a short introduction to the MATLAB is made in 2 MATLAB Basic
Functions and Operators. The chapters that follow describe basic features of signals and their
representation and properties, both in time and in frequency domain: 3 Digital Signals, 4 The
Discrete Fourier Transform and 5 Convolution. A generous chapter is reserved to digital filtering
in 6 Digital Filters. The chapters that follow describe applications ans special topics of DSP: 7
Audio Signal Processing, Audio Effects, Noise Reduction, 8 Modulation and Encoding, 9 Data
Compression and 10 Real-Time Digital Signal Processing.
Each chapter ends with a Concluding Exercises part, where the students will have to answer
multiple choice questions, simple queries, homework-type exercises and will run into project-type
problems.
The author is thankful to his professors for their advices and to his students for the discussions
and questions that evolved in the laboratory hours.

v
Introduction to DSP

1 Introduction to DSP

1.1 What is DSP?

DSP, or Digital Signal Processing, is the processing of signals using digital equipment and
methods. Even at if its origins, in electronics and electrical engineering, a signal meant an electrical
waveform carried by a wire, or later by a radio wave, we understand now by signal a stream of
information representing data that can be obtained from a large variety of sources, like stock market
indices progress, genome analysis, etc…
More generally, a signal is defined as any physical quantity that varies with time, space or any
other independent variable or variables. It can be described mathematically as a function of one or
more independent variables.
We can give many examples of such functions. In most of them, one value (amplitude) varies
in time.
Exercise 1.1 Let us have the signals:
x(t)=2ꞏt;
y(t)=sin(t)ꞏ2t-sign(t);
Describe signals x(t) and y(t). What do you consider to be the main difference?
For a grayscale picture, the color I(x,y) has to be defined for each pixel. If we have a color
picture, a 2 to 3 mapping has to be defined, associating to each pixel an RGB triple.
Exercise 1.2 Let us have an 8x8 pixels picture where the pixel with coordinates x an y has
the gray level:
I(x,y)=x2+y2;
If 0 is black, 1024 is white and the higher the level, the brighter the pixel, which one will be the
darkest pixel? What about the brightest one? How does the picture look like?
These signals are precisely defined by a functional relationship on the input variables. There
are many signals that cannot be described by such exact dependence relations. Almost all life-
related signals fall in this domain, such as speech, electrocardiogram and so on.

1.2 Signal Processing

Signals commonly need to be processed in a variety of ways. For example, the output signal
from a transducer may well be contaminated with unwanted electrical "noise". The electrodes
attached to a patient's chest when an ECG is taken measure tiny electrical voltage changes due to
the activity of the heart and other muscles. The signal is often strongly affected by "mains pickup"
due to electrical interference from the mains supply. Processing the signal using a filter circuit can
remove or at least reduce the unwanted part of the signal. Increasingly nowadays the filtering of
signals to improve signal quality or to extract important information is done by DSP techniques
rather than by analog electronics.
Most of the signals encountered in science and engineering are analog in nature. They are
usually functions of a continuous variable: time. Such signal can be processed directly using

1
Digital Signal Processing with Matlab® Applications

appropriate systems as analog filters, frequency analyzers, integrators and so on. We present in Fig
1.1 a direct analog processing of an analog signal.

Analog Analog
Input Output
Signal Analog Signal Signal
Processor

Fig. 1.1 Analog Signal Processing

Digital processing confers an alternative method for the analog processing of signals. In many
cases, the signal is initially in the form of an analog electrical voltage or current, produced for
example by a microphone or some other type of transducer. In some situations the data is already in
digital form - such as the output from a CD (compact disc) player.
An analog signal must be converted into digital (i.e. numerical) form before DSP techniques
can be applied. An analog electrical voltage signal, for example, can be digitized using an integrated
electronic circuit (IC) device called an analog-to-digital converter or ADC. This generates a digital
output in the form of a binary number whose value represents the electrical voltage input to the
device. This output is processed numerically and then is reconverted to the analog form with a
digital-to-analog converter DAC. Thus the signal is provided to the user in analog form, as in Fig.
1.2.

Analog Digital Input Digital Output Analog


Input Signal Signal Output
Signal A/D Digital D/A Signal
Converter Signal Processor Converter

Fig. 1.2 Digital Signal Processing

1.3 Digital Signal Processing

There is no sense in talking about Digital Signal Processing without the digital equipment to
run it, so we have to mention the development of the general-purpose microprocessors as a crucial
step in the evolution of DSP. The general-purpose microprocessors from the Intel x86 line can be
used for signal processing purposes (they are designed for broad-spectrum applications), but they
are not suited to do just massive simple numeric computations.
The Von Neumann general-purpose microprocessor architecture, where both the program and
the data are placed in a single memory and share the same address bus and the same data bus is not
fast enough for DSP requirements. Intensive manipulation of substantial data requires a special
memory for the data, with its own address bus and data bus. This microprocessor design is known
as Harvard architecture and being widely used in DSPs (DSP stands also for Digital Signal
Processor). If the requirements are higher, the DSPs can present application-specific hardware
accelerators. The most known DSPs producers like Analog Devices, Freescale and Texas
Instruments (in alphabetical order) make fortunes because of the mass integration of DSP chips in
almost all the appliances that surround us.

2
Introduction to DSP

The mathematics performed in DSPs is very simple, regular tasks like filtering consisting just
in multiplications and additions. The problem is to perform them very fast, at low production costs,
with little power consumption and with little space occupied by the hardware. This is the reason for
which other constraints as the flexibility, design time and development costs are overridden and
simpler electronic circuits are used, like field-programmable gate arrays (FPGA) or even
application-specific integrated circuits (ASIC).
Exercise 1.3 Give examples of general-purpose microprocessors, DSPs (you use them all
day long) and DSPs with application-specific hardware accelerators (some pay double for their
personal computer because of it).

1.4 Digital versus Analog

Digital Signal Processing has certain advantages over the analog processing:
- Flexibility – a DSP can be programmed to perform different operations. The simplest
example is the implementation of different filters, changing just the stored coefficients, instead of
designing a new circuit with new topology and new electronics for the analog counterpart.
- Easy and fast implementation – for the same reasons described above, if the hardware is
flexible enough, you have just to change the software or of pieces of it. You can design and test a
digital filter on any personal computer.
- Complexity – you can implement full applications containing intricate operations in the
same device, making it portable or even autonomous.
- Universality – you can perform any signal processing operation on digital, but there are
techniques that can be performed only in digital or infinitely easier than in analog, like linear
phase response for filtering, error correction for data transmission, data compression, storage,
etc…
- Reliability – the performance of a DSP does not deteriorate slowly in time and is not
dependant of the environmental conditions (temperature in the first place)
- Scalability – because of the digital way of functioning, you will not have noise and errors
when you connect digital devices.
- Reproducibility – many digital devices of the same type will perform the same, in contrast
to the analog ones, subject to component differences and tolerances.
- Precision – digital works better than analog for low frequencies.
There are as well some disadvantages:
- Speed Limitation, it has a strong dependency upon the digital processor used and the
number of arithmetic operations that must be performed;
- Accuracy – DSPs are affected by finite word length effects;
- Bandwidth - the bandwidth of DSPs is limited by the sampling rate and the frequency and
power of the available DSP.

1.5 Concluding Exercises

Exercise 1.4 Which one of the following is not a signal:


a) Music b) Voltage drop c) Tenet d) Movie

3
Digital Signal Processing with Matlab® Applications

Exercise 1.5 DSP stands for


a) Digital Signal b) Digital Signal c) Both of them d) None of them
Processing Processor
Exercise 1.6 Select the most complex IC from the following:
a) Digital Signal b) General Purpose c) FPGA d) ASIC
Processor Processor
Exercise 1.7 Which one of the following statements is true:
a) DSP is faster than b) DSP is slower than c) DSP and analog d) DSP and analog
analog processing analog processing processing are processing have
equivalent concepts approximately the
same speed
Exercise 1.8 Which one is more reliable:
a) Analog processing b) Digital processing c) Hybrid processing d) None of them
Exercise 1.9 A Compact Disk Player reads data in:
a) Analog format b) Digital format c) Hybrid format d) None of them
Exercise 1.10 Compared with the Von Neumann architecture, the Harward architecture is:
a) Faster b) Slower c) Equivalent as speed d) Simpler

4
2. MATLAB Basic Functions and Operators

2 MATLAB Basic Functions and Operators


MATLAB is a high-level interactive environment for scientifical and technical computing. It
combines programming, computation and visualization to offer solutions rich of theoretical insight,
expressed in familiar mathematical notation.
The name MATLAB comes from MATrix LABoratory. The “working element” is the matrix –
you can make directly matrix operations and you don’t need declarations, memory allocation or
dimensioning. You can work with matrices also at element level, making avoidable the use of
cycles.
MATLAB is composed by the central interpreter "MATLAB.exe", additional library files and
source files with the extension ".m". You can do basic mathematical operations, linear algebra,
matrix calculations, string manipulations, Fourier transforms, graphical representations.
MATLAB has also packages, called toolboxes, dedicated to specific applications, like signal
processing, symbolic math, neural networks, image processing, wavelet analysis, spline functions,
system identification, optimization, linear and nonlinear control systems, etc... Toolboxes are
comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment
to solve particular classes of problems. During this lab, you are required to use functions from the
MATLAB core, as well as from the Signal Processing Toolbox. Because any toolbox contains only
*.m files, you can write your own toolboxes or improve the existing ones writing new functions.
MATLAB allows the writing of time critical functions or the use of existing ones in one of the
programming languages: "C", "FORTRAN" or “JAVA”. The MATLAB Compiler converts
MATLAB code to applications and libraries that can run on systems that do not have MATLAB
installed.
Simulink, a companion program to MATLAB, is an interactive system for simulating nonlinear
dynamic systems. It is a graphical mouse-driven program that allows you to model a system by
drawing a block diagram on the screen and manipulating it dynamically. It can work with linear,
nonlinear, continuous-time, discrete-time, multivariable, and multirate systems.
Blocksets are add-ins to Simulink that provide additional libraries of blocks for specialized
applications like communications, signal processing, and power systems.
Real-time Workshop is a program that allows you to generate C code from your block
diagrams and to run it on a variety of real-time systems.
To differentiate the command line instructions, content of MATLAB files, and MATLAB
functions mentioned elsewhere, we will use the following conventions:
- >> courier font - represents command line instructions and their effect.
- courier font - represents the content of MATLAB script or function files.
- courier bold font – represents function names defined for the firs time
- times bold italic font- represents important terms, notes and filenames.
The sections regarding Exercises, Examples, Hints and Remarks will be emphasized for an
easier detection.

5
Digital Signal Processing with Matlab® Applications

2.1 Environment

There are several windows that will be available for use when you run MATLAB. The ones we
will use the most are: the command window, the help window, the command history window and
the workspace browser. They can be accessed in the Desktop tab and can be docked and undocked
in the general frame.

2.1.1 The Command Window

Fig. 2.1 The Command Window

This is the window where you actually type commands. We have a prompt, “>>”, where we
will write the instructions and then hit the “Enter” key. MATLAB will execute what we have typed
or will give an error message. If the line represents an operation not ended with a semicolon, the
result of the sentence will be displayed on the screen; if there is no assignment made, the general
variable ans will be created and the result of the expression will be assigned to it.
Example 2.1 If we type in the command window:
>> 1+1

the variable ans will be created, it will take the value 2 and it will be displayed in the command
window:
ans =
2

The command prompt can be used also for shell commands like dir, cd, pwd, mkdir, etc… or
commands belonging to the native operating system, preceded by the exclamation mark ‘!’. If the
sign ‘&’ is added at the end of the instruction, the output will appear in a separate window which
will remain opened after the command completes.
Example 2.2 Obtain the DOS command prompt by typing
>> !cmd

We will get in the command window the text:

6
2. MATLAB Basic Functions and Operators

Microsoft Windows XP [Version 5.1.2600]


(C) Copyright 1985-2001 Microsoft Corp.
D:\Program Files\MATLAB\R2006b\work>

Exercise 2.1 Observe that we have a different prompt now (it belongs to DOS). Return to
MATLAB prompt.
Exercise 2.2 Open a DOS session in a separate window. Close it.

2.1.2 The Help Window


The help window, shown in Fig. 2.2, offers direct access to online xml/html documentation and
can be accessed clicking the Help/Matlab Help or Desktop/Help Menus, pressing the F1 key or
typing in the command line >> helpbrowser

Fig. 2.2 The Help Window

2.1.3 The Command History Window


This window shows a list of all the commands we have typed during the current session. If we
want to redo a command, instead of retyping it, we can just double click on the command in the
Command History Window. It can be obtained by clicking the Desktop/Command History Menu or
typing in the command line >> commandhistory.

Fig. 2.3 The Command History Window

7
Digital Signal Processing with Matlab® Applications

2.1.4 The Workspace Browser


In computer programming, information needs to be stored in memory. We do this by defining
variables. MATLAB stores these variables in a part of computer memory called the workspace.
The workspace browser allows us to view what MATLAB is storing there. In the above example,
we defined the no variable, so the result of the result of the operation 1+1 was stored in the general
variable ans.

Fig. 2.4 The Workspace Browser Window

Example 2.3 We will compute the result of 1+2+3 and assign it to the variable a. If the
assignment is followed by a semicolon, no result will be seen in the command window, but we can
see the value of the variable a just invoking it, without semicolon at the end of the line.
>> a=1+2+3;
>> a

a =
6

To see the variables existent in the workspace type who or whos (long form).
To remove a variable varName from the workspace, type in the command window:
>> clear varName

To erase everything in the workspace, type:


>> clear all

Example 2.4 To erase only the variable “a” from the example above, type in the command
window:
>> clear a;

Exercise 2.3 Verify with the command whos and looking in the workspace browser the fact
that the variable a has been removed. Do you know other method to show this?

2.2 Working with Scalars

We can use the command line to write MATLAB code and find quickly the results of simple
mathematical functions applied on scalars.
Example 2.5 To find out how much sine(5) is, we can type:
>> sin(5)

8
2. MATLAB Basic Functions and Operators

We will get
>> ans =
-0.9589

Remark: The argument of trigonometric functions is expressed in radians. We have that


3
5 , so the result is near -1.
2
We can chain mathematical operations and group them using parentheses.
sin( 5)  sin(0)  sin(5)
Example 2.6 To find out how much , type:
cos(5)  cos(0)  cos(5)
>> (sin(-5)+sin(0)+sin(5))/(cos(-5)+cos(0)+cos(5))
ans =
0

MATLAB has predefined values for  and the complex imaginary unit, denoted with i or j.
Example 2.7 Typing in the command line pi for  and i, j for the complex imaginary unit,
we have:
>> pi
ans =
3.1416
>> i
ans =
0 + 1.0000i
>> j
ans =
0 + 1.0000i

Exercise 2.4 Find out the values for sine(2), sine(0), cosine(/3), tangent(/2), 1111, 101101,
10011001. Try to explain the last result.
Exercise 2.5 Find out the value of sine (). Why don’t we have the value 0?
Exercise 2.6 Find out the values of e2, ejπ,(1+j)/(1-j),
The numbers which are too large to be represented as floating-point values or the result of
operations like division by 0 are denoted by Inf. The result of operations that have unidentified
numerical results is NaN.
Example 2.8 Let’s see what happens when we divide 1 by 0:
>> 1/0
Warning: Divide by zero.
ans =
Inf

Remark: If you don’t want to get warning messages, type in the command window
>> warning off

Exercise 2.7 Find out the values of 0/0, 10001000, ∞-1, ∞+∞, ∞-∞, ∞/∞, ∞/0, 0/∞,1∞, 0.1∞,

(-1)
MATLAB provides many "standard" functions that we will use over and over again. Some
examples include:

9
Digital Signal Processing with Matlab® Applications

Function Meaning
sin(a) Returns the sine of a.
cos(a) Returns the cosine of a.
tan(a) Returns the tangent of a.
asin(a) Returns the inverse sine of a.
acos(a) Returns the inverse cosine of a.
atan(a) Returns the inverse tangent of a.
sqrt(a) Returns the square root of a.
abs(a) Returns the magnitude of a, works also for complex numbers.
angle(a) Returns phase of complex a.
conj(a) Returns complex conjugate of complex a.
real(a) Returns complex real part of a.
imag(a) Returns complex imaginary part of a.
mod(a, b) Returns the remainder of dividing a by b.
round(a) Returns the nearest integer closest to a.
ceil(a) Returns the nearest integer to a that is closest to positive
infinity.
floor(a) Returns the nearest integer to a that is closest to negative
infinity.
fix(a) Returns the nearest integer to a that is closest to zero.
exp(a) Returns ea.
log(a) Returns the natural logarithm of a.
log2(a) Returns the logarithm to base 2 of a.
log10(a) Returns the logarithm to base 10 of a.

Example 2.9 To see a plain list of the implemented elementary mathematical functions and
of their short description, type in the command line:
>> help elfun

2.3 Working with Matrices

2.3.1 Matrix Arithmetic Operations


Matrices are defined on lines, using semicolon to separate the lines. The square brackets are
considered the concatenation operators, so a matrix is defined inside its brackets.
1 2 3 4
Example 2.10 We will define the matrix A   
5 6 7 8
>> A=[1 2 3 4 ; 5 6 7 8]

gives
A =
1 2 3 4
5 6 7 8

10
2. MATLAB Basic Functions and Operators

Remark: It is a good practice to name variables that accommodate matrices starting with a
capital letter, in contrast with scalars and one-dimensional arrays. When necessary, use suggestive
names as myArray or MyMatrix.
Concatenation can be used to enlarge a matrix. Coma or space is used for concatenation on
columns and semicolon for concatenation on lines.
Example 2.11 We will add to A a line consisting of zeros and hereafter a column of ones to
the new matrix and create a matrix B
>> B=[A; [[0 0; 0 0] [[1; 3] [2; 4]]]]

gives
B =
1 2 3 4
5 6 7 8
0 0 1 2
0 0 3 4

Matrices can be added/ subtracted or multiplied/ divided with scalars:


Example 2.12 To create a matrix C equals with A plus 1, type.
>> C=A+1

and obtain:
C =
2 3 4 5
6 7 8 9

Exercise 2.8 Compute A-2, Aꞏ3, A/4.


Two variables that represent matrices can be added, subtracted, multiplied or divided, if the
operation is possible (depending on their sizes).
1
0 
Example 2.13 We will determine D, the result of the multiplication of A with  
0 
 
0 
>> D=A*[1; 0; 0; 0]

gives
D =
1
5

Exercise 2.9 Compute A+C, A-2ꞏC.


Example 2.14 If we want to compute E=B2-3ꞏB, we type
>> E=B^2-3*B

and we obtain

11
Digital Signal Processing with Matlab® Applications

E =
8 8 23 30
20 28 67 90
0 0 4 4
0 0 6 10

Exercise 2.10 Compute F= Bꞏ(B-3). Compare E with F. Why are they not equal?
MATLAB deals with matrices as its basic elements and almost all its operators and elementary
functions works on matrices, operating at element level, unless they are not operable directly on the
matrix.
Example 2.15 To find out the sines of the elements in the array [π/6 π/4 π/3 π/2 2π/3 3π/4
5π/6] type
>> angles=pi*[1/6 1/4 1/3 1/2 2/3 3/4 5/6];
>> sin(angles)
>> ans =
0.5000 0.7071 0.8660 1.0000 0.8660 0.7071 0.5000

Some functions need a sequence of numbers to work on

Function Meaning
difference(a) difference of successive elements in a (approximate derivative)
unwrap(a) unwraps phase angle of complex array a.

2.3.2 Special Matrices and Functions of Matrices


The function ones generate a matrix with all the elements equal to one.
ones(n) is an n-by-n matrix of ones.
ones(m,n) or ones([m,n]) is an m-by-n matrix of ones.
ones(size(a)) is a matrix of the same size as A, having all the elements equal to one.
Example 2.16 Let’s create a matrix with 2 lines and 3 columns, having all elements 1.
>> E=ones(2,3)
E =
1 1 1
1 1 1

The function zeros generates a matrix with all the elements equal to zero.
zeros(n) is an n-by-n matrix of zeros.
zeros(m,n) or zeros([m,n]) is an m-by-n matrix of zeros.
zeros(size(a)) is a matrix of the same size as A, having all the elements equal to zero.
Example 2.17 Let’s create a matrix with the same format as A, having all elements 0.
>> F=zeros(size(A))
F =
0 0 0 0
0 0 0 0

12
2. MATLAB Basic Functions and Operators

Exercise 2.11 Find out the function that creates the identity matrix and use it to assign the
identity matrix with 3x3 elements to the variable G.
Other important matrix functions we will use are:

Function Meaning
isempty(A) returns 1 if array A is void =[], 0 otherwise
size (A) returns the sizes of each dimension for array A
length(A) returns the size of the longest dimension for array A
numel(A) returns the number of elements in array A
find(A) returns the indices of nonzero elements in array A
all(A) returns 1 if all elements of A are nonzero (true), 0 otherwise
any(A) returns 1 if any element of A is nonzero (true), 0 otherwise
fliplr(A) returns array A flipped in left/right direction
flipud(A) returns array A flipped in up/down direction
reshape(A) returns an array with the elements of A, but with changed size
Exercise 2.12 Try some examples for these functions in the command line.

2.3.3 Slash Operator


\ Backslash or left division.

A\B is the matrix division of A into B, which is roughly the same as inv(A)*B , except it is
computed by Gaussian elimination. If A is an N-by-N matrix and B is a column vector with N
components, or a matrix whose columns have this form, then X = A\B is the solution to the
equation A*X = B. A warning message is displayed if A is badly scaled or nearly singular.
 x  2x2  5 1 2  x1   5 
Example 2.18 To solve the system  1 , equivalent with 3 4  x   11 , we’ll
3x1  4 x 2  11   2   
write:
>> A=[1 2; 3 4];
b=[5;11];
x=A\b

We will obtain
x =
1
2

that can be verified with >> bCheck=A*x.


Remark: Use left division (Gaussian elimination) to solve systems, not inv(A)b, which is
slower and less precise.
Example 2.19 The inverse of a matrix A, that can be find with inv(A) or A^(-1), can be
computed also with A\eye(size(A)).
For the previous a, we’ll type
AInv1=inv(A)
AInv2=A^(-1)
AInv3=A\eye(size(A))

13
Digital Signal Processing with Matlab® Applications

Exercise 2.13 Type


all(all(AInv1==AInv2))
all(all(AInv1==AInv3))

What happens? Why?


/ Slash or right division.

B/A is the matrix division of A into B, which is roughly the same as B*INV(A) , except it is
computed in a different way. More precisely, B/A = (A'\B')'.

2.3.4 Array Operations


MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are
defined by the rules of linear algebra. Array arithmetic operations are carried out element-by-
element. The period character (.) distinguishes the array operations from the matrix operations.
However, since the matrix and array operations are the same for addition and subtraction, the
character pairs .+ and .– are not used.
Remark: For array operations the operands have to have the same size.
Example 2.20 For the old matrices A and C, A.*C will give:
>> A.*C
ans =
2 6 12 20
30 42 56 72

Remark: The array multiplication is symmetric (i.e. A.*B=B.*A)

2.3.5 Sum, Transpose and Diag


For vectors, sum(x) is the sum of the elements of x. For matrices, sum(X) is a row vector
with the sum over each column of matrix X. For N-dimensional arrays, sum(X) operates along the
first non-singleton dimension.
Example 2.21 For the matrix A defined before,
>> sum(A)
ans =
6 8 10 12

Example 2.22 To find the sum of all elements in A, we can use:


>> sum(sum(A))
ans =
36

Example 2.23 To find the transposed of a matrix, the operator ‘ or the function transp has to
be used.
>> H=A'
H =
1 5
2 6
3 7
4 8

14
2. MATLAB Basic Functions and Operators

Example 2.24 If you want to have an array with the sums on lines of elements of matrix a,
type:
>> sum(A')'
ans =
10
26

The sum of the elements on the main diagonal can be obtained with the help of the diag
function, which picks off that diagonal.
Example 2.25 The function diag will extract the first diagonal from the matrix B:
>> diag(B)
ans =
1
6
1
4

and now we can find the sum of the elements on the first diagonal of B:
>> sum(diag(B))
ans =
12

Exercise 2.14 Find out the dedicated function that returns the sum of elements on the first
diagonal of a matrix and make a comparison with the result for matrix B.
The second diagonal, so-called antidiagonal, is not so important mathematically, so MATLAB
does not have a ready-made function for it. But a function originally intended for use in graphics,
fliplr, flips a matrix from left to right.
Example 2.26 We can extract now the second diagonal:
>> diag(fliplr(B))
ans =
4
7
0
0

2.3.6 Subscripts
The element in row i and column j of A is denoted by A(i,j). For example, A(2,3) is the element
from the second row and third column of matrix A.
Example 2.27 To find how much A(2,3) is, just type
>> A(2,3)
ans =
7

It is also possible to refer to the elements of a matrix A with a single subscript, A(k). This is
the usual way of referencing row and column vectors. But it can also apply to a fully two-
dimensional matrix, in which case the array is regarded as one long column vector formed from the
columns of the original matrix.

15
Digital Signal Processing with Matlab® Applications

Example 2.28 To find how much the 6th “columnwise” element of A is (and because A is a
4  4 matrix, this is just A(2,3) ), just type
>> A(6)
ans =
7

Example 2.29 If we try to access an element outside of the matrix, we obtain an error:
>> A(3,3)
??? Index exceeds matrix dimensions.

On the other hand, if we store a value in an element outside of the matrix, the size increases to
accommodate the newcomer:
Example 2.30 If we want to increase the size of A to 5  5 and to make the last element to be
, just type:
>> A(5,5)=pi
A =
1.0000 2.0000 3.0000 4.0000 0
5.0000 6.0000 7.0000 8.0000 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 3.1416

2.3.7 The Colon Operator


The colon, :, is one of MATLAB's most important operators. It occurs in several different
forms.
Example 2.31 The expression
>> 1:10

is a row vector containing the integers from 1 to 10


1 2 3 4 5 6 7 8 9 10

Example 2.32 To obtain not unitary spacing, specify an increment. For example
>> 100:-7:50

is
100 93 86 79 72 65 58 51

and
>> 0:pi/4:pi

is
0 0.7854 1.5708 2.3562 3.1416

Example 2.33 Subscript expressions involving colons refer to portions of a matrix.


>> k=3; j=4; A(1:k,j)

is represented by the first k elements (from the first k lines)of the jth column of A.

16
2. MATLAB Basic Functions and Operators

ans =
4
8
0

We can extract a submatrix or an array (line or column) from a given matrix;


>> I=A(2:3,[3:5 1])
I =
7 8 0 5
0 0 0 0

The colon by itself refers to all the elements in a row or column of a matrix and the keyword
end refers to the last row or column, so sum(A(:,end)) computes the sum of the elements in the
last column of a.
Example 2.34 Example:
>> sum(A(:,end))
ans =

3.1416

Exercise 2.15 Create a vector of the even integer numbers between 23 and 175.
Exercise 2.16 Create a vector of the odd integer numbers between 23 and 175. The numbers
must not be multiples of 5.
Exercise 2.17 Create in a single instruction the matrix:
  2 0 2 4 6
A   5 4 3 2 1
 0 0 0 0 0

Exercise 2.18 Create in a single instruction the matrix:


1 0 0 4
2 2 1 4
A
3 4 0 4
 
4 6 1 4
Exercise 2.19 Create in a single instruction the matrix:
0 1 2 3 4 5 6 
A 
0 1 4 9 16 25 36
Example 2.35 To see a plain list of the implemented elementary matrix functions and of their
short description, type in the command line:
>> help elmat

17
Digital Signal Processing with Matlab® Applications

2.3.8 Plot Vectors or Matrices


MATLAB provides a variety of functions for displaying vector data as line plots, as well as
functions for annotating and printing these graphs. The following table summarizes the functions
that produce basic line plots. These functions differ in the way they scale the plot’s axes. Each
accepts input in the form of vectors or matrices and automatically scales the axes to accommodate
the data.
The plot function has different forms depending on the input arguments. For example, if x is a
vector, plot(x) produces a linear graph of the elements of x versus the index of the elements of x. If
we specify two vectors as arguments, plot(x,y) produces a graph of y versus x.
Example 2.36 For example, these statements create a vector of values in the range [0, 2] in
increments of /100 and then use this vector to evaluate the sine function over that range.
MATLAB plots the vector on the x-axis and the value of the sine function on the y-axis, as can be
seen n Fig. 2.5.
t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
axis([-0.5 2*pi+.5 -1.1 1.1])
grid on

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1

0 1 2 3 4 5 6

Fig. 2.5 Simple plot of a sinusoidal function

Function Meaning
plot Graph 2-D data with linear scales for both axes
plot3 Graph 3-D data with linear scales for both axes
loglog Graph with logarithmic scales for both axes
semilogx Graph with a logarithmic scale for the x-axis and a linear scale
for the y-axis
semilogy Graph with a logarithmic scale for the y-axis and a linear scale
for the x-axis
plotyy Graph with y-tick labels on the left and right side

18
2. MATLAB Basic Functions and Operators

Plot(x,y) plots vector x versus vector y. If x or y is a matrix, then the vector is plotted versus
the rows or columns of the matrix, whichever line up.
Plot(x) plots the columns of x versus their index.
If x is complex, plot(x) is equivalent to plot(real(x),imag(x)). In all other uses of
plot, the imaginary part is ignored.
It is possible to specify color, linestyle, and markers, such as plus signs or circles, with:
plot(x,y,'color_style_marker')
where color_style_marker is a 1-, 2-, or 3-character string (delineated by single quotation
marks) constructed from a color, a linestyle, and a marker type:
Color strings are 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'. These correspond to cyan, magenta, yellow,
red, green, blue, white, and black.
Linestyle strings are '-' for solid, '- -' for dashed, ':' for dotted, '-.' for dash-dot, and 'none' for no
line.
The most common marker types include '+', 'o', '*', and 'x'.
For example, plot(x, y,'c+') plots a cyan plus at each data point.
plot(x1,y1,s1,x2,y2,s2,x3,y3,s3,...) combines the plots defined by the (x,y,s) triples, where the
x's and y's are vectors or matrices and the s's are strings.
Example 2.37 For example, we will make three superposed plots, like in Fig. 2.6. The
functions a, b and c depend on the variable t.
>> t=-5:.1:5;
>> a=t.^2-t+1;
>> b=-.5*t.^3+1;
>> c=a.*b/50;
>> plot(t,a,'r:',t,b,'m-',t,c,'og')
>> xlabel('t')
>> ylabel('a, b, c - functions of t')

80

60

40
a, b, c - functions of t

20

-20

-40

-60

-80
-5 0 5
t

Fig. 2.6 Plot of three functions on the same graph

19
Digital Signal Processing with Matlab® Applications

Exercise 2.20 Plot a sine function y=sin(t) for values of t between 0 and 4π;
Exercise 2.21 Plot three sines functions with the phase difference of 2/3 for values of t
between 0 and 4π;

2.3.9 Subplot
The function subplot creates axes in tiled positions.
It is usually called with subplot(m,n,p) and breaks the figure window into an m-by-n
matrix of small axes, selects the pth axes for the current plot, returning the axis handle. The axes are
counted along the top row of the figure window, then the second row, etc.
Example 2.38 The following code
>> t=-5:.1:5;
>> a=t.^2-t+1;
>> subplot(2,1,1);
>> plot(t,a,'r:')
>> subplot(2,1,2);

plots the next figure (observe that the second position is only prepared for drawing)

40

30

20

10

0
-5 0 5

0.8

0.6

0.4

0.2

0
0 0.2 0.4 0.6 0.8 1

Fig. 2.7 Two subplots in the same figure

The command subplot(m,n,p), if the axis already exists, makes it current, similar with
subplot(h), where h is an axis handle (the general method to make an existing axis the current
one for subsequent plotting commands).
If a subplot specification causes a new axis to overlap an existing axis, the existing axis is
deleted.
Example 2.39 A subplot statement is normally followed by a plot command in the
designated axis. We will plot in Fig. 2.8 a period for a sine and a cosine function.
t=0:.1:2*pi; x1=sin(t);
subplot(2,1,1), plot(t,x1,'r.')
axis tight, grid on
xlabel('Time'), ylabel('Amplitude for Sine')

20
2. MATLAB Basic Functions and Operators

x2=cos(t);
subplot(2,1,2), plot(t,x2,'b.')
axis tight, grid on
xlabel('Time'), ylabel('Amplitude for Cosine')

Amplitude for Sine


0.5

-0.5

0 1 2 3 4 5 6
Time

1
Amplitude for Cosine

0.5

-0.5

0 1 2 3 4 5 6
Time

Fig. 2.8 Sine and cosine representation

Exercise 2.22 Obtain the graphs 2*2 of a cosine, sine, tangent and cotangent function for
values between π/16 and 7π/16;

2.4 Script M-files

A script file is an external file that contains a sequence of MATLAB statements. By typing the
filename, subsequent MATLAB input is obtained from the file. Script files have a filename
extension of ".m" and are often called "M-files".
Example 2.40 Write in a file
a=10;
b=a/2+5;
b

and save it as test1.m. Type in the MATLAB command window


>> test1

We will obtain
>> b =
10

To execute such sources, it is not sufficient only to have the m-file. MATLAB will have to
know where to take the file from. If you encounter an error of the type
>> Undefined function or variable one2one

after you have called the file one2one.m, it is very probable that you are not in the directory
where the file is located and this directory is not present in the saved path (you may have also
mistyped the name of the file).

21
Digital Signal Processing with Matlab® Applications

Exercise 2.23 Create a sub-director (folder) with your name in the work directory and go
there with "cd" (change directory).
Save the sources you write from now on in this directory, eventually creating and using
subdirectories for each chapter. From this reason, we will end writing in the command line and we
will not have the command promt >> included in the source code from now on.
In MATLAB 5.x or higher you can use the Path Browser to add your directory to the path. For
MATLAB 4.x you’ll have to edit the matlabrc.m file.
Remark Take care also at the case of the letters. MATLAB is case sensitive and for both
variables and functions, they have to be called with the proper name.
Exercise 2.24 Write a script to find which number is bigger: 9889 or 8998.
Exercise 2.25 Create a vector x with the elements, xn = (-1)n+1/(2n-1), with n=1:100. What is
the sum of the elements of this vector?

2.5 Flow Control

Flow control (implementing semantically the algorithm in a given programming language) is


done in MATLAB using logical/relational constructs, selection constructs, and a variety of looping
constructs. Because of indexing and other matrix level operations in MATLAB, it is a good practice
to use it as rare as possible.

2.5.1 Relational Operators


The relational operators in MATLAB are:
Table 2.1 Relational Operators
Description Ope Short Functional
rator Form form
less than < x1< x2 lt(x1, x2)
greater than > x1>x2 gt(x1, x2)
less than or equal <= x1<=x2 le(x1, x2)
greater than or equal >= x1>=x2 ge(x1, x2)
equal == x1==x2 eq(x1, x2)
not equal. ˜= x1~=x2 ne(x1, x2)
The relational operators are working on arrays of the same size, operating at correspondent
element level or they are working on an array and a scalar.
A relation is actually the scalar 1 or 0 depending on whether the relation is true or false (you
should think of 1 as true and 0 as false).
Example 2.41 We will give some examples of the relational operators use:
3 < 5 % the truth value of 3 being less than 5 (true)
pi==3.1415 % the truth value of pi being equal with 3.1415 (false)
[1 2 ; 3 4]>ones(2) % the truth value of [1>1 2>1;3>1 4>1]
[1 2 ; 3 4]>1 % it is the same as above, with a scalar

We obtain:

22
2. MATLAB Basic Functions and Operators

ans =
1
ans =
0
ans =
0 1
1 1
ans =
0 1
1 1

2.5.2 Logical Operators


We have seen that the relational operators return the scalars 0 and 1 that represent the truth
values of the relation implied. We can work directly with logical operators, 0 representing the False
value and anything else the True value.
Table 2.2 Logical Operators Truth Table
Inputs/ x1 x2 not and or xor
Functions
Short Form ~x1 x1 & x2 x1 | x2

Funct. Form not(x1) and(x1, x2) or(x1, x2) xor(x1 ,x2 )

0 0 1 0 0 0
0 1 1 0 1 1
Values
1 0 0 0 1 1
1 1 0 1 1 0
We have also the logical operators/functions any and all , where any (A) is true if any
element of the array A is nonzero (true) and all (A) is true if all elements of the array A are
nonzero (true)
Example 2.42 We will give some examples of logic operators. For
1 & 2
[-1: 1] & 1
all(-2:2)
any(2==[0:4])

We have that:
ans =
1
ans =
1 0 1
ans =
0
ans =
1

23
Digital Signal Processing with Matlab® Applications

2.5.3 Selection Statements


Selection statements allow us to make programs that selectively execute pieces of code.
Selection is made using two types of statements that are typical for most programming languages -
the if statement and the switch statement
The general syntax of the if statement is
if expression_1
statements_block_1
elseif expression_2
statements_block_2
……
else
statements_block_last
end
The statements are executed if the real part of the expression has all non-zero elements. The
else and elseif parts are optional. We can use zero, one or more elseif parts.
Example 2.43 We will write some code that will determine the situation of a student after an
exam.
passing_grade = 5; minimum_grade = 1;
average_grade = 8; maximum_grade = 10;
grade = input('What grade do you have? ');
if grade < minimum_grade,
disp('There is something wrong with your grade!');
elseif grade < passing_grade,
disp('You failed the exam!');
elseif grade < average_grade,
disp('You passed the exam!');
elseif grade <= maximum_grade,
disp('You passed the exam very well!');
else
disp('There is something wrong with your grade!');
end;

Switch statements are used when there are a relatively large number of alternatives for a single
condition. The syntax is as follows:
switch switch_expr
case {case_expr_1_1,case_expr_1_2,case_expr_1_3,...}
statements_block_1
case {case_expr_2_1,case_expr_2_2,case_expr_2_3,...}
statements_block_2
……
otherwise
statements_block_last
end

24
2. MATLAB Basic Functions and Operators

Example 2.44 We will use the switch statement for the problem from the previous example.
grade = input('What grade do you have? ');
switch grade
case {1,2,3,4}
disp('You failed the exam!');
case {5,6,7}
disp('You passed the exam!');
case {8,9,10}
disp('You passed the exam very well!');
otherwise
disp('There is something wrong with your grade!');
end;

2.5.4 Loop Statements


Loops are constructs that allow us to execute a sequence of statements repeatedly. There are
two basic types of loops: the while loop and the for loop
The syntax for the while loops is:
while expression
statements_block
end
If expression is true (non-zero), the statements_block will be executed. Once MATLAB gets to
the end statement, it returns to the evaluation of expression. If the expression is true, then the code
block will be repeated again, until the expression is false. In that case, MATLAB skips the
statements_block and executes the first statement after the end keyword.
Example 2.45 Let us compute the sum of integer positive numbers smaller than a requested
input n using a while cycle
n=input('We will compute the sum of positive integer numbers up to n
= ');
index=1;
s=0;
while index<=n,
s=s+index;
index=index+1;
end;
disp(['The sum is ' num2str(s)])

The syntax for the for loop is:


for expression
statements_block
end
The expression has the form index=vector, where index is the loop index and vector is the
vector from where index takes its values one by one.
The vector is given usually in the colon form, like first : increment : last, or with the default
increment 1, like in first : last.

25
Digital Signal Processing with Matlab® Applications

Example 2.46 We will repeat the problem from Example 2.45 using a for cycle.
n=input('We will compute the sum of positive integer numbers up to n
= ');
s=0;
for index =1:n
s=s+index;
end
disp(['The sum is ' num2str(s)])

The break statement can be used to terminate a loop prematurely and the continue statement
for the execution thread to go directly to the next execution of the loop.
Exercise 2.26 Write three versions of code that will display the first nonprime integers larger
than 1000, ending when a prime number is encountered. Use different loop control statements.
Remark: MATLAB uses i and j as the names of two functions that return the complex
imaginary unit  1 . Using these variable names in loops, you will override that functions, so you
are advised to use variable names like index, indexLine, indexSample, etc…

2.6 Function M-files

New functions may be added to MATLAB's vocabulary if they are expressed in terms of other
existing functions. The commands and functions that comprise the new function must be put in a
file whose name defines the name of the new function, with a filename extension of '.m'. At the top
of the file must be a line that contains the syntax definition for the new function.
Example 2.47 The existence on disk of a file called sincos.m with the content:
function [s,c] = sincos(A,freq,theta,t)
s=A*sin(2*pi*freq*t+theta);
c=A*cos(2*pi*freq*t+theta);

defines a new function called sincos that calculates the values of sine and cosine functions with
the parameters: amplitude A, frequency freq, initial phase theta for the time values contained in the
vector t (time vector).
To obtain the values of sine and cosine for values of t in {0, 0.2, 0.4, 0.6, 0.8, 1}, we just write
>> [a,b]=sincos(1,2,0,0:0.2:1)

and obtain
a =
0 0.5878 -0.9511 0.9511 -0.5878 -0.0000
b =
1.0000 -0.8090 0.3090 0.3090 -0.8090 1.0000

The variables within the body of the function are all local variables.
Exercise 2.27 Write down a function a=hypo(b,c) that will compute the length of the
hypotenuse a of a right triangle given the lengths of the sides b an c.
Exercise 2.28 Write a MATLAB function with the name matMax.m that finds the maximum
(largest value) of a matrix and the location of this maximum. The input is the matrix A, and the
outputs will be maxValue: largest entry in A and rMax, cMax: integers specifying the (row,column)
location of the max value.

26
2. MATLAB Basic Functions and Operators

2.7 Working with Files

2.7.1 Importing an Existing File to MATLAB


There are several ways to import a file (usually data file) to MATLAB. Depending on the file
size, the method of file import can be chosen among those discussed below to fit your needs.
- Enter data explicitly in MATLAB command window. This method is practical only for a
small list of data.
- Create an M-file containing your data in explicit format. This is similar to the first method
except that with this method you may edit the content of the data file. Upon the completion of
data file editing, you may call this M-file in the MATLAB session.
- Create an M-file to handle the import of an existing file by using the load command. This
is particularly useful when you have a large file or a repeated files to be processed using
MATLAB. Example 1 bellows illustrates the use of this method.
- Use fopen, fread, and other MATLAB's low-level I/O functions to import or extract
existing external data files. This method is useful when data are imported from application with
established file formats. File permission settings are allowed in this file import method.
- Create a file in MAT-file format from a program such as FORTRAN or C and read this
MAT-file into MATLAB using the load command.
- Use of MEX-file to read the data. This is useful when subroutines are already available to
import files from an external application.
Example 2.48 In this example, we’ll use MATLAB to save and load a waveform in ASCII
format. We want to plot the amplitude of the waveform vs. time.
t=0:.1:2*pi; %create time vector
s=sin(t); %create sine waveform
save sineValues.dat t s -ASCII % save in ASCII form
clear all % clear the workspace
whos
A=load ('sineValues.dat') % load the data in matrix A
t=A(1,:); % the first line is the first variable
s=A(2,:); % the second line is the second variable
plot(t,s) % plot the data

2.7.2 Specialized I/O Functions


There are several methods to export your work in MATLAB to another format for use in other
applications. Like in the importing methods, depending on the size of the exported information, you
may choose from the following methods for exporting your MATLAB results:
For small size of results generated from MATLAB such as computed matrices, you may use
the diary command to generate a diary file. You may use a text editor to further manipulate this
diary file.
Use the save command with -ASCII option to create a file (user-specified filename) in
ASCII format, as was done in Example 2.48
Use save command to write out data as MAT-file in user-defined format then use a
programming language of your choice, e.g., FORTRAN or C, to extract information from MAT-
file.

27
Digital Signal Processing with Matlab® Applications

Use the fopen, fclose, fprintf, fwrite and other general or low-level I/O functions to write your
computed results to a file in a user-defined format.
Example 2.49 The following code will save the contents of the sineValues.dat text file in the
binary file sineValues.bin.
fid1 = fopen('sineValues.dat','r'); % open text data to read
fid2 = fopen('sineValues.bin','w','l'); % open binary file to write.
a = fscanf(fid1,'%f\n'); % reading in ASCII data

fwrite(fid2, a, 'float'); % writing binary data as float


fclose(fid1); % close source file
fclose(fid2); % close destination file

2.8 Efficiency

From DSP and especially real-time DSP perspective it is very important to establish
comparison criteria for the algorithms performance. Since the count of the floating point operations
needed to perform an algorithm is no longer practical (function flops, obsolete since MATLAB
6), we will consider as term of comparison for the efficiency of various methods the time required
to perform them.
You can do this using:
- a stopwatch timer
- the current time
You can start a stopwatch timer with the command tic, perform some operations and get the
elapsed time with the command toc.
An alternative is to get the current time and store the value, perform the operations and get
again the current time, from which you subtract the stored value. The current time can be obtained
with cputime (time since MATLAB session started) or clock (current date and time), that needs
conversion with etime.
Example 2.50 Let us do some computations and compare the measured times.
tic;
t2Start=cputime;
t3Start=clock;
x=1000;
for i=2:10^5,
x=sqrt(sin(x)+1);
end;
t1=toc;
t2=cputime-t2Start;
t3=etime(clock,t3Start);
format long
disp([t1 t2 t3])

After one run, you can obtain something like:


>> 0.349036260195081 0.350504000000001 0.341000000000001

Exercise 2.29 Explain why the values are different.

28
2. MATLAB Basic Functions and Operators

2.9 Concluding Exercises


Exercise 2.30 What will do the command plot(a,b), where a and b are one dimensional
vectors.
a) draw a line b) draw a c) draw two d) nothing e) it depends
polyline. polylines
1 2 3
Exercise 2.31 Find out the elements on the main diagonal of the matrix 4 5 6
7 8 9

Exercise 2.32 Write a function to return the solutions of the equation zn-a=0, with n,a  N.
Exercise 2.33 Write a function to plot the solutions of the equation zn-a=0, with n,a  N.
Exercise 2.34 Write a function to find the first 10000 prime numbers. Find out the computer
time needed for the run. Compare the results with the ones from your colleagues.
Exercise 2.35 Define an array x with 100 elements, where xn+1=sqrt(xn+1), x1=100. Why are
the last elements equal? Are they really equal?
Exercise 2.36 How much is the determinant of
0 3 6 6 
 2  5 1  10
A 
 2 3  4 6 
 
1 4 5 8 
Why?
Exercise 2.37 Solve the equation x5+2ꞏx4-3ꞏx2+x+1=0
Exercise 2.38 Solve the equation x50+2ꞏx4-3ꞏx2+x+1=0
Exercise 2.39 Solve the system
 x yz 3

 x  y  z  1
 x  3x  2 y  0

Exercise 2.40 Find out the time required to compute the first 1000 elements of the array x,
with x n  sin( x n 1  1) and x1  1;

Exercise 2.41 Plot the graph of 2ꞏsin(x)+3ꞏcos(x/2+2)-sin(x3) for x in [0 10]. Which are the
max and the min of these values?
Exercise 2.42 Determine the eigenvalues for the matrix
1 2 3
A  3 4 5
1 2 2

Exercise 2.43 Determine the polynomial y of degree 5 that fits the points
x(1 2 3 4 5 6 7) , y(2 3 1 4 5 6 7);

29
Digital Signal Processing with Matlab® Applications

Exercise 2.44 Compare the execution times for solving a linear system of degree 1000 using
x=A\b and x=inv(A)*b. Explain the difference.
Exercise 2.45 Find out the truth value of the sentence
>>100^100==100^100+100^90. Explain.
Exercise 2.46 Write five recursive functions

30
3 Signals, Sampling Theorem

3 Digital Signals
A signal x(t) is a continuous-time signal if t is a continuous variable and it is called a discrete-
time signal if t is a discrete variable. A continuous-time signal is also called an analog signal, but a
discrete-time signal has to satisfy another condition, regarding the fact that its values are digitized,
in order to be called a digital signal.
DSP deals with digital signals which in most cases originate from analog ones. This is done in
a process called analog to digital conversion (ADC ), where we have as input an analog signal and
as output binary numbers (quantized values) given at certain time instances. The time between
samples is called the sampling period, and the speed of conversion is called sampling rate or
sampling frequency. The number of bits used to represent a sample is called the number of bits per
sample.

Analog A/D Converter Digital


Signal Signal
Antialising Sample and Digitizer
Filter Hold

Fig.3.1 Basic blocks for Analog to Digital Conversion

The sampling frequency and the number of bits per sample are chosen according to technical,
economical and standardization restrictions. The technical requirements refer to the quantitative
qualitative and quantitative minimum that have to be fulfilled by the system and they require that
the sampling frequency to be larger than the Niquist rate and the number of bits per sample
(resolution of the system) to be high enough in order to insure a satisfactory precision. The
economic restrictions claim to choose an inexpensive solution with the lowest acceptable values for
the sample rate and the number of bits per sample and the standardization enforces some typical
values for them.
The sampling rate and the number of bits per sample depend on the type of application. Current
ranges for the sampling rate are
- 1-100 Hz for control systems,
- 100-1000 Hz for biomedical applications
- 8 KHz-48 KHz for audio applications,
- Several MHz to GHz for communication systems.
The data that is delivered by an ADC, like in Fig. 3.1 is represented in fixed point, so the
number of values that can be represent and used later is 2N for a representation with N bits per
sample. Typical values for N are 8, 12, 16 and 24 bps. Because the dynamic range for fixed point
wordlength is rather small (from −8388608 to +8388607 for 24 bps) and results in large errors,
floating point representation can be used later.
Digital Signals and the parameters of the systems that work on them are stored in digital
systems and are thus represented by a finite number of bits. This leads to differences from the
desired results and the real ones, prone to finite precision effects. These can be categorized in the
following classes:

31
Digital Signal Processing with Matlab® Applications

1. Quantization errors, with the main representatives:


- signal quantization
- parameter (e.g. filter coefficients) quantization
2. Arithmetic errors
- truncation noise
- overflow
This kind of errors is especially dangerous for systems with feedback (like IIR filters), where
they add at each loop.

3.1 Discrete-Time Signals

Some books use the notation x[n] to emphasize the discrete nature of the digital signal. We will
use x(n), for the sake of simplicity and compatibility with MATLAB notations, the difference to the
continuous signals being given by the use of arguments classical for integers: n, m, k, l, instead of
the time t. We will not use the arguments i as j as variables, reserving them for the imaginary part of
complex numbers.
A discrete time signal x(n) is a real function of an independent variable that is an integer. The
signal is not defined for non-integers values of n. This makes very simple the representation and
processing of the signal using the computer.
A signal can be defined by a rule that generates its samples or just by the enumeration of its
samples value (for finite signals).
Example 3.1 When you represent a continuous signal in MATLAB, you have to be careful at
the way you plot it. If you have the signal x(n)=n2 for n[-3,3], you can represent the signal with
plot:
n=-3:3; x=n.^2;
plot(n,x);
xlabel('n'), ylabel('x(n)=n^2')
title ('Parabola representation')

Parabola representation
9

5
x(n)=n2

0
-3 -2 -1 0 1 2 3
n

Fig. 3.2 Parabola plot with few points

32
3 Signals, Sampling Theorem

This representation, as it can be seen in Fig. 3.2, fails to show the continuity of the first
derivative of the signal (it has “corners")
Example 3.2 It is better to increase the number of points in the domain, in order to obtain a
better representation of the signal.
n=-3:.1:3; x=n.^2;
plot(n,x)
xlabel('n'), ylabel('x(n)=n^2')
title ('Parabola representation, more samples')

Parabola representation, more samples


9

5
x(n)=n2

0
-3 -2 -1 0 1 2 3
n

Fig. 3.3 Parabola plot with many points

Example 3.3 If the signal x(n)=n2 is defined just for the integer values of n,
n  {3,  2,  1, 0, 1, 2, 3} , it is wrong to use the function plot without parameters for representation
(this will represent “the envelope” of the signal). We will use the function stem to plot the discrete
signal in Fig. 3.4. We suggest using anytime the axis function for a better view of the signal.
n=-3:3; x=n.^2;
stem(n,x); axis([-4,4,0,10]);

Discrete parabola representation


10

6
x(n)=n2

0
-4 -3 -2 -1 0 1 2 3 4
n

Fig. 3.4 Stem representation of discrete parabola

33
Digital Signal Processing with Matlab® Applications

You can use also plot with style markers, as was seen in the MATLAB introduction chapter.
Exercise 3.1 Plot the values of x with small circles “mounted” on the envelope. The result
should look like Fig. 3.5.
Discrete parabola representation
10

6
x(n)=n2

0
-4 -3 -2 -1 0 1 2 3 4
n

Fig. 3.5 Discrete points and anvelope representation

Remark: From now on, we’ll deal with discrete signals, so all the signals we mention will be
implicitely discrete, unless they are not specified otherwise. The correct plot of them is made with
stem or with a marker that show the discreteness of the signal. Each time a digital signal is plotted
with a continuos line, it is made to emphasize the form of the envelope.
Exercise 3.2 Represent the signal x(n)=cos(n) for n  {3,  2,  1, 0, 1, 2, 3}
Exercise 3.3 Represent the signal x(n)=cos(n) for n  [3, 3]

3.2 Classification of Discrete-Time Signals

3.2.1 Real Signals and Complex Signals


A sequence composed of real numbers is a real signal and a signal whose elements are complex
numbers is a complex signal. The signals we will deal with from now on are real signals, if not
specified otherwise.
For a complex signal x(n) : D  C, D  N we can define:
- The Magnitude of x is given by:

x(k )  Re( x(k )) 2  Im( x(k )) 2  abs ( x(k )), k  D (3.1)

- The Phase of x is given by:


 Im( x(k )) 
Phase( x(k ))  arctan , k  D (3.2)
 Re( x(k )) 

34
3 Signals, Sampling Theorem

If the representation of a real signal is straightforward, for the depiction of complex signals,
one can:
- plot the sequence in the complex plane
- plot the real and the imaginary part separately
- plot the magnitude and phase separately
Exercise 3.4 Write a function reprComplex(x) that will represent the complex sequence x
in the complex plane. The points will be connected.
Exercise 3.5 Write a function reprRealImag(x) that will represent the complex sequence
x as two subplots: in the upper one it will be represented the real part function of index and in the
lower one the imaginary part versus index.
Exercise 3.6 Write a function reprMagPhase(x) that will represent the complex sequence
x as two subplots: in the upper one it will be represented the magnitude dependence of index and in
the lower one the phase dependence of index.

3.2.2 Finite Signals and Infinite Signals


If the sequence defining the discrete signal x is finite (has a finite number of terms), the signal
is finite, otherwise the signal is infinite. All the digital signals are finite (because of storage
constraints). If the signal is finite, we usually denote with N its number of elements.

3.2.3 Periodic Signals and Aperiodic Signals


A signal x(n) is periodic with period T ( T   * ) if and only if
x(n+T)=x(n) (3.3)
for all n where x(n) and x(n+T) are defined.
If there is no value of T that satisfies (3.1), the signal is called nonperidic or aperiodic.
The smallest of T for which (3.1) holds is called the fundamental period.
Because of the finite length of the signal that can be stored in a computer, we can always
extend them through periodicity, obtaining periodic signals. A MATLAB trick to do this will be
presented in the following.
Example 3.4 Let’s say we have a sequence of 10 terms and we want to build a signal with
200 samples repeating the initial sequence.
a=[1 2 4 5 6 7 8 9 5 3];
stem(a);
axis([0 length(a)+1 min(a)-1 max(a)+1])
xlabel('Index')
ylabel('Value')
title ('Discrete aperiodic sequence')

35
Digital Signal Processing with Matlab® Applications

Discrete aperiodic sequence


10

Value
5

0
0 1 2 3 4 5 6 7 8 9 10 11
Index

Fig. 3.6 Representation of a discrete aperiodic given sequence

If we want to extend through periodicity this signal, we will write the following code and we
will obtain the graphic from Fig. 3.7.
aPeriodic=ones(10,1)*a;
aPeriodic=reshape(aPeriodic',size(a,2)*10,1);
stem(aPeriodic);
axis([0 length(aPeriodic)+1 min(aPeriodic)-1 max(aPeriodic)+1])
xlabel('Index')
ylabel('Value')
title ('Discrete periodic sequence')

Discrete periodic sequence


10

6
Value

0
0 10 20 30 40 50 60 70 80 90 100
Index

Fig. 3.7 Representation for an extended through periodicity signal

Exercise 3.7 Explain how was this can be done. Write a function extendThroughPeriodicity
that will expand a signal n times by periodicity, using the MATLAB function repmat.
Exercise 3.8 Write a function isPeriodical that will return 1 if a sequence is periodical and 0
otherwise.

36
3 Signals, Sampling Theorem

Exercise 3.9 Write a function fundamentalPeriod that will return 0 if a sequence is


aperiodic and the fundamental period otherwise.
Exercise 3.10 Show that the sum of two periodical digital signals with fundamental periods
T1 and T2 is also a periodical signal. What is its fundamental period? Try it adding 2 sinuses of 4 Hz
and 6Hz.

3.2.4 Symmetric Signals and Antisymmetric Signals


A real valued signal x(n) is called symmetric (even) if
x(n)=x(-n), with n  Z (3.4)
If the signal x(n) is finite with N elements, it is called symmetric (even) if
x(n)=x(N-n+1), with n  1, N (3.5)
A real valued signal x(n) is called antisymmetric (odd) if
x(n)=-x(-n), with n  Z (3.6)
If the signal x(n) is finite with N elements, it is called antisymmetric (odd) if
x(n)=-x(N-n+1), with n  1, N (3.7)
Exercise 3.11 Show that if we denote by O an odd signal and with E an even signal, we have:

Table 3.1 Symmetry after arithmetical operation


First signal Operation Second signal Result
O + O O
E + E E
O * O E
E * E E
O * E O
Check this graphically, taking the element by element operations for two signals – one Odd
(can be a sine) and one Even (can be a cosine)
Remark: Any signal x(n) can be written as a sum of an even and an odd signal
 1
 x e ( n)  x(n)  x(-n) 
 2 (3.8)
1
 x o ( n)  x(n)  x(-n)
 2
Exercise 3.12 Write a function isEven that will return 1 if a sequence is even and 0 otherwise
Exercise 3.13 Write a function isOdd that will return 1 if a sequence is odd and 0 otherwise
Exercise 3.14 Prove that (3.8) is valid and write a function decomposeOddEven that will
build the xe and xo decomposition for a signal x. Test it for x(n)=n2-10ꞏn, n[-10,10] The result
should look like in Fig. 3.8.

37
Digital Signal Processing with Matlab® Applications

Decomposition of signal x in xodd and xeven


200

150

100

Value
50

-50

-100
-10 -8 -6 -4 -2 0 2 4 6 8 10
Index

Fig. 3.8 Decomposition of signal x(n) (blue points) as a sum


of an odd (green +) and an even (red x) component

3.2.5 Deterministic Signals and Random Signals


A signal is deterministic if its evolution is predictable and can be represented using explicit
mathematical expressions Almost all the signals we will deal with will be deterministic.
A random signal is unpredictable and must be represented with statistical models. It usually
represents the noise associated with acquisition, transfer, processing, etc… of electrical signals.
There are different statistics associated with the random values, the most used being the uniform
and the normal distribution (also called Gaussian distribution).
Exercise 3.15 Use MATLAB to generate a deterministic sequence of 1.000.000 numbers.
Plot it.
Exercise 3.16 Use MATLAB to generate a sequence of 100 random numbers, distributed
uniformly inside [0 1]. Plot it. Hint: use MATLAB rand function.
Exercise 3.17 Use MATLAB to generate a sequence of 1000 random numbers, distributed
uniformly inside [-1 1]. Plot it. Plot the histogram of the data. Hint: use MATLAB hist function.
The histogram is a plot of the distribution of values in a signal. On the x-axis we represent the
values the samples can take and on the y-axis we put the number of signal samples having each
particular value.
Exercise 3.18 Use MATLAB to generate a sequence of 1000 random numbers with mean 1
and standard deviation 1. Plot it. Plot the histogram of the data. Compare with the histogram of the
random numbers with uniform distribution.
Exercise 3.19 Use MATLAB to generate a sequence of 6 random integers uniformly
distributed in the set 1..49. Hint: use MATLAB randint function. Can you use this sequence to play
the “6 from 49” lottery game? Why not? Modify it.
The uniform distribution means that the probability for a number to take any value in the given
domain is equal, so its histogram is flat. In the normal distribution, the values near the mean have a
higher probability to appear and the histogram has the shape of the Gaussian bell.

38
3 Signals, Sampling Theorem

The functions that are used to generate random numbers are generating in fact “pseudo-
random” numbers/sequences. The “twister” algorithm, the default in MATLAB Versions 7.4 and
later, generates double precision values in the closed interval [2-53 1-2-53], with a period of
(219937-1)/2.
The uniform and normal probability distributions are continuous functions (distributions).
There are also other important continuous distributions, like gamma, chi-square, beta and the
multimodal one. There are also intrinsic discrete probability distributions such as the Poisson and
the binomial one. If you are interested in studying this topic further , maybe you need to install the
MATLAB Statistics toolbox.

3.3 Basic Waveforms

3.3.1 The Sampling Process – Time Vectors


Prior to perform DSP important operations, we’ll need the basement to work on. These are the
signals. We can use real signals using acquisition boards or we can create them using MATLAB.
We want to create basic waveforms that will be added, concatenated, etc…, to form the
“artificial” signals that will make the “cases“ for various algorithms through this book..
Remark: There are already several Signal Processing Toolbox functions that generate
waveforms. Their names can be seen typing in the command line >>help
tocwaveformgeneration. For Signal Processing Toolbox Version 6.7 (R2007a), they are:
chirp - Swept-frequency cosine generator
diric - Dirichlet (periodic sinc) function
gauspuls - Gaussian RF pulse generator
gmonopuls - Gaussian monopulse generator
pulstran - Pulse train generator
rectpuls - Sampled aperiodic rectangle generator
sawtooth - Sawtooth function
sinc - Sinc or sin(pi*x)/(pi*x) function
square - Square wave function
tripuls - Sampled aperiodic triangle generator
vco - Voltage controlled oscillator

They usually request as one of the inputs a vector that represents the time base. Here we have
to emulate the sampling process. For a sampling frequency fs and for data acquisition starting from
time t0 and ending at time tf we’ll have to define the time vector as an array from t0 to tf with the
increment 1/fs.
Example 3.5 For an acquisition started at second 3 and ended at second 5 with a sampling
frequency of 5 Hz, we have:
t0=3;
tf=5;
fs=5;
t=t0:1/fs:tf

t =
3.0000 3.2000 3.4000 3.6000 3.8000 4.0000 4.2000 4.4000 4.6000
4.8000 5.0000

39
Digital Signal Processing with Matlab® Applications

Because MATLAB waveform generation functions are not unitary (some require time as a
parameter, some matrices, etc..) we will try to generate signals – basic waveforms in a consistent
way using MATLAB. This “mini-toolbox” will be used subsequently to create simple and
composite signals that will serve as input for DSP blocks.
We will start for the very beginning with sinusoidal signals.
Example 3.6 We’ll write a function mySine having as arguments the amplitude A, the
frequency f, the initial phase φ and the time vector t.
x (A, f, φ, t)=Aꞏsin(2ꞏπ f t+φ) (3.9)
We’ll write a text file mySine.m that has to be in the current MATLAB path. The file will
contain
function y=mySine(A,freq,phi,t)
% mySine Sine wave generation.
% mysine(A,freq,theta,t) generates a sinusoidal wave
% with amplitude A, frequency freq
% and initial phase phi
% for the elements of time vector t.
y=A*sin(2*pi*freq*t+phi);

It is kind to write for every M-function you build some comments about it. MATLAB has a
very convenient mechanism for incorporating help into the system, even for user-written M-files.
The comment lines at the beginning of every function are used as help for that function. After you
saved the mySine.m file, type
>> help mySine.m

Example 3.7 Let’s test our function, generating and plotting a sinusoidal signal x sampled at
100 Hz (a time lag of 0.01s). The sampling starts in second 1 and ends in second 2. The signal has
amplitude 5, frequency 10 Hz, and no initial phase. We will plot both the values of the signal and its
envelope.
Remark You can write in a script file or in the command line. We advise you to write as much
as possible M-files with the number of the chapter/lab, underscore and a number to particularize
your progress (take a copy of your work when you leave the lab). You can write for example the file
testSine.m or work_3_1.m with the text
clear all
close all
clc
t=1:.01:2;
f=2;
A=5;
ph=0;
x=A*sin(2*pi*f*t+ph);
plot(t,x)
axis([1,2,-A-0.5,A+0.5])
title('Sine signal')
xlabel('Time (s)'), ylabel('Amplitude')

or
clear all, close all, clc
Fs=100;t0=1;tf=2;
t=t0:1/Fs:tf;
f=2;A=5;ph=0;

40
3 Signals, Sampling Theorem

x=mySine(A,f,ph,t);
plot(t,x, '.-')
axis([t0,tf,-A-0.5,A+0.5])
title('Sine signal'), xlabel('Time (s)'), ylabel('Amplitude')

Running that source file we obtain the graphic from Fig. 3.9.

Sin signal

1
Amplitude

-1

-2

-3

-4

-5

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2


Time (s)

Fig. 3.9 Plot of a sinusoidal waveform

3.3.2 Common Periodic Waveforms


The Signal toolbox provides functions for generating widely used periodic waveforms that
were enumerated at the beginning of the chapter 3.3.1.
The function sawtooth generates a sawtooth wave with peaks at ±1 and a period of 2π. An
optional width parameter specifies a fractional multiple of 2π at which the signal’s maximum
occurs.
Example 3.8 To generate 1 second of a 50 Hz sawtooth wave with a sample rate of 10 kHz
and plot 0.1 seconds of the generated waveform, we’ll use a script file containing
fs=1000;t0=0;tf=1;f=50;
t=t0:1/fs:tf;
x = sawtooth(2*pi*f*t);
plot(t,x,'.-'),
axis([0 0.2 -1.1 1.1])
title('Sawtooth signal'),
xlabel('Time (s)'),
ylabel('Amplitude')

The result is presented in Fig. 3.10.

41
Digital Signal Processing with Matlab® Applications

Sawtooth signal

0.8

0.6

0.4

0.2

Amplitude
0

-0.2

-0.4

-0.6

-0.8

-1

0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
Time (s)

Fig. 3.10 Plot of a sawtooth waveform

MATLAB function square generates a square wave with a period of 2π. An optional parameter
specifies duty cycle, the percent of the period for which the signal is positive.
Example 3.9 To generate 1 second of a 50 Hz square wave with a sample rate of 1 kHz, a
duty factor of 25% and plot 0.1 seconds of the generated waveform, we’ll use a script file
containing
fs=1000;t0=0;tf=1;f=50;
t=t0:1/fs:tf; dF=25;
x = square(2*pi*f*t,dF);
plot(t,x,'.-'),
axis([0 0.1 -1.1 1.1])
title('Square signal'), xlabel('Time (s)'), ylabel('Amplitude')

The result can be seen in Fig. 3.11.


Square signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
Time (s)

Fig. 3.11 Plot of a square waveform

42
3 Signals, Sampling Theorem

3.3.3 Common Aperiodic Waveforms


The toolbox also provides functions for generating several widely used a-periodic waveforms:
The function chirp with usual syntax chirp(t,f0,t1,f1) generates samples of a linear swept-
frequency cosine signal at the time instances defined in array t, where f0 is the instantaneous
frequency at time 0, and ,f1 is the instantaneous frequency at time t1. An optional parameter
‘method’ specifies alternative sweep methods.
Example 3.10 To compute 0.5 seconds of a linear chirp signal with a sample rate of 1 kHz,
that starts at DC and crosses 150 Hz at 1 second, we’ll use
fs=1000;t0=0;tf=.5;
t=t0:1/fs:tf;
f0=0;t1=1;f1=100
x = chirp(t,f0,t1,f1);
plot(t,x,'.-'),
axis([0 tf -1.1 1.1])
title('Chirp signal'), xlabel('Time (s)'), ylabel('Amplitude')

Chirp signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Time (s)

Fig. 3.12 Plot of a square waveform

3.3.4 User-defined Waveforms


You will have to create now your own basic waveforms. They will have as parameters the
amplitude, initial phase (if necessary), the significant time instants and, most important, the vector
of time instants when the sampling takes place.
Use Example 3.6 and build your own primitive MATLAB functions for
Exercise 3.20 myCosine function;
x (A, f, φ, t)=Aꞏcos(2ꞏπ f t+φ) (3.10)
Test by plotting the values and the envelope of myCosine for a signal with amplitude 5,
frequency 1, no initial phase and for a sampling with frequency 200 Hz, started in second 1 and
ended in second 4.

43
Digital Signal Processing with Matlab® Applications

Exercise 3.21 myConstant function;


x (A, t)=A (3.11)
Test by plotting the values and the envelope of myConstant for a signal with amplitude π and
for a sampling with frequency 10 Hz, started in second 1 and ended in second 10.
Exercise 3.22 myStep function;
 0, t  t0
x ( A, t0 t    (3.12)
 A, t  t0
Test by plotting the values and the envelope of myStep for a signal with amplitude 2 and
changing time 3, for a sampling with frequency 5 Hz, started in second -5 and ended in second 5.
Exercise 3.23 myImpulse function;
 A, t  t0
x ( A, t0 t    (3.13)
 0, t  t0
Test by plotting the values and the envelope of myImpulse for a signal with amplitude 2 and
changing time 2, for a sampling with frequency 10 Hz, started in second -2 and ended in second 3.
Exercise 3.24 myRectangularPulse function;
 A, t  t  t 2
x ( A, t1 t 2 , t    1 (3.14)
0, elsewhere
Test by plotting the values and the envelope of myRectangularPulse for a signal with
amplitude 2 and changing times 1 and 2, for a sampling with frequency 10 Hz, started in second -1
and ended in second 5.
Exercise 3.25 myLinear function;
x (t0 , , t     (t  t0 ) (3.15)
Test by plotting the values and the envelope of myLinear for a signal with slope -0.5 and
crossing 0 at 1 second, for a sampling with frequency 10 Hz, started in second -1 and ended in
second 5.
Exercise 3.26 myRamp function;
 0, t  t0
x (t0 , , t    (3.16)
  (t  t0 ), t  t0
Test by plotting the values and the envelope of myRamp for a signal with slope 0.5 and
changing point at 1 second, for a sampling with frequency 10 Hz, started in second 0 and ended in
second 5.
Exercise 3.27 myRampPulse function;
 A
  (t  t1 ), t1  t  t2
x ( A, t1 , t2 , t    t2  t1 (3.17)
 0, elsewhere

Test by plotting the values and the envelope of myRampPulse for a signal with amplitude 1,
starting in second 3 and ending in second 4, for a sampling with frequency 20 Hz, started in second
0 and ended in second 5.

44
3 Signals, Sampling Theorem

Exercise 3.28 myTriangularPulse function;


 A
 t  t  (t  t1 ), t1  t  t2
 2 1
 A
x ( A, t1 , t2 , t3 , t     (t  t3 ), t2  t  t3 (3.18)
 t 2  t3
 0, elsewhere


Test by plotting the values and the envelope of myTriangularPulse for a signal with amplitude
1, starting in second 1, reaching the max in second 3 and going back to 0 in second 4, for a
sampling with frequency 10 Hz, started in second -2 and ended in second 5.
Exercise 3.29 myWhiteNoise function;
x ( A, t   uniform random values in [-A A] (3.19)
Test by plotting the values and the envelope of myWhiteNoise for a signal with amplitude 1,
for a sampling with frequency 20 Hz, started in second 0 and ended in second 5.
The results for the Exercise 3.20-3.29 are shown in Fig. 3.13 and Fig. 3.14
Cos signal Constant signal

5 3.5

4 3
3
2.5
2

1 2
Amplitude
Amplitude

0 1.5

-1
1
-2

-3 0.5

-4 0

-5
-0.5
1 1.5 2 2.5 3 3.5 4 1 2 3 4 5 6 7 8 9 10
Time (s) Time (s)

Step signal Impulse signal


2.5 2.5

2 2

1.5 1.5
Amplitude
Amplitude

1 1

0.5 0.5

0 0

-0.5 -0.5
-5 -4 -3 -2 -1 0 1 2 3 4 5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3
Time (s) Time (s)

Fig. 3.13 Plots for user-defined waveforms from Exercises 3.20-3.23

45
Digital Signal Processing with Matlab® Applications

Rectangular pulse signal Linear signal


2.5

1.5

2 1

0.5
1.5
0

Amplitude
Amplitude

1 -0.5

-1
0.5
-1.5

0 -2

-2.5

-0.5
-1 0 1 2 3 4 5 -1 0 1 2 3 4 5
Time (s) Time (s)

Ramp signal Ramp pulse signal

2.5 2

2 1.5

1.5 1
Amplitude

Amplitude

1 0.5

0.5 0

0 -0.5

-0.5 -1

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time (s) Time (s)

Triangular pulse signal White noise signal


1.5
3

2.5
1
2

1.5 0.5
1
Amplitude

Amplitude

0.5 0

-0.5 -0.5

-1
-1
-1.5

-2
-1.5
-2 -1 0 1 2 3 4 5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time (s) Time (s)

Fig. 3.14 Plots for user-defined waveforms from Exercises 3.24-3.29

Another way of creating waveforms is to use synthetically their harmonic waves


decomposition.

46
3 Signals, Sampling Theorem

Example 3.11 The analytical formula for synthesis of a 50% duty cycle square wave with
angular frequency w0 as a sum of harmonically related sine waves (its Fourier series) can be written
as:
4 1 1 
x(t )   sin(2 f t   )  sin(2 3 f t   )  sin(2 5 f t   )   (3.20)
 3 5 
or

4 1
x(t ) 

 2k  1 sin 2 (2k  1) f t   
k 0
(3.21)

This formula can be used to compute approximately a square waveform, and any other function
can be decomposed in its Fourier series. It is not a practical approach when we know the analytical
definition for the waveform, because of the Gibbs phenomenon (we cannot take an infinite number
of terms in the series). However, it is important to see how the approximation goes better for larger
number of terms.
fs=1000; t0=0; t1=3;
t=t0:1/fs:t1;
A=1;f=1;phase0=pi;
for index=1:4
n=2^index;
x=mySquarePeriodical(A,f,phase0,n,t);
subplot(2,2,index)
plot(t,x)
xlabel('time')
ylabel('amplitude')
title(['Fourier Series with ' num2str(2^index) ' terms'])
end

Fourier Series with 2 terms Fourier Series with 4 terms


1.5 1.5

1 1

0.5 0.5
amplitude

amplitude

0 0

-0.5 -0.5

-1 -1

-1.5 -1.5
0 1 2 3 0 1 2 3
time time
Fourier Series with 8 terms Fourier Series with 16 terms
1.5 1.5

1 1

0.5 0.5
amplitude

amplitude

0 0

-0.5 -0.5

-1 -1

-1.5 -1.5
0 1 2 3 0 1 2 3
time time

Fig. 3.15 Fourier series synthesis for a square waveform

47
Digital Signal Processing with Matlab® Applications

3.4 Saving (Recording) a signal

The process of recording a given input is strongly related to the procedure of saving it on disk.
MATLAB offers a general, easy way to save variables from the workspace, as was shown in
Chapter 2.7. We will review them from the signal recording point of view.
The save command preserves the contents of the workspace in a MAT-file that can be read
with the command load in a later MATLAB session. For example
>>save variable

saves the entire workspace contents in the file variabile.mat. If desired, you can save only
certain variables by specifying the variable names after the filename.
Example 3.12 The sequence
>> t=1:100;
>> save varT t

will save the variable t in the file varT.mat.


To find out the content of the file, type
>> edit varT.mat

Example 3.13 It is almost impossible to read out what t was. Ordinarily, the variables are
saved in a binary format that can be read quickly (and accurately) by MATLAB. If you want to
access these files outside of MATLAB, you may want to specify an alternative format. The
parameter -ASCII will do this.
>> save varTASCII t –ASCII
>> edit varTASCII.mat

When you save workspace contents in text format, you should save only one variable at a time.
If you save more than one variable, MATLAB will create the text file, but you will be unable to
load it easily back into MATLAB.
You can recover the signals with the function load.
>> clear all; whos
>> load varT
>> whos

or
>> clear all; whos
>> load varTASCII
>> whos

The main problem is that this format (even in ASCII form) is not compatible with the ones
(usually different) in which the acquisition boards save the signals. You’ll have to learn how to
work with files and different representation formats in order to comply with the standard you meet.
An advantage is the fact that MATLAB uses the C format for variables.
Example 3.14 We’ll now generate a sine signal altered by uniform noise and save it in a file.
We'll reserve a header of 8 floats.
t=0:.01:10;
q=mySine(10,2,0,t)+myWhiteNoise(1,t);
s=fopen('sin.dat','w'); % open for writing the data file.

48
3 Signals, Sampling Theorem

header=[2, length(q), 1/(t(2)-t(1)), t(1), t(end),0,0,0];


fwrite(s,[header q],'float');
fclose(s);
s=fopen('sin.dat','r');
q2=fread(s,'float');
fclose(s);
header2=q2(1:8);
q2=q2(9:length(q2));
subplot(2,1,1);
plot(q2);
xlabel('index');
ylabel('amplitude');
t=header2(4):1/header2(3):header2(5);
subplot(2,1,2);
plot(t,q2);
xlabel('time');
ylabel('amplitude');

20

10
amplitude

-10

-20
0 200 400 600 800 1000 1200
index

20

10
amplitude

-10

-20
0 1 2 3 4 5 6 7 8 9 10
time

Fig. 3.16 Representation of a signal without and with header information

Exercise 3.30 Which identifier was chosen for a sinusoidal waveform?


Exercise 3.31 What is the difference between the two subplots?
Exercise 3.32 Save a step function in a file. The header will be like the previous one, with
the signal type identifier 8. Read and plot the signal.

3.5 Sampling Theorem

The Sampling Theorem states that if the highest frequency contained in an analog signal is
Fmax and the signal is sampled at a frequency fs>2ꞏfmax, then the signal can be recovered from its
samples. The sampling rate fN=2ꞏfmax is called Nyquist rate.
Remark1: In order to recover a signal, we must sample it at a frequency at least greater than
the Niquist rate.
Remark2: If we have a signal sampled at Fs, we have information only of the components
with f<Fs/2.

49
Digital Signal Processing with Matlab® Applications

Remark3: In order to reconstruct perfectly a signal, we must know all its values, because
reconstruction is made by a infinite sum of products of samples with the interpolation function.
Therefore, if the signal isn’t periodic, we cannot reconstruct it perfectly.
Exercise 3.33 What is interpolation? Give examples.
Example 3.15 Let's construct a sine signal and plot it. It will have the frequency of 1Hz,
amplitude 3 and initial phase /2. The “recording” time starts from 0 and ends at second 5. The
sampling rate is 100 Hz.
t0=0; tf=5;
A=3;f=1;ph=pi/4;
fs=[32 16 8 4 2 1];

for index=1:length(fs)
t=t0:1/fs(index):tf;
x=mySine(A,f,ph,t);
subplot(3,2,index)
plot(t,x,'.-')
axis([t0 tf -A-.5 A+.5])
title(['Sine signal of 1 Hz sampled at ' num2str(fs(index)) 'Hz']);
end

The graphs are shown in Fig. 3.17.

Sine signal of 1 Hz sampled at 32 Hz Sine signal of 1 Hz sampled at 16 Hz

2 2
0 0
-2 -2

0 1 2 3 4 5 0 1 2 3 4 5
Sine signal of 1 Hz sampled at 8 Hz Sine signal of 1 Hz sampled at 4 Hz

2 2
0 0
-2 -2

0 1 2 3 4 5 0 1 2 3 4 5
Sine signal of 1 Hz sampled at 2 Hz Sine signal of 1 Hz sampled at 1 Hz

2 2
0 0
-2 -2

0 1 2 3 4 5 0 1 2 3 4 5

Fig. 3.17 Sine signal of 1 Hz sampled at 32, 16, 8, 4, 2 1 Hz

Let's see how do they look when we try to reconstruct the signal at 100 Hz.
t0=0; tf=5;
A=3;f=1;ph=pi/4;
fs=[32 16 8 4 2 1];
fInterp=100;

50
3 Signals, Sampling Theorem

tInterp=t0:1/fInterp:tf;

for index=1:length(fs)
t=t0:1/fs(index):tf;
x=mySine(A,f,ph,t);
xInterp=interp1(t,x,tInterp,'spline');
x=mySine(A,f,ph,tInterp);
subplot(3,2,index)
plot(tInterp,x,tInterp,xInterp)
axis([t0 tf -A-.5 A+.5])
title(['1Hz Sine/ reconstruct. from ' num2str(fs(index)) ' Hz
samples']);
end

The reconstructions are presented in Fig. 3.18.

1Hz Sine/ reconstruct. from 32 Hz samples1Hz Sine/ reconstruct. from 16 Hz samples

2 2
0 0
-2 -2

0 1 2 3 4 5 0 1 2 3 4 5
1Hz Sine/ reconstruct. from 8 Hz samples 1Hz Sine/ reconstruct. from 4 Hz samples

2 2
0 0
-2 -2

0 1 2 3 4 5 0 1 2 3 4 5
1Hz Sine/ reconstruct. from 2 Hz samples 1Hz Sine/ reconstruct. from 1 Hz samples

2 2
0 0
-2 -2

0 1 2 3 4 5 0 1 2 3 4 5

Fig. 3.18 Spline reconstructions for the 1 Hz sine signal

What you have seen is aliasing in frequency, but we will discuss about this in the next
subchapter. We couldn’t reconstruct a signal sampled at a frequency smaller than twice its highest
(in this case – its own) frequency.
Exercise 3.34 An analog x(t) signal has the Niquist rate fN. What can you say about the
Niquist rate of the signal y1(t)=2x(t), y2(t)=x(2t); y3(t)=x(t/2); y4(t)= x2(t); y5(t)=dx(t)/dt;
t
y6(t)=  x( )d . Verify it graphically, taking x(t)=5ꞏsin(2πꞏ5ꞏt)+2ꞏsin(2πꞏ7ꞏt)+sin(2πꞏ11ꞏt).
0

51
Digital Signal Processing with Matlab® Applications

3.6 Aliasing

Aliasing a Sine Signal

Let’s continue with the study of aliasing. Consider the formula for a continuous-time
sinusoidal signal with frequency f0 and initial phase φ:
x(t )  sin(2f 0 t   ) (3.22)
We can sample x(t) at a rate fs = 1/Ts to obtain a discrete-time signal. We will use temporarily
the notation x[n], to avoid the confusion of discrete x(n) with continuous x(t)
 f 
x[n]  x(t ) t  nTs  x(t ) t  n / f s  sin  2 0 n    
 fs 
(3.23)
 f   f  kf s 
 sin  2 0 n  2kn     sin  2 0 n   
 fs   fs 
From (3.23), we see that for any fk=f0+kꞏfs, we have the same samples value.
Exercise 3.35 Make a plot of a sampled sine wave with amplitude 1. Let the frequency of the
sine wave be 1 Hz, and take samples over an interval of 1s with the sampling frequency 10Hz. The
phase φ will be 0. Plot the resulting discrete-time signal using stem.
Sine signal of 1Hz sampled at 10Hz
1

0.5
amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time
Sine signal of 11Hz sampled at 10Hz
1

0.5
amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time

Fig. 3.19 Sines of 1Hz and 11 Hz sampled at 10Hz

Exercise 3.36 Superpose on the existing stems the plots of the original sines, sampled this
time at 100 Hz. What do you observe?
The plots are presented in Fig. 3.20. We can see that different signals/sines have the same
sampling values because of the aliasing. The aliasing can be avoided by removing the components
that have frequencies larger that half of the sampling rate - low-pass filtering at fs/2.

52
3 Signals, Sampling Theorem

Sine signal of 1Hz sampled at 100Hz


1

0.5

amplitude
0

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time
Sine signal of 11Hz sampled at 100Hz
1

0.5
amplitude
0

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time

Fig. 3.20 Sines of 1Hz and 11 Hz sampled at 100Hz

2
Exercise 3.37 For the discrete signal x(n)  sin( n) , find out three analog signals that will
7
produce it when sampled. Verify it graphically, making a 3x1 plot with the signals, like in Fig. 3.20
Aliasing a Chirp Signal
A linear frequency-modulated signal makes a good test for aliasing, because the frequency
moves over a range. This signal is often called a “chirp,” due to the audible sound it makes when
played through a speaker. The mathematical definition of a chirp is
x(t )  cosat 2  2f1t    (3.24)
The instantaneous frequency of this signal can be found by taking the time derivative of the
phase (the argument of the cosine) and dividing by 2π. The result is
f i (t )  at  f1 (3.25)
which is a linear function of time and with speed a.
Chirp signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

9.5 9.6 9.7 9.8 9.9 10 10.1 10.2 10.3 10.4 10.5
Time (s)

Fig. 3.21 Detail of chirp signal from Example 3.10

53
Digital Signal Processing with Matlab® Applications

Exercise 3.38 Take the parameters of the chirp to be f1 = 0 kHz,  = 100 kHz/s, and φ zero.
This is the chirp we used for Example 3.10. If the total time duration of the chirp is 20 ms,
determine the frequency range that is covered by the swept frequency of the chirp. Make the
representation for the signal using fs=1KHz. Represent the time period from 9.5 to 10.5 s. Try to
explain what you see.
Exercise 3.39 Notice that the chirp signal exhibits intervals in time where the apparent
frequency gets very low. In fact, the instantaneous frequency is passing through zero at these points.
Determine from the plot the time when this happens. Find another time instant when the
instantaneous frequency is 0. Look at the graph and try to find out the moments when the
instantaneous frequency is maxim.
If the swept bandwidth of the chirp exceeds half of the sampling frequency, there will be
aliasing. The instantaneous frequency for a digital chirp will increase from 0 at time 0 to fs/2 at time
fs/2a, will decrease to 0 at time fs/a, will increase again, etc…

3.7 Binary Representation

We assumed for all the signals presented until now that they can be represented with infinite
precision. This is not true due to the finite representation characteristic of digital systems. This will
impede the results of digital processing due to its effect on signal samples, filter coefficients, and
results of computations.

3.7.1 Floating Point Representation


In a personal computer, there is a large range of values that are needed for data representation
and there is no a priori control on them. For this reason, the floating point representation is
preferred, due to its vast representation domain. For this reason, when versatility is desired or when
there is little control over the range of values, it is used also for DSPs.
A floating-point number n in radix r has the general form:
n  (1) S  M  r E (3.26)
where S gives the sign (+ or – for respectively 0 or 1) M is the mantissa or fraction (smaller
than 1) and E is the exponent (for radix r).
A number can have various floating-point representations for a given radix. For example, the
number π can be represented in basis 10 as:
  0.314159  101
  0.031415  10 2 (3.27)
  0.003141  10 3
From these representations it is chosen the normalized representation – the one for which M is
the largest (still smaller than 1).
Exercise 3.40 Prove that in a radix 2 representation, the MSB of M is nonzero.
MATLAB constructs the single-precision (or single) data type according to IEEE Standard
754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the
figure below:

54
3 Signals, Sampling Theorem

31 30 23 22 0

Sign Exponent Mantissa

Fig. 3.22 Single precision floating point format

The number is positive if sign=0 and negative for sign=1.


The exponent is stored on 8 bits, in excess 127 code (biased by 127), meaning it can go from -
12610 = 000000012 to +12710 = 111111102. The values 000000002 and 111111112 are left for
special conditions.
Exercise 3.41 Approximate mathematically the maximum positive number that can be
represented in single precision.
Example 3.16 Using single-precision you can store values from approximately -3.4 x 1038 to -
3.4 x 1038. We will verify it in MATLAB:
single(3.4*10^38)
ans =
3.4000e+038
single(3.5*10^38)
ans =
Inf

The fundamental data type used in MATLAB is the double (numeric, floating point, double
precision). Any value stored as a double requires 64 bits, formatted as shown in the figure below
(IEEE Standard 754

63 62 52 51 0

Sign Exponent Mantissa

Fig. 3.23 Double precision floating point format

The exponent is biased by 1023.


Exercise 3.42 Approximate the minimum positive number that can be represented in double
precision
Exercise 3.43 Establish by trials the minimum nonzero positive number that can be
represented in double precision
Hint
10^-1000
ans =
0

The double format provides approximately 15 decimal digits of precision, being a satisfactory
approximation for real numbers (analog values). The floating point, double precision representation
offers a good dynamic range associated with high precision in representation, but this comes with
the cost of demanding maneuvers for arithmetical operations. For a personal computer or for off-
line processing, there are no real-time constrains on the speed of computations and moreover, the

55
Digital Signal Processing with Matlab® Applications

size and price of the device is not a direct limiting factor, so we will use double representation in the
following through the book..
This is sometimes impractical for real-time DSPs, so a fixed point representation (associated
with faster/simpler hardware) has to be used.

3.7.2 Fixed Point Representation


Fixed-point DSPs are widely used since they generally provide higher processing speed at
lower cost than do the floating point systems. They need some control over the range of values that
are represented but this is undemanding for most systems and it is natural for many of them (think
of B/W images that have pixels represented on 8 bits).
In this representation, the position of the decimal point is fixed and the value of each digit in
the number depends on its position relative to it.
The positional form of a number is a set of side-by-side digits given generally in fixed-point
form as:

MSD Radix Point LSD

n r= nk-1 nk-2 … n1 n0 . n-1 n-2 … n-m+1 n-m r


Integer Part Fraction Part Basis

Fig. 3.24 Positional form of a number

The first k bits represent the integer part and the last m bits represent the fraction part. The
number is represented in basis r, so it can be represented in polynomial form as:
k 1
n r  n k 1 r k 1  n k  2 r k  2  ...  n1 r 1  n0  n 1 r 1  n  2 r  2  ...  n  m 1 r  m 1  n  m r  m  n r
im
i
i
(3.28)

Example 3.17 We will try represent the number π in a 4.4 fixed point basis 2 (binary - 8 bit)
representation. We have that
 10  3.141610  0  2 3  0  2 2  1  21  1  2 0  0  2 1  0  2 2  1  2 3  0  2 4
So the positional form representation of π10 in 4.4 2 is: 0011.0010
Exercise 3.44 Write a function that will convert a number n10 to a (k.m)2 representation. The
overflows will be saturated to the greatest number that can be represented. Test it to convert
123.456789 in (8.8)2, 0.123456789 in (0.8)2 and 123456 in (16.0)2.
The examples we used for the illustration of the fixed point representation were all positive and
they are called unsigned numbers. There are schemes to deal with them (grayscale for pixels) and
they are usually used in a form where the fraction part is missing (k.0)2 – unsigned char, unsigned
int, or where the integer part is missing (0.m)2 – normalized fractionary form.
Example 3.18 We will sketch the signal ex for x  [0 3] sampled at 10 Hz using a (8.0)2 - uint8
representation.
x=0:.1:3; y=exp(x);
z=uint8(y);
plot(x,z,'.',x,y,'r')
axis([0 3 0 20])

56
3 Signals, Sampling Theorem

20

18

16

14

12

10

0
0 0.5 1 1.5 2 2.5 3

Fig. 3.25 Integer representation of ex

Exercise 3.45 Compute the total representation error for the signal from Example 3.18.
Exercise 3.46 Sketch the signal sin(x) for x  [0  ] sampled at 10 Hz using a (0.8)2
representation.
Sometimes we have and negative numbers must be used in computers. Several schemes have
been devised for dealing with negative numbers in computers, using a sign bit, radix complement or
excess code representation.
For signed-magnitude representation, the MSB will store the sign. It is used 0 for plus and 1
for minus.
Example 3.19 If we use 8 bit signed magnitude representation, we will have the following
values:
310=0 0000011signed (8.0)2 ; -310=1 0000011signed (8.0)2; 16.2510=0 1111111signed (4.4)2 ;
010=0 0000000signed (8.0)2 ; -010=1 0000000signed (8.0)2 ;
As it can be seen, it has the disadvantage that 0 has different representations.
For radix complement representation
The radix complement of an k-digit number n is obtained by subtracting it from rk, This
operation is equivalent to complementing the number and adding 1 to the LSD. The 2’s complement
is the representation used in many cases for fixes representation.
Example 3.20
310’s complement = 3 +1=6+1=7; -310=1 0000011signed (8.0)2; 16.2510=0 1111111signed (4.4)2 ;
010=0 0000000signed (8.0)2 ; -010=1 0000000signed (8.0)2 ;

Thus, the radix complement is Nr + 1LSD where Nr = r n – 1 – Nr is the complement of a


number in radix r. Therefore, one may write

For excess code representation (offset code representation) we add a given number
(bias/offset) to our own in order to make it positive (usually B=rk-1-1).

57
Digital Signal Processing with Matlab® Applications

nexcess code=nr+Br
Example 3.21 For an (8.0) excess 127 binary representation, we have
3excess code=310+12710=13010=10000010 2=10000010(8.0) excess 127 binary;
-3excess code=-310+12710=12410=1111100 2=1111100 (8.0) excess 127 binary;
The most common use of the excess representation is for exponent representation in floating-
point number systems, as presented before.

3.8 Sample & Hold, Quantization

The analog signals of any kind are subject to a discretization process prior to by numerically
processed.
Example 3.22 We want to make a MATLAB function that will receive an expression/function
representing an analog signal depending on the variable t (time), the sampling starting and ending
time t0 and tf, as well as the sampling frequency fs. This function will return a discrete array
representing the discrete values of time when the function is sampled and the values of the function
in that points.
function [y,tVector]=sampleFunction(functionString,t0,t1,fs)
tVector=t0:1/fs:t1;vt=t0;
for index=1:length(tVector)
t=tVector(index); y(index)=eval(functionString);
end

sin(t 3 )
We will use this function to represent the sampled values of the function f (t )  for t in
2tg (t )
[0 1.5], sampled at 50Hz
t0=0; t1=1.5; fs=50; tHold=.1;
functionString = 'sin(t^3)/2^tan(t)';
[y,t]=sampleFunction(functionString,t0,t1,fs);
stem(t,y), xlabel('time'), ylabel('Amplitude')

The result is presented in Fig. 3.26

0.3

0.25

0.2

0.15
Amplitude

0.1

0.05

-0.05
0 0.5 1 1.5
time

Fig. 3.26 The graphic of the discretization made by sampleFunction for a given symbolic definition

58
3 Signals, Sampling Theorem

3.9 Concluding Exercises

Exercise 3.47 Aliasing is


a) A false name used b) The effect of c) The effect of allying d) The effect on a
to conceal one's mixing signals a signal with a sinusoid signal when it has
identity sampled at the same when the signal is been sampled at less
frequency sampled at a frequency than twice its highest
multiple of  frequency
Exercise 3.48 For the function f(x)=sinc(x)
a) f(0)=0 b) f(0)=1 c) f(0)=∞ d) none of them
Exercise 3.49 Consider the analog signal x(t)=3cos(100t);
What is the maximum sampling rate to avoid aliasing
a) 50 Hz b) 100 Hz c) 200 Hz d) 400 Hz e) all of them
Exercise 3.50 Consider the analog signal x(t)=3cos(100t);
What is the maximum sampling rate to avoid aliasing
a) 50 Hz b) 100 Hz c) 200 Hz d) 400 Hz e) none of them
Exercise 3.51 Consider the analog signal x(t)=2∙cos(100∙∙t);
Suppose the signal is sampled at Fs=200 Hz. What is the discrete signal obtained after
sampling:
a) 2∙cos(n∙/2), b) 2∙cos(t∙), c) 2∙cos(t∙/2), d) cos(t∙), tR+ e) none of them
nN tR+ tR+
Exercise 3.52 Consider the analog signal x(t)=3∙cos(2100∙∙t)+53∙sin(21000∙∙t)+
3∙cos(6000∙∙t);What is the Niquist rate for this signal?
Exercise 3.53 Write your own primitives for other 7 important waveforms (for example
mySawtooth, myStairs, mySinc, myChirp, myGaussBell, etc…)
Exercise 3.54 A continuous-time signal x is sampled with a sampling frequency of fs = 2
kHz. If a 1000-point DFT of 1000 samples is computed, what is the spacing between the frequency
samples X(k) in terms of the analog frequency?
Exercise 3.55 Write a function sampleAndHoldA that will receive a string representing a
function, the starting time t0, the ending time t1, and the sampling frequency fs and will return the
sampling time vector and the values of the function for the instants.
Exercise 3.56 Write a function sampleAndHoldD that will receive a string representing a
function, the starting time t0, the ending time t1, and the sampling frequency fs and will return the
sampling time vector and the values of the function for the instants.
Exercise 3.57 Write a function sampleAndHoldD that will receive a string representing a
function, the starting time t0, the ending time t1, and the sampling frequency fs and will return the
sampling time vector and the values of the function for the instants.
The values we computed form a discrete in time sequence of real numbers. As we seen in 3.7
they cannot be stored in the computer exactly.

59
Digital Signal Processing with Matlab® Applications

Exercise 3.58 A standard stereo analog audio tape recording with the length of 60 minutes
and with frequencies up to 12 KHz has to be converted to digital.
1. What is the maximum sampling frequency? Why?
2. What is the minimum sampling frequency? Why?
3. What level of quantization (bits per sample) would you choose?
4. What happens if the signal is sampled at 8 KHz?
5. What happens if it is sampled at 96 KHz, with 32 bits per sample. If no
encoding/compression is used, how large will be the digital soundtrack? What happens if we
discard each other second sample?

60
4 The Discrete Fourier Transform

4 The Discrete Fourier Transform

4.1 Discrete Fourier Transform – DFT

The Discrete Fourier Transform – DFT is a powerful tool used in DSP and it is the discrete
version of the Fourier Transform:

 x(t )e
 j 2  f t
X(f )  dt (4.1)


As you know, the DFT of a sequence {x(n) | n 0, N  1 , N  N} is


N 1 2
j nk
X ( k )   x ( n)e N
(4.2)
n 0

If we separate the complex exponential into its real and imaginary parts, we obtain
N 1
 2 2 
X (k )   x(n) cos( nk )  j sin( nk )  (4.3)
n0  N N 
2
j
Introducing WN  e N
, we obtain
N 1
X (k )   x(n)W Nkn (4.4)
n 0

Because MATLAB starts indexing the arrays from 1, instead of 0, (4.2) and (4.4) go to:
N 2
j ( n 1)( k 1)
X ( k )   x ( n)e N
(4.5)
n 1

or, using WN, to:


N
X (k )   x(n)WN( k 1)( n 1) (4.6)
n 1

where N is the number of elements in x..


Exercise 4.1 Write a function dft1(x) that will compute the DFT of the sequence x using
(4.5). You will use two cycles: in the exterior one you will compute the values of X(k) and in the
interior one you will make the summations.
Exercise 4.2 Verify the function from Exercise 4.1 computing the DFT of the sequence [1 1
1 1]. How much should it be?
Exercise 4.3 Write a function dft2(x) that will compute the DFT of the sequence x using
(4.6). You will compute the matrix WNk , k 0, N  1 , only once at the beginning of the program and
use later directly their values taking into consideration their periodicity. Test it for the sequence
input [1 1 1 1].
The equation (4.4) can be written using the matrix W, with
W(k,n)= WNkn (4.7)

61
Digital Signal Processing with Matlab® Applications

N 1
X (k )  W (k , n) x(n)  X  xW (4.8)
n0

Exercise 4.4 Write a function constructW that will use (4.7) to return the matrix W for a
given N. Construct the matrix W for N=1, 2, 4, 7, 16. What kind of matrix is W? Can you reduce the
number of computations in your source file?
Example 4.1 For N=4, we have for >> W=constructW(4)
W =
1.0000 1.0000 1.0000 1.0000
1.0000 0.0000 - 1.0000i -1.0000 - 0.0000i -0.0000 + 1.0000i
1.0000 -1.0000 - 0.0000i 1.0000 + 0.0000i -1.0000 - 0.0000i
1.0000 -0.0000 + 1.0000i -1.0000 - 0.0000i 0.0000 - 1.0000i

Remark: The matrix W is called the discrete Fourier transform matrix and is implemented
already in MATLAB by the function dftmtx. Type >> edit dftmx and compare the
implementation with yours.
Exercise 4.5 Write a function dft3(x) that will compute the DFT of the sequence x using
(4.8). This means that you will compute the matrix W having the dimension equal with x and then
compute X as the product xꞏW. Test it for the sequence input [1 1 1 1].
Example 4.2 Let us compare the results and the efficiencies of dft1, dft2 and dft3 versus
MATLAB’s DFT implementation – the function fft (Fast Fourier Transform algorithm) by running
them on a sequence of 2N random complex numbers (take N=12) with the real and imaginary part in
the range [0 1].
N=12;
x=rand(1,2^N)+j*rand(1,2^N);
tic; X=fft(x); t=toc
tic; X1=dft1(x); t1=toc
tic; X2=dft2(x); t2=toc
tic; X3=dft3(x); t3=toc

errAccepted=10^-6;

all(abs(X-X1)<errAccepted)
all(abs(X-X2)<errAccepted)
all(abs(X-X3)<errAccepted)

We obtain the results:


t =
0.0032
t1 =
18.6737
t2 =
4.8170
t3 =
79.0025
ans =
1
ans =
1
ans =
1

62
4 The Discrete Fourier Transform

Remark1: If your computer rather old, take N=10.


Remark2: We observe that all the methods worked fine and there is huge difference in the
running times, due to the used algorithm.
Remark3: The DFT has no corresponding function in MATLAB. It uses instead the function
fft that performs the Fast Fourier Transform (an efficient implementation of the DFT) to compute
the Discrete Fourier Transform of a vector or matrix.
Remark4: The Fast Fourier Transform works on complex numbers – when you test such an
algorithm on reals, you make more computations than necessary, obtaining wrong information
about its effectiveness,– there are versions of FFT designed specially for reals, which would
perform better than MATLAB’s fft. Anyhow, it overcome the other.
Exercise 4.6 Try to explain the results obtained for efficiency in Example 4.2.
Exercise 4.7 For each implementation make run tests like in example 4.2, with N going from
1 to 12. Plot the run time versus N - it is log2(no of points). Try to explain them.
The plots should look like in Fig. 4.1

FFT implementation DFT1 implementation


20
0.08
18
0.07
16

0.06 14
Computing time (s)

Computing time (s)

0.05 12

0.04 10

0.03 8

6
0.02
4
0.01
2
0
0
-0.01
0 2 4 6 8 10 12 14 16 18 0 2 4 6 8 10 12
log2(No. of samples) log2(No. of samples)

DFT2 implementation DFT1 implementation


20
5
18

4 16

14
Computing time (s)

Computing time (s)

3 12

10
2
8

1 6

0 2

0
-1
0 2 4 6 8 10 12 0 2 4 6 8 10 12
log2(No. of samples) log2(No. of samples)

Fig. 4.1 Computing time dependence on the logarithm from the number of samples
for different implementations of DFT

63
Digital Signal Processing with Matlab® Applications

In practice, the Discrete Fourier Transform is computed more efficiently and uses less memory
because of the FFT algorithm. DFT is a time critical procedure and for this reason it is computed all
the time using some flavor of FFT. In MATLAB, the only realization of DFT is the function fft, an
implementation of FFT.
Thus, y = fft(x) is the discrete Fourier transform of vector x, computed with the FFT
algorithm. If x is a matrix, y is the FFT of each column of the matrix. The fft function employs a
radix-2 fast Fourier transform algorithm if the length of the sequence is a power of two, and a
slower algorithm if it is not.
y = fft(x,n) is the n-point FFT. If the length of x is less than n, fft pads x with trailing
zeros to length n. If the length of x is greater than n, fft truncates the sequence x. If x is an array, fft
adjusts the length of the columns in the same manner.
The fft function is part of the standard MATLAB language as a built-in function. When the
sequence length is a power of two, fft uses a high-speed radix-2 fast Fourier transform algorithm.
The radix-2 FFT routine is optimized to perform a real FFT if the input sequence is purely real;
otherwise it computes the complex FFT. This causes a real power-of-two FFT to be about 40%
faster than a complex FFT of the same length.
When the sequence length is not an exact power of two, a separate algorithm finds the prime
factors of the sequence length and computes the mixed-radix discrete Fourier transforms of the
shorter sequences.
The execution time for fft depends on the sequence length. If the length of a sequence has
many prime factors, the function computes the FFT quickly; if the length has few prime factors,
execution is slower. For sequences whose lengths are prime numbers, fft uses the raw (and slow)
DFT algorithm. For this reason it is usually better to use power-of-two FFTs, if this is supported by
your application.
The Inverse Discrete Fourier Transform - IDFT is given by:
1 N 1
x ( n)  
N n 0
X (k )WNk n  x=XWi (4.9)

Exercise 4.8 Write a function idft3(X) that will compute the IDFT of the sequence X using
(4.9), computing the matrix Wi. Hint: Wi=conj(W)/N. Test it for the sequence input X=[4 0 0 0]
Exercise 4.9 Verify dft3 and idft3 showing that for a sequence of x of 10 random complex
numbers, idft3(dft3(x)) is x.
The Magnitude of X=DFT(x) is given by:

X (k )  Re( X (k )) 2  Im( X (k )) 2  abs ( X (k )) (4.10)

Exercise 4.10 Compute and represent the magnitude (absolute value) for the DFT of a sine
signal with frequency of 100 HZ, amplitude 1, zero initial phase, lasting one second, which is
samples at 1000Hz. Hint: use function abs. What kind of response have you obtained? Can you
explain?
Example 4.3 We will confirm the result from Exercise 4.10 computing analytically the
Fourier transform of a sine signal. We will use the MATLAB symbolic math toolbox:
syms f t % create symbolic variables
A=1;f=100;ph=0;
x=A*sin(2*pi*f*t+ph) % define symbolic the signal
X=fourier(x) % compute symbolic the Fourier transform
magnitude=abs(X) % compute the magnitude of the FT

64
4 The Discrete Fourier Transform

We obtain:
x =
sin(200*pi*t)
X =
i*pi*(-dirac(w-200*pi)+dirac(w+200*pi))
M =
pi*abs(dirac(w-200*pi)-dirac(w+200*pi))

that verifies Exercise 4.10 and our common knowledge that the magnitude of the DFT for a sine is
made by two symmetrical impulses.
Example 4.4 A common use of the Fourier transform is to find the frequency components of
a time-domain signal buried in noise. Let us make a signal consisting of 50 Hz and 120 Hz
sinusoids and corrupt the signal with zero-mean random noise, considering data sampled at
1000 Hz.:
fs=1000;
t=0:1/fs:0.1;
x=mySine(2,50,0,t)+ mySine(1,120,0,t)+ myWhiteNoise(1,t);
plot(t,x);
title('Sum of sine of 50 Hz, sine of 120 Hz and noise');
xlabel('time (s)');
ylabel('Amplitude');

Sum of sin of 50 Hz, sin of 120 Hz and noise


4

1
Amplitude

-1

-2

-3

-4
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
time (s)

Fig. 4.2 Representation in time of a signal composed by two sines and white noise

X=fft(x);
figure;
plot(abs(X));
axis tight;
xlabel('index')
ylabel('magnitude')
title('Magnitude for the DFT of signal s - Magnitude spectrum')

65
Digital Signal Processing with Matlab® Applications

Magnitude for the DFT of signal s - Magnitude spectrum

100

90

80

70

60

Modulus
50

40

30

20

10

10 20 30 40 50 60 70 80 90 100
Index

Fig. 4.3 Magnitude spectrum of a signal composed by two sines and white noise

Remark: Observe the symmetry of the graph.


Exercise 4.11 Answer why is the graph symmetrical? Observe the presence of the two spikes.
What would the spikes represent?
The phase of the X=DFT(x) is given by
 Im( X (k )) 
Phase( X (k ))  arctan  (4.11)
 Re( X (k )) 

Exercise 4.12 Plot the phase of the Fourier transform for the signal in the previous example.
It should look like in the next figure. Why is the graph antisymmetric?
Phase for the DFT of signal s
22

20

18

16

14
phase

12

10

10 20 30 40 50 60 70 80 90 100
index

Fig. 4.4 Phase spectrum of a signal composed by two sines and white noise

66
4 The Discrete Fourier Transform

4.1.2. DFT Frequency Representation


For a sequence x with N complex numbers, its Discrete Fourier Transform X will have also N
complex numbers. The graphs of X (magnitude and phase), represented as a function of index, are
in fact functions of frequency (spectrums). Such a graph can be scaled/ normalized, to represent the
magnitude/ phase as a function of other frequency related variables. They can be pulsations, with 2π
corresponding to N-1 (the last sample), frequencies, with fs corresponding to N-1, or even
normalized frequencies, with 2 corresponding to N-1(we will see later why).
The procedure to represent the DFT function of frequency (or similar measures) is very simple,
taking advantage that all the scales start in 0. The N point DFT is computed, its first value
corresponds to 0 and its last value corresponds to fs. From proportionality, the kth sample will
kf
correspond to s .
N
Magnitude
or Phase
2

1
0 fs


0 N-1

Fig. 4.5 Equivalent spectrum representations

4.1.3 DFT Symmetry


If x is a sequence of real numbers, and X=DFT(x), then X has the property that its symmetrical
samples are complex conjugated (comes directly from (4.2)):
X (k )  X ( N  k ) * (4.12)
This is equivalent saying that the real part of X has even symmetry and the imaginary part has
odd symmetry. More than that, from Exercise 3.11, the magnitude spectrum will have even
symmetry (Even*Even+Odd*Odd) and the phase spectrum will have odd symmetry (Odd/Even).
We present in Fig. 4.6 the even symmetry of the magnitude spectrum. A similar figure can be made
for the phase, but it will present odd symmetry. In the middle we will have a sample for an odd
number of samples N and nothing (just N/2 samples on each side) for N being an even number.

Magnitude

0
1 2
fs /2 fs
0 π 2π
(N-1)/2 N-1

Fig. 4.6 Magnitude spectrum representations

67
Digital Signal Processing with Matlab® Applications

In conclusion, if we make compute the DFT of a N point real sequence, we obtain N different
complex terms, but only the first N/2 are independent, the others being the complex conjugated of
the first ones.
Remark1: This is consistent with the conservation of information principle: we have a
sequence of N floats at input and we have the same information in a sequence of N/2 complex
numbers, each represented on two floats (the real and imaginary parts)
Remark2: It is consistent also with the sampling theorem: all the information in a signal
sampled at fs resides from 0 to fs/2.
More than that, if the input sequence x is defined for negative and positive time instants, we
have two interesting properties: if x is even, then X is real and even; if x is odd, X is complex and
odd.
Example 4.5 Let's try to represent an Impulse signal (variation in time) and its DFT
Magnitude (a characteristic of the frequency domain), without the even symmetry and regarding the
frequency domain.
A=1;fs=100;
t=-100:1/fs:100;
x=myImpulse(1,0,t);
subplot(2,1,1);
plot(t,x),axis([-5,5,-.5,1.5]);
title('Amplitude of the Impulse function'), ...
xlabel('Time (s)');ylabel('Amplitude');
X=fft(x);
Z=abs(X);
N=ceil(length(Z)/2);
Z=Z(1:N);

f = 0: fs/2/(N-1):fs/2;
subplot(2,1,2);
plot(f,Z);
title('Magnitude spectrum - single sided'), ...
xlabel('Frequency (Hz)');ylabel('Magnitude');

Amplitude of the Impulse function


1.5

1
Amplitude

0.5

-0.5
-5 -4 -3 -2 -1 0 1 2 3 4 5
Time (s)
Magnitude spectrum - single sided
1

1
Magnitude

1
0 5 10 15 20 25 30 35 40 45 50
Frequency (Hz)

Fig. 4.7 Time and frequency representation of an Impulse signal

68
4 The Discrete Fourier Transform

Exercise 4.13 Create a function reprAmplitudeMagnitudePhase that will receive a signal


given as its samples value and its sampling frequency or, alternatively, the sampling times
sequence, and will represent it in a 3 subplot figure: the signal, its magnitude spectrum and its phase
spectrum without the symmetry and regarding the frequency domain. There will be stressed out the
discrete nature of the signal and spectrum. Test it on the signal x defined in Example 4.3 (two
sinusoids with noise)

Time domain

Signal amplitude
5

-5
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
Time(s)
Magnitude spectrum
100
Magnitude

50

0
0 50 100 150 200 250 300 350 400 450 500
Frequency(Hz)
Phase apectrum
40
Phase(rad/s)

20

0
0 50 100 150 200 250 300 350 400 450 500
Frequency(Hz)

Fig. 4.8 Time evolution, magnitude and phase spectrum for a sum of two sines and white noise

Exercise 4.14 Test the symmetry property for the previous signal.

4.1.4 DFT Linearity


The Discrete Fourier Transform is linear in the sense that the DFT of a sum of signals is equal
with the sum of the DFTs of each signal. For two signals x and y
DFT ( x  y )  DFT ( x)  DFT ( y ) (4.13)
The major point in this property is that the spectrum of a mixture of signals will be the sum of
the individual spectrums and we will be able to identify some of them (at least the harmonic ones).
Since the DFT of an harmonic wave is an impulse at its corresponding frequency, the
magnitude spectrum shows the weights of the harmonic waves in which a signal can be
decomposed.
If we can find larger components in the frequency spectrum, it means that we have some
sinusoidal waves of the corresponding frequencies that participates plenary in that signal.
If we have a larger component at frequency 0, this means we have a large DC component in the
signal (DC can be also considered as an harmonic wave with frequency zero)
If the magnitude spectrum is uniformly random, this means that we have white noise as the
input signal.
Example 4.6 We will use the function reprAmplitudeMagnitudePhase (it is essential from
now on) to represent the a signal x that is a sum of a constant wave with amplitude 1, a sine of 50
Hz with amplitude 2, a cosine of 123 Hz with amplitude 3 and white noise with amplitude 1.
Consider data sampled at 1000 Hz for 1 second. The plot should look like in Fig. 4.9.

69
Digital Signal Processing with Matlab® Applications

Time domain

Signal amplitude
10

-10
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time(s)
Magnitude spectrum
2000

Magnitude
1000

0
0 50 100 150 200 250 300 350 400 450 500
Frequency(Hz)
Phase apectrum
50
Phase(rad/s)

-50
0 50 100 150 200 250 300 350 400 450 500
Frequency(Hz)

Fig. 4.9 Time/frequency representations for a sum of DC, two sines and white noise

We cannot recognize the components in the time domain, just being able to say we have noise
in the signal. In the frequency/magnitude representation, the DC and the two sines can easily be
recognized. We will make some zooms in the first two axes to check out the time representation and
especially the accuracy of the frequency for the sines (do we really have a sine of 123Hz?). The
results are presented in Fig, 4.10, and we can clearly recognize a spike at 123 Hz, the DFT of the
corresponding sine wave.

Time domain
Signal amplitude

5
0
-5
0.01 0.02 0.03 0.04 0.05 0.06
Time(s)
Magnitude spectrum
1500
Magnitude

1000
500
0
117 118 119 120 121 122 123 124 125 126 127
Frequency(Hz)
Phase apectrum
100
Phase(rad/s)

-100
0 50 100 150 200 250 300 350 400 450 500
Frequency(Hz)

Fig. 4.10 Details from Fig. 4.9 emphasizing the fuzziness of the time representation and the
precision of the magnitude spectrum

4.1.5 DFT Magnitude


If we take a closer look at Fig. 4.9, we can observe that the magnitude values of the DFT for
f=0 and f=50 Hz is 1000 and for f=123 it is 1500. We had the amplitude of DC 1, the sine of 50 Hz
had the amplitude 2 and the sine of 123 Hz had the amplitude 3, so the magnitudes for the sines
keep proportionality.

70
4 The Discrete Fourier Transform

Let us take the DFT of a constant signal having N samples with amplitude A. Replacing in
equation (4.2), we have
N 1 2 N 1 2
j nk j nk
X (k )   Ae N
 Ae N
(4.14)
n 0 n 0

For k=0, all the exponentials are 1 and X(0) is NꞏA. For k≠0, the sum of exponentials is 0 and
the magnitudes are 0.
Exercise 4.15 Prove that the magnitude for the DFT of a sine wave with frequency f0 and
NA
amplitude A, measured on N samples, is for the component at frequency f0 and 0 elsewhere.
2
We have thus a method to measure/recover from the DFT spectrum not only the frequencies of
the harmonic components, but also their amplitudes.

4.1.6 DFT Shifting


The shifting property states that if you change the sampling starting point in a periodic signal x
with m samples, the DFT of the new sequence Xm is related to the DFT of the sequence X through:
2
j mk
X m (k )  e N
X (k ) (4.15)

4.2 The Fast Fourier Transform – FFT

The Fast Fourier Transform is a method to compute efficiently the Discrete Fourier Transform.
It is in fact a collection of methods, because there are several algorithms that perform the DFT of a
sequence with N numbers (usually complex) in Nlog2N operations instead of N2.
Formula (4.4) can be rewritten as:
N 2 1 N 2 1
X (k )   x(2n)WNnk2  WNk
n 0
 x(2n  1)W
n 0
nk
N 2 (4.16)

And for X(k+N/2) we have


N 2 1 N 2 1
kN
X ( k  N 2)   x(2n)W
n 0
nk
N 2  WN 2
 x(2n  1)W
n0
nk
N 2 (4.17)

The two sums that appear in each equation are in fact the DFTs of the odd and even samples
from the sequence, so the process can go further. We will denote them with A(k) and B(k) and we
will put the relations together.
 X (k )  A(k )  W Nk  B(k )
 kN (4.18)
 X (k  N 2)  A(k )  W N 2  B(k )
The two relations are represented graphically in Fig. 4.11.

A(k) X(k)
k
B(k) X(k+N/2)
k+N/2

Fig. 4.11 FFT butterfly scheme

71
Digital Signal Processing with Matlab® Applications

The number under the arrows represents the power at which WN is taken. The schematics
represent the butterfly algorithm and it shows that we can use common terms in order to compute
X(k) and X(k+N/2)
The algorithm extends further, to the input numbers, on a number of levels equals with the base
2 logarithm of the number of points (we halve the computations at each level). For a sequence of 8
numbers, it looks like in Fig. 4.12.

x(0) X(0)
0 0 0
x(4) 4 X(1)
2 1
x(2) X(2)
0 4 2
x(6) 6 X(3)
4 3
x(1) X(4)
0 0 4
x(5) X(5)
4 5
2
x(3) X(6)
0 4 6
x(7) X(7)
4 6 7

Fig. 4.12 An 8 point FFT data flow

We have to take care at the order of the inputs, which has changed. It can be obtained using the
bit reversal method, where the bits for the position are reversed and the new number represents the
input for that position.
Example 4.7 For a 8 point DFT (indices form 0 to 7), for the first position we have index 0,
000 in binary, 000 reversed, 0 in decimal, so we will put x(0) there. For the second position we have
index 1, 001 in binary, 100 reversed, 4 in decimal, so we will put x(4). For the third position we
have index 2, 010 in binary, 010 reversed, 2 in decimal, so we will put x(2) there, etc… For the last
position we have index 7, 111 in binary, 111 reversed, 7 in decimal, so we will put x(7) there.
Exercise 4.16 Write a function that will generate the order of 2N inputs using the bit reversal
method.
Example 4.8 We will present some code for performing FFT. There are two functions:
myFFT that makes the computations and createCoresp that computes the pairs for the butterflies
and the weights on a given level.

function y=myFFT(x)
% this function computes the FFT of the vector x
% with x having a power of 2 number of samples
% (radix 2, decimation in time algorithm)

n=length(x);% number of samples


N=log2(n); % number of levels
W=exp(-2*j*(0:(n-1))*pi/n); % compute the vector WN
x=x(bitrevorder(1:n)); % bit reversal for the input

72
4 The Discrete Fourier Transform

for indexLevel=1:N % for each level of decimation


% compute the coresponding pairs of indices and
% the order of the WN coefficients for the butterfly
[corespMatrix,pow]=createCoresp(N,indexLevel);
for index=1:n/2 % apply the butterfly n/2 times
% butterfly computation
y(corespMatrix(index,:))=x(corespMatrix(index,:))*[1 1;…
W(pow(index,:))];
end
x=y; % make the data transfer to the next level
end

function [corespMatrix,pow]=createCoresp(N,k)
% this function computes the corresponding pairs of indices
% and the order of the asociated weights for the current level k
% out of the total number of N levels

array=transp(1:2^(k-1)); % array of indices


pairArray=array+2^(k-1); % corresponding pair
corespMatrix=[array pairArray]; % create block
pow=(1:2^(N-k):2^N)'; % create weights block
pow=reshape(pow,numel(pow)/2,2);
for index=1:N-k
corespMatrix=[corespMatrix;corespMatrix+2^(index+k-1)] ; % enlarge
the pairs
pow=[pow;pow]; % enlarge the weights
end

To test it against the built-in FFT, create a test file or write in the command line
n=10;
x=rand(1,2^n); % generate a random sequence
sum(abs(myFFT(x)-fft(x))) % compare with FFT

We can claim the equality because


>>ans =
3.5231e-012

4.4 Concluding Exercises

Exercise 4.17 The Fourier transform of a sine function is

a) A sine function b) An impulse function c) Two impulse d) None of them


functions

Exercise 4.18 The magnitude spectrum of the Fourier transform for a Gaussian function is

a) Looking like the b) A complex function c) Looking like a d) None of them


initial function square function

73
Digital Signal Processing with Matlab® Applications

Exercise 4.19 A continuous-time signal x is sampled with the sampling frequency fs = 4 kHz.
We compute the 1024-point DFT X’(k) of 1024 samples from x . What is the spacing between the
frequency samples X(k) in terms of the analog frequency?
Exercise 4.20 A continuous-time signal x is sampled with the sampling frequency fs = 4 kHz.
Can you compute the 1024 -point DFT X’(k) of 1024 samples from x?
Exercise 4.21 A continuous-time signal x is sampled with the sampling frequency fs = 2 kHz.
Can you compute the 999 -point DFT X’(k) of 999 samples from x?
Represent the following basic waveforms (their primitives were set in Chapter 3) amplitude
variation and also magnitude and phase spectrums.
Exercise 4.22 Sine function
Exercise 4.23 Cosine function
Exercise 4.24 Constant function
Exercise 4.25 Step function
Exercise 4.26 Impulse function
Exercise 4.27 Rectangular Pulse function
Exercise 4.28 Linear function
Exercise 4.29 Ramp function
Exercise 4.30 White noise
Exercise 4.31 Sawtooth function
Exercise 4.32 Sinc function
Exercise 4.33 Chirp function
Exercise 4.34 Gauss (bell) function

74
5 Convolution

5 Convolution

5.1 Linear Convolution

We remember from the continuous systems the convolution integral signal z=x*y defined as:

z (t )   x( ) y (t   )d (5.1)


There are several notations for convolution, such as stars and asterisks, but we will use the
notation z=x*y to designate the signal z as the convolution of the signals x and y.
In the digital domain, (5.1) can be written as

z ( n)   x(k ) y (n  k )
k  
(5.2)

For finite causal sequences, where x(k) is zero outside [0 N-1], we have z=x*y, with:
N 1
z ( n)   x ( k ) y ( n  k ) (5.3)
k 0

As can be seen each convolution term is a sum of products of two sequences elements, one
going forward and the other backward. There are several methods used to ease the convolution
computation by hand.
Example 5.1 Let’s compute the convolution of the sequences x=[1 0 2 0] and y = [1 2 3 4].
  
We will put the vectors x and y as lines in a table and we will create the vector y flipping the

vector y from left to right.
Table 5.1 Signal values

y 1 2 3 4

x 1 0 2 0

y 4 3 2 1


We will create another table, with the vector x (0) (meaning no time shift) and the shifted
 
versions of y that overlap x (0). If the vector x has N and y has M elements, the maximum shift of

y to left is –M+1 and to the right is N-1. For each line we will compute and write in the last column
 
the scalar product of the shifted y (k) with the vector x (0) from the first line. We highlighted the

columns where x (0) is defined, the vector being 0 in other positions and not participating in the
product.
The procedure is followed in Table 5.2
Remark: Because the convolution is commutative, it is convenient to flip the vector having
more elements (easier to follow the summation terms).

75
Digital Signal Processing with Matlab® Applications

Table 5.2 Convolution computation

  
x (0) - - - 1 0 2 0 - - - x ꞏ y (k
)


y (-3) 4 3 2 1 - - - - - - 1

y (-2) - 4 3 2 1 - - - - - 2

y (-1) - - 4 3 2 1 - - - - 5

y (0) - - - 4 3 2 1 - - - 8

y (1) - - - - 4 3 2 1 - - 6

y (2) - - - - - 4 3 2 1 - 8

y (3) - - - - - - 4 3 2 1 0
Example 5.2 To verify the calculus, we write:
>> x=[1 0 2 0];
>> y=[1 2 3 4];
>> z=conv(x,y)
z =
1 2 5 8 6 8 0

Exercise 5.1 Find the result of the linear convolution of the vectors and [1 0 2 ] and [1 2 3 4]
Exercise 5.2 What is the number of elements of z=x*y, where x and y have respectively N
and M elements?
Exercise 5.3 For the signals x=[1 2 3 4 5] and y=[7 0 5 0 3 0], make by hand the
convolutions x*y and y*x. Verify the results using the conv function. Do you notice anything?
Exercise 5.4 For x=rand(1,20) and y=rand(1,10), verify that x*y = y*x, using the function
conv. Prove that x*y = y*x.
Exercise 5.5 Make the convolution of the signals
x=[1 2 3 4 5 6 5 4 3 2 1] with h=[1 0 0 0 ]
x=[1 2 3 4 5 6 5 4 3 2 1] with h=[0 0 1 0 ]
What do you notice?
Remark: Convolution, FIR digital filtering (see Chapter 6.2) and the polynomial
multiplication are basically the same operation.

5.2 Circular Convolution

In the case of linear convolution, we cut the infinity extremities of the equation (5.2) in order to
obtain (5.3) with the drawback of loosing important theoretical properties. If the sequences x(n) and
y(n) are periodical with period N, the convolution sum will be periodical and instead of talking of
infinite sequences we can compute the convolution over the initial sequences, extending them
circularly through periodicity when needed.

76
5 Convolution

This way, we define the circular convolution (also called periodic convolution) as a linear
convolution of the periodic extensions of the finite sequences being convolved, in which each of the
finite sequences of length N defines the structure of one period of the periodic extensions.
If x(n) and y(n) are real-valued N-periodic sequences, y(n) is the circular convolution of x(n)
and h(n) defined as
N 1
z ( n)  x ( n) * N y ( n)   x ( k ) ~
y (n  k ) (5.4)
k 0
where ~ y (n) is the extension of y(n) through periodicity.
If one of the sequences is shorter, it must be padded with zeros in order to have the same length
for computing circular convolution. When N is the main period/ length for both sequences, we will
denote the circular convolution
z ( n)  x ( n) * C y ( n) (5.5)
meaning that we have convolution over the whole length of the sequences and nothing more.
The circular convolution will be denoted otherwise as * N , referred as circular convolution modulo
N, where N will be a number indicating the length of the zeros padded sequences to be shifted.
Exercise 5.6 Find the result of the modulo 4 circular convolution of the vectors [1 2 3 4] and
[1 0 2 ]. Compare the result with Exercise 5.1
Exercise 5.7 Complete x and y with zeros in order to have 5 elements each. Compute the
circular convolution and compare with the result of the modulo 5 circular convolution of the initial
vectors [1 2 3 4] and [1 0 2 ]. Compare the result with Exercise 5.1 and Exercise 5.6.
Exercise 5.8 Complete x and y with zeros in order to have 8 elements each. Make their
circular convolution (it is in fact x(n) *8 y (n) ). Compare the result with Exercise 5.1
2  n 2  n
Exercise 5.9 Let x(n)  cos( ) and y (n)  sin( ) . For N=16, compute z  x * c y
N N
The matrix representation of cyclic (or ''circular'') convolution is a circulant matrix:
 x1 x2  x N 1 xN 
x x1  x N 2 x N 1 
 N
C ([ x1 x 2  x N 1 x N ])         (5.5)
 
 x3 x4  x1 x2 
 x 2 x3  xN x1 

Each row of a circulant matrix is obtained from the previous row by a circular right-shift.
Circulant matrices are thus always Toeplitz (but not vice versa).
 x1 x2  x N 1 x N   y1 
x x1  x N 2 x N 1   y 2 
 N
z  x * c y  x * N y  z  C ([ x1 x 2  x N 1
T
x N ]) y T          (5.6)
   
 x3 x4  x1 x 2   y N 1 
 x 2 x3  xN x1   y N 

Exercise 5.10 Write a function that will compute the circulant matrix C corresponding to the
vector x.

77
Digital Signal Processing with Matlab® Applications

Some of the last exercises called attention to the circular and linear convolution properties (like
commutativity). We will shortly enumerate them, including some hints of how they can be used to
simplify the evaluation of the convolution sum or to offer insight in the convolution/filtering
process.

5.3 Convolution Properties

Convolution is a linear operator and, consequently, has a number of essential features including
the commutative, associative, and distributive properties. We will use for the graphical
exemplification (including graphical ones) the case of system theory (filters for DSP, see chapter 6),
where the output of a system y(n) is the convolution of the input x(n) and of the impulse response
h(n), i.e.
y(n)= x(n)*h(n) (5.7)
We continue to use this notation as the default one for convolution for strategic purposes: y(n)
is the universal designation for the output of a system that is obtained by convolution of the input
x(n) with the impulse response h(n) characteristic to the system/filter. The convolution properties
are fundamental for the filtering process and will be used later to obtain simpler equivalent
structures for the filtering blocks.

5.3.1 Commutative Property


The commutative property of the convolution operator states that the order in which two
sequences are convolved does not matter.
x(n)*h(n)=h(n)*x(n) (5.8)
This means that the output of a system with the impulse response h(n) for the input x(n) is the
same as the output of a system with the impulse response x(n) for the input h(n).

x(n) y(n) h(n) y(n)


h(n) x(n)

Fig. 5.1 The commutative property interpretation

Example 5.3 We will test the commutativity for a random h(n) with 1000 coefficients in [0
1] and a random x(n) with 10000 samples in [0 1].
M=1000; N=10000; errAccepted=10^-10;
h=rand(1,M); x=rand(1,M);
z1=conv(h,x);
z2=conv(x,h);
if ~all(abs(z1-z2)<errAccepted),
disp('not commutative')
else
disp('commutative')
end

The result is
>>
commutative

78
5 Convolution

5.3.2 Associative Property


The associative property of the convolution operator states that:
(x(n)*h1(n))*h2(n)= x(n)*(h1(n))*h2(n)) (5.9)
This means that a system formed by two filters with the impulse responses h1(n) and h2(n)
connected in series is equivalent with a system whose impulse response is equal with h1(n)*h2(n).
hseries((h1(n), h2(n))= h1(n)*h2(n) (5.10)

x(n) y(n) x(n) y(n)


h1(n) h2(n) h1(n)  h2(n)

Fig. 5.2 The associativity property interpretation

A direct consequence is the fact that


(x(n)*h1(n))*h2(n)= (x(n)*h2(n))*h1(n) (5.11)
This means that a system formed by filters serially connected, it doesn’t matter the order of the
filters.

x(n) y(n) x(n) y(n)


h1(n) h2(n) h2(n) h1(n)

Fig. 5.3 The associativity property interpretation

Exercise 5.11 Test the associativity for random h1(n) and h2(n) with 1000 coefficients in [0 1]
and a random x(n) with 10000 samples in [0 1].

5.3.3 Distributive Property


The distributive property of the convolution operator states that
x(n)*(h1(n)+h2(n))= x(n)*h1(n)+ x(n)*h2(n) (5.12)
This means that a system formed by two filters with the impulse responses h1(n) and h2(n)
connected in parallel is equivalent with a system whose impulse response is equal with h1(n)+ h2(n).
Hparallel((h1(n), h2(n))= h1(n)+h2(n) (5.13)

h1(n)
x(n) y(n) x(n) y(n)
+ h1(n)+ h2(n)

h2(n)

Fig. 5.4 The distributivity property interpretation

79
Digital Signal Processing with Matlab® Applications

Exercise 5.12 Test the distributivity for random h1(n) and h2(n) with 1000 coefficients in [0
1] and a random x(n) with 10000 samples in [0 1].

5.3.4 Linear Convolution – Circular Convolution Equivalence


We’ve seen in exercise 5.8 that the linear convolution is comparable with the circular
convolution for specific values of the modulo.
If x(n) and h(n) are finite-length sequences of length N and respectively M then:
x(n)*h(n)= x(n) * p h(n), if P = M+N-1 (5.14)
i.e. the P point circular convolution x(n) * p h(n) is equivalent to linear convolution x(n)*h(n) if
P = M+N-1. For P > M+N-1 we will have in x(n) * p h(n) a zeros padded version of x(n)*h(n), but we
can recover x(n)*h(n) after we remove the last P-M-N+1 zeros.
Exercise 5.13 Take the signals x=[1 2 3 4]; y=[1 0 2 0] and compute z= x * y . Compute now
zP= x * P y , with P going from 0 to 8. Verify the equivalence of convolutions for P higher or equal
with 7.

5.3.5 Time-Frequency Correspondence


Circular convolution in the time domain is equivalent with elementwise product in frequency
domain.
y= x *c h  DFT(y)=DFT(x).*DFT(h) (5.15)
Exercise 5.14 For the sequences x=[1 2 3 4]; h=[1 0 2 0], compute y= x *c h . Verify that if
the signals have the DFTs respectively X, H, Y, then Y=X.•H (elementwise product).
Exercise 5.15 Express the reciprocal of property (5.15) and verify it.
Example 5.4 Let us verify the property 5.15 for the signals h being sinc(300*t) and x being
white noise defined for the time t in [0 1] sampled at 1KHz. We will compute y=x*h, X=DFT(x),
H=dft(h) and Y=dft(y). We will represent stacked the graphs of X, H and Y.
fs=1000; t0=0; t1=1;
t=t0:1/fs:t1;
h=sinc(t*300);
x=rand(size(t))-.5;
y=conv(h,x);
H=fft(h);
X=fft(x);
Y=fft(y);

subplot(3,1,1)
plot(abs(X))
title('X'); xlabel('index'); ylabel ('Magnitude')

subplot(3,1,2)
plot(abs(H))
title('H'); xlabel('index'); ylabel ('Magnitude')

subplot(3,1,3)
plot(abs(Y))
title('Y'); xlabel('index'); ylabel ('Magnitude')

80
5 Convolution

X
40

Magnitude
20

0
0 200 400 600 800 1000 1200
index
H
5

Magnitude
0
0 200 400 600 800 1000 1200
index
Y
100
Magnitude

50

0
0 500 1000 1500 2000 2500
index

Fig. 5.5 Convolution in time is equivalent with elementwise product in frequency

As it can be seen, the vector X is uniformly distributed , but the vector Y is maximal on sides,
because of the multiplication with vector H.
Remark: Because of elementwise multiplication, each DFT acts as a mask for the other.

5.4 Fast Convolution

5.4.1 FFT Convolution


The convolution is a method extensively used in DSP, particularly for its employment in the
case of FIR filtering (see Chapter 6.2). There we convolute the coefficients of the filter h(n) (they
can be hundreds) with the input signal x(n) (it can be very long – millions of samples), resulting a
large number of operations.
Exercise 5.16 Suppose you have a system the computes the convolution of a given internal
sequence with 1000 elements with an incoming signal sampled at 48.000 Hz. Assuming that you
need 4 computer clocks to perform a multiplication and 1 for a summation, compute the minimum
frequency of the system, in order to ensure the output at a frequency of 48.000 Hz.
One alternative to compute the convolution of two signals is to use the property that states that
multiplication in the frequency domain corresponds to convolution in the time domain. We can
compute beforehand the DFT of the filter coefficients H(n), transform the input signal into the
frequency domain using the DFT, multiply them, and then transform them back into the time
domain using the Inverse DFT.
Specifically, if h(n) is M points long and x(n) is N points long. h(n) may be linearly convolved
with x(n) as follows:
- Pad with zeros the sequences h(n) and x(n) to respectively hP(n) and xP(n) so that they are of
length P=M+N-1.
- Find the P -point DFTs of hP(n) and xP(n).
- Multiply the DFTs elementwise to form the product Y(k) = H(k)∙X( k ), k  1, P .
- Find the inverse DFT of Y(k) and obtain y=h*x

81
Digital Signal Processing with Matlab® Applications

It looks like it is more difficult to perform convolutions using DFTs, since the effort in
computing a DFT is equivalent with the one to perform a linear convolution (N2 multiplications).
However, using the Fast Fourier Transform (NlogN multiplications) to compute the DFT and the
IDFT (3 FFTs and some other products) significant computational reductions can be realized.
By using the FFT algorithm to calculate the DFT, convolution via the frequency domain can be
faster than directly convolving the time domain signals and the result is the same.. For this reason,
FFT convolution is also called fast convolution.
Exercise 5.17 Find out the number of multiplications necessary to compute the convolution
of if h(n) and x(n) having respectively M and N elements. Compare it to the one required to perform
regularly the linear convolution of the sequences.

x(n) X(n)
FFT
X(n)•H(n) x ( n) * h( n)
IFFT

h(n) FFT H(n)

Fig. 5.6 Fast convolution procedure

Exercise 5.18 For two sequences x and y having 2N elements, compute the number of
operations needed for convolution using the transfer to frequency domain using DFT and back
using IDFT.
Example 5.5 Let us compare the times necessary to compute the convolution of two
sequences x and y having 212 samples using normal convolution and fast convolution.
N=2^12
x=rand(1,N);
h=rand(1,N);
tic
y1 = conv(x,h);% normal convolution
t1=toc
% Now try 'fast' version
X= fft([x zeros(1,N)]); H = fft([h zeros(1,N)]); % transfer to
frequency domain
Y =X.*H; % product element by element
tic
y2=ifft(Y); % transfer to time domain
t2=toc
y2=y2(1:2*N-1);
errAccepted=10^-10;
isOK=all(abs(y1-y2)<errAccepted)

Running this code we obtain something like:


t1 =
0.2096
t2 =
7.3697e-004
isOK =
1

82
5 Convolution

Remark: Please observe again the large difference in the computing times an the fact that this
approach is still perfectible because it uses complex FFT for real sequences – in order to be
comparable, we should have taken the convolution of two sequences of complex numbers or use
sequences of real numbers, but a modified FFT/IFFT algorithm tuned for real values/ conjugate-
symmetric data
In spite of its computational advantages, there are some difficulties with the DFT approach.
For example, if x(n) is very long, we must commit a significant amount of time computing very
long DFTs and in the process accept very long processing delays. In some cases, it may even be
possible that x(n) is too long to compute the DFT. The solution to these problems is to use block
convolution, which involves segmenting the signal to be filtered, x(n), into blocks. Each block is
then convoluted with h(n), and the output blocks are pieced together to form the sequence y(n).
There are common two block convolution techniques. One of them is called overlap-save, and the
other - overlap-add.

5.4.2 The Overlap-Save Method


The overlap-save process splits the input x(n) of length N into blocks of length L with L≥ M+1,
where M is the length of h(n). The convolution of each block with the filter is computed using fast
convolution, but only the last L elements in a segment are not aliased – the first P-L are discarded.
The output segments are concatenated to obtain the convolution result.
Algorithm: The following steps describe the process illustrated in Fig. 5.7:
- Apply P-point FFT to the expanded (zero-padded) impulse response sequence h(n) to obtain
H(n), where n = 1, P
- Split the input sequence x(n) into K blocks of length L xm(n), where m is the segment index,
m = 1, K and n is the index inside the block n= 1, L – the last block will be padded with zeros.
- Apply P-point FFT to the blocks padded to the left with zeros to obtain Xm(n), where m =
1, K and n = 1, P .

- Multiply the H(n) with each Xm(n) to get Ym(n) = H(n)Xm(n), with m = 1, K and n = 1, P .

- Perform P-point IFFT of Ym(n) to obtain ym(n), with m = 1, K and n = 1, P ..


- Discard the first P-L samples from each ym(n). Concatenate the resulting K segments having
L samples and discard the samples from M+N-1 to KꞏL. You’ve obtained y(n).

83
Digital Signal Processing with Matlab® Applications

M P
h(n)

L 2L 3L N KL
x(n)

P
x1(n) P
P-L x2(n) P
P-L x3(n)
P-L P
xk(n)
P P-L
y1(n) + P
P-L y2(n) + P
P-L y3(n) +
P-L P
yk(n)
P-L

y(n) M+N-1 KL

Fig. 5.7 Overlap-Save Block Convolution Method

Exercise 5.19 Write a function OverlapSave(h,x) that will return the convolution of the
sequences h and x using the overlap-add method.
Remark: The function should comply with the following specifications:
- The FFT length P should be a power of 2.
- P≥ L+M-1 and L≥ M+1
- You will segment the longer input signal from x(n) or h(n), suing the symmetry property of
convolution.
- The function must return a vector that has the correct length M+N-1, with no extra zeros.
Exercise 5.20 How many DFTs and inverse DFTs of length N = 64 are necessary to linearly
convolve a sequence x(n) of length 1000 with a sequence h(n) of length 64 using the overlap-add
method?

5.4.3 The Overlap-Add Method


We have that:
M 1
y ( n)  h ( n ) * x ( n )   h( k ) x ( n  k ) (5.2)
k 0

Assume that x(n) = 0 for n<0 and that N - the length of x(n) is much greater than M-the length
of h(n) . In the overlap-add method, x(n) is partitioned into non-overlapping subsequences of length
L as illustrated in Fig. 5.8 Thus, x(n) may be written as a sum of shifted finite-length sequences of
length L,

84
5 Convolution

Algorithm: The following steps describe the process illustrated in Fig. 5.8:
- Apply P-point FFT to the expanded (zero-padded) impulse response sequence h(n) to obtain
H(n), where n = 1, P
- Split the input sequence x(n) into K blocks of length L xm(n), where m is the segment index,
m = 1, K and n is the index inside the block n= 1, L – the last block will be padded with zeros.
- Apply P-point FFT to the blocks padded to the right with zeros to obtain Xm(n), where m =
1, K and n = 1, P .

- Multiply the H(n) with each Xm(n) to get Ym(n) = H(n)Xm(n), with m = 1, K and n = 1, P .

- Perform P-point IFFT of Ym(n) to obtain ym(n), with m = 1, K and n = 1, P ..


- Concatenate the blocks ym(n) and if there is a superposition, add the values. Discard the
samples from M+N-1 to (K-1) L+P. You’ve obtained y(n).

M P
h(n)

L 2L 3L N KL
x(n)

L P
x1(n) L P
x2(n) L P
x3(n)
L P
xk(n)

L P
y1(n) + L P
y2(n) + L P
y3(n) +
L P
yk(n)

y(n) M+N-1 (K-1)L+P

Fig. 5.8 Overlap-Add Block Convolution Method

Exercise 5.21 Write a function OverlapAdd(h,x) that will return the convolution of the
sequences h and x using the overlap-add method.
Remark: The function should comply with the following specifications:
- The FFT length P should be a power of 2.
- P≥ L+M-1 and L≥ M+1

85
Digital Signal Processing with Matlab® Applications

- You will segment the longer input signal from x(n) or h(n), suing the symmetry property of
convolution.
- The function must return a vector that has the correct length M+N-1, with no extra zeros.
Exercise 5.22 How many DFTs and inverse DFTs of length N = 64 are necessary to linearly
convolve a sequence x(n) of length 1000 with a sequence h(n) of length 64 using the overlap-add
method?
The conv function in MATLAB actually calls filter to do its computation. This is efficient
when the lengths of the signals to be convolved are small. However, if the signal lengths are long
(e.g., > 30), filter is quite slow and a convolution based on the FFT should be used. In fact, such an
option is available in MATLAB with the fftfilt function.

5.4.4 Fast Convolution Exploits


The first experiment to run involves a comparison of FFT convolution versus direct
convolution, as implemented with conv. For extremely short convolutions, the direct convolution is
more efficient, but for longer ones the log2 N behavior of the FFT makes it much faster. A generally
quoted number for the crossover point where the FFT has fewer operations is N  32 if you look for
the closest power of 2. However, this number is a function of the precise implementation, so we
would like to deduce MATLAB’s crossover point.
Example 5.6
errAccepted=10^-10;
maxPower=11;
powerArray=1:maxPower;

for index=powerArray
N=2^index;
x=rand(1,N);
y=rand(1,N);
tic
z1 = conv(x,y);% normal convolution
t1(index)=toc;
% Now try 'fast' version
X= fft([x zeros(1,N)]); Y = fft([y zeros(1,N)]); % transfer to
frequency domain
Z =X.*Y; % product element by element
tic
z2=ifft(Z); % transfer to time domain
t2(index)=toc;
z2=z2(1:2*N-1);

if ~all(abs(z1-z2)<errAccepted)
error('different results')
end

end

86
5 Convolution

Comparison of computation times for convolution


0.09
Normal convolution
0.08 Fast convolution

0.07

0.06

0.05

time (s)
0.04

0.03

0.02

0.01

0
1 2 3 4 5 6 7 8 9 10 11
Log2(Length of the sequence)

Fig. 5.9 Comparison of computation times for normal and fast convolution

The MATLAB function used to perform fast convolution is called fftfilt. It is in fact used
for filtering, so the output is truncated to the same number of elements as the input.

5.5 Concluding Exercises

Exercise 5.23 Test the associative property of the convolution operator for random h1(n) and
h2(n) with 1000 coefficients each in [0 1] and a random input x(n) with 10000 samples in [0 1].
Exercise 5.24 Test the distributive property of the convolution operator for random h1(n) and
h2(n) with 1000 coefficients each in [0 1] and a random input x(n) with 10000 samples in [0 1].

87
Digital Signal Processing with Matlab® Applications

6 Digital Filters

6.1 Digital Filter Properties

6.1.1 Digital Systems


A filter is a system that selectively changes an entity, favoring some of its parameters and
inhibiting others. For signals, filters change the wave characteristics (shape, amplitude, frequency
and/or phase) in a desired manner. A digital filter will thus be the procedure implemented in
hardware and/or software, which will operate upon a digital input signal to produce a digital output
signal for the purpose of achieving a filtering objective.
Common filtering objectives are:
- to improve the quality of a signal (e.g. to remove or to reduce noise);
- to extract information from signals;
- to separate two or more signals previously combined, in order to make better use of the
communication channel .
Ideal frequency filter passes certain frequencies without any change and completely stops the
other frequencies. The range of frequencies that are passed without attenuation is the Pass Band
(PB) of the filter, and the range of frequencies that are cut is the Stop Band (SB).
We consider a filter as a black-box system, which has certain inputs, and gives some outputs.
To this black-box we associate a transfer function H which maps the inputs to the outputs.

continuous time
x(t) y(t)
H()

x(n) H(z) y(n)


discrete time

Fig 6.1 Black-box representation of a filter (continuous/discrete case)

The operator (transfer function) H acts upon the input signals and produces the output signals.
y=Hx (6.1)
Remark: y is not the product of H and x, but the transformation of x by H.
We will now repeat some properties from the system theory.
A system is additive if the output produced for the sum of two input signals is equal to the sum
of the outputs produced for each input individually:
H(x+y)=Hx+Hy (6.2)
A system is homogenous if the output produced for a product of an input signal with a constant
is equal to the product of that constant with the output for the initial signal
H(αꞏx)=αꞏHx (6.3)

88
6 Digital Filters

A system that is both additive and homogenous is said to be linear. For a linear system:
H(αꞏx+βꞏy)=αꞏHx+βꞏHy (6.4)
The equation (6.1) is valid also at a specific moment in time t:
y(t)=Hx(t) (6.5)
A system is time invariant if and only if
y(t-τ)=Hx(t-τ) (6.6)
holds for any τ and any x(t). t). A time-invariant system is also called a fixed system or
stationary system. A system that is not time invariant is called a time-varying system, variable
system, or nonstationary system. A system that still doesn’t react (give output) to (recent) input is
called a relaxed system.
A system is called memoryless or without memory if the output at any time depends only on
the input at the same time. Otherwise, the system is with memory.
A system is causal if its output at a time t depends only on the inputs received at time instants
earlier or equal with t. This can be stated also as the fact that identical histories go to identical
state(non-implication of the future). More exactly:
If any two inputs x1 and x2 are identical prior to time τ
x1(t)=x2(t) for  t≤τ (6.7)
a system is called causal if and only if
Hx1(t)=Hx2(t) for  t≤τ (6.8)
A noncausal or anticipatory system is one in which the present output depends upon future
values of the input. Noncausal systems are encountered only in theory and if the output depends
only on a finite set of future inputs, the system can be made causal, just delaying the output until we
have all the necessary input values.
Let us denote with h(n,k) the response of a linear time-invariant digital system in moment n to
the impulse δ(k). The output at instance n, y(n) will be a superposition of the effects produced by
each input x(k), weighted by its impulse response h(n,k) – the dependence of output n of input k.

y ( n)   h(n, k )  x(k )
k  
(6.9)

Since the system is time-invariant the impulse response h(n,k) depends only in the delay n-k,
so we can write:

y ( n)   h( n  k )  x ( k )  h( n)  x ( n)
k  
(6.10)

The impulse response is designatory for the behavior of a system and can be obtained by
computation or by applying an impulse signal at the input of a system.
The output of a system can be found as the convolution of the system impulse response and of
the corresponding input. If a digital filter's impulse response h(n) is finite length, and the input x(n)
is also finite length (the practical case) the filter output can be computed digitally exactly using
convolution.
In the general case (6.10), if we make the Fourier transform, we obtain that
Y ( )  H ( )  X ( ) (6.11)

89
Digital Signal Processing with Matlab® Applications

where H(ω) represents the transfer function of the system/filter.


For ideal filter we have the following magnitude transfer function:

1, in PB
H ( )   (6.12)
 0, in SB
where PB denotes the Pass Band – the frequency domain where harmonic components passes
unaltered and SB represents the Stop Band – a domain where the frequency components do not
pass at all.
The simplest case is that of an ideal Low-Pass (LP) digital filter, whose frequency response
can be expressed as:

1, for    c
H ( )   (6.13)
 0, for  c    
where c is the cutoff frequency corresponding to the location of a sharp cutoff edge,
representing the transition from the frequencies that pass unaltered to the ones that are blocked, as
shown in Fig. 6.2(a). The frequency response can have real values and thus it will represent directly
the magnitude response of the filter, the phase being zero. The ideal frequency responses of other
basic frequency-selective filters are shown in Fig. 6.2.

H(ω) H(ω)
(a) (b)
1 1

0 ωc π ω 0 ωc π ω

H(ω) (c) H(ω) (d)

0 ωc1 ωc2 ω ωc1 ωc2 ω

Fig. 6.2 Basic ideal filter types (a) Low Pass Filter, (b) High Pass Filter,
(c) Band Pass Filter, (d) Band Stop Filter

We discussed in chapter 4.1.2 and we represented graphically in Fig. 4.5 some equivalent
methods to express the frequency

90
6 Digital Filters

H(ω) H(f)
(a) (b)
1 1

0 ωc π ω 0 fc fs/2 f

H(w) DFT(h(n))
(c) (d)
1 1

0 wc 1 w 0 nc N/2 n

Fig. 6.3 Equivalent transfer function representations: (a) (analog) pulsation, (b) frequency,
(c) normalized frequency, (d) (impractical) DFT of a finite impulse response

The filter effect to selectively allow spectral components to pass is given by property (6.11)
and we described it in chapter 5.3.5. Because we have elementwise product in the frequency
domain, the transfer function characteristic will act like a mask on the input signal spectrum, letting
to pass unaltered the components whos frequency is in the domain where |H(f)|=1, canceling the
components whos frequency is in the domain where |H(f)|=0 and attenuating the others
corresponding to their |H(f)|.
X(f)
1

0 fs/2 f
H(f)
1
*

0 fc fs/2 f
Y(f) =
H(f).*X(f)
=
1

0 fc fs/2 f

Fig. 6.4 Filter effect in frequency domain

In the practical case we can take only a truncation of the impulse response (we have a finite
domain of representation in computing devices), so we will have differences comparing with the
ideal/theoretical case. In Fig. 6.5 we present superposed the transfer function of an ideal low pass
filter (LPF) – thick black line and the one of the obtainable filter.

91
Digital Signal Processing with Matlab® Applications

1-δp
1 Ap
1+δp

As

δs
0 fp fc fs Fs/2 f

Fig. 6.5 The real transfer function for a LPF

The ideal filter parameters are


- The cutting frequency fc that in the digital case is normalized with fs/2: what we need is
fc
wc 
FS / 2
Practical filter specification includes some tolerances like:
- Transition Band width: from fp to fs;
- Admissible noise/ ripple in PB: δp;
- Admissible noise/ ripple in SB: δs;
The practical filter specifications/performances depend heavily on the order of the filter n (the
number of filter coefficients), that is usually the second parameter (besides the normalized cut-off
frequency wc) needed for the filter design.
In the passband, the frequency response is required to satisfy
1  p  H  f   1  p , 0  f  f p (6.14)

and in the stopband


H  f    s , f s  f  FS / 2 (6.15)

with the response unspecified in the transition band.


s  H  f   1  p f p  f  fs , (6.16)

Given the set of specifications, filter design consists in obtaining an analytical approximation
to the desired filter characteristics, in the form of the transfer function, given as H(s) for continuous
time systems (analog filters) or as H(z) for discrete time systems (digital filters).
Usually the transfer function is strictly rational function, i.e. it is the ratio of two polynomials
with the degree of the denominator greater than the degree of the numerator. For those polynomials
we may compute the roots. Roots of the denominator are called poles, and the roots of numerator
are called zeroes. The plot of poles and zeroes in the s-plane is called pole-zeroes diagram. This is
another powerful instrument for filter analysis and design.

92
6 Digital Filters

Initially filters were of analog type, there were few applications with digital filters. So, there is
a lot of theory and studies on analog filters. Lately, applications for digital filters were developed.
There is a straight forward transformation from analog to digital filters. We have to pass from s-
plane to z-plane, i.e. from continuous Laplace transform to discrete Laplace transform.

6.1.2 Types Of Digital Filters


The input-output difference equation that associates the input and the output of a digital filter
can be written as:
N M
y (n) bk  x(n  k ) ak  y (n  k ) (6.17)
k 0 k 1

or it can be expressed in the z domain as the transfer function H(z):


N

1
b  b  z  ...  bn  z n b k  z k
H ( z ) 0 1 1  k 0
(6.18)
a0  a1  z  ...  am  z m M

a
k 0
k z k

We can simplify the relation (6.18) with a0 and obtain a new set coefficients, where a0=1.
The filter coefficients start with in MATLAB with index 1, instead of 0, because of the
standard indexing scheme used for vectors. MATLAB stores the coefficients in two row vectors,
one for the numerator – b and one for the denominator – a. The transfer function H(z) will be:
b(1)  b(2)  z 1  ...  b(n  1)  z  N
H ( z ) (6.19)
1  a (2)  z 1  ...  a (m  1)  z M
and it is defined univocally by the row vectors a and b. The order of the filter is defined as the
maximum of n and m.
The filter difference equation (6.17) will become in MATLAB:
N 1 M 1
y (n) bk  x(n  k  1)  ak  y (n  k  1) (6.20)
k 1 k 2

When n = 0 (that is, b is a scalar), the filter is an Infinite Impulse Response (IIR), all-pole,
recursive, or autoregressive (AR) filter.
When m = 0 (that is, a is 1), the filter is a Finite Impulse Response (FIR), all-zero,
nonrecursive, or moving-average (MA) filter.
If both n and m are greater than zero, the filter is an IIR, pole-zero, recursive, or autoregressive
moving-average (ARMA) filter.

6.1.3 Filter Characteristics


The digital filters are characterized by specific aspects, some related to their behavior in the
time domain and other by their behavior in the frequency domain. All of them are determined by the
values of the coefficient vectors b and a and the design of a filter involves primarily the finding of
these coefficients.
Regarding the time, the main instruments are the differential equation of the filter, as in (6.17)
and the transfer function (6.18) (they are equivalent).

93
Digital Signal Processing with Matlab® Applications

A digital filter receives an input and operate on it according to equation (6.20). The MATLAB
function y=filter(b,a,x) will return the output y of a filter described by the vectors a and b
acting on the input x.
Exercise 6.1 For the filter defined by b=[1 2 3] and a=1, write its differential equation.
Make the substitutions and compute its output for the input x=[1 0 2 0].
Example 6.1 We will find out the output of the filter from Exercise 6.1 from the given input
b=[1 2 3];
a=[1];
x=[1 0 2 0];
y=filter(b,a,x)

We have that
y =
1 2 5 4

Still here, we get insight into the characteristics of the system dynamics watching the response
of the filter to specific inputs, such as the step response and the impulse response.
For the impulse response, we track the output of the filter when it receives as input an impulse.
The MATLAB function for the impulse response is [h,t]=impz(b,a), and it returns both the
impulse response h and the time samples t when h is evaluated for the digital filter whose transfer
function is determined by the numerator and denominator represented by the vectors b and a,
respectively
For the step response, we watch the output of the filter when it receives as input a step
function. The MATLAB function for the step response is [h,t]=stepz(b,a), and it returns both
the step response h and the time samples t when h is evaluated for the digital filter determined by
the vectors b and a.
From the frequency point of view, we are interested in the frequency response and in the
phase response.
The function [H,f]=freqz(b,a,fs) returns the frequency response complex vector H and
the corresponding frequency vector f, where H is evaluated for the digital filter determined by the
vectors b and a working for the sampling frequency fs. Since we are interested usually in the
magnitude (modulus) of H, we use abs(H) for the representation (usually with plot, since we are
interested in the envelope) of the frequency response magnitude.
The function [phi,f]=phasez(b,a,fs) returns the frequency response phase vector phi (it
is the unwrapped phase of H) and the corresponding frequency vector f, where phi is evaluated for
the digital filter determined by the vectors b and a working for the sampling frequency fs.
Crucial information is given by the position in the complex plane of the transfer function poles
(points where the denominator represented by a is zero) and of the transfer function zeros (points
where the numerator represented by b is zero).
The poles and zeros can be found either using the function roots to find the solutions of the
polynomial equations given by a and b and represented them later or plotted directly using
zplane(b,a) where b and a are row vectors - zeros are marked with 'o's, poles are marked with
'x's and multiple zeros or poles are indicated as a multiplicity number in the upper right part of the
zero or pole.
Remark: zplane(p,z) plots directly the zeros specified in column vector z and the poles
specified in column vector p.

94
6 Digital Filters

Example 6.2 We will consider an FIR filter defined by b=[1 2 3] and a=1. Its differential
equation is y(n)=x(n)+2∙x(n-1)+3∙x(n-2) and its transfer function is H(z)=1+2∙z-1+3∙z-2. We will
determine the impulse response, step response and we will show them in Fig. 6.6, as well as the
frequency response and the phase response, shown in Fig. 6.7, and in the end we will determine and
represent in Fig. 6.8 the poles and zeros of this filter.
b=[1];
a=[1 -2];

[h,t]=impz(b,a,20); %20 samples for a better view


subplot(1,2,1)
stem(t,h);
title('Impulse response'); xlabel('index'); ylabel('Amplitude')

[h,t]=stepz(b,a,20); %20 samples for a better view


subplot(1,2,2)
stem(t,h);
title('Step response'); xlabel('index'); ylabel('Amplitude')

Impulse response Step response


3 6

2.5 5

2 4
Amplitude

Amplitude

1.5 3

1 2

0.5 1

0 0
0 5 10 15 20 0 5 10 15 20
Index Index

Fig. 6.6 Impulse response and step response for the FIR filter y(n)=x(n)+2ꞏx(n-1)+3ꞏx(n-2)

[H,f]=freqz(b,a);
subplot(1,2,1)
plot(f,abs(H));
title('Frequency response'); xlabel('index'); ylabel('Magnitude')

[phi,f]=phasez(b,a);
subplot(1,2,2)
plot(f,phi);
title('Phase response'); xlabel('index'); ylabel('Phase')

95
Digital Signal Processing with Matlab® Applications

Frequency response Phase response


6 0

5.5
-1

5
-2
4.5

-3

Magnitude
4

Phase
3.5 -4

3
-5
2.5

-6
2

1.5 -7
0 1 2 3 4 0 1 2 3 4
Frequency Frequency

Fig. 6.7 Frequency response and phase response for the FIR filter y(n)=x(n)+2ꞏx(n-1)+3ꞏx(n-2)

zplane(b,a)
title('Filter poles and zeros');

Filter poles and zeros


1.5

0.5
Imaginary Part

2
0

-0.5

-1

-1.5
-1.5 -1 -0.5 0 0.5 1 1.5
Real Part

Fig. 6.8 Poles and zeros for the FIR filter y(n)=x(n)+2ꞏx(n-1)+3ꞏx(n-2)

The poles and the zeros can be displayed with


disp(['The poles are: ' num2str([roots(a)]')])
disp(['The zeros are: ' num2str([roots(b)]')])

and we found that


The poles are:
The zeros are: -1-1.4142i -1+1.4142i

In the past example we chosen 20 samples for the impulse response and step response
representation in order to have a better view, otherwise the responses are 3 samples long (the step
response evens out to 0).
Exercise 6.2 Prove that the impulse response of a FIR filter is made by its coefficients

96
6 Digital Filters

Exercise 6.3 Prove that the step response of a FIR filter is made by the cumulated sum of its
coefficients.
As we have seen in exercise 6.2, the impulse response of a FIR filter is represented by its
coefficients, so it is finite in time (after a number of output samples equals with the filter length, it
levels to 0) and also in amplitude (its maximum magnitude of output is given by the largest
coefficient in absolute value). This is the true reason for which the filters that have a=1 (the current
output depends linearly only on the input sequence) are called Finite Impulse Response Filters.
Exercise 6.4 Represent the characteristics shown in Example 6.2 for the all-pole IIR filter
defined by b=1 and a=[1 -2]. Its differential equation is y(n)=x(n)+2∙y(n-1) and its transfer function
1
is H ( z )
1  2  z 1
We present in Fig. 6.9-6.11 the characteristics required in Exercise 6.4..
5 5
x 10 Impulse response x 10 Step response
6 12

5 10

4 8
Amplitude

Amplitude

3 6

2 4

1 2

0 0
0 5 10 15 20 0 5 10 15 20
Index Index

Fig. 6.9 Impulse response and step response for the IIR filter y(n)=x(n)+2ꞏy(n-1)

Frequency response Phase response


1 6.5

0.9 6

0.8 5.5

0.7 5
Magnitude

Phase

0.6 4.5

0.5 4

0.4 3.5

3
0 1 2 3 4 0 1 2 3 4
Frequency Frequency

Fig. 6.10 Frequency response and phase response for the IIR filter y(n)=x(n)+2ꞏy(n-1)

97
Digital Signal Processing with Matlab® Applications

Filter poles and zeros

0.5

Imaginary Part
0

-0.5

-1

-1 -0.5 0 0.5 1 1.5 2


Real Part

Fig. 6.11 Poles and zeros for the IIR filter y(n)=x(n)+2ꞏy(n-1)

If we try to represent the impulse response for this filter, we find out that it is unbounded.
> [h,t]=impz(b,a)

h =
1
2
4
8
16
32
64
128
256
512
1024
2048
4096….

Exercise 6.5 Find y(n) as a function of n for the filter defined in Exercise 6.4. Prove that y(n)
is unbounded.
As we have seen in Exercise 6.4 and in Exercise 6.5, the impulse response of a IIR filter
(where a is not a scalar anymore - the current output depends linearly also on previous values of the
output) can be unbounded both in time (it never ends) and in amplitude. This is the true reason for
which the filters that have reaction (a is a polynomial of degree larger than 1) are called Infinite
Impulse Response Filters (IIR).

6.2 FIR - Finite Impulse Response Filters

Finite Impulse Response (FIR) filters assumes that the output at any time depends linearly
just on the current and the previous inputs:
N
y ( n )  h ( k )  x ( n  k ) (6. 21)
k 0

98
6 Digital Filters

The transfer function for FIR will be:


N
H ( z ) h ( k )  z  k (6.22)
k 0

A filter modifies a signal and the change can be seen from both time and frequency domains.
Sometime we are interested in the improvement of the time/amplitude properties of a signal and we
will design the filters for elimination of the noise, smoothing of a signal, etc…, that operates in the
time domain.
Other times we want to remove/augment specific frequency components by designing specific
transfer function/ frequency response. There are several methods for obtaining the transfer function
of a FIR filter and we will regard the most used of them:
- window method
- optimal method
- frequency sampling method

6.2.1 Time Domain Filtering


6.2.1.1 Moving average filters

The moving average is the most common filter in DSP, mainly because it is the simplest one.
In spite of its plainness, it is often used for time domain encoded signals, being optimal for the
reduction random noise while retaining a sharp step response. In the same time, the moving average
filter is the worst filter for frequency domain encoded signals, with little ability to separate one band
of frequencies from another.
A moving average filter of order N-1 is a FIR filter that has the current output equal with the
average of the last N inputs.
N 1
1
y ( n )
N
 x(n  k )
k 0
(6.23)

Let us presume that we measure a variable that is both slowly varying and also corrupted by
random noise. Since nearby points measure very nearly the same underlying value, averaging can
reduce the level of noise, so it can be useful to replace each data point by some kind of local
average of surrounding data points. This procedure is called smoothing and moving average filters
are excellent smoothing filters.
Example 6.3 Let us use the moving average filter to remove the noise from a sine signal.
fs=50;t0=0;tf=3;
t=t0:1/fs:tf; x=mySine(1,1,0,t);
subplot(3,2,1), plot(t,x,'.-'), axis([t0 tf -2 2])
title('Original signal'), xlabel('Time (s)'), ylabel('Amplitude')
x=x+myWhiteNoise(1,t); subplot(3,2,2), plot(t,x,'.-')
axis([t0 tf -2 2]),
title('Noisy signal'), xlabel('Time (s)'), ylabel('Amplitude')

for index=1:4
N=2^index; b=ones(1,N)/N; y=filter(b,1,x);
subplot(3,2,index+2),plot(t,y,'.-'), axis([t0 tf -2 2])
title(['Filtered signal N = ' num2str(N)]), xlabel('Time (s)')
ylabel('Amplitude')
end

99
Digital Signal Processing with Matlab® Applications

Original signal Noisy signal


2 2

Amplitude

Amplitude
0 0

-2 -2
0 1 2 3 0 1 2 3
Time (s) Time (s)
Filtered signal N = 2 Filtered signal N = 4
2 2
Amplitude

Amplitude
0 0

-2 -2
0 1 2 3 0 1 2 3
Time (s) Time (s)
Filtered signal N = 8 Filtered signal N = 16
2 2
Amplitude

Amplitude
0 0

-2 -2
0 1 2 3 0 1 2 3
Time (s) Time (s)

Fig. 6.12 Denoising using moving average filters with N coefficients

In order to increase the performance in the frequency domain, we can use the multiple pass
moving average filters, which use repeatedly a relatively low order moving average filter.
6.2.1.2 Savitzky-Golay smoothing filters

Savitzky-Golay smoothing filters (also called digital smoothing polynomial filters or least
squares smoothing filters) are typically used to "smooth out" a noisy signal whose frequency span
(without noise) is large. In this type of application, Savitzky-Golay smoothing filters perform much
better than standard averaging FIR filters, which tend to filter out a significant portion of the
signal's high frequency content along with the noise. Although Savitzky-Golay filters are more
effective at preserving the pertinent high frequency components of the signal, they are less
successful than standard averaging FIR filters at rejecting noise when noise levels are particularly
high. The particular formulation of Savitzky-Golay filters preserves various moment orders better
than other smoothing methods, which tend to preserve peak widths and heights better than
Savitzky-Golay.
Savitzky-Golay filters are optimal in the sense that they minimize the least-squares error in
fitting a polynomial to each frame of noisy data. We can use b = sgolay(k,f) to obtain a SG
filter, or filter directly the data using y = sgolayfilt(x,k,f) applies a Savitzky-Golay FIR smoothing
filter to the data in vector x. The polynomial order k must be less than the frame size, f, which must
be odd. If k = f-1, the filter produces no smoothing.
Exercise 6.6 Use a Savitzky-Golay filter to smooth the data from Example 6.3.
Exercise 6.7 Find and compare the MSE (Mean Square Error) of the recovered signal
relative to the initial one from the previous two exercises.
6.2.1.3 Matched Filters

Matched filters are used to determine the existence/position of particular waveforms buried
within other signals. This filters use as kernel a mirrored template of the signal you are looking for.
This template is known as a matched filter and if, during the convolution (that is why we mirror the

100
6 Digital Filters

template), it overlaps with something similar in shape, the output will be very large (local
maximum).
Example 6.4 We will use a matched filter to extract triangular signals mingled with a
sinusoid and noise. The matched filter template will have also triangular form, with similar, but
different values. After a high pass filtering ( to eliminate the residual sine) we obtain the triangular
waveforms, with delays because of the filtering.
fs=200;t0=0;tf=3;
t=t0:1/fs:tf;
t1=0;t2=1.5;t3=2;dt=.1;
x=myRampPulse(3,t1,t1+dt,t)+myRampPulse(3,t2,t2+dt,t)+myRampPulse(3,t
3,t3+dt,t);
subplot(2,2,1), plot(t,x)
title('Original signal'), xlabel('Time (s)'), ylabel('Amplitude')
x=x+myWhiteNoise(2,t)+mySine(2,1,0,t);
subplot(2,2,2)
plot(t,x)
title('Noisy signal'), xlabel('Time (s)'), ylabel('Amplitude')
b=[2:-.1:0]; a=1;
y=filter(b,a,x);
subplot(2,2,3), plot(t,y)
title('Matched Filtered signal'), xlabel('Time (s)'),
ylabel('Amplitude')
b=fir1(100,.03,'high');
z=filter(b,a,y);
subplot(2,2,4),plot(t,z)
title('HP Filtered signal'), xlabel('Time (s)'), ylabel('Amplitude')

Original signal Noisy signal


3 10

2 5
Amplitude

Amplitude

1 0

0 -5
0 1 2 3 0 1 2 3
Time (s) Time (s)
Matched Filtered signal HP Filtered signal
100 40

50
20
Amplitude

Amplitude

0
0
-50

-100 -20
0 1 2 3 0 1 2 3
Time (s) Time (s)

Fig. 6.13 Finding triangular waveform in noise using matched filters

101
Digital Signal Processing with Matlab® Applications

6.2.2 Window Method


6.2.2.1 Ideal Frequency response

Let us take an ideal digital lowpass filter with a cut-off frequency of c rad/s. It will have the
desired (ideal) frequency response H d   equal with 1 inside [- c c ] and 0 otherwise (we take
both lateral bands).
 1, for   [c c ]
H d     (6.24)
0, otherwise

Hd(ω)
1

-π -ωc 0 ωc π ω

Fig. 6.14 Ideal frequency response for a LP FIR Filter (both bands)

Exercise 6.8 Write a function plotIdealFrequencyResponse(omega) that will plot the ideal
   3 5 
frequency response H d   for ωc given. Test it for ωc = , , , , . The result for ωc =
6 4 2 4 6 4
should look like in Fig. 6.15.
Ideal frequency response for c = 0.25•
1.5

1
H()

0.5

-0.5
-3 -2 -1 0 1 2 3
 (rad/s)


Fig. 6.15 Ideal LPF frequency response for ωc =
4

Its corresponding impulse response sequence hd n  is


1  1    c
hd n     H  e d  2   e d  
 j n c j n c
sinc( n) (6.25)
2 
d
  c

102
6 Digital Filters

Exercise 6.9 Write a function plotIdealImpulseFrequencyResponse(omega,N) that


will plot the impulse response hd(ωc ,n) and the frequency response Hd(ωc ,n)for ωc given and
   3 5
n  N , with N=10, 100, 1000 (three subplots). Test it for ωc = , , , , . The result for
6 4 2 4 6

ωc = should look like in Fig. 6.16
4

0.5 2

H()
h(n)

0 1

-0.5 0
-10 -5 0 5 10 -4 -2 0 2 4
n  (rad/s)
0.5 2

H()
h(n)

0 1

-0.5 0
-100 -50 0 50 100 -4 -2 0 2 4
n  (rad/s)
0.5 2
H()
h(n)

0 1

-0.5 0
-1000 -500 0 500 1000 -4 -2 0 2 4
n  (rad/s)

Fig. 6.16 Real LPF impulse response and frequency response



for ωc = and orders 20, 200, 2000
4

We encountered in Exercise 6.9 an impediment induced by (6.25): we are not able to plot the
impulse response of the filter because it is infinite. The filter described by (6.25) is not
implementable since its impulse response is infinite and noncausal. To create a finite-duration
impulse response, we will truncate it by applying a window and retaining the central section of
impulse response.
The effect on the frequency response (ripple, large transition) is given by the change of the
infinite Fourier series representation for H d   to a finite one for the obtainable filter – Gibbs
phenomenon.
H   corresponds in fact to a finite Fourier series approximation for H d   :
N
H     h(n)e  jnT (6.26)
n 0

where The simplest and most obvious way to design an FIR filter is just to truncate the ideal
response hd n  outside the interval 0  n  N to produce hn  , i.e.,

h (n), n  0,1,..., N
h( n)   d (6.27)
 0, otherwise

103
Digital Signal Processing with Matlab® Applications

It is known that the minimum mean square error (MSE) in approximating a periodic function
by a finite Fourier series is obtained by truncating the infinite Fourier series for that function. Hence
the design specified by (6.1) corresponds, in fact, to the minimum MSE approximation of H d  
by H   . However, truncation of the Fourier series also produces the familiar Gibbs phenomenon,
which will be manifested in H   , especially if H d   is discontinuous. And since all frequency-
selective filters are ideally discontinuous at their band edges, as illustrated in the figure below,
simple truncation of the impulse response will often yield an unacceptable FIR design.
The truncation itself can be considered as masking with a rectangular window, that will
multiplies with 1 the coefficients it preserve and with 0 the coefficients it cancel. We can choose
windows that are not so rough at limits and can limit the oscillations in the frequency response. In
the following we will study intensively the effects of different windows.
6.2.2.2 Rectangular window

To show more precisely the effect of simple truncation, we rewrite (6.27) as


h(n)  wRectangular (n)  hd (n) (6.28)
where w R (n) is the rectangular window function defined as

1, n  0,1,2,..., M
wRectangular (n)   (6.29)
0, otherwise
We have
1
H    WRectangular ( )  H d ( ) (6.30)
2
where WRectangular   is the z transform of wRectangular (n) evaluated for z  e jT . The sequence
w R (n) has the same form as the impulse response of a simple averaging filter; hence, from the
analysis of that filter we obtain
T
sin( N  1)
WRectangular    2 (6.31)
T
sin
2
The convolution of WRectangular   with the desired magnitude response H d   produces the
Gibbs phenomenon in H   , as illustrated in the next figure for an ideal lowpass filter. Note the
characteristic 9-percent overshoot associated with the Gibbs phenomenon in both the passband and
stopband of H '   . This overshoot remains approximately constant no matter how large M is
made, with only the width of the ripples becoming narrower as M is increased. Thus, the maximum
passband ripple is 0,75 dB above the desired (0 dB) gain; while the attenuation associated with the
maximum stopband ripple is only about 21 dB. H '   is redrawn in logarithmic form in the figure
below.
We represent in Fig. 6.17 the impulse response and the frequency response obtained for a
rectangular window with 64 coefficients.

104
6 Digital Filters

Fig.6.17 Rectangular window with 64 coefficients

To design a FIR filter using the window method, we will call the MATLAB function fir1. The
implicit form b=fir1(n, w) returns a LP filter of order n and the normalized cut-off frequency w
designed with the Hamming window. The filter will thus have n+1 coefficients (the window size
f
being also n+1) and the normalized cut-off frequency of w  c (we discussed about this in
fs / 2
chapter 6.1.1 - take a look at Fig. 6.3). We will learn later about the Hamming window, but we can
choose the rectangular window specifying it as a third parameter. We will use the function
w=rectwin(n) that returns the weights of a rectangular window of size n.
Example 6.5 Let us make a rectangular window of size 4.
w=rectwin(5)
w =
1
1
1
1
1

Example 6.6 Let us design a LP filter of order 4 using the rectangular window method. To
design a filter of order n (this means n+1 coefficients) we have to use a window of size n+1. The
cut-off frequency will be 2KHz for a sampling frequency is 10KHz, so the normalized cut-off
frequency will be 0.4.
fs = 10000; % sampling frequency
fCut=2000; % cuting frequency
% Find the characteristics for FIR, LP, Rectangular window, order=4
order=4; % order of the filter
w=fCut/(fs/2); a = 1; % FIR Filter
b = fir1(order,w,rectwin(order+1)) % compute the coefficients,
rectangular window

Exercise 6.10 Write a function plotFreqPhaseResp(b,a,fs,titleString,fCut) that


will plot under the title titleString (if it exists) the frequency response magnitude , magnitude (dB)
and phase for the filter specified by coefficient vectors b and a, working at sampling frequency fs
and represent superposed the limits at specified frequencies fcut (if it exists). Use it to represent
filters with the requirements from Example 6.5, having the orders 4, 40, 400, 4000. The graphics
should look like in Fig. 6.18.

105
Digital Signal Processing with Matlab® Applications

Order 4 FIR Filter designed with Rectangular window Order 40 FIR Filter designed with Rectangular window
1 1.5

0.8
1
Magnitude

Magnitude
0.6

0.4
0.5
0.2

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 50

-20
Magnitude (dB)

Magnitude (dB)
0
-40

-60
-50
-80

-100 -100
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 0

-1
-10
Phase (rad)

Phase (rad)
-2

-3
-20
-4

-5 -30
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

Order 400 FIR Filter designed with Rectangular window Order 4000 FIR Filter designed with Rectangular window
1.5 1.5

1 1
Magnitude

Magnitude

0.5 0.5

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

50 50

0
0
Magnitude (dB)

Magnitude (dB)

-50
-50
-100
-100
-150

-150 -200
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 0

-100 -1000
Phase (rad)

Phase (rad)

-200 -2000

-300 -3000
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

Fig.6.18 Rectangular window FIR filters of order 4, 40, 400 and 4000

106
6 Digital Filters

The basic problem with the rectangular window is the abruptness of the corresponding
truncation of hd n  . As we shall see, this problem can also be viewed as the result of trying to
obtain too narrow a transition band in H   . What we need instead are window functions that are
tapered at the ends and thereby produce smaller ripples in H   (at the expense of wider transition
bands). In that context, the (shifted) window-function transform W ' (   0 ) is itself the frequency
response of the effective bandpass filters comprising the spectrum analyzer. Several of the most
popular and useful of these windows functions are described below.
6.2.2.3 Hanning Window

The Hanning window corresponds to a simple raised cosine and is given by


1 2n  2 n
w Hanning (n)  1  cos   sin , n  0,1,2,..., M (6.32)
2 M  M
Of course, this and all other functions equals zero outside the interval 0  n  M . Note that the
Hanning window has an actual length of M-1, not M+1, because the two end values defined by
(6.32) are also zero. The first sidelobe of the window-function transform WN ' ( ) is down by 31 dB
from the mainlobe amplitude, and the mainlobe width is 4s /( M  1) . When WN ' ( ) is convolved
with H d '   to produce H '   , the maximum stopband ripple of the resulting (frequency-
selective) filter is down by about 44 dB from the passband gain, as shown in the figure below,
compared with only 21 dB for the rectangular window. However, since the width of the transition
band corresponds roughly to the mainlobe width of WN ' ( ) , it is more than twice that resulting
from the rectangular window.

Fig. 6.19 Hanning window with 64 coefficients

Exercise 6.11 Design a LP filter of order 40 using the Hanning window method with the cut-
off frequency of 2KHz for a sampling frequency is 10KHz. Use plotFreqPhaseResp to
represent the frequency and phase response.
Exercise 6.12 Design a BP filter of order 100 using the Hanning window method with the
cut-off frequencies of 2KHz and 3 KHz for a sampling frequency is 10KHz. Use
plotFreqPhaseResp to represent the frequency and phase response.

107
Digital Signal Processing with Matlab® Applications

6.2.2.4 Hamming window

The Hamming window is given by


2n
wHamming (n)  0.54  0.46 cos , n  0,1,2,..., M , (6.33)
M
The Hamming window is not zero to the ends, but 0.08. The transform W M ( ) is shown in fig.
7.9, from which it is seen that the peak sidelobe is now the third, not the first, and it is down by
about 41 dB from the mainlobe level. The mainlobe width, however, is about the same as for the
Hanning window.
The maximum stopband ripple for a frequency-selective filter designed via the Hamming
window is approximately 53 dB below the passband gain, as seen in the figure below.
Thus, since the transition widths resulting from the Hamming and Hanning windows are about
the same, the Hamming window is usually more appropriate for FIR filter design than is the
Hanning. We noted that the opposite is often the case in spectrum analysis because the sidelobes of
W N ' ( ) fall off at a rate of 18 dB/octave; while W M ' ( ) falls off at only 6 dB/octave.

Fig. 6.20 Hamming window with 64 coefficients

Example 6.7 Let us compute the coefficients of a Hamming filter and make a check with
MATLAB’s implementation. We will take a LP filer with the cut-off frequency 0.3 and the order of
the filter 21.
wc=.3; % normalized cu-off
n=21; % order
h=wc*sinc(wc*[-(n-1)/2:(n-1)/2]); % impulse response is sinc
window=hamming(n); % compute window coefficients with hamming
b1=h.*window'; % computed multiplying sinc with Hamming window
b2=fir1(n-1,wc); % computed directly using fir1

plot(b1,'ob'), hold on, plot(b2,'r') % make the superposed plots


xlabel('Index'), ylabel('Amplitude')

The results can be seen in Fig. 6.21.

108
6 Digital Filters

0.3

0.25

0.2

0.15

Amplitude
0.1

0.05

-0.05
0 5 10 15 20 25
Index

Fig. 6.21 Window coefficients comparison

Exercise 6.13 Design a HP filter of order 20 using the Hamming window method with the
cut-off frequency of 2 KHz for a sampling frequency is 10 KHz. Use plotFreqPhaseResp to
represent the frequency and phase response. Represent the impulse response and the step response.
Exercise 6.14 Design a HP filter of order 20 using the Hamming window method with the
cut-off frequency of 4.9 KHz for a sampling frequency is 10 KHz. Use plotFreqPhaseResp to
represent the frequency and phase response. Find out the zeros of the filter. Represent them
graphically.
Exercise 6.15 Design a BS filter of order 20 using the Hamming window method with the
cut-off frequencies of 2 KHz and 3 KHz for a sampling frequency is 10KHz. Use
plotFreqPhaseResp to represent the frequency and phase response.

6.2.2.5 Blackman window

An even lower sidelobe level is provided by the Blackman window.


2n 4n
wBlackman n   0.42  0.5 cos  0.08 cos , n  0,1,2,..., M (6.34)
M M
The maximum sidelobe for this window is down by 57 dB; while the maximum stopband
ripple for the resulting FIR filter is down by 74 dB. The width of the mainlobe is 6 s / M , which is
50 percent larger than for the Hanning and Hamming windows. A resulting filter of WB ' ( ) is
shown in the figure below.
These transition bandwidths reflect the fact that the passband ends at the first frequency (  c )
where the magnitude response is less than 1   1 (see next figures); while the stopbands begins at
the first frequency (  r ) where the response is less than  2 . The rough transition-bandwidth
estimates of k s / M from the mainlobe widths of the rectangular (k=2), Hanning and Hamming
(k=4), and Blackman (k=6) windows, on the other hand, actually correspond to the spacing between
the last peak ( 1   1 ) in the passband and the first peak in the stopband. (To see this, consider
carefully the convolution of an ideal lowpass frequency response and the window transform.)
Hence, the rough estimates from the mainlobe widths are too conservative.

109
Digital Signal Processing with Matlab® Applications

Fig. 6.22 Blackman window with 64 coefficients

Exercise 6.16 Design a HP filter of order 20 using the Blackman window method with the
cut-off frequency of 2 KHz for a sampling frequency is 10 KHz. Use plotFreqPhaseResp to
represent the frequency and phase response.
Exercise 6.17 Design a LP filter of order 10 using the Hamming window method with the
cut-off frequency of 4.9 KHz for a sampling frequency is 10 KHz. Use plotFreqPhaseResp to
represent the frequency and phase response.
6.2.2.6 Kaiser window

A very flexible family of window functions has been developed by Kaiser. These windows are
nearly optimum in the sense of having the largest energy in the mainlobe for a given peak sidelobe
level. They are closely related to the spheroidal wavefunctions, which are the optimum time-
limited, continuous-time functions in a similar sense. The Kaiser windows are of the form

I 0   1  1  2n / M  
2

wKaiser n     , n  0,1,2,..., M , (6.35)


I 0  
where I0(x) is the modified zero order Bessel function of the first kind and  is a shape
parameter determining the tradeoff between the mainlobe width and the peak sidelobe level. Typical
values for  are in the range 4    9 . I0(x) is most easily computed from its power series
expansion, taking a truncated number of terms, the first 15 or at most 25 terms being sufficient for
practical applications.

 
2
 x k
L
 2 
I 0 ( x)  1    L  25 (6.36)
k 1  k ! 
 
The effect of  on the various features of interest is illustrated in the table below, which is due
to Kaiser. There are surveyed the peak sidelobe, the transition bandwidth and the maximum ripple
in the stopband on their dependence on .

110
6 Digital Filters

Table 6.1 Properties of the Kaiser Window FIR filters depending on β


Peak Sidelobe Transition Bandwidth Max. Stopband
 Parameter ( s / M )
Level (dB) Ripple (dB)
2.0 -19 1.5 -29
3.0 -24 2.0 -37
4.0 -30 2.6 -45
5.0 -37 3.2 -54
6.0 -44 3.8 -63
7.0 -51 4.5 -72
8.0 -59 5.1 -81
9.0 -67 5.7 -90
Empirical design formulas have also been derived by Kaiser for the parameters  and M,
given the desired values for the transition bandwidth and the peak sidelobe level. Referring to first
figure let the normalized transition bandwidth be defined by
  ( r   c ) /  s (6.37)
and the stopband attenuation be written as
A  20 log10  2 (6.38)
Because  1   2 , as defined in the last figure, the order M of the FIR filter is approximated by:
A  7.95
M  . (6.39)
14.36
The shape parameter  can be determined from
 0.1102( A  8.7), A  50
  (6.40)
0.5842( A  21)  0.07886( A  21), 21  A  50
0.4

which is accurate to better than 1 percent over the useful range of A.

Fig. 6.23 Kaiser window with 64 coefficients, β=5

111
Digital Signal Processing with Matlab® Applications

6.2.2.7 Window Method FIR Filters Summary

The most important features of the rectangular, Hanning, Hamming, Blackman and Kaiser
windows are summarized in the table 6.1, including their estimates for the transition bandwidth,
their PB ripple and the formulas used for window coefficients calculation.
Table 6.2 Window Method for FIR filter design

PB Peak
Name of the SB cut
Transition ripple Sidelobe
window Window function
bandwidth Level [dB]
function [dB] [dB]
Rectangular 0.9/N 0.7416 13 21 1
2 n
Hanning 3.1/N 0.0546 31 44 0.5  0.5 cos
N
2 n
Hamming 3.3/N 0.0194 41 53 0.54  0.46 cos
N
2 n 4 n
Blackman 5.5/N 0.0170 57 74 0.42  0.5 cos  0.08 cos
N N
A  7.95
,  2n 
2
14.36 I 0 ( 1    )
Kaiser 0.0274 37 54  N  1
3.2/N for
I 0 ( )
=5
Where I0 is zero order modified Bessel function of the first kind,  - controls the way in which
window function is tapping the edges in time domain.
The window design method consists in multiplying the ideal impulse response function with a
window like function and the result will be a filter type function which will try to approximate the
required filter, each of the functions solving a specific problem in a better way.
The main disadvantage of window method is a loose control over the filter parameters and a
long time for development. Since, it is not straight forward the dependency of window length over
the performance of the resulting filter it requires long feedback and a lot of tests before a final
version. However, it is a very simple method, since almost all elements are given, and it is well
suited for powerful processors or software implementation.
Since now, we designed the filters starting from the filter type, the frequency requirements and
the order of the filter.
Algorithm: For FIR design using window method with a given order you have to:
- specify the “ideal” or desired frequency response of the filter HD(f);
- for the given hardware, determine the maximum order M that can be achievable.
- implement filters of order M for all known windows (multiply the ideal impulse response
for order M with the specific windows: h(n) = hD(n) W(n) )
- choose from all the frequency responses H(f) the most appropriate monitoring the
parameters that are important for your application, like the transition bandwidth, the PB ripple,
etc…

112
6 Digital Filters

This approach is not an economical one, because it doesn’t care about the resources (the order
of the filter is taken “maximal”). Actually, using Table 6.1 or similar relations, the order of the filter
can be taken the minimal one that guarantees some desired properties. This way, the order of the
filter will be different for different window types and we can choose the window with the minimum
number of coefficients/ the minimum order filter.
Algorithm: For FIR design using window method with given properties you have to:
- specify the “ideal” or desired frequency response of the filter HD();
- choose a window function that satisfies the PB or SB specifications and determine the
number of filter coefficients N, using appropriate relationships between the filter length and the
transition bandwidth f; maybe other requirements.
- implement filters for all the known windows of minimal order Mi that insures the desired
properties (multiply the ideal impulse response for order Mi with the specific window: hi(n) =
hD(n) Wi(n) )
Example 6.8 Let us design a LP filter of using the rectangular window method with the cut-
off frequency of 2 KHz for a sampling frequency is 10 KHz. The filter should have a transition
bandwidth of 200Hz. Use plotFreqPhaseResp to represent the frequency and phase response
% Find the characteristics for FIR, LP, Rectangular window
order=ceil(0.9/(fTrans/fs)); % find the order of
the filter
b = fir1(order,fCut/(fs/2),rectwin(order+1)); % compute the
coefficients, rectangular window
a = 1; % FIR Filter
titleString = ['FIR LP Filter, Rectangular window, order '
num2str(order)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut-fTrans/2 fCut+fTrans/2])

FIR LP Filter, Rectangular window, order 45


1.5
Magnitude

0.5

0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

50
Magnitude (dB)

-50

-100
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

-10
Phase (rad)

-20

-30

-40
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Fig. 6.24 FIR LP Filter designed with the rectangular window method

113
Digital Signal Processing with Matlab® Applications

Exercise 6.18 Design 4 LP filters of using the Hanning, Hamming, Blackman and Kaiser
window methods with the cut-off frequency of 2 KHz for a sampling frequency is 10 KHz. The
filter should have a transition bandwidth of 200Hz. The Kaiser filter should insure also a ripple in
the passband of .01 and in the stopband of 0.1. Use plotFreqPhaseResp to represent the
frequency and phase response.
Hint 1: For the Kaiser filter use the function keiserord.
The desired characteristics are presented in Fig. 6.25.

FIR LP Filter, Hanning window, order 155 FIR LP Filter, Hamming window, order 165
1.5 1.5
Magnitude

Magnitude
1 1

0.5 0.5

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

100 50
Magnitude (dB)

Magnitude (dB)
0
0
-50
-100
-100

-200 -150
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 0
Phase (rad)

Phase (rad)

-50 -50

-100 -100

-150 -150
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

FIR LP Filter, Blackman window, order 275 FIR LP Filter, Kaiser window, order 112,  =3.3953
1.5 1.5
Magnitude

Magnitude

1 1

0.5 0.5

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

100 50
Magnitude (dB)

Magnitude (dB)

0
0
-50
-100
-100

-200 -150
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 0

-50 -20
Phase (rad)

Phase (rad)

-100 -40

-150 -60

-200 -80
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

Fig. 6.25 FIR LP Filter designed with the Hanning, Hamming,


Blackman and Kaiser window methods

114
6 Digital Filters

Exercise 6.19 Design a 50th order bandpass FIR filter that will have to attenuate the 50 Hz
powerline noise with amplitude 1 present in a sawtooth wave with frequency 1Hz and amplitude 2.
Motivate your decisions. Explain the differences from the beginning of the sequence.
Hint: You can use the RMS error as comparison criteria.

6.2.3 Frequency-Sampling Method


For arbitrary, nonclassical specifications of H d   , the calculation of hn  , n=0,1,…,N, using
calculations like (6.24) or an appropriate approximation can be a substantial computation task. In
such situations, it may be preferable to employ a design technique that utilizes specified values of
H d   directly, without the necessity of determining hd n  . One simple technique of this type is
based upon the specification of the samples of H d   at M=N+1 uniformly spaced frequencies  k
in the interval (0,  s ) and, as such, is an application of the discrete-Fourier transform (DFT). To
simplify our derivation of this technique, however, we will assume two appropriate forms for hn 
and show that these do, in fact, lead to a frequency-sampling interpretation.
The desired frequency response is interpolated onto a dense, evenly spaced grid of length np,
where np is 512 by default. If two successive values of f are the same, a region of lap points is set
up around this frequency to provide a smooth but steep transition in the requested frequency
response.
By default, lap is 25. The filter coefficients are obtained by applying an inverse fast Fourier
transform to the grid and multiplying by a window; by default, this is a Hamming window.
Example 6.9 We will design LP filters with similar characteristics as the ones we made until
now, using the sampling frequency method: the cut frequency 2 KHz for a sampling frequency of
10 KHz. We will try different filter orders: 4, 40, 400 and 4000.
The results for the minimal and maximal orders (4 and 4000) are shown in Fig. 6.26.

fs = 10000; % sampling frequency


fCut=2000; % cuting frequency

% Find the characteristics for FIR, LP, Frequency Sampling method


orderSet=[4 40 400 4000]; % order set to be checked
w=[0 fCut fCut fs/2]/(fs/2); % normalized frequencies
A=[1 1 0 0 ];% amplitudes
a = 1; % FIR Filter

for indexOrder=1:length(orderSet)
order=orderSet(indexOrder); %order of the filter
b = fir2(order,w,A); % compute the coefficients
titleString = ['Order ' num2str(order) ' FIR Filter designed with
Frequency Sampling method'];
plotFreqPhaseResp(b,a,fs,titleString,fCut)
end

115
Digital Signal Processing with Matlab® Applications

Order 4 FIR Filter designed with Frequency Sampling method Order 4000 FIR Filter designed with Frequency Sampling method
0.8 1.5

0.6
Magnitude

Magnitude
1
0.4
0.5
0.2

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 100
Magnitude (dB)

Magnitude (dB)
-10 0

-20 -100

-30 -200
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 0

-2
Phase (rad)

Phase (rad)
-1000
-4
-2000
-6

-8 -3000
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

Fig. 6.26 Low Pass FIR Filters of orders 4 and 4000 designed with frequency sampling method

Exercise 6.20 Explain why do not we have a steep response even for the order 4000.
Compare the filters of order 400 and 4000. What have we gained in the second case? Create a filter
with the given specifications, order 1000, having a sharp transition band.
Exercise 6.21 Create a two FIR filters using the FSM, orders 40 and 400 that must have a
custom frequency characteristic. Hint: we present a filter with triangular characteristic in Fig. 6.27.
Order 40 FIR Filter designed with Frequency Sampling method Order 400 FIR Filter designed with Frequency Sampling method
1 1
Magnitude

Magnitude

0.5 0.5

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 0
Magnitude (dB)

Magnitude (dB)

-50 -50

-100 -100

-150 -150

-200 -200
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

10 100

0 0
Phase (rad)

Phase (rad)

-10 -100

-20 -200

-30 -300
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

Fig. 6.27 Arbitrary frequency response FIR filters designed with the frequency sampling method

For filters with a gain other than zero at fs/2, the order of the filter N must be even, otherwise it
will be automatically incremented by one.

116
6 Digital Filters

6.2.4 Least-Square Linear-Phase Method


The function firls designs a linear-phase FIR filter that minimizes the weighted, integrated
squared error between an ideal piecewise linear function and the magnitude response of the filter
over a set of desired frequency bands.
Example 6.10 Design a least-square linear-phase filter of order 50 with the cut-off frequency
of 2 KHz for a sampling frequency is 10 KHz. The filter should have a transition bandwidth of
200Hz
fs = 10000; % sampling frequency
fCut=2000; % cuting frequency
fTrans=200; % width of the transition band
n=50; % order of the filter
fPass=fCut-fTrans/2;
fStop=fCut+fTrans/2;
w=[0 fPass fStop fs/2]/(fs/2);
A=[1 1 0 0];
% Find the charact. for least square linear-phase FIR filter design
b = firls(n,w,A);
a = 1;
titleString = ['FIR LP Filter, least square linear-phase FIR, order '
num2str(n)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut-fTrans/2 fCut+fTrans/2])

FIR LP Filter, least square linear-phase FIR, order 50


1.5
Magnitude

0.5

0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

50
Magnitude (dB)

-50

-100
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

-10
Phase (rad)

-20

-30

-40
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Fig. 6.28 Low Pass FIR Filter designed with least-square linear-phase methods

Exercise 6.22 Design a LP filter of order 50 working at 10KHz that must have a custom
frequency characteristic: it will stop the frequencies up to 2KHz, it will start to let them pass
linearly from 2KHz to 3KHz, such that the attenuation at 3KHz is 0 and will start to stop them
linearly from 3KHz to 4KHz, such that at 4KHz it will be a stop band filter and will continue to be
SB up to 5Hz. Hint: We present it in Fig. 6.29.

117
Digital Signal Processing with Matlab® Applications

Custom Filter, least square linear-phase FIR, order 50


1

Magnitude
0.5

0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

0
Magnitude (dB)
-50

-100

-150
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

10

0
Phase (rad)

-10

-20

-30
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Fig. 6.29 Arbitrary frequency response FIR filter designed


with the least square linear-phase method

6.2.5 Parks-McClellan Optimal Equiriple Method


It is based on the concept of equally distributed ripple in pass band and stop band. It uses the
minimax approach which yields a good quality filter.
The function firpm designs a linear-phase FIR filter using the Parks-McClellan algorithm,
which designs filters with an optimal fit between the desired and actual frequency responses. The
filters are optimal in the sense that the maximum error between the desired frequency response and
the actual frequency response is minimized. Filters designed this way exhibit an equiripple behavior
in their frequency responses and are sometimes called equiripple filters. firpm exhibits
discontinuities at the head and tail of its impulse response due to this equiripple nature
Example 6.11 We will design a custom filter, with the same triangular characteristic as in the
preceding exercises. The characteristics are presented in Fig. 6.30.
fs = 10000; % sampling frequency
fCut1=2000; % cuting frequency
fCut2=3000;
fCut3=4000;
df=50;
w=[0 fCut1-df fCut1+df fCut2-df fCut2+df fCut3-df fCut3+df
fs/2]/(fs/2); % normalized frequencies
A=[0 0 0 1 1 0 0 0 ];% amplitudes
n=50;
a = 1; % FIR Filter
% Find the characteristics for LP with Parks-McClellan algorithm
b = firpm(n,w,A);
titleString = ['Custom Filter, Parks-McClellan optimal FIR, order '
num2str(n)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut1 fCut2 fCut3])

118
6 Digital Filters

Custom Filter, Parks-McClellan optimal FIR, order 50


1.5

Magnitude
1

0.5

0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

50

Magnitude (dB)
0

-50

-100
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

-10
Phase (rad)

-20

-30

-40
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Fig. 6.30 Arbitrary frequency response FIR filter designed with the Parks-McClellan method

6.3 IIR - Infinite Impulse Response Filters

Infinite Impulse Response (IIR) filters assumes that the output at any time depends linearly not
just on the current and the previous inputs, but also on the previous outputs, like in (6.17).
The transfer function H(z) of an IIR filter is given by (6.18).
The methods for IIR filter design were developed in the analog domain and each one is optimal
in some sense, the choice of optimality deciding the nature of the design.
Butterworth filters are optimal in the sense of having a maximally flat amplitude response,
Chebyshev type I and II have rapid rolloff rate near the cutoff frequency, but lose of monotonicity
in the passband or in the stopband, where they are still equiripple and Elliptic filters offer the
sharpest transition band, at the expense of equiripple in both the pass- and stopbands
The standard IIR filters (Butterworth, Chebyshev and Elliptic) are much easier to design as
analog filters which are then converted to digital filters. This means carrying out the design over the
s plane instead of the z plane, where the s plane is the complex plane over which analog filter
transfer functions are defined.
The analog transfer function Ha(s) is very much like the digital transfer function Hd(z), except
that it is interpreted relative to the analog frequency axis s=jωa (the ``jω axis'') instead of the digital
frequency axis z=ejωT (the ``unit circle''). In particular, analog filter poles are stable if and only if
they are all in the left-half of the plane, i.e., their real parts are negative.
The most common used techniques to transform analog filters to digital ones are the impulse-
invariant, step-invariant and primarily, the bilinear transform designs.

119
Digital Signal Processing with Matlab® Applications

6.3.1 The Bilinear Transform


The bilinear transform maps bijectively the analog domain into the digital one by a function of
the form:
2 1  z 1
s (6.41)
T 1  z 1
where T is the sampling period in the digital domain.
It will convert the transfer function of an analog filter Ha(s) into the transfer function of the
digital equivalent Hd(s). The digital filter will have the properties of the analog prototype, being
stable if the analog one is stable.
Algorithm: To obtain the transfer function Hd(z).from a standard analog filter having the
transfer function Ha(s) you need to:
- Obtain the transfer function Ha(s) for the desired analog prototype filter
- In the transfer function Ha(s) make the substitution (6.1) and obtain the digital transfer
function Hd(z).
1  z 1
- The transfer function Hd(z) will contain various powers of the ratio because Ha(s) is
1  z 1
a ratio of polynomials in s, so multiply the numerator and the denominator with the highest power
of 1+z-1 (the highest power of s in Ha(s)) and collect the terms to obtain an expression of the form
(6.18).
Example 6.12 Let us determine the digital counterpart of the analog filter defined by
1
H ( s)  3 (it is a third order Butterworth LPF, see 6.3.2) that will work at the
s  2s  2s  1
2

sampling frequency of 8KHz. We can try the substitutions by hand or we can use the symbolic math
toolbox from MATLAB.
syms h z s % create symbolic variables
fs=8; T=1/fs; % find the sampling period
s=(2/T)*(1-z^-1)/(1+z^-1); % define s
h=1/(s^3+2*s^2+2*s+1) % express the analog transfer function, the
substitution is made automatically
pretty(simplify(h)) % express the transfer function in a nicer way

h =
1/((16-16/z)^3/(1+1/z)^3+2*(16-16/z)^2/(1+1/z)^2+2*(16-
16/z)/(1+1/z)+1)
3
(z + 1)
-----------------------------------
3 2
4641 z - 12765 z + 11747 z - 3615

We will use now the bilinear transform from the signal processing toolbox to compute the
denominator and numerator of the digital transfer function.
fs=8; % define the sampling frequency
bAnalog=1; % define the analog transfer function
aAnalog=[1 2 2 1]
[bDigital,aDigital] = bilinear(bAnalog,aAnalog,fs)% the numerator and
denominator of the digital transfer function

120
6 Digital Filters

We obtain that:
bDigital =
1.0e-003 *
0.2155 0.6464 0.6464 0.2155
aDigital =
1.0000 -2.7505 2.5311 -0.7789

that are proportional (identical for polynomials) with [1 3 3 1] and [4641 -12765 11747 -
3615] obtained previously.

6.3.2 Butterworth Filters


Butterworth filters are optimal in the sense of having a maximally flat amplitude response in
the passband and the stopband, as measured using a Taylor series expansion about DC. Of course,
the trivial filter H(z)=1 has a perfectly flat amplitude response, but that's an allpass, not a lowpass
filter. Therefore, to constrain the optimization to the space of lowpass filters, we need constraints on
the design, such as H(1)=1 and H(-1)=1. That is, we may require the DC gain to be 1, and the gain
at half the sampling rate to be 0.
The general expression for the transfer function of an nth-order Butterworth analog lowpass
filter is given by
1 1
H a ( s)  
n
( s  s1 )  ( s  s 2 )    ( s  s n ) (6.42)
 (s  s
k 1
k )

2 k  n 1
j 2k  n  1 2k  n  1
where s k  e 2n
 cos(  )  j  sin(  )
2n 2n
Exercise 6.23 Determine by hand the analog transfer function for a Butterworth filter of order
3. Hint: You used it in Exercise 6.10.
Example 6.13 Let us compute the coefficients of a Butterworth filter of order N using 6.41
and check it for N=3 against MATLAB function butter called with the last argument ‘s’ – for
analog design.
N=3;
arg=pi*(2*(1:N)+N-1)/(2*N);
poles=cos(arg)+j*sin(arg);
a1=poly(poles)
[b,a2]=butter(N,1,'s')

and we obtain
a1 =
1.0000 2.0000 - 0.0000i 2.0000 - 0.0000i 1.0000 - 0.0000i
b =
0 0 0 1.0000
a2 =
1.0000 2.0000 2.0000 1.0000
Exercise 6.24 Display the coefficients of the Butterworth filter analog prototypes for N taking
the values from 1 to 10. Hint: use MATLAB function butter called with the last argument ‘s’ and
display just the a’s coefficients.

121
Digital Signal Processing with Matlab® Applications

The Butterworth approximation results from the requirements that the magnitude response be
maximally flat in both the passband and the stopband. That is, the first (2N-1) derivatives of
H c  j  are specified to equal zero at   0 and at    . The general specifications in (1.1) and
2

(1.2) can be employed with arbitrary  1 ,  2 ,  c and  r to determine the required filter order N,
from which the filter design is readily obtained. Very often, however, the cutoff frequency  c of a
Butterworth filter is chosen to correspond to the half-power or 3-dB point of H c  j  , in which
case 1   1  1 / 2 . We will assume in the following that this is the case since the general
properties of the Butterworth design are unaffected by this assumption.
The magnitude response resulting from (6.41) after we replace s=jω specifications is of the
form

1
H   
2N
  (6.43)
1   
 c 

Example 6.14 Let us study the variation of H with the quantized frequency
c
omegaOverOmegaC=0:0.1:5;
axes; c=colormap('lines');
hold on;
for N=1:8
A= 1./( sqrt(omegaOverOmega0.^(2*N)+1));
plot(omegaOverOmega0,A ,'color',c(N,:));
text(omegaOverOmega0(30-2*N),A(30-2*N),['N=' num2str(N)]);
end
grid
title('Butterworth LP Filter Charasteristics')
xlabel('Normalized frequency \omega/\omega_c'), ylabel('Amplitude')

Butterworth LP Filter Charasteristics


1

0.9

0.8

0.7

0.6
Amplitude

0.5

0.4
N=1
0.3

0.2
N=2
N=8
0.1
N=3
N=7
N=6N=5N=4
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Normalized frequecy /c

Fig. 6.31 Butterworth LP characteristics for order N taking values from 1 to 8

122
6 Digital Filters

Note that H c 0   1 and H c  j c   1 / 2 , as desired, and that H c  j  falls off as 1 /  N


for all    c . Note also that H c  j  is monotonically decreasing for all  and thus is
monotonic in both the passband and the stopband.
The Butterworth magnitude response is shown in figure above for several values of the order N
with both linear and logarithmic (dB) scales. The logarithmic scale is especially appropriate because
for    c , H c  j  approaches an asymptotic rolloff of approximately –6N dB per octave, as
indicated in figure b). A continuous-time Butterworth filter is often specified simply by the 3-dB
cutoff frequency and the rolloff rate. A discrete-time Butterworth filter can also be specified in this
way, but as we shall see, the rolloff can actually be greater (with the bilinear transformation) in the
discrete-time case.
By analytic continuation of H c  j   H c  j H c*  j   H c  j H c  j  , the system
2

function of the continuous-time Butterworth filter must satisfy


1
H c s H c  s   2N
 s  (6.44)
1   
 j c 
and the poles of H c s H c  s  are thus given by

s k   1  j c 
1/ 2N
(6.45)
that is consistent with (6.42). These 2N poles are equally spaced around a circle of radius  c in
the s plane and symmetrically located with N respect to both the real and imaginary axes.
Exercise 6.25 Create a function ButterworthPoles(N,wc) that will return if required or
represent graphically otherwise the poles of an analog Butterworth filter of order N using the
formula (6.45). Test it for N=1, N=2, N=3 and =4,
In figure 6.32 we represented the poles for N=3 and N=4 .and wc=200.
Remark: there are poles on the real axis for N odd, and not for N even, but there are never
poles on the j axis.
Poles in s plane Poles in s plane

200 200

150 150

100 100

50 50
Imaginary axis
Imaginary axis

0 0

-50 -50

-100 -100

-150 -150

-200 -200

-200 -150 -100 -50 0 50 100 150 200 -200 -150 -100 -50 0 50 100 150 200
Real axis Real axis

Fig. 6.32 Butterworth filter poles for N=3 and N=4

123
Digital Signal Processing with Matlab® Applications

Since we are ultimately interested in the poles of H c s  alone, we must select these N poles
from the 2N poles of H c s H c  s  . For a stable and causal filter, this is straightforward since we
must choose the N poles lying in the left-half plane for H c s  , with H c  s  then having the right-
half-plane poles. Note that H c s  has no zeros (except at infinity), and thus the Butterworth is one
of the all-pole continuous-time designs.
We can use the butter function with parameter ‘s’ to obtain the analog version of the
Butterworth filter or we ca n use it directly to find the coefficients of the digital filter. The function
butterord is called regularly to determine the minimum order of the filter that will insure the
desired characteristics.
Example 6.15 We will design a LP Butterworth filter working at 10KHz, which will have the
separation frequency at 2KHz and the transition band of 400Hz. The filter will exhibit a ripple of
1dB in the passing band and an attenuation of 70dB in the stop band.
fs = 10000; % sampling frequency
fCut=2000; % cuting frequency
fTrans=400; % width of the transition band
Rp=1; Rs=70; % ripples

fPass=fCut-fTrans/2; fStop=fCut+fTrans/2; % cutting frequencies


Wp=fPass/(fs/2); Ws=fStop/(fs/2); % normalization
% Find the characteristics for IIR , LP, Butterworth
[order, W]=buttord(Wp,Ws,Rp,Rs)
[b,a] = butter(order,W);
titleString = ['IIR LP Butterworth Filter of order ' num2str(order)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut-fTrans/2 fCut+fTrans/2],
[Rp NaN Rs])

The frequency and phase response are presented in Fig. 6.32.

IIR LP Butterworth Filter of order 33


1.5
Magnitude

0.5

0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

200
Magnitude (dB)

-200

-400

-600
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

-20
Phase (rad)

-40

-60

-80
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Fig.6.32 Butterworth LP filter frequency and phase response

124
6 Digital Filters

Exercise 6.26 Design a digital Butterworth LPF of order 30 with the cut-off frequency of 100
Hz, working at a sample rate of 500 Hz. Represent graphically and compare the result with the filter
from example 6.13.
Exercise 6.27 Design a fourth-order digital Butterworth bandpass filter with the lower cut-off
frequency 200 Hz, upper cut-off frequency 400 Hz for sampling frequency 1000 Hz.

6.3.3 Chebyshev Filters


A more rapid rolloff rate near the cutoff frequency than that of the Butterworth design can be
achieved at the expense of a loss of monotonicity in the passband and/or the stopband. The
Chebyshev designs (types I and II) maintain monotonicity in one band but are equiripple in the
other band, as illustrated in figure below.
The squared magnitude response for a Chebyshev-I design is of the form
1
H c  j  
2
(6.46)
1   T N2  /  c 
2

where T N  x  is the Nth-order Chebyshev polynomial defined by

TN x   cos( N cos 1 x)  cosh[ N cosh 1 x]. (6.47)


Exercise 6.28 Represent the variation of H for a Chebyshev I LPF with the quantized

frequency , as in Fig. 6.33.
c
Chebyshev I LP Filter Charasteristics
1

0.9

0.8

0.7

0.6
Amplitude

0.5

0.4

0.3

0.2

0.1

0
0 0.5 1 1.5 2 2.5 3
Normalized frequecy /2

Fig. 6.33 Chebyshev I LPF characteristics for order N taking values from 1 to 4

The Chebyshev polynomials can be generated recursively by


TN 1  x   2 xTN  x   T N 1 x  , with T0 ( x)  1 , T1  x   x , T2 x   2 x 2  1 … (6.48)
Exercise 6.29 Write a function that will compute recursively the coefficients of the
Chebyshev polynomial of order N.

125
Digital Signal Processing with Matlab® Applications

Since TN 1  1 for all N, the square magnitude response at cutoff is equal with 1 / 1   2  , and
 2 is thus determined from the passband ripple  1 as
1
2  1 (6.49)
1   1 2
Therefore, a Chebyshev-I design is specified by the cutoff frequency, the passband ripple, and
the order N, with the latter determining the stopband characteristics. The required value of N for
given stopband specifications is
cosh 1 (1 /  2  )
N (6.50)
cosh 1  r /  c 
The poles of H c s  are found from those of H c s H c  s  as in the Butterworth case and lie
on an ellipse in the s plane. The zeros of H c s  are all at infinity for a type-I filter, and hence this is
another all-pole continuous-time design.
To obtain H c s H c  s  , we need the square of the Nth-order Chebyshev polynomial T N  x  . It
is shown that
T2 N ( x )  1
T N2 ( x)  (6.51)
2
and it is much easier to calculate T2 N ( x) recursively than to square T N  x  directly.
The squared magnitude response for a Chebyshev-II design is of the form
1
H c  j  
2

 T  r /  c  
2
(6.52)
1  2  N

 T  r /   
2
N

It is readily observed from the response at cutoff that (6.49) still holds for  2 , and since (6.46)
and (6.52) are the same for    r , (6.50) holds as well for N. The zeros of this design are
obviously not at infinity, but rather lie along the j axis to produce the equiripple stopband
characteristic.
Closed-form solutions for the poles and zeros of Chebyshev filters have been determined.
For a type-I filter, let
1/ N
1  1   2 
   (6.53)
  
 
And define
   1    1
sinh   , cosh   (6.54)
2 2

k 
2k  1 (6.55)
2N
We will use (6.54) and (6.55) to define (6.56)
 k  sinh  sin  k  c ,  k  cosh  cos  k  c (6.56)

126
6 Digital Filters

Then, the poles of the type-I lowpass filter are given by  k  j k , k  1,2,..., N , where
 c r  k   
k  and  k  2 c r 2k (6.57)
 k  k
2 2
 k  k
The zeros lie on the j axis and are given by
j r
sk  , k  1,2,..., N (6.58)
cos  k
Example 6.16 We will design a LP Chebyshev type I and a Chebyshev type II filter working
at 10KHz, which will have the separation frequency at 2KHz and the transition band of 400Hz. The
filter will exhibit a ripple of 1dB in the passing band and an attenuation of 70dB in the stop band
fs = 10000; % sampling frequency
fCut=2000; % cuting frequency
fTrans=400; % width of the transition band
Rp=1; Rs=70;
fPass=fCut-fTrans/2;fStop=fCut+fTrans/2;
Wp=fPass/(fs/2);cWs=fStop/(fs/2);
% Find the characteristics for IIR , LP, Chebyshev type 1
[order, W]=cheb1ord(Wp,Ws,Rp,Rs);
[b,a] = cheby1(order,Rp,W);
titleString = ['IIR LP Chebyshev type 1 Filter of order '
num2str(order)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut-fTrans/2
fCut+fTrans/2],[Rp NaN Rs])
% Find the characteristics for IIR , LP, Chebyshev type 2
[order, W]=cheb2ord(Wp,Ws,Rp,Rs);
[b,a] = cheby2(order,Rs,W);
titleString = ['IIR LP Chebyshev type 2 Filter of order '
num2str(order)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut-fTrans/2
fCut+fTrans/2],[Rp NaN Rs])

IIR LP Chebyshev type 1 Filter of order 13 IIR LP Chebyshev type 2 Filter of order 13
1 1.5
Magnitude

Magnitude

1
0.5
0.5

0 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 50
Magnitude (dB)

Magnitude (dB)

0
-200
-50
-400
-100

-600 -150
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

0 5

0
Phase (rad)

Phase (rad)

-10
-5
-20
-10

-30 -15
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz) Frequency (Hz)

Fig.6.34 Chebyshev type I and type II LP filters frequency and phase response

127
Digital Signal Processing with Matlab® Applications

6.3.4 Elliptic Filters


When Butterworth and Chebyshev designs fail to offer a steep enough rolloff for a given order,
we are trying to use elliptic filters.
Elliptic (Cauer) filters offer the sharpest transition band (optimum in this sense), at the expense
of equiripple in both the pass- and stopbands. For given specifications, it will meet them with the
minimum order.
The design of elliptic filters is the most difficult and computationally intensive of the
Butterworth, Chebyshev Type I and II and requires the transfer from the analog to the digital
domain using the bilinear transform
The squared magnitude response is of the form
1
H c  j  
2
(6.59)
1   U N2  /  c 
2

where U N   is a Jacobean elliptic function. A discussion of elliptic functions is beyond the


scope of this book, and we will rely on the availability of tabulations or CAD programs to obtain
continuous-time elliptic filter designs. We can observe however, that the zeros of H c s  must lie
along the j axis, as for the Chebyshev-II case.
It is reasonable at this point to ask why the elliptic design is not always chosen to meet
specifications of the type in the first figure since it does so with lower-order N than for any other
filter design. One reason, as we have already noted, is that the Chebyshev designs are monotonic in
one band, and the Butterworth design is monotonic in both. Another important reason is that the
phase response of the elliptic design is more nonlinear in the passband, especially near cutoff, than
the response of the other designs.
Example 6.17 We will design a LP elliptic filter working at 10KHz, which will have the
separation frequency at 2KHz and the transition band of 400Hz. The filter will exhibit a ripple of
1dB in the passing band and an attenuation of 70dB in the stop band.
The result is shown in Fig. 6.35.

fs = 10000; % sampling frequency


fCut=2000; % cuting frequency
fTrans=400; % width of the transition band
Rp=1;
Rs=70;

fPass=fCut-fTrans/2;
fStop=fCut+fTrans/2;

Wp=fPass/(fs/2);
Ws=fStop/(fs/2);

% Find the characteristics for IIR , LP, Elliptical


[order, W]= ellipord(Wp,Ws,Rp,Rs);
[b,a] = ellip(order,Rp,Rs,W);
titleString = ['IIR LP Elliptical Filter of order ' num2str(order)];
plotFreqPhaseResp(b,a,fs,titleString,[fCut-fTrans/2
fCut+fTrans/2],[Rp NaN Rs])

128
6 Digital Filters

IIR LP Elliptical Filter of order 7


1

Magnitude
0.5

0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Magnitude (dB)
-50

-100

-150
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

0
Phase (rad)

-5

-10

-15
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)

Fig.6.35 Elliptic LP filter frequency and phase response

6.3.5 Bessel Filters


Bessel filters are designed to have maximally flat group-delay characteristics, so they have
little ripple for both impulse and step responses.
Lowpass Bessel filters have a monotonically decreasing magnitude response, as do lowpass
Butterworth filters. Compared to the Butterworth, Chebyshev, and elliptic filters, the Bessel filter
has the slowest rolloff and requires the highest order to meet an attenuation specification.
The design of elliptic filters is the most difficult and computationally intensive of the
Butterworth, Chebyshev Type I and II and requires the transfer from the analog to the digital
domain using the bilinear transform
The squared magnitude response is of the form
b0
H c s   (6.60)
qn ( s )

n
(2n  k )!
qn ( s )   bk s k , bk  nk (6.61)
k 1 2 k!(n  k )!

qn  (2n  1)qn1  s 2 g n2 (6.62)


Remark: Bessel filters are only LP.
Remark: Bessel filter roots are not available for order N > 25

129
Digital Signal Processing with Matlab® Applications

6.4 Filter Design and Analysis

6.4.1. General Steps for Filter Design


In order to design a filter, we start from the problem specification and stop with the
software/hardware product that meets these requirements. If we refer just to the technical part, we
can specify the following filter design steps:
- Specification of filter requirements
- Calculation of filter coefficients
- Representation of the filter by a suitable structure (realization)
- Analysis of the effect of finite words representation
- Implementation of filter in software and/or hardware
- Testing

6.4.2 Characteristics of FIR and IIR filters


An important alternative has to be chosen before the actual calculation of the filter coefficients
using a particular filter design: we have to decide if we will construct an FIR or an IIR class filter.
Their intrinsic properties helps in this selection:
- FIR filters can have an exactly linear phase response;
- IIR have a nonlinear phase response especially at the band edges;
- FIR filters are more stable;
- FIR filter requires more coefficients for sharp cutoff than IIR filter;
- Analog filters can be transformed into equivalent IIR filters meeting similar specifications
and this is not possible with FIR filters as they have no analog counterpart;
- The effects of using a limited number of bits to implement filter, such as round off noise
and coefficient quantization error have a larger influence on IIR filters because of the recursivity;
Remark: When to use IIR or FIR filters:
- Use FIR if the number of filter coefficients is not too large and in particular if little or no
phase distortion is desired;
Window method offers a very simple and flexible way for computing FIR filter coefficients,
but it does not allow the designer adequate control over filter parameters.
Frequency sampling method allows a recursive realization of FIR filters, it can be
computationally efficient.
Optimal method it should be the method of first choice, unless the practical application dictates
otherwise or an A/D facility is not available.
- Use IIR when the only important requirements are sharp cut off filters, especially those
using elliptic characteristics, and will give fewer coefficients than FIR.
Butterworth has maximally flat amplitude response in the passband and the stopband.
Chebyshev offers a better roll-off rate near the cutoff frequency than that of the Butterworth
and ripple in only one of the passband or the stopband.

130
6 Digital Filters

Elliptic has fast roll-off.


Bessel has little ripple for both impulse and step responses.

6.4.3 Filter Structure (Realization)


For the representation of the filter by a suitable structure/ realization, we must know the
transfer function H(z). The graphical representation of the filter structure, also called the signal flow
graph or the system/ filter diagram is made using arrows to specify the signal flow, squares with
z-k inside to specify a delay of k samples (always integer), triangles with adjacent numbers to
specify the product (weighting) with that figure and circles to specify addition or subtraction of
signals – if several operations are concerned, the sign is put near the incoming arrow.
For FIR filters, the current output value is a linear combination of the current and previous
input values:
N
y (n) bk  x(n  k ) (6.63)
k 0

We will use delay blocks to represent the advance in time of the signal. If x(n) is placed at the
first filter tap, corresponding to b0, the previous input x(n-1) has advanced to the tap corresponding
to b1, x(n-2) corresponds to b2 and so on. The intuitive way is to place the input to the left, the
output to the right and the filter taps horizontally.

+ + … + y(n)
b0 b1 b2 … bM

x(n) z-1 z-1 … z-1

x(n) x(n-1) x(n-2) x(n-M)

Fig. 6.36 Direct Form/ Transversal FIR filter implementation

Because of this structure, a FIR filter is also called a transversal filter, or a tapped delay line.
An alternative representation is with the filter signal flow rendered vertically, like in Fig. 6.37.
b0
x(n) + y(n)
z-1
b1
+
-1
z
b2
+



z-1
bM

Fig. 6.37 Transversal FIR filter implementation – vertical representation

131
Digital Signal Processing with Matlab® Applications

For an IIR filter, we have also linear dependence of the past outputs, so we will recall (6.17) as
the difference equation for IIR filters.
N M
y (n) bk  x(n  k ) ak  y (n  k ) (6.64)
k 0 k 1

The structure of a filter defined by (6.64), called Direct Form I, is shown in Fig. 6.38 for M>N
and we can recognize the left substructure as corresponding to a FIR filter (dependence on inputs)
and the right part as corresponding to the dependence of previous values of the output (reaction).
b0
x(n) + + y(n)
z-1 z-1
b1 -a1
+ +
z -1
z-1
b2 -a2
+ +

… … …
… z-1
… -aN

z-1
bM

Fig. 6.38 Direct-Form-I implementation of an IIR filter

If the transfer functions for the left and right part are respectively H1(z) and H2(z), we can write
H 1 ( z ) H 2 ( z ) H 2 ( z ) H 1 ( z ) (6.65)
because the multiplication of complex numbers is commutative. We can thus change the order
of the filter sections, or in the general case, choose any order for a series connection of filters.
Remark: The numerical precision is affected by the ordering of blocks in a series combination.
Changing the order of the filter sections, we get the structure presented in Fig,6.33.

b0
x(n) + + y(n)
z-1 z-1
-a1 b1
+ +
-1 -1
z z
-a2 b2
+ +


… …

z-1 …
-aN
z-1 bM

Fig. 6.39 Reversed implementation for the Direct-Form-I of an IIR filter

132
6 Digital Filters

We observe that the central part of the structure contains two redundant delay lines, so we will
use just one. The derived realization is called Direct Form II.

b0
x(n) + + y(n)
z-1
-a1 b1
+ +
-1
z
-a2 b2
+ +


… …

-aN

z-1 bM

Fig. 6.40 Direct-Form-II implementation of a IIR filter

The important achievement of Direct Form II is the reduced number of delay blocks, obtained
by their joint usage.
Remark: The Direct Form I and Direct Form II are identical for FIR Filters.
6.4.3.1 Cascade structure

The equation (6.18), where N≥M, can be factored into a cascade of second-order subsystems,
such that
P
H ( z )  H k ( z ) (6.66)
k 1

Where P is the integer part of (N+1)/2 and Hk(z) has the general form:
bk 0  bk1 z 1  bk 2 z 2
H k ( z ) (6.67)
1  a k1 z 1  a k 2 z  2
This implies the realization of the filter as a series of second-order blocks:

x(n)=x1(n) x2(n) x3(n) xP(n) y(n)=yP(n)


H1(z) H2(z) … HP(z)
y1(n) y2(n) yP-1(n)

Fig. 6.41 Cascade structure of second order blocks

The structure of a second order system component is presented in Fig. 6.42.

133
Digital Signal Processing with Matlab® Applications

xk(n) bk0 xk+1(n)


+ +
-1
yk(n)
z
-ak1 bk1
+ +
z-1
-ak2 bk2
+ +

Fig. 6.42 Second-order block component of the cascade structure

6.4.3.2 Parallel structure

The equation (6.18), where N≥M, can be expressed as partial fraction expansion
N
H ( z ) C   H k ( z ) (6.68)
k 1

with
Ak
H k ( z ) (6.69)
1  p k z 1
where pk represents the pole of order k and Ak the coefficient of the partial fraction expansion
(residue).
This implies the realization of the filter as a parallel connection of second-order blocks:

y1(n)
H1(z)

x(n) y2(n)
H2(z)

yN(n) y(n)
HN(z) +

Fig. 6.43 Parallel structure of second order blocks

where we combined the complex conjugated poles to form blocks with the equations:
bk 0  bk1 z 1
H k ( z ) (6.70)
1  a k1 z 1  a k 2 z  2

134
6 Digital Filters

The structure of a second order system component is presented in Fig. 6.44

x(n) bk0 yk(n)


+ +
z-1
-ak1 bk1
+ +
z-1
-ak2
+

Fig. 6.44 Second-order component of the parallel structure

6.4.5 Filter Design Functions in MATLAB


We will show the usage of MATLAB Signal Toolbox functions for filter design.
The functions return b and a, arrays corresponding to the filter coefficients for the direct and
recursive part.
The necessary parameters are:
N = order of the filter (maximum order of b and a)
Wn = normalized cutoff frequency (the frequencies are divided by fs/2)
Rp = pass band ripple
Rs = stop band ripple
MATLAB provides us with all the functions that we need to design digital filters using analog
prototypes.
For Low-Pass Filters
[b,a] = butter(N,Wn)
[b,a] = cheb1(N,Rp,Wn)
[b,a] = cheb2(N,Rs,Wn)
[b,a] = ellip(N,Rp,Rs,Wn)

For High-Pass Filters


[b,a] = butter(N,Wn,'high')
[b,a] = cheb1(N,Rp,Wn,'high')
[b,a] = cheb2(N,Rs,Wn,'high')
[b,a] = ellip(N,Rp,Rs,Wn,'high')

Band-Pass Filters
[b,a] = butter(N,[Wn1,Wn2])
[b,a] = cheb1(N,Rp,[Wn1,Wn2])
[b,a] = cheb2(N,Rs,[Wn1,Wn2])
[b,a] = ellip(N,Rp,Rs,[Wn1,Wn2])

Band-Stop Filters
[b,a] = butter(N,[Wn1,Wn2],'stop')

135
Digital Signal Processing with Matlab® Applications

[b,a] = cheb1(N,Rp,[Wn1,Wn2],'stop')
[b,a] = cheb2(N,Rs,[Wn1,Wn2],'stop')
[b,a] = ellip(N,Rp,Rs,[Wn1,Wn2],'stop')

MATLAB has also some interactive applications that can help you design the desired filter.
Fdatool
fvtool
sptool

Filter Design Toolbox is a collection of tools that provides advanced techniques for designing,
simulating, and analyzing digital filters. It extends the capabilities of Signal Processing Toolbox
with filter architectures and design methods for complex real-time DSP applications, including
adaptive filtering and multirate filtering, as well as filter transformations.

6.5 Special Topics

6.5.1 Nonlinear Filtering


There are important DSP techniques that are not linear. We will take as example the median
filtering, a nonlinear technique that uses a sliding window and replaces the sample placed in the
center of the window with the central value of the sorted window sequence (the median). If the
window has an even length, it will use the average of the two central values. The sequence is
padded with zeros at its boundaries.
The function y=medfilt1(x,n) returns the signal y as the result of median filtering on signal
x using a sliding window of size n.
Example 6.18 The median filters are an excellent choice when deal with interpolation of
corrupted or missing samples in speech and audio signals. Let us take a sine wave with every that
was corrupted on 10% of its data.
fs=50;t0=0;tf=3;
t=t0:1/fs:tf;
A=4; T=1; ph=0;
x=mySawtooth(4,1,0,t);
subplot(3,1,1), plot(t,x), axis([t0 tf -0.2 A+.1])
xlabel('Time'), xlabel('Amplitude'), title('Original signal')
noiseProb=.1; noiseA=3;
noise=(rand(size(x))<noiseProb).*myWhiteNoise(noiseA,t);
x=x+noise;
subplot(3,1,2), plot(t,x), axis([t0 tf -0.2 A+.1])
xlabel('Time'), xlabel('Amplitude'), title('Signal with noise')
n=3;
x=medfilt1(x,n);
subplot(3,1,3), plot(t,x), axis([t0 tf -0.2 A+.1])
xlabel('Time'), xlabel('Amplitude'), title('Median filtered signal')

136
6 Digital Filters

Original signal
4

Amplitude
2

0
0 0.5 1 1.5 2 2.5 3
Time
Signal with noise
4

Amplitude
2

0
0 0.5 1 1.5 2 2.5 3
Time
Median filtered signal
4
Amplitude

0
0 0.5 1 1.5 2 2.5 3
Time

Fig. 6.45 Example of median filtering

Exercise 6.30 Compute the MSE for the signal recovered in Example 6.18. Try different
window sizes and find out which offers the best results for this case.
Exercise 6.31 What types of signal/noise are fit for median filtering?

6.5.2 Deconvolution
Let us consider a analog signal x(t) that passes through a linear time-invariant system with
impulse response h(t), that alters the signal. We have normally access just to the output and many
times we want to recover the initial signal, like in the case of a communication channel (the original
signal), a scrambling mechanism (the original message), etc…

convolution deconvolution

x(t) y(t) y(t) ~ x(t)


h(t) h (t )

Fig. 6.46 Convolution/deconvolution process

~
What we have to do is to determine the inverse or deconvolving filter h (t ) , that has the
~ ~
property that h(t )  h (t )   (t ) , because x(t )   (t )  x(t ) . Once found h (t ) , we can recover the
~
original signal x(t) as x(t )  y (t )  h (t )
~
In the digital case, for a system that has the impulse response h(n), we want to find h (n) , with:
~
h( n)  h ( n)   ( n) (6.71)

137
Digital Signal Processing with Matlab® Applications

In the frequency domain, for a system with the transfer function H(z), this is equivalent to find
~
H ( z ) , such that
~
H ( z)  H ( z)  1 (6.72)
The deconvolution is a difficult problem because:
~
- the inverse filter can be unstable (the zeros of H(z) became the poles of H ( z ) )
- we can have noise added on the channel, that will be also deconvoluted;
- all the quantization, representation, computing etc… errors are adding
- the reconstruction of the original signal requires exact knowledge about H(z), which is
difficult to obtain.
MATLAB function uses the function deconv to perform deconvolution using long division:
[x,r]=deconv(y,h) will return x and r such that y=x  h+r.
Example 6.19 Let us try to find the signal x that convoluted with the filter [1 2 3] make a
signal composed of the prime numbers smaller than 40.
clr
h=[1 2 3]; % impulse response
y=primes(40) % prime numbers smaller than 40
[x,r]=deconv(y,h) % initial signal and reminder
rest=y-conv(x,h) %test

We obtain that:
y =
2 3 5 7 11 13 17 19 23 29 31 37
x =
2 -1 1 8 -8 5 31 -58 46 111
r =
0 0 0 0 0 0 0 0 0 0 -329 -296
rest =
0 0 0 0 0 0 0 0 0 0 -329 -296

There are methods belonging to the ”blind deconvolution” class that try to recover the original
signal without knowing the exact impulse response h(n).

6.5.3 Digital Restrictions


All of the digital filter designs presented up until now have been based on infinite-precision
mathematics. That is, we have assumed that all of the signal samples, filter coefficients, and results
of mathematical computations are represented exactly or with infinite precision. In most cases we
have used the double data type in C to approximate such precision. In Think C for the Apple
Macintosh, the double data type has a 64-bit mantissa that provides approximately 19 decimal digits
of precision. In Turbo C for the PC, the double data type has a 52-bit mantissa that provides
approximately 15 decimal digits of precision. For most practical situations, either 15 or 19 digits of
precision is a reasonable approximation to infinite precision. Furthermore, the double type is a
floating-point format and thus provides good dynamic range in addition to high precision.
Although floating-point formats are used in some digital filters, cost and speed considerations
will often dictate the use of fixed-point formats having a relatively short word length. Such formats
will force some precision to be lost in representations of the signal samples, filter coefficients, and
computation results. A digital filter designed under the infinite-precision assumption will not

138
6 Digital Filters

perform up to design expectations if implemented with short-word-length, fixed-point arithmetic. In


many cases, the degradations can be so severe as to make the filter unuseable. This chapter
examines the various types of degradations caused by finite-precision implementations and explores
what can be done to achieve acceptable filter performance in spite of the degradations.

6.6 Concluding Exercises

Exercise 6.32 In order to have linear phase, a FIR has to


a) Have b) Have all the c) Have real zeros d) Linear phase e) None of them
symmetric zeros in the unit cannot be
coefficients circle obtained using
FIR
Exercise 6.33 An IIR filter is
a) A filter that b) A recursive c) An Infinite d) All of them e) None of them
incorporates data filter Impulse Response
feedback filter
Exercise 6.34 A pole of a filter is
a) An artifact b) Generated by a c) Given by a d) All of them e) None of them
leading to feedback element solution of the
frequency in a filter equation from the
dependent gain in denominator of
a signal the transition
function
Exercise 6.35 An FIR filter has N coefficients and an IIR has P, Q coefficients at the
numerator, respectively denominator, with P+Q=N.
a) The FIR filter can be b) The IIR filter can be c) Both filters are implemented
implemented with a smaller implemented with a smaller with the same number of delays
number of delays than the IIR number of delays than the FIR
filter filter
Exercise 6.36 In order to be stable a digital system has to have the transfer function with:
a) All the poles in b) All the zeros in c) The zeros in d) The poles in e) The poles in
the unit circle the unit circle complex complex the left complex
conjugate pairs conjugate pairs semi-plane
Exercise 6.37 If the zeros and poles of a system occur in complex-conjugate pairs the system
ensure:
a) Stability b) Linearity c) Real d) All of the e) None of the
coefficients above above

Exercise 6.38 Represent superposed the frequency responses of averaging filters having N=1,
2, 4, 16, 32, 64 coefficients. Explain the results.
Exercise 6.39 Design a second-order digital Butterworth filter using bilinear transform. The
cut-off frequency is 1 kHz at a sampling frequency of 10 kHz.
Exercise 6.40 Repeat the previous problem for designing a highpass filter with the same
specifications.

139
Digital Signal Processing with Matlab® Applications

z 3  3z  2
Exercise 6.41 Given the transfer function H ( z )  , implement it using the direct
z2  z  2
form I and II, series and parallel realization.
z 6  3z 5  2 z 4  z 3  3z  2
Exercise 6.42 Given an IIR filter transfer function H ( z )  , realize
z6  2z5  z4  z2  2z 1
the transfer function in cascade canonical form, and parallel form.
Exercise 6.43 The difference filter is defined as y(n) = b0x(n) – b1x(n-1). Represent the
frequency response of the filter and show that this is a highpass filter.
Exercise 6.44 Design and plot the magnitude response of three IIR lowpass filters
(Butterworth, elliptic and Bessel designs) with the following specifications:
- Sampling rate of 8 kHz.
- Passband edge at 800Hz
- Stopband edge at 1200 Hz
- Passband ripple of 1 dB
- Minimum stopband attenuation of 60 dB
Exercise 6.45 Design using all the studied methods and show the characteristics (time and
frequency) of a bandpass filter with the following specifications:
- Sampling rate of 8 kHz.
- Passband edges at 1200 Hz and 2000 Hz
- Stopband edges at 1000 Hz and 2200 Hz
- Passband ripple of 1 dB
- Minimum stopband attenuation of 30 dB
Exercise 6.46 Design a type I Chebyshev IIR highpass filter with passband edge at 700 Hz,
stopband edge at 500 Hz, passband ripple of 1 dB, and minimum stopband attenuation of 32 dB.
The sampling frequency is 2 kHz. Plot the magnitude response of the design filter.
Exercise 6.47 Design a Parks-McClellan filter with a W characteristic that fits the points (0
Hz, 1), (500 Hz, 0), (1000 Hz, 0.5), (2000 Hz, 0), (4000 Hz, 1) for a sampling frequency of 8000
Hz. Show the filter results in both time domain and frequency domain.
Exercise 6.48 You have to put up several telephone quality audio signals (fs=8000 Hz) in a
channel with 44.1KHz total bandwidth (audio CD quality). How can you do this? How many
signals can be accommodated? Define the cutoff frequencies and design the filters to avoid
interference among these signals.
Exercise 6.49 Create a FIR filter (optimal, frequency, windows – Rectangular, Hamming,
Hanning, Blackman, Kaiser, etc….). The filter parameters are the cutting frequencies and the width
of transfer band. . If you have the index number n in your group, the filter type is
n mod 4 Type
0 LP
1 HP
2 BP
3 BS

140
6 Digital Filters

- Use the filter to filter the wave.


- Display the output function of time
- Display the FFT of output
- Play the sound before and after the filtering

Exercise 6.50 Write a function that will implement the window method for filter design. If
you have the index number n in your group, the method is
n mod 5 Method
0 Rectangular
1 Hamming
2 Hanning
3 Blackman
4 Kaiser

141
Digital Signal Processing with Matlab® Applications

7 Audio Signal Processing, Audio Effects, Noise Reduction

7.1 Digital Audio

The interest in digital audio is justified by the wide range of applications regarding
sound/music and speech processing domains (recording, storage, transmission and playing of audio
for telephony, entertainment, secure communications, hearing aids, voice recognition, command
recognition, etc…)
The digital processing of audio signal does not differ from DSP, but impose special constrains
on it. It will have to deal with psychoacoustics, its requirements being parameters and

Analog Analog
Audio Audio
Signal DSP Block Signal
Analog conditioning A/D Digital Signal D/A Analog conditioning
(antialising filter) Converter Processor Converter (reconstruct. filter)

Fig. 7.1 Basic blocks for Audio Digital Signal Processing

Analog anti-aliasing filters (part of analog conditioning) are needed at the input to remove the
signals that might alias down into baseband, as results from the sampling theorem. After the anti-
aliasing filter, the analog/digital converter (ADC) quantizes the continuous input into discrete
levels. ADC technology has shown considerable improvement in recent years due to the
development of oversampling and noise-shaping converters. After digital processing, the output of
the system is given to a digital/analog converter (DAC) which converts the discrete levels into
continuous voltages or currents. This output must also be filtered with a low pass filter to remove
the aliases. Subsequent processing can include further filtering, mixing, or other operations.
Most of the hifidelity audio sampling rates allow for reproduction of the full hearing range of
humans (typically 50 Hz to 11 KHz in a middle age adult, possibly up to 16 kHz in children), but
most of the speech frequencies are lower than 4KHz. Because of the sampling theorem, the
sampling frequencies for good quality audio/ music should be larger than 30Kz and larger than
7KHz for voice communication. The quantization with 16 bps (bits per sample) provides good
quality for most audio applications and 8 bps is enough for voice applications.
Standardization is necessary because audio is a specific domain of signal processing, with a
well defined area, but extremely large, considering the number of applications.
Example 7.1 Open from Start/Programs/Accessories/Entertainment the application “Sound
recorder” or call it directly from MATLAB with:
>> !sndrec32

Use it to record a few seconds of audio/ speech. When you save the record, in the lower part of
the “Save as” window, you have specified the characteristics of the file to be saved (the default is a
“*.wav” file with the format PCM 44.100 kHz, 16 Bit, Stereo). Click on the button “Change..” to

142
7-Audio Signal Processing, Noise Reduction

see other standard formats. From the Name: pop-up you can choose some standard alternatives:
“CD Quality”, “Radio Quality” and “Telephone Quality”. They use PCM (Pulse Code Modulation)
format (but you can choose compression formats like AC3, etc…) and different sampling
parameters (sampling frequency, number of bps and number of channels – mono or stereo). The
channel bandwidth is specified, measured in kbits per second. Save the file in some different
formats. It would be nice if you would use the “line in” to record music played by a Hifi device.
Play the records later and observe the differences.
The standard sampling rates for audio A/D converters are:
- 8 kHz: Poor quality voice telecommunications. The sampling rate of most telephonic based
speech communication is 8 kHz because most the of key speech frequency components are
contained in the bandwidth from 0 to 4 kHz.
- 11.025 KHz, 12KHz: Medium quality voice telecommunications
- 16 kHz: Teleconferencing. It is the sampling rate for good fidelity teleconferencing
equipment and for the DSP application area of hearing aids. When the audition is lasting a long
period of time, poor or medium quality audio becomes tiresome, so better quality is required, even
if it is not indispensable.
- 22.050 KHz, 24KHz: Radio Quality. Better quality is required by music broadcasting, but it
is not a Hifi transmission, not even the full hearing range being covered.
- 32 kHz: (low) Hifi for broadcasting (video or radio). It starts the standard hifi range,
representing the sampling rate of NICAM stereo audio for TV systems. It is not an exciting
quality, but it is a double to video, being compensated by it.
- 44.1 kHz: Standard (home) Hifi. It is the sampling rate of Compact Disc (CD) players.
In the early days of digital audio recording video tapes were used, using the standards of 525 lines
at 60Hz and 625 lines at 50Hz. In 60Hz video, there are 35 blanked lines, so we have 490 active
lines per frame and 245 lines per field. In 50Hz video, there are 37 blanked lines, so we have 588
active lines per frame and 294 lines per field. If we choose to store three audio samples per line,
we get: 60∙245∙3=50∙294∙3=44100Hz.
- 48 kHz: Professional Hifi. It is the sampling rate of digital audio tape (DAT) recorders, and
the sampling rate used by most professional audio systems.
- 96 kHz: Very Hifi The sampling rate of the emerging superfidelity standard. It is used also
for multiplexing.
The standard number of bits per sample is 8 or 16 for the above frequencies. For hifi
professional systems, it can be 24 or 32 bits per sample.
The number of channels can be:
- 1: mono audio.
- 2: stereo
- 2.1: stereo with central subwoofer
- 4.1, 5.1, 6.1, 7.1 as different surround arrangements.
- Movies can have many audio channels (like 10-20) played in the movie theatre to offer
surround sensation.
MATLAB has implicit support for reading and writing Microsoft wave (".wav") and
NeXT/SUN audio (".au") sound files. In the following we will deal with the ".wav" files.

143
Digital Signal Processing with Matlab® Applications

The function used to load ".wav" files is wavread. It supports multichannel data, up to 32 bits
per sample and Microsoft PCM data format only. Its general syntax is:
[y,fs,bits,opts]=wavread(filename,fmt);

where filename is a string representing the name of the file (single quotes and .wav appended
if none is given) and fmt is the format of the data to be read (one from 'double' or 'native'). If fmt is
'double' (the default value, if nothing is given), the read data will be double-precision normalized
samples (-1 ≤ y ≤ 1) and if it is 'native', the samples will keep their native data type ('uint8' for 8 bits
per sample, 'int16 ' for 16 bits per sample, 'int32' for 24 bits per sample and 'single' (floating point)
for 32 bits per sample.
The values of y represent the amplitude values of the samples, each channel being represented
on a separate column. Its values are in a range given by the fmt parameter (for the default value
'double', the values are in [-1,+1]). The value of fs represents the sampling frequency (in Hz) and
bits is the number of bits per sample used to encode the data in the file. Standard values for fs are
8000, 11025, 22050, 44100 and 48000 Hz and the value of bits can be 8, 16, 24 or 32. The structure
opts contains additional information contained in the WAV file. Its content differs from file to file,
but typical structure fields include '.fmt' (audio format information) and '.info' (text which may
describe title, author, etc.).
Example 7.2 We will use the wavread function to get the signal stored in the file
“ding.wav”, stored in the Media directory of Windows, and to obtain information about it.
>> [y,fs,bps,opts]=wavread('c:\windows\media\ding','native');

The inspection of the values gives:


>> size(y)
ans =
20191 2
>> class(y)
ans =
int16
>> fs =
ans =
22050
>> bps =
ans =
16
>> opts
opts =
fmt: [1x1 struct]
info: [1x1 struct]

>> opts.fmt
ans =
wFormatTag: 1
nChannels: 2
nSamplesPerSec: 22050
nAvgBytesPerSec: 88200
nBlockAlign: 4
nBitsPerSample: 16
>> opts.info
ans =
icop: '1998 Microsoft Corporation'

144
7-Audio Signal Processing, Noise Reduction

Example 7.3 To see the data in the signal y, we can display its with:
>> y

but we cannot display the result here because of its size, being a matrix with 20191 lines and 2
columns, as indicating by:
>> size(y)

We will plot instead its waveform.


>> plot(y)

8000

6000

4000

2000

-2000

-4000

-6000

-8000
0 0.5 1 1.5 2 2.5
4
x 10

Fig. 7.1 Representation of the signal “ding.wav”

We can see again that the signal is a stereo one (two channels), but the representation is made
as a function of the index of the sample. If we want to integrate timing information, we will make a
time vector, containing the sampling instants.
Example 7.4 We will write a special function reprTime(a,y,fs) that will represent the
signal y in the axes a, function of the sampling time vector generated by the sampling frequency fs.
It will have to work with multichannel signals, where the channels are represented on lines or on
columns.
function reprTime(a,y,fs)

if size(y,2)>size(y,1)
y=y';
end
t=0:1/fs:(length(y)-1)/fs;

axes(a);
plot(t,y,'-');
% title('Time domain')
xlabel('Time(s)')
ylabel('Amplitude')

145
Digital Signal Processing with Matlab® Applications

8000

6000

4000

2000

Amplitude
0

-2000

-4000

-6000

-8000
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time(s)

Fig. 7.2 Representation of the signal “ding.wav” function of time

From the last plot, it can be easily observed the extension in time of the signal – almost one
second.
Example 7.5 Let’s try to hear how this signal sounds like. To play it (send it to the Windows
wave audio device), we will use the function wavplay. Its general syntax is
wavplay(y,fs)

where y is the vector of the samples amplitudes and fs is the audio rate/ sampling frequency (in
Hz). The vector y is a matrix having two columns for stereo signal (each channel on a separate
column), its values being of one of the types double, int16, or uint8. Double precision data samples
must be normalized (in the range [-1 +1]), values outside this range being clipped. If you want to
hear the signal properly, you will have to play it with the audio rate equal with the sampling
frequency read from the .wav file.
To play the “ding.wav” signal, just type:
>> wavplay(y,fs)

Other functions that can be used to play music are sound and soundsc.
Example 7.6 We will write a special function reprMag(a,y,fs) that will represent the
magnitude sptectrum of the DFT for the signal y in the axes a, function of the frequencies vector
generated by the sampling frequency fs .
function reprMag(a,y,fs)
if size(y,2)>size(y,1)
y=y';
end
y=double(y); % cast to double for fft
axes(a);
Y=fft(y);
N=ceil(length(Y)/2);
Y=Y(1:N,:);
f=0:(fs/2)/(N-1):fs/2;
plot(f,abs(Y),'-')
% title('Magnitude spectrum')
xlabel('Frequency(Hz)'), ylabel('Magnitude')

146
7-Audio Signal Processing, Noise Reduction

7
x 10
2

1.8

1.6

1.4

1.2

Magnitude
1

0.8

0.6

0.4

0.2

0
0 2000 4000 6000 8000 10000 12000
Frequency(Hz)

Fig. 7.3 Spectrum for the signal “ding.wav”

Use the zoom to see that you have two graphs, one for each channel. Even if the components
for each channel appear to be very different in time, they have very similar spectra.
The function wavwrite(y,filename) writes the data stored in the variable y to a
WAVE file called filename. The filename input is a string enclosed in single quotes. The data has a
sample rate of 8000 Hz and is assumed to be 16-bit. Each column of the data represents a separate
channel. Therefore, stereo data should be specified as a matrix with two columns. Amplitude values
outside the range [-1,+1] are clipped prior to writing.
Example 7.7 Let us combine the stereo channels in a single one (mono), store it to the disk
and check the results.
yMono=mean(y,2);
yMono=yMono/max(abs(yMono));
wavwrite (yMono,fs,bps,'dingMono');
[yM,fsM,bpsM,optsM]=wavread('dingMono','native');
figure
plot(t,yM)

4
x 10
4

1
Amplitude

-1

-2

-3

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time(s)

Fig. 7.4 Representation of a mono audio signal obtained from “ding.wav” function of time

147
Digital Signal Processing with Matlab® Applications

Remark Besides showing how to save a wave file, we modified a stereo audio signal, making
it mono. This operation fit in the audio signal processing. In the next chapter, we will discuss about
this kind of operations.

7.2 Audio Effects

Digital data representation is important to prevent the degradation commonly associated with
analog storage and manipulation. This is very familiar to anyone who has compared the musical
quality of cassette tapes with compact disks. In a typical scenario, a musical piece is recorded in a
sound studio on multiple channels or tracks. In some cases, this even involves recording individual
instruments and singers separately. This is done to give the sound engineer greater flexibility in
creating the final product. The complex process of combining the individual tracks into a final
product is called mix down. DSP can provide several important functions during mix down,
including: filtering, signal addition and subtraction, signal editing, etc.
Audio signal processing includes a wide variety of effects and filtering techniques. We will
study some of them.

7.2.1 Silence
Silence is the effect of canceling the sound for a given period. The signal will have the value 0
all that time.
Exercise 7.1 Write a function y=silence(x, fs ,t1 ,t2) that will silence the signal x
sampled at fs in the time interval [t1 t2] seconds. If t1 or t2 are inappropriate, it will display an error
message, specifying the accepted limits for them (the length of the sound). Test it on the signal from
the file 'tada.wav' for t1=.2s and t2=1.4s.

7.2.2 Volume settings


The audio signal volume is represented in the signal itself by the maximum absolute amplitude
present, compared with the maximum one allowed by the digital representation.
Exercise 7.2 Write a function v=getMaxVolume(x, bps) that will return the ratio from
the maximum amplitude on each channel of signal y and the maximum amplitude allowed by the
representation with bps bits per sample. If the parameter bps is missing, it will be taken 1
(normalized signal)
Exercise 7.3 Write a function y=modifyAmplitude(x, k) that will return the sign al y
having the sample values on each channel equal with the values of the samples from x multiplied
individually with the corresponding element from the vector k (a constant for each channel). If k is
a scalar, each channel will be multiplied with the value of k.

7.2.3 Balance
Exercise 7.4 Write a function p=getChannelsPower(x) that will return a vector
containing the power p enclosed in each channel of the signal y.
Exercise 7.5 Using the functions getChannelsPower and modifyAmplitude, write a
function y=equalizePower(x) that will amplify the channels of x such that ys channels will have
equal power.

148
7-Audio Signal Processing, Noise Reduction

7.2.4 Fading In
Fading in is the effect of gradually increase of the volume at the start of playing a sound.
Exercise 7.6 Write a function y=fadeIn(x,fs,dt) that will apply the fading in effect
on signal x, sampled at fs, on a period of dt seconds – the signal will start from 0 and increase linear
until it will reach its ordinary value after dt. If dt is too large, it will display an error message,
specifying the maximal time lag for fading in (the length of the sound); Test it on the signal from
the file 'tada.wav' for dt=1 second.

7.2.5 Fading Out


Fading out is the effect of gradually decrease of the volume at the end of playing a sound.
Exercise 7.7 Write a function y=fadeOut(x,fs,dt) that will apply the fading out
effect on signal x, sampled at fs, on a period of dt seconds – the signal will start from dt seconds to
the end and decrease linear until it will reach 0 at the end of the play. If dt is too large, it will
display an error message, specifying the maximal time lag for fading out (the length of the sound);
Test it on the signal from the file 'tada.wav' for dt=1.9 seconds.

7.2.6 Echo
The echo is a repetition of a sound by reflection of sound waves from the surfaces. This effect
is based on using a delay and an attenuation in the waveform. A delay higher than 50 ms is heard as
an echo. The procedure is the following: first we delay the audio signal and then it is played back
over the original signal after the delay time (the echo period can be extended from milliseconds to
seconds). We can put this procedure in a simple equation:
y(n)= x(n)+αx(n-k) (7.1)
Where k is the number of samples delay introduced by the longer sound path, being equal with
time delay dt divided with the sampling frequency fs, and α is a subunitary coefficient representing
the attenuation. The transfer function is:
H(z)=1+αz-k (7.2)
Exercise 7.8 Write a function y=echo(x,fs,alpha,dt) that will apply the echo effect on
signal x, sampled at fs with a delay of dt seconds and an attenuation coefficient alpha. Test it on the
signal from the file 'tada.wav' for dt=0.05 seconds and alpha=.3.

7.2.7 Reverberation
One of the most interesting DSP applications in music preparation is artificial reverberation. If
the individual channels are simply added together, the resulting piece sounds frail and diluted, much
as if the musicians were playing outdoors. This is because listeners are greatly influenced by the
echo or reverberation content of the music, which is usually minimized in the sound studio. DSP
allows artificial echoes and reverberation to be added during mix down to simulate various ideal
listening environments.
The reverberation results from the multiple reflections that occur in a room. Practically,
reverberation is a combination of delayed and attenuated sound waves. Here, we have chosen a
filter that realizes an infinite number of echoes spaced k sampling periods apart with exponentially
decaying amplitude. The following relation can be used:

149
Digital Signal Processing with Matlab® Applications

z k
H ( z)  (7.3)
1    z k
Exercise 7.9 Plot the impulse response defined by (7.3) for k=100 and α=0.3.
Exercise 7.10 Write a function y=reverb(x,fs,alpha,dt) that will apply the reverb
effect on signal x, sampled at fs with a delay of dt seconds to the first echo and an attenuation
coefficient alpha. Test it on the signal from the file 'tada.wav' for dt=0.05 seconds and alpha=.5.

7.2.8 DC Blocker
The DC blocker is an indispensable tool in digital waveguide modeling and other applications.
It is often needed to remove the dc component of the signal circulating in a delay-line loop. It is also
often an important tool in multi-track recording, where dc components in the various tracks can add
up and overflow the mix.
The DC blocker is a recursive filter specified by the difference equation
y(n)=x(n)-x(n-1)+k∙y(n-1) (7.4)
where k is a parameter which is typically somewhere between and (for a 44.1 kHz
sampling rate, k=0.995 is good). The transfer function is
1  z 1
H ( z)  (7.5)
1  k  z 1
thus, there is a zero at DC ( ) and a pole near DC at z=k. Far away from DC, the pole and
zero approximately cancel each other.
Exercise 7.11 Create a function y=DCBlock(x,k) that will remove the DC from the signal x
using the coefficient k. Test it on the Test it on the signal from 'tada.wav' after you add an offset of
0.5 to it.

7.2.11 Equalizer
This effect deals with different filtering processes. The frequency of a played sound (track) is
adjusted to make the sound better (adjustment for room acoustics, for removing the noise). The
simplest equalizer comprises of three types of filters: low-pass, band-pass and high-pass, each one
wih its weighting coefficient..
Exercise 7.12 Add to your audio player a graphic equalizer with three bands: LP from 0 to
300 Hz, BP from 300 Hz to 6 KHz and HP from 6 KHz up. The sound to be equalized will be
filtered first by these three filters, each frequency band component being weighted with a constant
and added to the result.

7.3 Noise Reduction

Most signals are acquired with noise contamination. This noise is corrupting the signal and can
lead to misinterpretation or lose of information from the original signal. Sometimes, it is so strong
the signal is unrecognizable.
We define the Signal to Noise Ratio (SNR) as depending on the ratio of signal and noise
powers:

150
7-Audio Signal Processing, Noise Reduction

PSignal
SNR  10  log (7.6)
PNoise
When we express it related to amplitudes, we have:
ASignal
SNR  20  log (7.7)
ANoise
For a very poor quality telephone line, SNR can be as low as 10dB, but for a CD or MP3
player, the SNR is higher than 90dB.
We will create artificial noise and it to signals. The methods used to reduce it are general and
can be used for other types of signals.
Example 7.8 Consider simulating a noisy signal consisting of a sine wave with additive
Gaussian measurement noise. The following script simulates this noisy signal. In the next section,
methods to reduce the affect of the noise will be considered.
% Noisy signal generation and analysis
fs=8000;t0=0;tf=.1; t = t0:1/fs:tf; % time base
s = mySine(1,100,0,t); % signal
n = 0.1*randn(size(t)); % noise
x = s + n; % noisy signal
SNR = 20*log10(std(s)/std(n)) % signal to noise ratio, dB
plot(t,x), xlabel('Time (s)'),ylabel('Signal amplitude'),...
title([ 'Noisy Sine signal, SNR = ' num2str(SNR)])

Noisy Sine signal, SNR = 17.1418


1.5

0.5
Signal amplitude

-0.5

-1

-1.5
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
Time (s)

Fig. 7.5 Sine signal contaminated with Gaussian noise, SNR=17.14

Exercise 7.13 We will load the file “chimes.wav” and we will contaminate both channels
with white noise. Because the original sound signal is somehow concentrated in frequency, we will
filter the contaminated signal band-pass. The filter characteristics can be seen in Fig. 7.6 and the
signals can be observed in Fig. 7.7.
[x,fs]=wavread('chimes.wav');
t=(1:length(x))'/fs;
y=x+[myWhiteNoise(.1,t) myWhiteNoise(.1,t)];
[b,a]=butter(4,[600 1600]/(fs/2));
z=filter(b,a,y);

151
Digital Signal Processing with Matlab® Applications

reprTime(subplot(3,2,1),x,fs) % representation
reprMag(subplot(3,2,2),x,fs)
reprTime(subplot(3,2,3),y,fs)
reprMag(subplot(3,2,4),y,fs)
reprTime(subplot(3,2,5),z,fs)
reprMag(subplot(3,2,6),z,fs)
plotFreqPhaseResp(b,a,fs,'Butterworth filter',[600 1600])
wavplay(x,fs),pause(.5), wavplay(y,fs),pause(.5), wavplay(z,fs)

Butterworth filter
1.5
Magnitude

0.5

0
0 2000 4000 6000 8000 10000 12000
Frequency (Hz)

200
Magnitude (dB)

-200

-400
0 2000 4000 6000 8000 10000 12000
Frequency (Hz)

0
Phase (rad)

-5

-10

-15
0 2000 4000 6000 8000 10000 12000
Frequency (Hz)

Fig. 7.6 Butterworth BP filter of order 4

0.5 200
Magnitude
Amplitude

0 100

-0.5 0
0 0.2 0.4 0.6 0.8 0 5000 10000 15000
Time(s) Frequency(Hz)
0.5 200
Magnitude
Amplitude

0 100

-0.5 0
0 0.2 0.4 0.6 0.8 0 5000 10000 15000
Time(s) Frequency(Hz)
0.5 200
Magnitude
Amplitude

0 100

-0.5 0
0 0.2 0.4 0.6 0.8 0 5000 10000 15000
Time(s) Frequency(Hz)

Fig. 7.7 Signal denoising

152
7-Audio Signal Processing, Noise Reduction

7.4 Concluding Exercises

Exercise 7.14 Write an application that will be able to load *.wav files from the disk, show
the waveform, the Fourier Transform, and have the capability to play the sound/ Additional
information – Name of the file, sampling frequency, length in time, number of channels and number
of bits per sample should be displayed/.
Remark: All channels will be represented – superposed or using subplot – both in time and in
frequency.
Exercise 7.15 Improve the application from the previous exercise by making it able to filter
the signal and to save the result. In Fig. 7.8 it is presented an application that uses polynomial
filtering to smooth the signal.

Fig. 7.8 Application with filtering interface

Exercise 7.16 Create 3 sound effects of your own. Add them to the graphical interface.
Exercise 7.17 Create a small keyboard that will use the following frequencies for the keys
C D E F G A B
262 Hz 294 Hz 330 Hz 349 Hz 392 Hz 440 Hz 494 Hz
The sounds will be sine waveforms with the sampling frequency 44100 Hz and the duration 1s or
less.
Exercise 7.18 Create a small keyboard with 2 octaves. The parameters for the application
(taken from edit boxes) will be the length of a note and the sampling frequency. Tell if the sampling
frequency affects the quality of the signal.
Exercise 7.19 Make your application capable to record the track you played.
Exercise 7.20 Make an application that will show the STFT (Short Time Fourier Transform)
of a *.wav file. Use the window length 1024 samples and the interleave of 512 samples.
Exercise 7.21 Make the show synchronous, if possible. Describe the method you use.
Exercise 7.22 Create an application that will be able to:

153
Digital Signal Processing with Matlab® Applications

- Open an *.wav file and read its data.


- Display the waveform function of time
- Display the FFT of the waveform

Exercise 7.23 Create an application that will be able to enhance an audio signal with the
surround effect (using reverberation). You can use some of the known algorithms, like Gerzon or
Schroeder.
Room with emitter and receiver Traces in the room
4 4

3 s 3

2 2

1 r 1

0 0
0 1 2 3 4 5 0 1 2 3 4 5
Original signal Simulated sorround signal
0.2 0.2

0.1 0.1

0 0

-0.1 -0.1

-0.2 -0.2
0 0.5 1 1.5 2 2.5 0 0.5 1 1.5 2 2.5
4 4
FFT of the original signal x 10 FFT of the ssound signal x 10
600 600
500 500
400 400
300 300
200 200
100 100

0 2000 4000 6000 8000 10000 0 2000 4000 6000 8000 10000

Fig. 7.9 Application with reverberation interface

154
8- Modulation and Encoding

8 Modulation and Encoding


This chapter is dedicated to the ways data is transmitted and stored. We will present briefly the
types of analog modulation, present some aspects of digital modulation and introduce the
techniques where analog data is stored in digital format, as a complement of chapter.

8.1 Analog Modulation

For almost any communication channel/media, there are limitations on the frequency bands
that can pass, so the signal carrying information to be transmitted is modified, transmitted through
the media, collected at the receiver and modified back in the initial form.

8.1.1 Modulation-Demodulation
Modulation represents the variation of amplitude, phase, or frequency of a carrier signal with
respect to a message (modulator) signal.
The function y = modulate(x,Fc,Fs,'method',opt)modulates a message signal with a
specified modulation method, where x is the message signal, Fc is the carrier frequency, Fs is the
sampling frequency, method is a flag for the desired modulation method and opt is any additional
argument that the method requires. (Not all modulation methods require an option argument). You
can see all the options when typing >> help modulate.
The analog modulation methods include:
- AMDSB–SC or AM: Amplitude Modulation, Double Side-Band, Suppressed Carrier
- AMDSB–TC: Amplitude Modulation, Double Side-Band, Transmitted Carrier
- AMSSB: Amplitude Modulation, Single Side-Band
- FM: Frequency Modulation
- PM: Phase Modulation
- PTM: Pulse Time Modulation
- PWM Pulse Width Modulation
- PPM: Pulse Position Modulation
- QAM: Quadrature Amplitude Modulation
The demodulate function restore the original signal from the modulated one.
The syntax for demod is x = demod(y, Fc,Fs,'method',opt) and it uses any of the
methods shown for modulate, but the syntax for quadrature amplitude demodulation requires two
output parameters.
[x1,x2] = demod(y,Fc,Fs,'qam')

For the modulation of a signal having the bandwidth BW with a carrier having the frequency
Fc, the sampling frequency Fs must satisfy:
Fs > 2∙Fc + BW (8.1)
If the input x is a matrix and not a vector, modulate and demod process each column of the
array, considering them different channels of the signal.

155
Digital Signal Processing with Matlab® Applications

8.1.2 Amplitude Modulation-AM


We usually call by AM the Amplitude Modulation, Double Sideband, Suppressed Carrier
(AMDSB). The modulated signal is obtained by multiplying the original signal x by a sinusoid of
frequency fc.
y = x.*cos(2∙π∙fc∙t) (8.2)
Example 8.1 We will modulate and demodulate a chirp signal sampled at 400 Hz with the
carrier at 30Hz using AM.
fs=400; t0=0;tf=1; t=t0:1/fs:tf; % define sampling

f0=1; f1=3; % define original = chirp signal


origSignal = chirp(t,f0,tf,f1);

% represent original signal


reprTime(subplot(4,2,1),origSignal,fs); axis([0 1 -1.1 1.1])
reprMag(subplot(4,2,2),origSignal,fs)

fc=30; % define carrier = cosine signal


carrier=myCosine(1,fc,0,t)

% represent carrier signal


reprTime(subplot(4,2,3),carrier,fs); axis([0 1 -1.1 1.1])
reprMag(subplot(4,2,4),carrier,fs)

% compute modulated signal = product of carrier and original


% modulatedSignal=modulate(origSignal,fc,fs,'am');
modulatedSignal=origSignal.*carrier;

% represent modulated signal


reprTime(subplot(4,2,5),modulatedSignal,fs); axis([0 1 -1.1 1.1])
reprMag(subplot(4,2,6),modulatedSignal,fs)

% compute demodulated signal


demodulatedSignal=demod(modulatedSignal,fc,fs,'am');

% represent demodulated signal


reprTime(subplot(4,2,7),demodulatedSignal,fs); axis([0 1 -1.1 1.1])
reprMag(subplot(4,2,8),demodulatedSignal,fs)

The graphics are shown in Fig. 8.1


Exercise 8.1 Explain the form of the modulated signal ((c), 1).
Exercise 8.2 What is the bandwith of the original signal defined in Example 8.1? What is
the frequency of the carrier? What is the bandwidth of the modulated signal? Why?
Exercise 8.3 Verify relation (8.1). Explain its necessity.
Exercise 8.4 Observe the changing in the amplitude of the signal when it is recovered by
demodulation. Explain. How can the amplitude be also recovered?
Remark: In the graphical representations of this chapter, there will be 4 lines of plots,
a) original signal; b) carrier; c) modulated signal and d) demodulated signal, even if they will not be
nominated explicitly.

156
8- Modulation and Encoding

Magnitude
200

Amplitude
1
a) 0 100
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Magnitude
200
Amplitude
1
0 100
b)
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Magnitude
100
Amplitude

c) 0 50
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Magnitude
100
Amplitude

1
d) 0 50
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Fig. 8.1 Amplitude modulation of a chirp signal: a) original signal; b) carrier;


c) modulated signal; d) demodulated signal

Exercise 8.5 Modulate and demodulate a periodical square signal sampled at 400 Hz using
AM with the carrier at 30Hz. The signal has amplitude 1, frequency 3Hz, initial phase 0, a duty
factor 33% and an offset of 1. The graphics you should obtain are depicted in Fig 8.2.
Magnitude

400
Amplitude

2
1 200
0 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude

200
Amplitude

1
0 100
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude

200
Amplitude

2
0 100
-2 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude

200
Amplitude

2
1 100
0 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Fig. 8.2 Amplitude modulation of a periodical square signal

157
Digital Signal Processing with Matlab® Applications

8.1.3 Frequency Modulation-FM


Frequency Modulation creates a sinusoid with instantaneous frequency that varies with the
original signal x.
y = cos(2∙π∙fc∙t+ k∙cumsum(x)) (8.3)
, where cumsum is a rectangular approximation to the integral of x. Modulate uses k as the
constant of frequency modulation. If k is not present, modulate uses a default of:
K = (fc/fs) 2∙π i/(max(max(x))) (8.4)
s.t. the maximum frequency excursion from fc is fc Hz
Exercise 8.6 Modulate and demodulate a periodical square signal sampled at 400 Hz using
FM with the carrier at 30Hz. The signal has amplitude 1, frequency 3Hz, initial phase 0, a duty
factor 33% and an offset of 1. The graphics you should obtain are shown in Fig 8.3.

Magnitude
400
Amplitude

2
1 200
0 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude

200
Amplitude

1
0 100
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude

200
Amplitude

1
0 100
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude

100
Amplitude

0.4
0.2 50
0
0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Fig. 8.3 Frequency modulation of a periodical square signal

8.1.4 Phase Modulation-PM


Phase Modulation creates a sinusoid of frequency fc whose phase varies with the message
signal x.
y =cos(2∙π∙fc∙t+ k*x) (8.5)
The function modulate uses for PM a default of
k =π/(max(max(x))) (8.6)
so the maximum phase excursion is π radians.

158
8- Modulation and Encoding

Exercise 8.7 Modulate and demodulate a periodical square signal sampled at 400 Hz using
PM with the carrier at 30Hz. The signal has amplitude 1, frequency 3Hz, initial phase 0, a duty
factor 33% and an offset of 1. The graphics you should obtain are presented in Fig 8.4.

Magnitude
400

Amplitude
2
1 200
0 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Magnitude
200
Amplitude

1
0 100
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Magnitude
200
Amplitude

1
0 100
-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
Magnitude 400
Amplitude

2
0 200
-2
0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Fig. 8.4 Phase modulation of a periodical square signal

Example 8.2 To see the MATLAB example of analog modulation using AM, AMSSB, FM
and PM, type in the command line >> moddemo.
5
Original

-5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7

0.067404

0.13481

0.20221

0.26961

0.33702

0.40442

0.47183

0 0.01 0.02 0.03 0.04 0.05 0.06


2
Demodulated

-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
Time (Seconds)

Fig. 8.5 MATLAB’s analog modulation demonstration Moddemo

159
Digital Signal Processing with Matlab® Applications

8.2 Discrete Modulation

In digital communication systems, discrete modulation techniques are used to modulate the
source information signal. Discrete modulation can be considered just the procedure of sending a
signal in digital form or, like in analog modulation, the modification of a transmittable signal
according to the information from the message signal, both of them being digital and having just a
discrete set of values.
The discrete modulation methods include:
- PCM: Pulse-Code Modulation
- DM: Differential Modulation
- DPCM: Differential Pulse-Code Modulation
- FSK: Frequency-Shift Keying
- PSK: Phase-Shift Keying
- DPSK: Differential Phase-Shift Keying
- MPSK: M-ary Phase-Shift Keying
- QAM: Quadrature Amplitude Modulation
Remark: For some of the methods, the word modulation is replaced by keying. Another term
used for discrete modulation is encoding.

8.2.1 Pulse-Code Modulation - PCM


Pulse Code Modulation is a procedure of converting (sampling, quantizing and encoding ) an
analog signal in a digital one where the samples are stored as fixed length digital words in a serial
bit stream.
The sampling operation generates a flat-top pulse amplitude modulation (PAM) signal. The
quantizing converts the actual sampled value into the nearest of the 2N amplitude levels for an N bits
per sample quantization. The PCM signal is obtained from the quantized PAM signal by encoding
each quantized sample value into a digital word.
PCM is widely used in audio systems, because it can be easily converted back into analog
signals.
Exercise 8.8 Write the function [pcmSignal,normConstant] = pcmEncode
(origSignal,n) that will return the modulated signal pcmSignal and the normalization constant
normConstant used to modulate the original signal origSignal with PCM on n bits and the function
decodedSignal = pcmDecode (pcmSignal,n,normConstant) that will return the
demodulated signal decodedSignal obtained from the PCM modulated on n bits signal pcmSignal
having the normalization constant normConstant.
Exercise 8.9 Test the PCM coding/decoding functions on the chirp signal defined at
Example 8.1. For a 2 bit quantization, the graphics should look like in Fig. 8.8 or better.
Exercise 8.10 Explain the differences in the amplitude reconstruction and in the magnitude
spectrum.

160
8- Modulation and Encoding

1 200

Magnitude
Amplitude
0 100

-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
2 400

Magnitude
Amplitude

0
200
-2
0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
1 200

Magnitude
Amplitude

0 100

-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Fig. 8.8 Pulse Code Modulation of a chirp signal

8.2.2 Differential Pulse-Code Modulation - DPCM


PCM is not the only way we can encode wave data. Instead of encoding the individual sample
value, we can encode the difference between the current value and the last one. Since most waves
are continuous and relatively smooth, for a fast sampling, the difference is relatively small and it
can be represented on fewer bits.
Differential Pulse Code Modulation is a procedure of converting an analog into a digital
signal in which an analog signal is sampled and then the difference between the actual sample value
and its predicted value (the predicted value is based on previous sample or samples) is quantized
and then encoded to form a digital value.
In such a system the difference between the current sample and a predicted version of that
sample gets quantized, coded, and transmitted to the receiver. This difference is referred to as the
prediction error and is given by
e(n)  x(n)  xˆ (n) (8.7)
The prediction is based on previously transmitted and decoded information and can be linear
or nonlinear, fixed or adaptive. The difference signal e(n) is then passed through a quantizer. The
signal at the output of the quantizer is the quantized prediction error eq(n), which is entropy
encoded transmission. The first step at the receiver is to decode the quantized prediction error. After
decoding, dq(n) is added to the predicted value of the current sample xˆ (n) to yield the reconstructed
sample value x(n). Note that as long as a quantizer is included in the system, the output signal will
not exactly equal the input signal.

161
Digital Signal Processing with Matlab® Applications


Predictor

+
-
+
x + e eq d Channel
Quantizer Encoder

Fig. 8.6 Differential Pulse Code Modulation Encoder


Predictor

+
Channel d d + x
Decoder

Fig. 8.7 Differential Pulse Code Modulation Decoder

Remark: DPCM codewords represent differences between samples unlike PCM where code
words represented a samples value. Most source signals show significant correlation between
successive samples so encoding uses redundancy in sample values which implies lower bit rate.
In a typical PCM-encoded speech waveform, there are often successive samples taken in which
there is little difference between the amplitude of the two samples. This necessitates transmitting
several identical PCM codes, which is redundant. Differential pulse code modulation (DPCM) is
designed specifically to take advantage of the sample to sample redundancy in typical speech
waveforms. With DPCM, the difference in amplitude of two successive samples is transmitted
rather than the actual sample. Since the range of sample differences is typically less than the range
of individual samples, fewer bits are required for DPCM then conventional PCM.
Exercise 8.11 Write the function [dpcmSignal,firstValue,normConstant] =
dpcmEncode (origSignal,n) that will return the modulated signal dpcmSignal, starting value
firstValue and the normalization constant normConstant used to modulate the original signal
origSignal with DPCM on n bits and the function decodedSignal = dpcmDecode
(dpcmSignal, n, firstValue, normConstant) that will return the demodulated signal
decodedSignal obtained from the DPCM modulated on n bits signal dpcmSignal having the first
value firstValue and the normalization constant normConstant.
Exercise 8.12 Test the DPCM coding/decoding functions on the chirp signal defined at
Example 8.1. For a 2 bit quantization, the graphics should look like in Fig. 8.9 or better
Exercise 8.13 Explain the differences in the amplitude reconstruction and in the magnitude
spectrum.

162
8- Modulation and Encoding

1 200

Magnitude
Amplitude
0 100

-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
2 400

Magnitude
Amplitude

0
200
-2
0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)
200
1

Magnitude
Amplitude

0 100

-1 0
0 0.5 1 0 50 100 150 200
Time(s) Frequency(Hz)

Fig. 8.9 Differential Pulse Code Modulation of a chirp signal

8.2.3 Delta modulation - DM


Delta Modulation is the simplest form of differential pulse code modulation, where the pulses
differences are quantized on 1 bit and there is used a fixed first order predictor.
The output is 1 if the signal increases and 0 if the signal decreases, the DM coding in fact the
direction of differences in signal amplitude instead of the value of difference as DPCM does.


Integrator

-
x + d
e eq
1 bit quantizer Encoder

Fig. 8.10 Delta Modulation Encoder

Remark: For DM, the sampling rate is dependent on the variation/derivative of the signal
because a signal that changes rapidly will require a fast sampling for acceptable performance.
Important characteristic of DM is that waveform that is delta modulated needs oversampling,
i.e. the signal must be sampled faster than necessary, sampling rate for DM is much higher than

163
Digital Signal Processing with Matlab® Applications

Nyquist rate (twice bandwidth). Two types of distortion limits the performance of DM: slope
overload distortion and granular noise.
Slope overload distortion - is caused by use of step size delta which is too small to follow
portions of waveform that has a steep slope and can be reduced by increasing the step size. Granular
noise - is caused by too large step size in signal parts with small slope and can be reduced by
decreasing the step size.

8.3 Concluding Exercises

Exercise 8.14 Modulate and demodulate using Pulse Time Modulation (PTM) the signal
defined in Exercise 8.5 (periodical square with amplitude 1, frequency 3Hz, initial phase 0, a duty
factor 33% and an offset of 1, sampled at 400 Hz, with the carrier at 30Hz).
Exercise 8.15 Modulate and demodulate using Pulse Width Modulation (PWM) the signal
defined in Exercise 8.5 (periodical square with amplitude 1, frequency 3Hz, initial phase 0, a duty
factor 33% and an offset of 1, sampled at 400 Hz, with the carrier at 30Hz).
Exercise 8.16 Modulate and demodulate using Quadrature Amplitude Modulation (QAM) the
signal defined in Exercise 8.5 (periodical square with amplitude 1, frequency 3Hz, initial phase 0, a
duty factor 33% and an offset of 1, sampled at 400 Hz, with the carrier at 30Hz).
Exercise 8.17 Modulate and demodulate the signal from “ringout.wav” using AM. Inspect the
reconstruction visually and auditory. Do you observe differences? Try different variations for
modulation.
Exercise 8.18 Re-encode the signal from “ringout.wav” using PCM with 6 bits per sample.
Inspect the reconstruction visually and auditory. Do you observe differences? Try different
variations for modulation.
Exercise 8.19 Encode the signal from “ringout.wav” using DPCM with 4 bits per sample.
Inspect the reconstruction visually and auditory. Do you observe differences? Try different
variations for modulation.
Exercise 8.20 Take three different *.wav file and modulate them, shifting their spectrum in
different bands. Add the initial signals and hear them. Add the modulated signals and hear them.
Demodulate them in the base-band and hear the results. Write the modMultiAM and
demodMultiAM functions to implement this method. It represents the technique used to transmit
more than one signal over a communication channel.
Exercise 8.21 Modulate and demodulate the signal from “ringout.wav” using FM. Inspect the
reconstruction visually and auditory. Do you observe differences? Try different variations for
modulation.
Exercise 8.22 Modulate and demodulate the signal from “ringout.wav” using PM. Inspect the
reconstruction visually and auditory. Do you observe differences? Try different variations for
modulation.
Exercise 8.23 For tone dialing, the telephones generate Dual Tone Multi Frequency (DTMF)
signals to dial other numbers. When a key from the touch pad is pressed, it generates two tones,
corresponding to the column and the row where the key is placed in table 8.1. The frequencies are
chosen to avoid the interference and to help the detection.
Create a touch pad that will send to the speakers the tones corresponding to the keys you click
with the mouse. Take care to not superpose the sounds corresponding to the figures (dial the figures
separately).

164
8- Modulation and Encoding

Table. 8.1: DTMF encoding table


Freq. 1209Hz 1336Hz 1477Hz

697Hz 1 2 3
770Hz 4 5 6
852Hz 7 8 9
941Hz * 0 #
Exercise 8.24 Create an application that will use the microphone to record the Dual Tone
Multi Frequency tones played by the speakers of another computer (you can use also just one
computer) and to decode the dialed number using a band pass filter bank.

165
Digital Signal Processing with Matlab® Applications

9 Data Compression
Data compression is a process of finding the most efficient representation of an information
source in order to minimize communication bandwidth or storage space. It often consists of two
stages—the first is the choice of a (probabilistic) model for the source and the second is the design
of an efficient coding system for the model. In this section, we will concentrate on the second
aspect of the compression process, though we will touch on some common sources and models in
the last subsection.
Thus, a data compressor (sometimes called a source coder) maps an information source into a
sequence of bits, with a corresponding decompressor, that given these bits provides a reconstruction
of the source. Data compression systems can be classified into two types: lossless, where the
reconstruction is exactly the original source, and lossy, where the reconstruction is a distorted
version of the original source. For lossless data compression, the fundamental lower bound on the
rate of the data compression system is given by the entropy rate of the source. For lossy data
compression, we have a compromise between the compression rate and the errors we have at the
recovery of the signal.
Compression means putting the data in a more compact form and it is required for both transfer
(less bandwith) and storage (less space). Beside the compression factor that can be insured, an
important parameter of the compression/decompression algorithms is the complexity of the
calculus: we can choose algorithms that will promise better compression ratios for the cost of a
larger computing cost. From a moment on, little compression gain can be achieved with further
increase in the number/complexity of the calculations.

9.1 Lossless Compression

We use lossless compression when we need that the data obtained after
reconstruction/decompression to be identical with the initial one. We need this if the data is very
important and we cannot afford the loose of quality (medical, life-saving information) or if the
modify of data change dramatically the information stored inside (ASCII text can have different
meaning after the changing of a few letters).
Exercise 9.1 Give examples of words and sentences that will change their meaning after
changing or removing a few letters.

9.1.1 Packing
Packing is a method that uses fewer bits to represent a sample, when the number of values a
signal can have allows a quantization/representation with a smaller number of bits than the number
of bits used for the initial representation. The samples bits are then packed to fit the initial
representation.
Exercise 9.2 Let us take the sequence:
x=[0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3]
Write a program buildSequence1(n) that will return the sequence x for the input n=3.
Represent graphically the sequence as in Fig. 9.1

166
9 Data Compression

Sequence x
3.5

2.5

1.5

0.5

0
0 5 10 15 20 25

Fig. 9.1 Graphical representation of the sequence x from Exercise 9.2

The sequence x has 25 elements and it is represented on 25 bytes if we use 8 bits (1 byte) to
encode each sample (consider it to be an unsigned integer or an ASCII code).
The sequence has just 4 symbols and there is the possibility to encode them on less than 8 bits.
For n different symbols we need Nbits to represent any symbol, with
N bits  ceil(log 2 n) (9.1)
where ceil(k) represents the smallest integer larger or equal with k
Example 9.1 For the four symbols in sequence x we need 2 bits per symbol and a natural
choice for the representation is:
Table 9.1 Codes for the symbols in sequence x from Example 9.1

No. Symbol Code


1 '0' 00
2 '1' 01
3 '2' 10
4 '3' 11
This way, the sequence becomes:
x=[00, 00, 01, 00, 00, 00, 00, 01, 01, 10, 00, 00, 00, 00, 00, 00, 00, 00, 01, 01, 01, 01, 10, 10,
11]
If we pack the groups of two bits in bytes (8 bits sequences), completing with 6 zeros to have a
byte in the last position, we have that:
x=[00000100, 00000001, 01100000, 00000000, 00000101, 01011010, 11000000]

167
Digital Signal Processing with Matlab® Applications

The sequence x can now be written converting the binary representation to unsigned integers
on 8 bits:
x=[4, 1, 96, 0, 5, 90, 192]
We used thus 7 bytes for the representation instead of 25 (28% of the initial data storage).
Exercise 9.3 Write a function [y,table,noElem]=packToByte(x) that will receive a
sequence of symbols x (numbers, characters, etc..) and will return the table of conversion
(dictionary) table, the sequence of unsigned integers y obtained after packing and the number of
elements noElem in the initial sequence (to know how much zero bits we used for completion at the
end of the initial sequence in order to have an integer number of bytes). The codes from table are
given in increasing order with a chosen order for the elements in x.
Exercise 9.4 Write a function x=unpackFromByte(y,table,noElem) that will receive a
sequence of packed bytes y, the table of conversion (dictionary) table and the number of elements
noElem in the initial sequence (to know how much zero bits have been used for completion to byte
at the end of the initial sequence) and will return the initial sequence of symbols x.
Example 9.2 If the previous functions are used on the sequence from Example 9.1, writing:
n=3;
x=buildSequence1(n);
[y,table,noElem]=packToByte(x)
xDecoded=unpackFromByte(y,table,noElem);
x==xDecoded

, we obtain:
y =
4 1 96 0 5 90 192
table =
[0] '00'
[1] '01'
[2] '10'
[3] '11'
noElem =
25
ans =
Columns 1 through 21
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Columns 22 through 25
1 1 1 1

Exercise 9.5 Test the functions packToByte and unpackFromByte to compress and
decompress the sequence:
'in spain the rain is mainly in the plain'
Find out the compression ratio.
Hint: For the dictionary given in Table 9.2 we have that
y=[71, 10, 129, 71, 11, 50, 9, 20, 112, 74, 6, 20, 117, 192, 71, 11, 50, 8, 81, 71]

168
9 Data Compression

Table 9.2 Codes for the symbols in the string from Exercise 9.5

No. Symbol Code


1 '' 0000
2 'a' 0001
3 'e' 0010
4 'h' 0011
5 'i' 0100
6 'l' 0101
7 'm' 0110
8 'n' 0111
9 'p' 1000
10 'r' 1001
11 's' 1010
12 't' 1011
13 'y' 1100
Exercise 9.6 What is the main deficiency when using dictionaries for compression?
Packing uses the same number of bits to represent each signal. Variable-length encoding
schemes such as the coding Huffman have the property that the codes for more frequent characters
are shorter than the ones for less probable characters.

9.1.2 Huffman Encoding


Huffman coding is an entropy coding lossless compression technique and it is often used as
the final step after decomposition and quantization of a signal (the decomposition/quantization part
rarely manages to produce a sequence of completely independent and random symbols) or after a
lossy compression. .
An entropy coder usually only exploits the symbol probabilities independent of previous
symbols, so it is optimal for uncorrelated sequences. For signal compression a decorrelation process
usually precede the entropy coding, but often the decorrelation is not perfect.
Example 9.3 Let us build the histogram for the sequence x defined in Exercise 9.2.
n=3;
x=buildSequence1(n);
hist(x,0:.5:n)
ylabel('Number of elements')
xlabel('Value')
title('Histogram of \itx')

We obtain the graphic from Fig. 9.2.

169
Digital Signal Processing with Matlab® Applications

Histogram of x
14

12

10

Number of elements
8

0
-0.5 0 0.5 1 1.5 2 2.5 3 3.5
Value

Fig. 9.2 Histogram for the sequence x defined in Exercise 9.2

The distribution of sequence x values is given in table 9.3


Table 9.3 Symbols distribution in sequence x from Exercise 9.2
Symbol No. of symbols
'0' 14
'1' 7
'2' 3
'3' 1
Total 25

It seems that it would be economical to use codes with a reduced number of bits for the
symbols that appear often and longer codes for the symbols that appear seldom instead of using the
same number of bits for each symbol (see Table 9.1 - we used 2 bits for the 4 symbols in Example
9.1)
In assigning new bitcodes for the symbols, we have to take care that each code will be uniquely
decoded. This is also called the prefix property, i.e. no character’s encoding is a prefix of any other.
Let us assign respectively the codes 0, 1 and 01 for the symbols ‘0’, ‘1’ and ‘2’. Let us suppose
that we have to decode the bit-stream 0010. What sequence does it represent: ‘0010’ or ‘020’? With
the prefix guarantee, there is no ambiguity in determining where the character boundaries are. We
start reading from the beginning, and gather bits in a sequence until we find a match in the
dictionary. That indicates the end of a symbol, so we extract it and go to the next bit that is the start
of a new symbol.
To accomplish this, Huffman coding creates a binary tree that is called the "Huffman tree".
Each left branch represents a bit of 0 in the code and each right branch represents a 1. Each symbol
has to represent a leaf in order to insure the different prefix. Such a tree is presented in Fig. 9.3.

170
9 Data Compression

0 1

'0'
0 1

'1'
0 1

'2' '3'

Fig. 9.3 Binary tree insuring prefix difference

For this tree, the codewords assigned to the symbols are presented in Table 9.4.
Table 9.4 Variable length codes for 4 symbols with prefix guarantee
No. Symbol Code
1 '0' 0
2 '1' 10
3 '2' 110
4 '3' 111
Remark: For a given set of symbols, there is more than one Huffman trees.

0 1 0 1

'3'
0 1 0 1 0 1

'2' '0' '1' '2' '3'


0 1

'0' '1'
Fig. 9.4 Four symbols different binary trees insuring prefix difference

What differentiates the trees is the position of the symbols: a frequent symbol should stay
closer to the root than a rare one.

171
Digital Signal Processing with Matlab® Applications

The Huffman algorithm is a greedy algorithm for building the tree from the bottom up.
Algorithm: Huffman Algorithm
- Count the number of times each symbol/character/value appears. This is will represent the
frequency/probability pi for each symbol i, with 1≤i≤N, where N is the number of distinct
characters in the input stream.
- Arrange the symbols in decreasing order of their frequencies that p1 p2 . . . pN.
- Create a collection of N small, one-node trees. Each of these N trees represents a distinct
symbol i and has a weight equal with its frequency pi.
- While there is more than one (sub)tree in the set, create recursively trees by merging the two
trees with the smallest associated weights pk-1 and pk, with pi  pk-1  pk for any tree i. The
resultant tree will have as left subtree the tree with weight pk-1 and as right subtree the tree with
weight pk and its associated weight will be pk-1+pk.
Remark: The constructed tree is used for:
- Compression – we assign codewords for the symbols by walking the tree and then replace
symbols in the sequence with their codewords
- Decompression – we walk the tree corresponding to the bitstream and extract the symbols.
Example 9.4 For the sequence from Exercise 2, x=[0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 2, 2, 3], having the distribution shown in Table 9.3, we start with the set of trees
presented in Fig. 9.5.a , continue with the trees from Fig. 9.5.b and end with the tree from Fig. 9.5.c

N3
'0' '0'
0 25 1
14 14

'1' '1' '0' N2


14 0 11 1
7 7

'2' N1 '1' N1
3 0 4 1 7 0 4 1

'3' '2' '3' '2' '3'


a) 1 b) 3 1 c) 3 1

Fig. 9.5 Huffman binary tree bottom-up design

Replacing the symbols with the codewords from Table 9.4, we have that
x=[0, 0, 10, 0, 0, 0, 0, 10, 10, 110, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 110, 110, 111]
and after packing we obtain that
x= [00100000, 10101100, 00000001, 01010101, 10110111],
with 5 bytes for the sequence representation instead of 25 (20% of the initial data storage).

172
9 Data Compression

Remark: Two optimizations of Huffman encoding are:


- Adaptive Huffman code dynamically changes the code words according to the change of
probabilities of the symbols.
- Extended Huffman compression can encode groups of symbols rather than single symbols.
Example 9.5 Let us create the Huffman tree and dictionary for the sequence from Exercise
9.5.
Table 9.5 Symbols table and dictionary for the sequence from Exercise 9.5

No. Symbol No. of symbols Code


1 '' 8 10
2 'i' 7 001
3 'n' 6 010
4 'a' 4 011
5 'e' 2 1101
6 'h' 2 1100
7 'l' 2 1111
8 'p' 2 1110
9 's' 2 00001
10 't' 2 00000
11 'y' 1 00011
12 'm' 1 000101
13 'r' 1 000100

N13

40
N12 N11

24 16
N10 N9 ' ' N8

14 10 8 8
N7 'i' 'n' 'a' N4 N5

7 7 6 4 4 4
N6 N2 'h' 'e' 'p' 'l'
4 3 2 2 2 2
't' 's' N1 'y'
2 2 2 1
'r' 'm'
1 1

Fig. 9.6 Huffman tree for the sequence from Exercise 9.5

173
Digital Signal Processing with Matlab® Applications

You can find implemented in MATLAB’s Communications Toolbox the functions


- dict = huffmandict(symbols,p), which computes the code dictionary for the symbol
set symbols having the probabilities/frequencies p;
- comp = huffmanenco(sig,dict), which computes the compressed signal comp
starting from the original signal and using the Huffman codes described by the code dictionary
dict
- dsig = huffmandeco(comp,dict), which decodes the numeric Huffman code vector
comp using the code dictionary dict
Example 9.6 We will use these MATLAB functions to code/decode using Huffman
algorithm the sequence defined in Exercise 9.2.
n=3;
x=buildSequence1(n);
noElem=numel(x);
symbols=unique(x);
noSymbols=length(symbols);
for index=1:noSymbols
prob(index)=length(find(x==symbols(index)));
end
prob=prob/noElem;

dict=huffmandict(symbols,prob);
y=huffmanenco(x,dict);
xDecoded=huffmandeco(y,dict);

ok=all(x==xDecoded)
compression=numel(y)/(noElem*8)

We obtain:
ok =
1
compression =
0.2000

Exercise 9.7 Use MATLAB functions to code/decode using Huffman algorithm the
sequence defined in Exercise 9.5
The Huffman coding algorithm produces optimal source codes in the sense that the code words
satisfy the prefix condition and the average block length is minim. However, it is dependent of the
source statistics and we need to know the probabilities of occurrence for all the source letters for
discrete memoryless sources and of joint probabilities of blocks for sources with memory. Since the
statistics of real sources output is regularly unknown, we can make just estimations about these
probabilities (even empirically, by observing long sequences), so we can apply Huffman for DMS.
There is also the possibility of applying adaptive Huffman coding dynamically, but it will require a
great deal of computations, making it less practical, as well as for the case of estimating joint
probabilities for sources with memory. These computational constrains limit the Huffman algorithm
application for real sources.
Exercise 9.8 Create the functions huffmanTree, huffmanDictionary, huffmanEncoding and
huffmanDecoding that will allow you to compress data using the Huffman algorithm.
Exercise 9.9 Test your Huffman algorithm code on the sequence defined in Exercise 9.5

174
9 Data Compression

9.1.3 Run Length Encoding


In a sequence there are successions of samples that have the same value because the signal
doesn’t change. Run-length encoding (RLE) is a method that that takes advantage of these
repetitions in order to decrease the size of the signal. When a symbol is repeated many times, the
symbol is stored and in the next byte/bytes the number of repetitions of that symbol.
The simplest form of RLE is at the bit-level, where you just say the value of the first bit, its
number of repetitions and then alternatively, the number of repetitions for the other bit value and
the first one.
When RLE is used at a higher level, a special flag is used to mark the fact that a coding part
represents the number of repetitions and not a symbol. Run-length encoding can be used on only
one of the characters/symbols (the value 0, that can appear in a signal), several of the symbols, or
all of them.
Exercise 9.10 Create a function y=codeRleASCII(x) and one x=decodeRleASCII(y)
that will take advantage of the fact that the ASCI characters use only 7 bits out of 8. For this reason
the first bit will be considered as a flag: when it is 0, a real character is represented and when it is 1,
it represents the number of repetitions (27 repetitions can encoded in one byte). Test them on the
string “111+222=333;4444+55=4499”
Let us suppose we want to encode a symbol sequence like the one from Exercise 9.2 using
RLE. We will express first each symbol with the minimal number of bits. For this sequence, as was
shown in Example 9.1, this number is 2 and the packed sequence is:
00, 00, 01, 00, 00, 00, 00, 01, 01, 10, 00, 00, 00, 00, 00, 00, 00, 00, 01, 01, 01, 01, 10, 10, 11
We will start now to code it using RLE. We will create 3 bit packs, where the first bit in the
pack is 0 for the code of a symbol and 1 for the number of repetitions. If a symbol appears just
once, it will not be followed by the pack representing the number of repetitions. If the number
appears more than 3 times – pack coded 111, it will be coded on more than one pack. For the given
sequence, the RLE codes are
000, 110, 001, 000, 101, 100, 001, 110, 010, 000, 110, 100, 001, 101, 100, 010, 110, 011
Remark: observe the coding of the 4th to 8th symbols 000 (symbol coded by 00) and 101 100 (
binary 100=4 repetitions).
Exercise 9.11 Can you spare some more space in the coding of repetitions?
After packing on 8 bits, the sequence looks like:
00011000, 10001011, 00001110, 01000011, 01000011, 01100010, 11001100
Remark: We obtained a compression to 7/25= 28% of the initial data storage, the same as
when we just reduce the number of bits per pixel, but this is because of the reduced number of
repetitions and of the short sequence
Exercise 9.12 Write a function [y,table,nElements,nBitsPerSymbol]=packRLE(x)
and a function x=unpackRLE(y,table,nElements,nBitsPerSymbol) that will encode/
decode a symbol sequence using RLE.

175
Digital Signal Processing with Matlab® Applications

9.1.4 Lempel-Ziv coding


The Lempel-Ziv source coding algorithm is independent of the source statistics, in contrast to
the Huffman algorithm.
In the Lempel-Ziv algorithm, the sequence at the output of the discrete source is parsed into
variable-length words. The dictionary consists initially in words made by the source symbols. A
new word is introduced every time a block of symbols from the source differs from the known
dictionary words in the last symbol. The words are listed in a dictionary, which stores the location
of the existing words. In encoding a new word, we simply specify the location of the existing word
in the dictionary and append the new symbol.
Remark: Because Welch enhanced the algorithm later, the method is also known as Lempel-
Ziv-Welch or LZW algorithm.
Algorithm: LZW coding
word = GET input symbol
WHILE there are still input symbols DO
symbol = GET input symbol
IF word+symbol is in the word table THEN
word = word+symbol
ELSE
OUTPUT the code for word
ADD word+symbol to the word table
word = symbol
END IF
END WHILE
OUTPUT the code for word

Algorithm: LZW decoding


READ oldCode
OUTPUT oldCode
WHILE there are still input symbols DO
READ newCode
word = GET translation of newCode
OUTPUT word
symbol = first symbol in word
ADD oldCode + symbol to the dictionary
oldCode = newCode
END WHILE

176
9 Data Compression

Example 9.7 As an example, considering the character sequence defined in Exercise 9.5, we
obtain:
Table 9.6 Symbols table and dictionary for LZW on the sequence from Exercise 9.5
input code word output
'in' 256 'in' 'i'
'' 257 'n ' 'n'
's' 258 ' s' ''
'p' 259 'sp' 's'
'a' 260 'pa' 'p'
'i' 261 'ai' 'a'
'n ' 262 'in ' 256
't' 263 ' t' ''
'h' 264 'th' 't'
'e' 265 'he' 'h'
'' 266 'e ' 'e'
'r' 267 ' r' ''
'a' 268 'ra' 'r'
'in' 269 'ain' 261
' i' 270 'n i' 257
's' 271 'is' 'i'
'' 272 's ' 's'
'm' 273 ' m' ''
'a' 274 'ma' 'm'
'inl' 275 'ainl' 269
'y' 276 'ly' 'l'
'' 277 'y ' 'y'
'i' 278 ' i' ''
'n t' 279 'in t' 262
'he' 280 'the' 264
' p' 281 'e p' 266
'l' 282 'pl' 'p'
'a' 283 'la' 'l'
'in' 269

The source decoder for the code constructs an identical copy of the dictionary at the receiving
end of the communication system and decodes the received sequence in step with the transmitted
data sequence.
Remark: We encoded 40 characters on 29 words of 9 bits that represents 261 bits, but needs
264 bits to be stored on 33 bytes. We obtained thus a compression to 33/40 = 82.5%. We benefited
for the repetitions of characters, but we were not efficient because the sequence we have considered
is very short. As the sequence is increased in length, the encoding procedure becomes more
efficient and results in a more compressed sequence at the output of the source.
Exercise 9.13 Create the functions LZWEncoding, LZWDecoding and isInLZWTable that
will allow you to compress and decompress data using the LZW algorithm.
Exercise 9.14 Test your LZW code on the sequence defined in Exercise 9.5

177
Digital Signal Processing with Matlab® Applications

9.2 Lossy Compression

Lossy compression has the goal of obtaining maximum compression with minimal quality loss.
In this case perfect recovery is not possible but we have a large data compression and consequently
a higher transmission rate. This is used in applications like digital communications, images, music
and video compression, etc…
Example 9.8 If we store “full” 10 digits positive integers (the numbers cannot be smaller
than 109), we may not be interested in the last digits and we can afford loosing them by storing just
the first 8 digits. After decompression our numbers will be the initial ones with the last two digits
replaced by “00”. The maximal error we can have is 99 and the minimal stored number is 108+99,
so the maximal relative error is 99/(108+99)≈10-6.
Exercise 9.15 What is the maximal error if you store the last 8 digits?
The easiest methods for lossy compression are the ones regarding coarser sampling and
quantization. This involves discarding some of the samples or reducing the number of bits per
sample. Both these procedures have the desired effect: the data file becomes smaller at the expense
of signal quality.

9.2.1 Downsampling
A simple method to reduce the size of a signal is to discard some of its samples. This technique
results in reducing the number of samples, renouncing at each second sample being equivalent with
halving the sample rate and leading to half of storage space/bandwidth.
Exercise 9.16 Write a function y=downsampling (x, n) that will return a signal y having
each nth sample of a given signal x, starting with the first one.
Exercise 9.17 What is the compress ratio insured by the previous function?
Exercise 9.18 Test it on the sound signal from ringout.wav for n = 8. What will be the new
sampling rate? Compare auditory the sounds. Graphically, the results can be seen in Fig. 9.7.
Exercise 9.19 What do you have to do in order to play the downsampled sound at the initial
frequency (restore it)?

Time domain Time domain


0.5 0.5
Signal amplitude

Signal amplitude

0 0

-0.5 -0.5
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Time(s) Time(s)
Magnitude spectrum Magnitude spectrum
600 60

400 40
Magnitude

Magnitude

200 20

0 0
0 1000 2000 3000 4000 5000 6000 0 100 200 300 400 500 600 700
Frequency(Hz) Frequency(Hz)

Fig.9.7 Downsampling for the ringout.wav signal

178
9 Data Compression

9.2.2 Downquantization
In the downquantization method, we use less bits to encode a sample. This technique results in
reducing storage space for each sample, renouncing at half of the bits leading to half of storage
space/bandwidth.
Exercise 9.20 Write a function y=downquantisation (x, k) that will return a signal y
having the sample represented on k bits..
Exercise 9.21 What is the compress ratio insured by the previous function, if a sample is
stored initially on n bits?
Exercise 9.22 Test it on the sound signal from “ringout.wav” , for k = 2.? Compare auditory
the sounds. Graphically, the results can be seen in Fig. 9.8.

Time domain Time domain


0.5 0.5
Signal amplitude

Signal amplitude
0 0

-0.5 -0.5
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Time(s) Time(s)
Magnitude spectrum Magnitude spectrum
600 600

400 400
Magnitude

Magnitude

200 200

0 0
0 1000 2000 3000 4000 5000 6000 0 1000 2000 3000 4000 5000 6000
Frequency(Hz) Frequency(Hz)

Fig.9.8 Downquantisation for the ringout.wav signal

9.2.3 Transform Compression


For both sound and image, the low frequency components of a signal are more important than
the high frequency components. Removing some of the high frequency components will result in a
loss of perceivable information that can be negligible.
The transform used for the spectrum is the Direct Cosine Transform, that has the advantage of
working with real numbers. The high frequency components of the DCT are discarded and the
stored/transmitted information is made by the remained coefficients.
Example 9.9 We will compress the audio signal from the file “ringout.wav” with a
compression ratio of 10%. The results can be seen in Fig. 9.9.
fileName='d:\Windows\Media\ringout';
[y,fs,noBits]=wavread(fileName);
reprSignal(y,fs)
wavplay(y,fs)
k=.1; n=length(y);
Y=dct(y);
Y(round(k*n):n)=zeros(size(round(k*n):n));
yRecovered=idct(Y);
reprSignal(yRecovered,fs)
wavplay(yRecovered,fs)

179
Digital Signal Processing with Matlab® Applications

Time domain Time domain


0.5 0.5

Signal amplitude
Signal amplitude

0 0

-0.5 -0.5
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Time(s) Time(s)
Magnitude spectrum Magnitude spectrum
600 600

400

Magnitude
400
Magnitude

200 200

0 0
0 1000 2000 3000 4000 5000 6000 0 1000 2000 3000 4000 5000 6000
Frequency(Hz) Frequency(Hz)

Fig.9.9 Transform compression for the ringout.wav signal

9.3 Concluding Exercises

Exercise 9.23 Write a program buildSequence2(n) that will return the sequence x from Fig.
9.10 for the input n=4.
Sequence x
3.5

2.5

1.5

0.5

0
0 5 10 15 20 25

Fig. 9.10 Graphical representation of the sequence x for Exercise 9.23

Exercise 9.24 Look at a table containing the Morse code. Observe which are the characters
that have short codes and which are the ones that have long ones. Explain why.
Exercise 9.25 Take the audio file ding.wav. Find out if you can pack it on fewer bits. Find out
the compression ratio. Decompress it and verify that the result is the same as the initial signal.
Exercise 9.26 Take the audio file ding.wav Compress it using Huffman algorithm. Find out
the compression ratio. Decompress it and verify that the result is the same as the initial signal.

180
9 Data Compression

Exercise 9.27 Take the audio file ding.wav Compress it using the RLE algorithm. Find out
the compression ratio. Decompress it and verify that the result is the same as the initial signal.
Exercise 9.28 Take the audio file ding.wav Compress it using the LZW algorithm. Find out
the compression ratio. Decompress it and verify that the result is the same as the initial signal.

181
Digital Signal Processing with Matlab® Applications

10 Real-Time Digital Signal Processing

10.1 Real-Time Constrains

As we stated earlier, there are three types of signals: continuous-time (analog), discrete time
and digital. Most encountered signals are analog, being continuous in time and having a continuous
range of amplitude values; the discrete-time signals are the restriction of these signals at specific
instances of time and the digital signals are discrete both in time and in amplitude.
The basic blocks of a DSP system are presented in Fig. 10.1.

Analog Analog
Input Output
Signal Antialising A/D Digital Signal D/A Reconstruct. Signal
Filter Converter Processor Converter Filter

Fig. 10.1 Basic blocks for Digital Signal Processing

There are basically two types of DSP applications: real-time and non-real-time. For the non-
real-time ones, the signals are already obtained and stored (usually digitized) and there is no
stringent need to perform operations on them at a specific instance and to finish them in a given
period. For real-time applications, the signals have to be processed in a specific time frame.
The signal is usually in analog form and it is acquired and processed further on-line. If the
timing requirements are not met and the output is not computed in the allowed time span, the
process that depend on the real-time DSP will fail (data is missed, decisions cannot be taken
because of missing parameters)…
This timing constraint for real-time DSP systems can be better understood if we think of a
sample-by-sample processing of a signal whose highest frequency is fM, where the signal has to be
sampled with a sampling frequency fs ≥ 2ꞏfM. We need that the processing of one input sample to be
finished before the next input sample arrives, so
Tp +TI/O ≤ T (10.1)
where Tp is the processing time, TI/O is the time spend for Input/Output operations (rather
constant) and T =1/fs is the sampling period. From here:
1 1
Tp   TI/O   TI/O (10.2)
fs 2 fm
so the processing time must be less than a figure imposed by the system.
Exercise 10.1 What should be the minimum frequency of a DSP working sample-by-sample
that processes an input in 10 CPU clocks, has the time lag for I/O of 10 µs and deals wih an audio
signal having the maximum frequency of 20 KHz?
In order to increase the performances of a DSP system, we can optimize the filters, reducing
the number of taps, or choose a DSP with better performances (especially increased speed of DSP
Multiplication-Accumulates – MACs) or a better architecture using circular buffering, zero
overhead looping, more pipelines, etc…

182
10 Real-Time Digital Signal Processing

Because a real-time DSP application has time constrains, it usually has all the blocks form Fig.
9.1 working together, even if sometimes the analog converting blocks are missing, the desired form
of the signal being the digital one.

10.2 The DAQ Toolbox

Starting with the version 5.3, MATLAB offers integrated methods to acquire analog data in a
PC and to output analog signals. The soundboard of the computer can be set to be the device used
for the acquisition and output. This provides an easy way to familiarize with the concept of data
acquisition and online digital signal processing (real-time DSP).

10.2.1 Data Input


The function ai = analoginput(adaptor) will construct an analog input object
associated with the device/ adaptor.
The supported adaptors are:
- advantech
- mcc
- nidaq
- winsound
The winsound device represents the soundboard of the computer and it can be used as an
acquisition/output device for analog signals. In the following, we will not discuss every function
in detail, but describe the most important functions and their features. For the rest, please use
MATLAB help.
Example 10.1 Let us create, examine and close an input device ai.
ai=analoginput('winsound');
addchannel(ai,1);
disp(ai);
delete(ai)

The device ai is defined as:


>> Display Summary of Analog Input (AI) Object Using 'Vinyl AC'97
Audio (WAVE)'.

Acquisition Parameters: 8000 samples per second on each channel.


8000 samples per trigger on each channel.
1 sec. of data to be logged upon START.
Log data to 'Memory' on trigger.

Trigger Parameters: 1 'Immediate' trigger(s) on START.

Engine status: Waiting for START.


0 samples acquired since starting.
0 samples available for GETDATA.

AI object contains channel(s):

Index: ChannelName: HwChannel: InputRange: SensorRange:


UnitsRange: Units:
1 'Mono' 1 [-1 1] [-1 1]

183
Digital Signal Processing with Matlab® Applications

[-1 1] 'Volts'

What we have seen is just an outline of the object ai. In order to see its fields/properties and
their values, type in the command line >> get(ai) before deleting the object and obtain:
BufferingConfig = [512 30]
BufferingMode = Auto
Channel = [1x1 aichannel]
ChannelSkew = 0
ChannelSkewMode = None
ClockSource = Internal
DataMissedFcn = @daqcallback
EventLog = [1x0 struct]
InitialTriggerTime = [0 0 0 0 0 0]
InputOverRangeFcn = []
InputType = AC-Coupled
LogFileName = logfile.daq
Logging = Off
LoggingMode = Memory
LogToDiskMode = Overwrite
ManualTriggerHwOn = Start
Name = winsound0-AI
Running = Off
RuntimeErrorFcn = @daqcallback
SampleRate = 8000
SamplesAcquired = 0
SamplesAcquiredFcn = []
SamplesAcquiredFcnCount = 1024
SamplesAvailable = 0
SamplesPerTrigger = 8000
StartFcn = []
StopFcn = []
Tag =
Timeout = 1
TimerFcn = []
TimerPeriod = 0.1
TriggerChannel = [1x0 aichannel]
TriggerCondition = None
TriggerConditionValue = 0
TriggerDelay = 0
TriggerDelayUnits = Seconds
TriggerFcn = []
TriggerRepeat = 0
TriggersExecuted = 0
TriggerType = Immediate
Type = Analog Input
UserData = []

WINSOUND specific properties:


BitsPerSample = 16
StandardSampleRates = On

The values of these properties can be obtained with the get function and can be set with the
set function.

184
10 Real-Time Digital Signal Processing

Exercise 10.2 Explain the effect of the following code


ai=analoginput('winsound');
get(ai)
addchannel(ai, 1);
set(ai, 'SampleRate', 44100);
set(ai, 'TriggerType', 'immediate');
set(ai, 'SamplesPerTrigger', 48000);
set(ai, 'TriggerRepeat', 4);
get(ai)
delete(ai)

Exercise 10.3 Try to set the sample rate for acquisition at 24KHz. What happens?
The function start (obj) will initiate the execution of the device object obj, that will go on start
and execute according to its properties.
Once an input device is started, we can obtain data from it using the functions getsample,
getdata or peekdata.
The function data=getsample(obj) returns a vector containing one immediate sample of
data from each channel of obj.
The function data = getdata(obj, samples) returns from analog object obj the
specified number of samples samples. The variable data is a samples-by-noChannels matrix where
noChannels is the number of channels in obj. The function getdata is a blocking function that
returns execution control to the MATLAB workspace once the requested number of samples
become available. The property SamplesAvailable of obj will automatically be reduced by the
number of samples returned by getdata.
The function data = peekdata(obj,samples) returns from obj the latest number of
samples specified by samples. If samples is greater than the number of acquired samples, all
available samples will be returned with a not-sufficiency warning message. The function peekdata
is an non-blocking function that immediately returns data samples and execution control to the
MATLAB workspace, but it may happen that not all requested data to returned. Peekdata only
looks at the data, it does not remove the data from the data acquisition engine
Example 10.2 Connect the line output (or headphones output) of a stereo audio device (radio,
mp3 player, etc…) to the line input of the PC using a stereo cable with jack connectors. Let the
music play and run the following code:
recTime=3;
ai = analoginput('winsound'); % create the analog input object ai
addchannel(ai,1:2);% add two hardware channels (stereo) to ai
set(ai,'SampleRate',44100) % configure the sampling rate to 44.1 kHz
set(ai,'SamplesPerTrigger',44100*recTime) % record data for recTime
start(ai) % start the acquisition
wait(ai,recTime+1) %pauses matlab until acquisitions completes
[data time] = getdata(ai); % acquire data
subplot(2,1,1);plot(time,data(:,1)), axis tight % plot the data on
channel 1
subplot(2,1,2);plot(time,data(:,2)), axis tight% plot the data on
chennel 2
delete(ai) % delete the object
clear ai % free memory space - eventually clear all

Exercise 10.4 Save the data in a wave file

185
Digital Signal Processing with Matlab® Applications

Example 10.3 An oscilloscope is integrated in the daq toolbox. It can be run with >>
daqscope and the waveform it shows without any input is presented in Fig. 10.2.

Fig. 10.2 Graphical interface for daqscope

Exercise 10.5 What is the frequency of the signal displayed by the oscilloscope? Can you
deduce what produced it? Hint: the sample rate is 44100 Hz and we have 4410 samples on the
scope, covering 5 main periods of the signal.
Exercise 10.6 Use the daqscope to see the sound waveform from a music player.
Exercise 10.7 Run the application lineinput. Edit it and try to understand its functioning

10.2.2 Data Output


The function ai = analogoutput(adaptor) will construct an analog output object
associated with the device/ adaptor. Again, “winsound” represents the sound board of the PC.
The functioning of a device object for analog output is similar with that of an analog input,
with the difference that the functions used to output data are putsample and putdata.
Exercise 10.8 Run the application daqsong. Edit the source and figure out the way it works.

Fig. 10.3 Waveform sent to output in dagsong

186
10 Real-Time Digital Signal Processing

Example 10.4 We will generate basic waveforms using the application daqfcngen. To
visualize the waveform, we will use the same soundcard, working in duplex mode, connecting a
stereo cable from line output to line input. Type in the command line >> daqfcngen, daqscope
and select a sine wave from the waveform generator. The trace on the scope should look like in Fig.
10.4

Fig. 10.4 Generation of a sine waveform

10.3 Fast Algorithms

10.3.1 Fast Fourier Transform


Probably the most important algorithm for DSP is the Fast Fourier Transform that was
examined in Chapter 4.2.
Exercise 10.9 What is the number of multiplications for the FFT of a sequence with 2n
complex elements, compared with DFT?
For real time processing, we don’t have commonly access to the whole sequence we want to
represent in the frequency domain, and in fact we don’t want this. We are typically interested in the
frequency information evaluated for the evolution of the signal during relatively short periods of
time and we want to see how does it changes in time. It is the case of a spectrum analyzer for music.
When we hear a piece of music, we are not interested in the distribution of frequencies over the
whole play, but what happens in the current moment, as a visual confirmation of the audible
information. Seeing the spectrum in a specific moment, we are able to change the equalizer settings,
in order to make it more appropriate.
Dennis Gabor adapted the Fourier transform to analyze only a small section of the signal at a
time -- a technique called windowing the signal. Gabor's adaptation, called the Short-Time Fourier
Transform (STFT), maps a signal into a two-dimensional function of time and frequency.

187
Digital Signal Processing with Matlab® Applications

In order to compute the STFT, the signal is divided into frames/windows with a fixed size,
sometimes with interleaving (common) samples and the DFT is computed for each frame. This way,
the STFT represents a compromise between the time- and frequency-based views of a signal. It
provides some information about both when and at what frequencies a signal event occurs.
However, you can only obtain this information with limited precision, and that precision is
determined by the size of the window.
Exercise 10.10 Create an application that will be able to load a file from the disk and to play it.
It will display synchronized the magnitude spectrum of the STFT, the way Winamp does.

10.3.2 Fast Matrix Multiplication


Let's recall the formula of matrix multiplication:
n
[C ] m p  [ A] mn  [ B] n p , with C( j , k )   A( j, l )  B(l , k ) (10.3)
l 1

Exercise 10.11 Write a function C=matMult_c(A,B) that will multiply two matrices A and B
using (10.3). Test (accuracy and rapidity) it versus the embedded matrix multiplication.
Exercise 10.12 What is the number of multiplications needed to multiply two 2n×2n matrices
using the classic method?
For two 22 matrices A and B, we have:
 a11 a12  b11 b12   c11 c12 
a    (10.4)
 21 a22  b21 b22  c21 c22 
with
c11= a11 b11+ a12 b21
c12= a11 b12+ a12 b22
(10.5)
c21= a21 b11+ a22 b21
c22= a21 b12+ a22 b22
In order to calculate the four elements of C, we have to do 4∙2=8 multiplications and 4∙1=4
additions. It is interesting that there is a way to compute this matriceal product with a smaller
number of multiplications. The result is very simple and we owe it to V. Strassen who discovered it
so late, in 1969.
To compute C, we define the following 7 quantities:
p1 = (a11 + a22)( b11 + b22)
p2 = (a21 + a22) b11
p3 = a11 ( b12 - b22)
p4 = a22 ( b21 - b11) (10.6)
p5 = (a11 + a12) b22
p6 = (a21 - a11)( b11 + b12)
p7 = (a12 - a22)( b21 + b22)
It can be easy verified that we can compute the four elements of C - see (10.5) - using them:
c11 = p1 + p4 - p5 + p7
(10.7)
c12 = p3 + p5

188
10 Real-Time Digital Signal Processing

c21 = p2 + p4
c22 = p1 + p3 - p2 + p6
For this procedure, represented by (10.6) and (10.7), we have 7 multiplications and 18
additions, loosing a multiplication but gaining 10 additions, so we don’t gain so much for the
operations count.
Nevertheless, the main feature of (10.6) and (10.7) is the fact that they do not use
commutativity, so they can be used also for submatrices/blocks multiplication. Since matrix
multiplication can be subdivided into block submatrix operations (replacing elements aij, bij, cij and
pk with block submatrices Aij, Bij, Cij and matrices Pk. this leads directly to a divide-and-conquer
type fast algorithm.
We can use this procedure recursively for submatrix multiplications, halving the number of
elements in the matrix at each call (we will board the matrices with zeros to make them square
matrices with the number of lines/columns a power of 2, if necessary)
Example 10.5 The code for two square matrices A and B (having n lines/columns, with n a
power of 2) multiplication is presented in the function C=matmult_r(A,B,n).
function C=matmult_r(A,B,n)
if n>1,
m = n/2; i=1:m; j=m+1:n;
P1 = matmult_r( A(i,i)+A(j,j), B(i,i)+B(j,j),m);
P2 = matmult_r( A(j,i)+A(j,j), B(i,i),m);
P3 = matmult_r( A(i,i), B(i,j)-B(j,j),m);
P4 = matmult_r( A(j,j), B(j,i)-B(i,i),m);
P5 = matmult_r( A(i,i)+A(i,j), B(j,j),m);
P6 = matmult_r( A(j,i)-A(i,i), B(i,i)+B(i,j),m);
P7 = matmult_r( A(i,j)-A(j,j), B(j,i)+B(j,j),m);
C = [ P1+P4-P5+P7 P3+P5; P2+P4 P1+P3-P2+P6 ];
else
C=A*B;
end;

Exercise 10.13 Test matmult_r versus matmult_c (for accuracy and rapidity). What are
the results? Can you give an explanation?
Exercise 10.14 What is the number of multiplications needed to multiply two 2n×2n matrices
using the Strassen method? Compare with the result from Exerise 10.6
Exercise 10.15 What is the computational reduction (in percents from the number of
multiplications needed) if you multiply 1024×1024 matrices?
Exercise 10.16 Suppose we could multiply together two 3×3 matrices with only 22
multiplications. What would be the number of multiplications for two 3n×3n matrix product?
Compare with standard matrix product.

10.4 Concluding Exercises

Write an application similar to Winamp, that will be able to load wav and mp3 files, play them,
showing their spectrum (STF transform), and real-time equalize the output signal. The inferface
should be similar with the one from Fig. 10.5.

189
Digital Signal Processing with Matlab® Applications

Fig. 10.5 Audio player interface

190

You might also like