Digital Signal and Image Processing using MATLAB, Volume 1 - Gérard Blanchet - E-Book

Digital Signal and Image Processing using MATLAB, Volume 1 E-Book

Gérard Blanchet

0,0
163,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

This fully revised and updated second edition presents the most important theoretical aspects of Image and Signal Processing (ISP) for both deterministic and random signals. The theory is supported by exercises and computer simulations relating to real applications. More than 200 programs and functions are provided in the MATLAB language, with useful comments and guidance, to enable numerical experiments to be carried out, thus allowing readers to develop a deeper understanding of both the theoretical and practical aspects of this subject. This fully revised new edition updates : * the introduction to MATLAB programs and functions as well as the Graphically displaying results for 2D displays. * Calibration fundamentals for Discrete Time Signals and Sampling in Deterministic signals. * image processing by modifying the contrast. * also added are examples and exercises.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 541

Veröffentlichungsjahr: 2014

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Contents

Foreword

Notations and Abbreviations

Introduction to Matlab

1 Variables and constants

2 Operations and functions

3 Programming structures

4 Graphically displaying results

5 Converting numbers to character strings

6 Input/output

7 Program writing

Part I Deterministic Signals

Chapter 1 Signal Fundamentals

1.1 The concept of signal

1.2 The concept of system

1.3 Summary

Chapter 2 Discrete Time Signals and Sampling

2.1 Fundamentals of sampling

2.2 Plotting a signal as a function of time

2.3 Spectral representation

Chapter 3 Spectral Observation

3.1 Spectral accuracy and resolution

3.2 Short term Fourier transform

3.3 Summing up

3.4 Application examples and exercises

Chapter 4 Linear Filters

4.1 Definitions and properties

4.2 The z-transform

4.3 Transforms and linear filtering

4.4 Difference equations and rational Tf filters

4.5 Connection between gain and poles/zeros

4.6 Minimum phase filters

4.7 Filter design methods

4.8 Oversampling and undersampling

Chapter 5 An Introduction to Image Processing

5.1 Introduction

5.2 Color spaces

5.3 Geometric transformations of an image

5.4 Frequential content of an image

5.5 Linear filtering

5.6 Other operations on images

5.7 Jpeg lossy compression

Part II Random Signals

Chapter 6 Random Variables

6.1 Random phenomena in signal processing

6.2 Basic concepts of random variables

6.3 Common probability distributions

6.4 Generating an r.v. with any type of p.d

6.5 Uniform quantization

Chapter 7 Random Processes

7.1 Introduction

7.2 Wide-sense stationary processes

7.3 Estimating the covariance

7.4 Filtering formulae for Wss random processes

7.5 Ma, Ar and Arma time series

Chapter 8 Spectra Estimation

8.1 Non-parametric estimation of the psd

8.2 Ar estimation

8.3 Estimating the amplitudes and the frequencies

8.4 Periodograms and the resolution limit

Chapter 9 The Least Squares Method

9.1 The projection theorem

9.2 The least squares method

9.3 Linear predictions of the Wss processes

9.4 Wiener filtering

9.5 The Lms (least mean square) algorithm

Part III Appendices

Chapter 10 Hints and Solutions

H1 Signal fundamentals

H2 Discrete time signals and sampling

H3 Spectral observation

H4 Linear filters

H5 An Introduction to image processing

H6 Random variables

H7 Random processes

H8 Spectra estimation

H9 The least squares method

Chapter 11 Appendix

A1 Fourier transform

A2 Discrete time Fourier transform

A3 Discrete Fourier transform

A4 z-Transform

Bibliography

Index

First edition published 2006 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc. © ISTE Ltd 2006

This edition published 2014 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.

Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the undermentioned address:

ISTE Ltd27-37 St George’s RoadLondon SW19 4EUUK

www.iste.co.uk

John Wiley & Sons, Inc.111 River StreetHoboken, NJ 07030USA

www.wiley.com

© ISTE Ltd 2014The rights of Gérard Blanchet and Maurice Charbit to be identified as the authors of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988.

Library of Congress Control Number: 2014942418

British Library Cataloguing-in-Publication DataA CIP record for this book is available from the British LibraryISBN 978-1-84821-640-2

MATLAB®is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® software does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or use of the MATLAB® software.

Foreword

Simulation is an essential tool in any field related to engineering techniques, whether it is used for teaching purposes or in research and development.

When teaching technical subjects, lab works play an important role, as important as exercise sessions in helping students assimilate theory. The recent introduction of simulation tools has created a new way to work, halfway between exercise sessions and lab works. This is particularly the case for digital signal processing, for which the use of the MATLAB® language, or its clones, has become inevitable. Easy to learn and to use, it makes it possible to quickly illustrate a concept after introducing it in a course.

As for research and development, obtaining and displaying results often means using simulation programs based on a precise “experimental protocol”, as it would be done for actual experiments in chemistry or physics.

These characteristics have led us, in a first step, to try to build a set of exercises with solutions relying for the most part on simulation; we then attempted to design an introductory course on Digital Signal and Image Processing (DSIP) mostly based on such exercises. Although this solution cannot replace the traditional combination of lectures and lab works, we do wonder if it isn’t just as effective when associated with exercise sessions and a few lectures. There is of course no end in sight to the debate on educational methods, and the amount of experiments being conducted in universities and engineering schools shows the tremendous diversity of ideas in the matter.

Basic concepts of DSIP

The recent technical evolutions, along with their successions of technological feats and price drops have allowed systems based on micro-controllers and microprocessors to dominate the field of signal and image processing, at the expense of analog processing. Reduced to its simplest form, signal processing amounts to manipulating data gathered by sampling analog signals. Digital Signal and Image Processing, or DSIP, can therefore be defined as the art of working with sequences of numbers.

The sampling theorem

The sampling theorem is usually the first element found in a DSIP course, because it justifies the operation by which a continuous time signal is replaced by a discrete sequence of values. It states that a signal can be perfectly reconstructed from the sequence of its samples if the sampling frequency is greater than a fundamental limit called the Nyquist frequency. If this is not the case, it results in an undesired effect called spectrum aliasing.

Numerical Sequences and DTFT

The Discrete Time Fourier Transform, or DTFT, introduced together with the sampling theorem, characterizes the spectral content of digital sequences. The analogy between the DTFT and the continuous time Fourier transform is considered, with a detailed description of its properties: linearity, translation, modulation, convolution, the Parseval relation, the Gibbs phenomenon, ripples caused by windowing, etc.

In practice, signals are only observed for a finite period of time. This “time truncation” creates ripples in the spectrum and makes it more difficult to the separate two close frequencies in the presence of noise. This leads to the concept of frequency resolution. The DTFT is a simple way of separating two frequencies, but only if the observation time is greater than the inverse of the difference between the two frequencies. The frequency resolution will allow us to introduce the reader to weighting windows. However, a more complete explanation of the concept of resolution can only be made if noise disturbing the signal is taken into account, which is why it will be studied further when random processes are considered.

The Discrete Fourier Transform, or DFT is the tool used for a numerical computation of the DTFT. Because this calculation involves a finite number of frequency values, the problem of precision has to be considered. There are a few differences in properties between the DFT and the DTFT, particularly regarding the indexing of temporal sequences that are processed modulo N. Some examples of this are the calculation of the DTFT and the DFT of a sinusoid, or the relation between discrete convolution and the DFT. At this point, the fast algorithm calculation of the DFT, also called FFT (Fast Fourier Transform), will be described in detail.

Filtering and Elements of Filter Design

Linear filtering was originally used to extract relevant signals from noise. The basic tools will be introduced: the discrete convolution, the impulse response, the frequency response, the z-transform. We will then focus on the fundamental relation between linear filtering with rational transfer functions and linear constant-coefficient recursive equations.

Filter design is described based on a few detailed examples, particularly the window method and the bilinear transform. The concepts of over-sampling and under-sampling are then introduced, some applications of which are frequency change and the reduction of quantization noise.

An introduction to images

Image processing is described in its own separate chapter. Many of the concepts used in signal processing are also used in image processing. However images have particular characteristics that require specific processing. The computation time is usually much longer for images than it is for signals. It is nevertheless possible to conduct image processing with MATLAB®. This theme will be discussed using examples on 2D filtering, contour detection, and other types of processing in cases where the 2D nature of the images does not make them too different from a ID signal. This chapter will also be the opportunity to discuss image compression and entropic coding.

Random Processes

Up until now, the signals used as observation models have been described by functions that depend on a finite number of well known parameters and on simple known basic functions: the sine function, the unit step function, the impulse function, etc. This type of signal is said to be deterministic.

There are other situations where deterministic functions cannot provide us with a relevant apprehension of the variability of the phenomena. Signals must then be described by characteristics of a probabilistic nature. This requires the use of random processes, which are time-indexed sequences of random variables. Wide sense stationary processes, or WSSP, are an important category of random processes. The study of these processes is mainly based on the essential concept of power spectral density, or psd. The psd is the analog for WSSP of the square module of the Fourier transform for deterministic signals. The formulas for the linear filtering of WSSP are then laid down. Thus, we infer that WSSPs can also be described as the linear filtering of a white noise. This result leads to a large class of stationary processes: the AR process, the MA process, and the ARMA process.

Spectral Estimation

One of the main problems DSIP is concerned with is evaluating the psd of WSSPs. In the case of continuous spectra, it can be solved by using non-parametric approaches (smooth periodograms, average periodograms, etc.) or parametric methods based on linear models (AR, MA, ARMA).

The least squares

This chapter discusses the use of the least squares method for solving problems. This method is used in a number of problems, in fields such as spectral analysis, modelling, linear prediction, communications, etc. We will discuss such methods as the gradient and LMS algorithms and the Wiener equalization.

As a Conclusion

One of the issues raised by many of those who use signal processing has to do with the artificial aspect introduced by simulation. For example, we use sampling frequencies equal to 1, and therefore frequencies with no dimension. There is a risk that the student may lose touch with the physical aspect of the phenomena and, because of that, fail to acquire the intuition of these phenomena. That is why we have tried, at least in the first chapters, to give exercises that used values with physical units: seconds, Hz, etc.

This work discusses important properties and theorems, but its objective is not to be a book on mathematics. Its only claim, and it is certainly an excessive one, is to show how interesting signal and image processing can be, by providing examples and exercises we chose because they were not too trivial.

All of the subjects discussed far from cover the extent of knowledge required in this field. However they seem to us to be a solid foundation for an engineer who would happen to deal with DSIP problems.

Notations and Abbreviations

 

empty set

k,n

k

n

rect

T

(

t

)

sinc(x)

1

(

x

A

)

(

a

,

b

]

{

x

:

a

<

x

b

}

δ

(

t

)

 

Re(

z

)

 

real part of

z

Im(

z

)

 

imaginary part of

z

i

or

j

x

(

t

)

X

(

f

)

 

Fourier transform

(

x

*

y

)(

t

)

 

continuous time convolution

 

(

x

*

y

)(

t

)

 

discrete time convolution

 

x

or

x

 

vector

x

I

N

 

(

N

×

N

)-dimension identity matrix

A

*

 

complex conjugate of

A

A

T

 

transpose of

A

A

H

 

transpose-conjugate of

A

A

–1

 

inverse matrix of

A

ADC

 

Analog to Digital Converter

ADPCM

 

Adaptive Differential PCM

AMI

 

Alternate Mark Inversion

AR

 

Autoregressive

ARMA

 

AR and MA

BER

 

Bit Error Rate

bps

 

bits per second

cdf

 

cumulative distribution function

CF

 

Clipping Factor

CZT

 

Causal

z

-Transform

DAC

 

Digital to Analog Converter

DCT

 

Discrete Cosine Transform

d.e./de

 

difference equation

DFT

 

Discrete Fourier Transform

DTFT

 

Discrete Time Fourier Transform

DTMF

 

Dual Tone Multi-Frequency

dsp

 

digital signal processing/processor

e.s.d./esd

 

energy spectral density

FIR

 

Finite Impulse Response

FFT

 

Fast Fourier Transform

FT

 

continuous time Fourier Transform

HDB

 

High Density Bipolar

IDFT

 

Inverse Discrete Fourier Transform

i.i.d./iid

 

independent and identically distributed

IIR

 

Infinite Impulse Response

ISI

 

InterSymbol Interference

LDA

 

Linear Discriminant Analysis

lms

 

least mean squares

MA

 

Moving Average

MAC

 

Multiplication ACcumulation

OTF

 

Optical Transfer Function

PAM

 

Pulse Amplitude Modulation

PCA

 

Principal Component Analysis

p.d./pd

 

probability distribution

ppi

 

points per inch

p.s.d./psd

 

Power Spectral Density

PSF

 

Point Spread Function

PSK

 

Phase Shift Keying

QAM

 

Quadrature Amplitude Modulation

rls

 

recursive least squares

rms

 

root mean square

r.p./rp

 

random process

SNR

 

Signal to Noise Ratio

r.v./rv

 

random variable

STFT

 

Short Term Fourier Transform

TF

 

Transfer Function

WSS

 

Wide (Weak) Sense Stationary (second order) process

ZOH

 

Zero-Order Hold

ZT

 

z

-Transform

Introduction to MATLAB

In this book the name MATLAB® (short for Matrix Laboratory) will refer to:

– the program launched by using the command matlab in Dos or Unix environments, or by clicking on its icon in a graphic environment such as x11, Windows, MacOS, etc.
– or the language defined by a vocabulary and syntax rules.

MATLAB® is an interpreter, that is to say a program that remains in the computer’s memory once it is launched. MATLAB® displays several subwindows (Figure 1) one of which is a command window used to type commands (instructions, functions names, program names), which are then directly “interpreted”.

Figure 1 – The MATLAB®command window on MS-Windows

MATLAB® can be used in two ways: by direct execution of the commands typed in the command window or by the execution of programs. When working on large-sized projects, we use several programs and/or functions, which should be organized by location on the disc. Examples of these three situations are as follows:

1. Direct execution by typing demands directly in the command window: as an example type x=log(2) at the MATLAB® prompt in the command window. The answer is shown in Figure 2.

Figure 2 – The resultlog(2)is given by the interpreter

Executed commands can be recalled by pressing the ↑ arrow to scroll through all previously typed commands, or a sequence of letters followed by ↑. All commands beginning with these letters then scroll.
2. Execution of a program: a program regroups commands in a text file also called script. The user gives it a name, with the extension m. The “built-in” editor can be used, which is highly recommended, or otherwise an external editor can be used to create such files. If a program is named prog1.m, just type prog1 in the command window to start the execution. MATLAB® looks for the file in the current directory. If it is not found there, it looks in other paths known. This list can be obtained by typing path.
The typed type prog1 enables the contents of the file to be viewed.
EXAMPLE 1(Using the built-in editor) The integrated editor is designed to type the programs, and can be activated by selecting New script in the file menu or by typing edit in the command window. Type the two program lines of Figure 3.
Once the program has been typed and saved under the name example1.m, the type example1 and example1 commands give:

Figure 3 – Typing a program with the built-in editor

The execution of a program can also be launched by selecting the icon in the edit window or by using the F5 control key.
3. Development of a project: this requires the creation of several programs and/or functions (see paragraph 3.3). This implies in principle the creation of a directory in which all of these elements, and their associated data files will be stored. This is called the working directory. During each new session of work on the project, this directory in MATLAB® should be indicated. This can be achieved by selecting Current Folder in the subwindow. Once this is done, pwd can be typed to verify (Figure 4).
The definition of the directory path can also be done by selecting the item set path … in the menu file. The path can also be defined in the command prompt window (by clicking on the icon with “…” in the top-right corner of the window), or by using the command addpath.
The functions path, addpath, rmpath, genpath, pathtool a savepath can be used for managing the access path to the various directories used by MATLAB® (see documentation, online help, or type help path).

Figure 4 – Example of a current directory which is the default directory under Mac OS

To avoid repeating this manipulation at each new work session, a startup.m file can be created in the startup directory. When using linux or Mac OS, the latter is obtained by the command userpath (Figure 4).
With Windows, the userpath command can also be used, unless the Start in field in the Properties dialog box window is already initialized with an access path.

1 Variables and constants

1.1 Vectors and matrices

The MATLAB® language is dedicated to matrix calculations and was optimized in this perspective. The variables handled as a priority are real or complex matrices. A scalar is a 1 × 1 matrix, a column vector is a matrix with only one column, and a line vector a matrix with only one line.

The notation (ℓ × c) indicates that the considered variable has ℓ lines and c columns.

EXAMPLE 2(Assignment of a real matrix) Type a=[1 2 3;4,5,6] at the MATLAB® prompt in the command window:

The size(a) instruction gives the number of rows and columns of a.

Values are assigned to the elements of a matrix by using brackets. A space, or a comma, is a separator, and takes you to the next column, while the semicolon takes you to the next line. Elements are indexed starting from 1. The first index is the line number, the second one is the column number, etc. In our example, a(1,1)=1 and a(2,1)=4. The assignment a=[1 2;3 4 5] will of course lead to an error message, since the number of columns is different for the first and second lines.

Character strings can also be assigned to the elements of a matrix. However, the string length must be compatible with the structure of the matrix. For example, N=['paul';'john'] would be correct, whereas N=['paul';'peter'] would cause an error.

When the vector’s components form a sequence of values separated by regular intervals, it is easier to use what is called an “implicit” loop of the type (indB:step:indE). This expression refers to a list of values starting at indB and going up to indE by increments of step. Values cannot go beyond indE. The increment value step can be omitted if it is equal to 1.

EXAMPLE 3 (Implicit enumeration) Type a=(0:1:10) or a=(0:10). MATLAB® returns:

EXAMPLE 4 (Incremented implicit enumeration) Type a=(0:4:10). MATLAB® returns:

The last element of a vector is indicated by the reserved word end. In the previous example, a(end) indicates that its value is 8.

There are two other suites of value constructs: linspace and logspace. Thus x=linspace(-10,15,100) returns a vector line of 100 numerical values ranging from –10 to +15. x=logspace(-1,2,100) gives 100 values between 10–1 and 102 “on a logarithmic scale”.

It is possible to extend the size of a matrix. The interpreter takes care of available space by dynamically allocating memory space during the execution of the typed instruction.

EXAMPLE 5 (Extension of matrix) Type the following commands one after the other:

COMMENTS:

– when defining variables and objects, the language takes into account whether letters are capital or lowercase;
– typing “;” at the end of a command line prevents the program from displaying the results of an operation;
– several instructions can be written on the same line (using the previous example, a=[1 2 3; 4 5 6], a=[a;a], a=[a;a] could be typed). Instructions are separated by commas, in this case, or semicolons depending whether or not a display of the results is required by the user;
– the display format can be modified by using the format command. Executing format long, for example, changes the number of displayed significant digits from 5 to 15;
– the user must bear in mind that MATLAB® dedicates memory space every time a variable is used for the first time. All of the variables used during a work session are stored in the computer’s memory, which means it is necessary to free space from time to time so as not to get the OUT OF MEMORY error message (see the clear command in the documentation or type help clear).

1.2 Predefined matrices

The following commands are used to obtain certain particular matrices:

– ones(L,C) returns a matrix with L lines and C columns containing nothing but ones. ones(1,N) returns a line vector made up of N ones;
– zeros(L,C) returns a matrix with L lines and C columns containing nothing but zeros;
– eye(L,C) returns the L × C matrix with ones on the diagonal and zeroes everywhere else. eye(1,N), for example, will return a line vector with one “1” followed by N – 1 “0”;
– randn(L,C) returns a matrix with L lines and C columns containing a centered gaussian distributed sample with a variance equal to 1;
– rand(L,C) returns a matrix with L lines and C columns containing a sample uniformly distributed on the interval (0,1);
– aside from the usual matrices, such as Hilbert, Hadamard, Vandermonde, etc., a large number of predefined matrices are available using the gallery function. For a list of these matrices, type help gallery.

The reshape function is used to change the size of a matrix, for example, to go from a (2 × 6) matrix to a (3 × 4) matrix (refer to documentation, or type help reshape). This change of size can also be done directly, as shown in the following example:

EXAMPLE 6 (Changing the size of a matrix) Type:

which would be the equivalent of c=reshape(a,3,4). The zeros(3,4) command initializes the choice of size for the matrix c. The purpose of the next instruction c(:)=a is to fill out the matrix c, column by column, with the sequence of 12 values taken from acolumn by column, a and c must have the exact same number of elements.

The symbol % indicates that what follows in the line is a comment and will therefore not be executed.

EXAMPLE 7 (Predefined matrices) The instructions:

return a line vector containing 10 alternate 1 and –1. As we will see, the same thing can be done with (-1).^[0:9] (see section 2.2).

1.3 Constants and initialization

eps, realmin and realmax are other constants provided for limit test purposes. Their values are respectively: 2.220446049250313e–16, 2.225073858507201e–308 and 1.797693134862316e+308. realmin and realmax correspond to the extreme values that can be obtained in the “floating-point double precision” coding of the IEEE-754 standard.

The constants Inf (∞) and NaN (Not-a-Number) can also be used in the calculations. It should be noted that NaN is not strictly speaking a constant but an indication on the impossibility of making a calculation. Type 0/0 to see the result.

EXAMPLE 8 (Special constants)Inf or NaN can be used as any other constant in operations between matrices.

1.4 Multidimensional arrays

Multidimensional arrays (not supported by some old versions) are an extension of the normal two-dimensional matrix. One way to create such an array is to start with a 2-dimension matrix that already exists and to extend it. Type:

The repmat and cat functions are provided in order to build multidimensional arrays.

1.5 Cells and structures

In the most recent versions of MATLAB®, there are two groups of data that are more elaborate than scalar arrays and character string arrays: the first one is called a cell and the second a structure.

EXAMPLE 9 (Cell definition) In an array of cells, the elements can be of any nature, numerical value, character string, array, etc. Type:

langcell is made up of three elements: the first one is a character string, the second one is a column vector, and the third one is a scalar. This example shows the difference in syntax between an array and a cell, a left brace ({) and a right brace (}) being used instead of a left square bracket ([) and a right square bracket (]). As for the content, langcell(2) refers to the vector [6.5000;2.3], langcell{2} to the content of this vector, and langcell{2}(1) to the numerical value 6.5.

A structure is defined by the struct instruction. The following example defines a structure, called langstruc, comprising three fields: Language, Version, and Year.

EXAMPLE 10 (Defining a structure) The instruction struct assigns the character string MATLAB to the first field, the character string 6.5 to the second field, and the numerical value 2002 to the third field:

The second instruction displays the content of langstruc.Year, which is 2002. A 1 × 1 dimension structure is organized in the same way as an n × 1 dimension array of cells, where n is the number of fields of the structure. Cells can therefore be compared to structures with unnamed fields.

EXAMPLE 11 (Defining a structure) We define a structure named langstruc, comprised of two sets. Each set contains all three fields Language, Version, and Year. Fields are initialized, respectively, with the sequences of two character strings MATLAB and C, of the two values 6.5 and 15.1, and of the two values 2002 and 2003:

These objects can be handled using certain functions: isstruct, fieldnames, setfield, rmfield, cellfun, celldisp, num2cell, cell2mat, cell2struct, struct2cell, etc.

EXAMPLE 12 (Defining a structure from cells) An example of a conversion is as follows:

The third parameter (2) that is part of the instruction cell2struct indicates the dimension of langcell that needs to be taken into account to define the number of fields. Here, for example, size(langcell,2) means that the number of fields is 3.

2 Operations and functions

2.1 Matrix operations

The main matrix operations are the following:

EXAMPLE 15 (A few operations) Type the following commands:

The vectors a and b are real (4 × 1) line vectors. The scalar c is therefore equal to the scalar product of the vectors a and b. On the other hand, d is a “multiplication table”-type (4 × 4) matrix.

2.2 Pointwise operations

EXAMPLE 16 (Pointwise operations) Type the following commands and check the result:

In this sequence of instructions:

EXAMPLE 17 (Alternating sequence)

(-1).^[0:9] leads to a sequence of alternating 1 and –1.

COMMENTS:

– in term by term operations, matrices must have the same dimensions;
– while the operation ' (apostrophe) transconjugates a matrix, the operation .' (period-apostrophe) transposes without conjugating;
– the conj function conjugates each element of a matrix; one obtains .

EXAMPLE 18 (Transposition and transconjugation) Type:

The bsxfun function (Binary Singleton Expansion Function) can also be used to perform operations point to point. Type help bsxfun.

EXAMPLE 19 (Pointwise operations withbsxfun) Type the following commands and check the result:

2.3 Mathematical functions

Certain functions handle matrices only as an array of values. This is the case for functions such as: abs, sqrt, exp, cos, sin, log, tan, acos, asin, atan, etc.

EXAMPLE 20 (Exponential function) Type:

tims is a (1 × 1024) line vector and therefore tims'*fq is a (1024 × 3) matrix. You can see this for yourself by typing, at the end of the previous program, the command whos:

Built-in functions

There is a large number of library functions that can be called using the MATLAB® language. Some are provided with the MATLAB® interpreter, while others have to be paid for, as part of extra modules.

Among the functions available in the basic version, the user will for example find mathematical functions such as the exponential exp, the logarithm log, the usual trigonometric functions, etc. or functions that have more to do with signals and images such as the Fourier transform fft, the 2D convolution conv2, etc.

Some of these functions are written in the MATLAB® language, while others are written in machine language, for reasons of execution speed.

EXAMPLE 21 (“Source programs” and compiled programs) Type:

type compan. MATLAB® displays the text of the compan function (if the version permits), which can be found in one of the folders of your hard-drive. However, if the instruction type fft is executed, MATLAB® returns:

??? Built-in function

meaning that this function is compiled and that its source code cannot be accessed.

In the most recent versions of MATLAB®, many functions that used to be written as “.m” programs were rewritten and now appear as “Built-in”.

2.4 Matrix functions

As we have seen, the exp(A) command calculates the exponential of each element of the matrix A. This operation must not be confused with the matrix exponential. The letter “m” at the end of the functions expm(A), logm(A), sqrtm(A) indicates that we are dealing with matrix functions. For example, eA is defined by:

and is obtained with the function expm(A).

There is also a function called funm that can be used to calculate any function of a matrix. Type help funm.

2.5 Searching elements using min, max, find, etc. functions

1. The min, max, median functions provide the requested – minimum, maximum, median – value and the index in which it has been found in the matrix.
EXAMPLE 22 (min, max, medianfunctions) Type:
median(a) or median(a,1) provide the median value in each of the columns, median(a,2) the median value along each of the rows.
max(a) gives the maximum in each of the columns. max(a,[],1) (equivalent to max(a)) and min(a,[],2) give the maximum and the minimum along the dimensions 1 and 2, respectively.
2. The find function provides the indices for which the given condition is met.
EXAMPLE 23 (Search function) Taper:
rand(3,3) provides a (3 × 3) table obtained by the “random” drawing of numbers in the interval [0, 1]. The first form of find gives the linear index, while the second provides pairs of indices of items (ℓ(i), c(i))that meet the condition (Matlab provides the function ind2sub to convert between the linear index and the index in each dimension). Type idx=find(a>.5); [l,c]=ind2sub(size(a),idx).
EXAMPLE 24 (Some other functions) The following instructions build the pairs (m, n), m ≠ n, obtained from the four figures 1 to 4.
triu(A,1) provides the strictly upper triangular part of A.

2.6 Other useful functions

The eig function returns the eigenvalues and the eigenvectors of a matrix. The poly function returns the characteristic polynomial associated to a matrix, or a polynomial whose roots are a given vector. The roots function returns the roots of a polynomial.

EXAMPLE 25 (A few functions – 1) Type:

EXAMPLE 26 (A few functions – 2) Type:

3 Programming structures

A program execution can be modified using the Boolean variable’s tests and iterated using loops.

3.1 Logical operators on Boolean variables

The logical operators AND (symbol &), OR (symbol |), and NOT (symbol ~) operate on Boolean quantities. The “false” Boolean value is coded as 0 and “true” as a non-zero value. The Boolean constants true and false can be used. x=true, x=1, x=0==0 give the same result.

Boolean quantities can be used in structures such as “if … elseif … else … end”, “switch … case … case … otherwise … end” or “while … end”.

EXAMPLE 27 (Logical functions) Type:

isnan, isinf, isfinite, isstr, ischar, etc. are Boolean functions used for testing the type or the state of the variables.

3.2 Program loops

The for … end program structure works as a calculation loop.

EXAMPLE 28 (Program loops) Type:

As is the case for many interpreters, loops tend to deteriorate calculation performances considerably. The user is therefore advised not to use them, by replacing them with matrix functions when possible.

EXAMPLE 29 (Avoiding loops) Type:

tic “starts the clock”, toc stops it and displays the time duration since tic. The instruction c=a .^ 2 returns a matrix c identical to the matrix b. However its execution is much faster.

3.3 Functions

In the same way that MATLAB® puts functions at our disposal, it is possible to also develop functions to fit our needs. The structure of a function is as follows:

outp1, outp2, etc. are variables calculated by the myfunc function, inp1, inp2, etc. are input parameters. Comment lines – starting with % – following the constructs of the declaration function assist the function and can be initiated with help myfunc. The nargin and nargout functions are used to test the number of input parameters and output, respectively. The error function allows an error message to be displayed, stopping the program execution:

4 Graphically displaying results

4.1 2D display

Display windows are chosen using the command figure(n), where n is a window number (integer ≥ 1). Inside the active window, the plot command can be used to graphically display results:

– if x and y are two real vectors of the same length, the plot(x,y) displays the graph of y as a function of x;
– if x and y are two real matrices of the same size, the plot(x,y) command displays the first column of y as a function of the first column of x, the second column of y as a function of the second column of x, and so on until there are no columns left. Each line has its own color;
– if x is a real vector with a length of N, and y is a size (N × K) real matrix, the plot(x,y) command displays the K graphs corresponding to the K columns of y as a function of x;
– if x is a complex vector, the plot(x) displays the graph of the imaginary part of x as a function of the real part of x (see example 30). There may be problems if some components of the vectors are real. Therefore, preferably use plot(real(x),imag(x)) instead of plot(x);
– if the command subplot(3,2,4) or subplot(324) is added before the plot command, the graph is divided in six “sub-windows” organized in three lines of two columns each, and the display is done in sub-window number 4.

EXAMPLE 30 (Drawing of a circle) Type the following instructions:

The axis([-1.2 1.2 -1.2 1.2]) command forces particular values on the minima and the maxima of the x- and y-coordinates. The axis('square') command forces the display to appear in a square (same units in abscissa and ordinate).

The zoom command is used to zoom in on a particular part of the graph.

Windows and graphs are objects whose properties can be consulted and modified using get and set. These properties can also be accessed from the pull-down menus, meaning that the user does not have to reprogram them. See exercise 2.8, page 93 which describes another way to go about this. Following the instructions of the example 30, type set(gca, 'color','yellow'). The color property color, corresponding to the color of the background of the graphic object – sub-window 2 – is set to yellow.

EXAMPLE 31 (Drawing an ellipse)

where c is a positive constant, X0 is a dimension 2 vector characterizing the center of the ellipse, and E is a 2 × 2 positive matrix, meaning that, for any complex vector Y, YHEY is a positive number. A simple way of obtaining a positive matrix is to take any real matrix G and to calculate GTG.

Starting with this procedure, we now write a function with the ellipse’s center defined by any vector XO, the positive matrix E, and the constant c as input parameters.

HINTS : type:

Test this function2 with the following program, choosing several values of c and:

Test program:

Figure 5 – Drawing of several ellipses

EXAMPLE 32 (Plotting a Bode diagram) Consider the transfer function G(s):

Type (see Figure 6):

Figure 6 – Bode diagram of  

The semilogx function is almost identical to the plot function, plot except that the ordinate is plotted in logarithmic scale.

4.2 3D display

EXAMPLE 33 (Drawing a 3D Gaussian) Taper:

The print -depsc filename function exports a figure with the “encapsulated color postscript” format.

Figure 7 – Drawing a Gaussian withsurf

Surfaces given by pointsM(x(t), y(t), z(t))

The plot3 function allows general 3D curves to be plotted, under the path or point form.

EXAMPLE 34 (Plotting a portion of a sphere) Type:

The sphere, ellipsoid and cylinder functions allow standard surfaces to be plotted.

4.3 Notes on plotting a curve

When plotting parametric curves in maps, it is useful to use the full capacity of MATLAB® to manipulate the complexes. Consider for example a Bezier curve (cubic spline) such as those used by Adobe Illustrator®. The equation for such a curve is given by:

(1)

where P0, P1, P2 and P3 are control points, P0 and P3 are end points of the curve and P1 and P2 are sometimes called handles.

Figure 8 – Plotting a portion of a sphere

EXAMPLE 35 (Plotting a Bezier curve) The mybezier.m program plots the Bezier curve, which is defined by the four points P0, P1, P2 and P3.

4.4 Animations

EXAMPLE 36 (Progressive plotting, usingdelete)

The myanim.m program builds the animation by plotting the path segments and by displaying the extremity of the segment (a circle), identifying the object by the handlehh, for a length of time set by the pause function. Once this appropriate time has passed, the circle object is “destroyed” by delete(hh).

Figure 9 – A Bezier curve

There are functions to carry-out this type of animation (see comet and comet3).

EXAMPLE 37 (Animated plot, usingerasemode)

The myanim2 program plots a Lissajous curve, in which the phase of the second “input” yt varies.

5 Converting numbers to character strings

As an example, let us consider the text(x,y,'text') command. It allows the user to add text to a graph, placed at coordinates (x, y). To add a numerical value to the text command, it must first be converted to a character string. This can be done with the num2str command.

EXAMPLE 38 (Numbers and character strings) Type:

The sprintf command can also be used to build a character string. In fact, it is used by num2str.

EXAMPLE 39 (Creating a character string) Type:

The expression sprintf(…) leads to a character string obtained by converting the numerical value to the format specified by format. For example, the %10.4f format converts the given value with 4 decimal points. For more information, it is recommended to read the printf function’s description in C language.

The functions str2num and hex2num should also be looked into.

6 Input/output

MATLAB® makes it possible to perform input-output operations from the keyboard, on the screen (as it was explained in the previous paragraph with sprintf) or on files. Here are the main functions:

– input, ginput, … for keyboard acquisition;
– disp, sprintf, … to display on the screen;
– gtext, plot, grid, title, … to display in a graph;
– load, save to load or save parts of the variables in a file, or all of them, in a format specific to MATLAB®. By default, files have the extension .mat.
It is recommended that the MATLAB® version is checked using the backup data. If the version is different, it is necessary to specify the format when you save the file (save);
– fopen, fread, fwrite for input-output with formatting.

EXAMPLE 40 (Input/output in a file) Type:

This program creates the try1.dat file of 16 bit integers, then reads its content in variable y.

7 Program writing

MATLAB® offers several facilities for writing and developing programs. Some of which are provided below.

7.1 Developing and testing performances

Breakpoints and “debug” mode

It is possible to “define” breakpoints in the editing window (red disk in the left column of Figure 10). The execution of programs stops there, allowing the contents of variables to be viewed. This can be done in the command window, in which the prompt key becames K>>, or the edit window by positioning the cursor of the mouse over the name of the variable.

Development tools

Besides the functions tic and toc (see example 29) that can be used to test execution times, there are several other tools to test performance and the form of the programs.

1. mlint <prog_name>: analyses the script prog_name.m and provides indications for improving the syntaxe, the readability and maintainability.

Figure 10 – Defining a breakpoint

2. depfun <prog_name>: gives a list of all the scripts and functions of which prog_name.m needs to be executed.
3. the cells (cells in the editor): these are defined as a subset of code lines that can be executed to test effects. These sub-sets are simply separated by lines starting with %% (Figure 11).
4. the performance test for the myanim2 is given by the function profile:
It is obtained by the profile viewer command. The obtained information is shown in 12.
5. local processing of errors:

7.2 Various functions

– executing strings: the eval function executes the instruction given by a string.

Figure 11 – Defining cells for testing purposes

EXAMPLE 41 (Creating files) The testeval.m program creates five files in the MATLAB® format (this format depends on the version, therefore it may be necessary to specify the version of MATLAB® that these files will be used with). The name of the file is built in the program loop:
– calls to the operating system: the operating system commands can be requested in the command window by using the exclamation point:
copies the file datfile1.dat under the name datf.mat in the current directory when the operating system is Unix;

Figure 12 – Measuring the performances

7.3 Using other languages

MATLAB® authorizes the use of procedures written in an evolved language such as C, Pascal or Fortran. These programs belong to the type called MEX. Throughout the rest of this book, we will use only predefined functions and those we are going to build in the MATLAB® language.

MATLAB® also makes it possible to create programs with a graphic interface, combining buttons, pull-down menus, scrolling windows, etc. Using these possibilities is a good way of building “press-a-button” demonstrations or lab works that help to emphasize certain properties. The demonstrations included with MATLAB® are an excellent source of documentation for creating such programs.

1 The problem of solving a linear system in the least-squares sense plays a crucial role in signal processing. This will be discussed further in Chapter 9.

2 Save this function under the name ellipse.m. It will be used later on.

Part I

Deterministic Signals

Chapter 1

Signal Fundamentals

Although this work is mainly focused on discrete-time signals, a discussion of continuous-time signals cannot be avoided, for at least two reasons:

– the first reason is that the quantities we will be using – taken from numeric sequences – are taken from continuous-time signal sampling. What is meant is that the numeric value of a signal, such as speech, or an electroencephalogram reading, etc., is measured at regular intervals;
– the second reason is that for some developments, we will have to use mathematical tools such as Fourier series or Fourier transforms of continuous-time signals.

The objective is not an extensive display of the knowledge needed in the field of deterministic signal processing. Many other books have already done that quite well. We will merely give the main definitions and properties useful to further developments. We will also take the opportunity to mention systems in a somewhat restricted meaning, this word referring to what are called filters.

1.1 The concept of signal

A deterministic continuous-time signal is defined as a function of the real time variable t:

The space made up of these functions is completed by the Dirac pulse distribution, or δ(t) function. Actually a distribution (a linear functional), this object can be handled just like a function without any particular problems in the exercises we will be dealing with.

The following functions spaces are considered:

– L1() is the vector space of summable functions such that |x(t)|dt < + ∞;
– L1(a, b) is the vector space (vector sub-space of L1()) of functions such that ;
– L2() is the vector space of finite energy functions such that |x(t)|2dt < + ∞;
– L2(a, b) is the vector space (vector sub-space of L2()) of functions such that ;
– the set of “finite power” functions characterized by:

L2(0, T) has the structure of what is called a Hilbert space structure with respect to the scalar product ∫x(t)y*(t)dt, a property that is often used for decomposing functions, for example in the case of Fourier series.

In the course of our work, we will need to deal with a particular type of signal, in sets that have already been defined, taken from +.

1.1.1 A few signals

We will often be using particular functions characteristic of typical behaviors. Here are some important examples:

(1.2)

(1.4)

1.1.2 Spectral representation of signals

Fourier series

(1.5)

– a signal with a bounded support on (t1, t2) is also expandable in a Fourier series, but the series converges to the periodized function outside of the (t1, t2) interval;
– expression 1.5 indicates that Xk is the k-th component of x(t) in the orthonormal basis of the complex exponentials in the Hilbert spaceL2(0, T);
– is the best length M approximation of x(t) in the sense of the least squares. Sometimes can be used instead of to indicate that the Fourier series convergence is ensured and is not uniform, which results in:

(1.6)

– when x(t) is continuous, xM(t) converges uniformly to x(t) for any t, when M → + ∞;
– if x(t) shows first order discontinuities, xM (t) will converge to the half sum of the left and right limits of x(t). Finally, xM(t) can show some non-evanescent oscillations in the neighborhoods of all discontinuities. This phenomenon is referred to as the Gibbs phenomenon;
– we have Parseval’s relation:

(1.7)

Because the first member of 1.7 is by definition the signal’s power, the sequence {|Xk|2} can be interpreted as the power distribution along the frequency axis. It is also called power spectral density, or psd.
Fourier transform

The spectral contents X(f) of the function x(t) ∈ L1() ∩ L2() can be represented by an integral that uses complex exponentials, an integral we will call Fourier transform:

(1.8)

|X(f)| is called spectrum of x(t). The Fourier transform’s main properties are summarized in Appendix Al.

The convolution property 11.1 leads to Parseval’s formula:

(1.9)

Because the left member of 1.9 is, by definition, the signal’s energy, |X(f)|2 can be interpreted as the energy distribution along the frequency axis. It is also called energy spectral density, or esd.

More generally, we have:

(1.10)

EXAMPLE 1.1 (Analytical signal)

Using the properties of the continuous-time Fourier transform, show that the real part of z(t) is equal to x(t), and determine its imaginary part called the Hilbert transform of x(t).

HINTS: let:

Using the Fourier transforms, we get:

Likewise, let:

Using the Fourier transforms, we get:

As a conclusion, the analytical signal associated with the real signal x(t) is written (Figure 1.1):

where refers to the Hilbert transform of x(t).

Figure 1.1 – Analytical signal construction

1.2 The concept of system

A system transforms the signal x(t) and delivers a signal y(t), the result of this alteration. We will refer to this transformation as , and x(t) and y(t) will be called the input and the output of the system respectively.

Filters

A filter with x(t) as the input and y(t) as the output is a system defined by:

(1.11)

The existence of the integral has to do with how the set of considered signals x(t