75
Signal Processing under Matlab: Introduction Signal Processing under Matlab: Introduction 1 Signals and Systems Lecture-19 Signal Processing under Matlab

Signal Processing Under Matlab

Embed Size (px)

Citation preview

Page 1: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 1/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 11

Signals and Systems

Lecture-19

Signal Processing under Matlab

Page 2: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 2/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 22

Matlab Variables

• Matlab stores variables in the form of matrices having dimensionsM×N:

 – M is the number of rows.

 – N is the number of columns.

 – A 1×1 matrix is a scalar.

 – A 1×N matrix is a row vector.

 – A M×1 matrix is a column vector.

• Examples:>> x = 5

 – Real scalar x=5.

>> x = [1 2 5 7]

 – A row vector composed from the 4 elements 1, 2, 5 and 7.

>> x = [1 ; 2 ; 5 ; 7]

 – A column vector composed from the 4 elements 1, 2, 5 and 7.

Page 3: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 3/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 33

Complex numbers

• If the variable i is not redefined by the user, then complex numbers canbe written under the form z = x+yi.

• Examples:

>> x = 5+2i

 – Complex scalar x=5+2i.

>> x = [1+i 2 5-i]

 – A row vector composed from the 3 complex numbers 1+i, 2 and 5-i.

>> x = [1 ; (2+i)/(3-i)]

 – A column vector composed from the 2 complex numbers 1 and 0.5+0.5i.

Page 4: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 4/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 44

Basic operations (1)

• The basic arithmetic operations on matrices (and of course on scalarsthat are special cases of matrices) are:

 – addition (+), subtraction (-), multiplication (*), division (/), power (^) and

complex transpose ( ’ ).

• Examples:

>> A = [1 2 ; 3 4]

 – Defines the 2×2 matrix

>> B = A’

 – Results in the matrix B that is the complex transpose of A:

>> A + B

 – Results in the 2×2 matrix

>> A * B

 – Results in the 2×2 matrix

⎥⎦

⎤⎢⎣

⎡=

43

21 A

⎥⎦

⎤⎢⎣

⎡=

42

31 B

⎥⎦

⎢⎣

85

51

⎥⎦

⎤⎢⎣

2511

115

Page 5: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 5/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 55

Basic operations (2)

• Examples:

>> A = [1 2+i ; 3 4]

 – Defines the 2×2 matrix

>> B = A’ – Results in the matrix B that is the complex transpose of A:

>> B = A .’

 – Results in the matrix B that is the transpose of A:

>> B = A^2

 – Results in the matrix B=A*A:

⎥⎦

⎤⎢⎣

⎡ +=

43

21 i A

⎥⎦

⎤⎢⎣

−=

42

31

i B

⎥⎦

⎢⎣

+ 42

31

i

⎥⎦

⎤⎢⎣

+

++

i

ii

32215

51037

Page 6: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 6/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 66

Basic operations (3)

• The element-by-element arithmetic operations are defined by: – (.*) , (./) , (.^) and , (.’)

• Examples:

>> A = [1 2 ; 3 4] ; B = [-2 0 ; i 3];

>> A * B

 – Results in the 2×2 matrix

>> A .* B

 – Results in the 2×2 matrix

• Examples:

>> A = [1 2 3] ; B = [-2 0 i];

>> A .* B

 – Results in the 1×3 row vector [-2 0 3i].

>> A * B

 – Results in an error “inner matrix dimensions must agree”.

⎥⎦

⎤⎢⎣

+−

+−

1246

622

i

i

⎥⎦

⎤⎢⎣

⎡−

123

02

i

Page 7: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 7/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 77

Math functions

• The math functions include: – sin, cos, tan

 – asin, acos, atan

 – exp, log, log10

 – sqrt, sin

 – abs (returns the magnitude) and angle (returns the argument in radians).

• Examples:

>> x = 2+3i ; x’

 – Results in the scalar 2 - 3i.

>> mag = abs(x)

 – Results in the scalar mag = 3.6056.

>> arg_rad = angle(x) ; arg_degree = arg_rad*180/pi;

 – Results in the scalars arg_rad = 0.9828 and arg_degree = 56.31.

Page 8: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 8/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 88

Plotting (1)

• Let x and y be two vectors of the same length. Plotting y as a functionof x can be done using the function “plot”.

 – plot(x,y)

• Examples: Plot the functions te-t and te-tcos(8πt) in the interval [0 8] s.

>> t = [0:0.02:8];

>> x = t.*exp(-t);

>> y = t.*exp(-t).*cos(8*pi*t) ; %(or y = x.*cos(8*pi*t));

>> plot(t,x,b’); >> hold on; >> plot(t,y,’r’);

 – Line 1 defines a vector t = [0 0.02 0.04 0.06 0.08 … 8];

 – Lines 2 and 3 define the functions x and y.

 – plot(t,x,’b’) plots x as a function of t (the color of the curve is blue).

 – “hold on” permits to plot more than one curve on the same figure.

 – plot(t,y,’r’) plots y as a function of t (the color of the curve is red).

Page 9: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 9/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 99

Plotting (2)

• The result is given by:

0 1 2 3 4 5 6 7 8-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

Page 10: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 10/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1010

Plotting (3)

• The x-axis and the y-axis can be labeled using the functions “xlabel”and “ylabel”. A title can be added using the function ‘title’

>> xlabel(‘Time (s)’);

>> ylabel(‘Amplitude’);

>> title(‘x and y’);

0 1 2 3 4 5 6 7 8-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

Time (s)

   A  m  p   l   i   t  u   d  e

x and y

Page 11: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 11/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1111

Plotting (4)

• Legends of the different curves can be added using the function“legend”. A grid can be added using the function “grid on”.

>> legend(‘x(t)’, ‘y(t)’)

>> grid on

0 1 2 3 4 5 6 7 8-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

Time (s)

   A  m  p   l   i   t  u   d  e

x and y

x(t)

y(t)

Page 12: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 12/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1212

Plotting (5)

• The ranges of the x-axis and the y-axis can be limited using thefunctions “xlim” and “ylim”.

>> xlim([0 4]); % limits the values of the x-axis between 0 and 4.

>> ylim([-0.1 1]); % limits the values of the y-axis between -0.1 and 1.

0 0.5 1 1.5 2 2.5 3 3.5 4-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

Time (s)

   A  m  p   l   i   t  u   d  e

x and y

x(t)

y(t)

Page 13: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 13/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1313

Polynomials (1)

• Matlab represents polynomials as row vectors of polynomialcoefficients.

• Examples:

>> p = [1 4 -5];

 – p corresponds to the representation of the polynomial s2 + 4s -5.

>> poly2str(p,’s’)

 – Represents the polynomial as a function of s. The answer will be:

>> s^2 + 4s -5

• Examples: Define the polynomial s5 + 2s2 + s + 5.

>> p = [1 0 0 2 1 5];

 – In order to check the result, we type:

>> poly2str(p,’s’)

The result of the previous command is:

>> s^5 + 2s^2 + s + 5

Page 14: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 14/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1414

Polynomials (2)

• The function “polyval” is used to evaluate a polynomial for certainvalues of the variable.

• Examples:

>> p = [1 4 -5];

>> a = polyval(p,[0]);

>> b = polyval(p,[1 5 10]);

 – In line 1, we defined the polynomial p(s) =s2 + 4s - 5.

 – In line 2, we evaluate a = p(0) = -5.

 – In line 3, we evaluate p(s) for s = 1, 5 and 10. The answer is the row

vector b = [0 40 135].

Page 15: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 15/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1515

Polynomials (3)

• The function “roots” is used to find the poles of a polynomial.

• Examples:

>> p = [1 4 -5];

>> r = roots (p); – The result is the column vector r composed from the roots of the function

s2 + 4s - 5. These roots are given by -5 and 1.

• Example: Determine the roots of the polynomial s4 + s3 + s2 + s – 1.

Check the result by plotting this function.

>> p = [1 1 1 1 -1];

>> r = roots (p);

>> x = -2:0.01:2;

>> y = x.^4 + x.^3 + x.^2 + x -1;

>> plot(x,y);

Page 16: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 16/75

Page 17: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 17/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1717

Polynomials (5)

• Consider the two polynomials p1 and p2. The product of thesepolynomial can be determined using the function “conv”:

 – p = conv(p1,p2)

• Example: determine the product of the polynomials x2 + 2x + 3 and

x5 + 3x -7.

>> p1 = [1 2 3];

>> p2 = [1 0 0 0 3 -7];

>> p = conv(p1,p2);

>> X = poly2str(p,’x’);

 – The vector p will be given by: p = [1 2 3 0 3 -1 -5 -21].

 – The last line will result in the string X given by:X = x^7 + 2 x^6 + 3 x^5 + 3 x^3 - 1 x^2 - 5 x - 21

Page 18: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 18/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1818

Convolution (1)

• The function “conv” can be also applied to determine the convolutionbetween 2 DT sequences.

• Example (assignment-7, exercise 4.2): Consider the DT LTI system

whose impulse response is given by: h[n] = 0.2δ[n] – 0.5δ[n-2] + 0.4

δ[n-3]. Determine the response of the system to the input sequencex[n] = {-1 , 1 , 0 , -1}.

>> h = [0.2 0 -0.5 0.4];

>> x = [-1 1 0 -1];

>> y = conv(h,x);

 – The output sequence y will be given by:

y = [-0.2 0.2 0.5 -1.1 0.4 0.5 -0.4].

Page 19: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 19/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 1919

Convolution (2)

• To plot y, we use the function “stem”.>> L = length(y); % returns the length of vector y.

>> n = 0:1:L-1; % n = 0, 1, 2, … L-1

>> stem(n,y)

-1 0 1 2 3 4 5 6 7-1.2

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

Page 20: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 20/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2020

Laplace Transform (1)

• Consider a LTI system whose transfer function is given by:

• Factorizing the numerator and the denominator of H(s) results in:

Where – z1 … zm are the zeros of H(s).

 – p1 … pn are the poles of H(s).

 – k is the gain of the transfer function.

0

1

1

0

1

1)(bsbsb

asasas H 

n

n

n

n

m

m

m

m

L

L

++

++=

−−

−−

( ) ( )( ) ( )n

m

 ps ps

 zs zsk s H 

−−

−−=

L

L

1

1)(

Page 21: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 21/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2121

Laplace Transform (2)

• To determine the zeros, poles and gain of a transfer function, we usethe function “tf2zp” (transfer function to zero-poles):

[z,p,k] = tf2zp(num,den)

Where

 – num and den correspond to the numerator and denominator of the

transfer function respectively.

 – The zeros will be stored in the vector z, the poles in vector p and the gain

in the scalar k.

• Example: Determine the zeros and poles of:

>> num = [2 3];

>> den = [1 4 0 5];

>> [z,p,k] = tf2zp(num,den);

 – The result will be z = -1.5, k = 2 and

54

32)(23 ++

+=ss

ss H 

⎥⎥⎥

⎢⎢⎢

=

1.07i-0.13

1.07i+0.13

 4.27-

 p

Page 22: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 22/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2222

Laplace Transform (3)

• To visualize the location of the zeros and poles of a transfer function,we use the function “pzmap”

pzmap(num,den)

where:

 – num and den correspond to the numerator and denominator of the

transfer function respectively.

• Example: In the previous example:

>> num = [2 3] ;

>> den = [1 4 0 5];

>> pzmap(num,den);

 – From this plot, we can determine if the system is stable or not.

Page 23: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 23/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2323

Laplace Transform (4)

• The result is given by:

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5-1.5

-1

-0.5

0

0.5

1

1.5Pole-Zero Map

Real Axis

   I  m  a

  g   i  n  a  r  y   A  x   i  s

Page 24: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 24/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2424

Laplace Transform (5)

• The inverse of the function “tf2zp” is given by the function “zp2tf”:[num,den] = zp2tf(z,p,k)

Where:

 – z is a vector containing the zeros, p is a vector containing the poles and k

is a scalar that stands for the gain.

 – The numerator and denominator of the transfer function will be stored in

the vectors num and den respectively.

• Example: Determine transfer function of a system whose gain is equalto 1, whose poles are given by 1+i, 1-i and 2 and that has no zeros.

>> z = [ ]; % the system has no zeros (the numerator is a constant).

>> p = [1+i 1-i 2] ; k = 1;

>> [num,den] = zp2tf(z,p,k);

 – The answer is num = [0 0 0 1] and den = [1 -4 6 -4].

 – In other words, the transfer function is equal to:464

1)(

23 −+−=

ssss H 

Page 25: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 25/75

Page 26: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 26/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2626

Laplace Transform (7)

 – The result is given by:

 – Note that the first system (in blue) is not stable. The second system (in

red) is stable since all of its poles lie in the open LHP.

0 1 2 3 4 5 60

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

0.5

Time (sec)

   A  m  p   l   i   t  u   d  e

Step response

Page 27: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 27/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2727

Laplace Transform (8)

• To plot the impulse response of CT systems, we use the function“impulse”:

y = impulse(num,den,t)

 – num and den correspond to the numerator and denominator of the

transfer function respectively.

 – t corresponds to the time interval over which the impulse response y 

must be plotted.

• Example: Plot the impulse response of a system whose gain is equal

to 1, whose poles are given by 1+i, 1-i and 2 and that has no zeros.Plot the impulse response of a system whose gain is equal to 1, whose

poles are given by -1+i, -1-i and -2 and that has no zeros.

>> z = [ ] ; k = 1; p = [1+i 1-i 2] ; p1 = [-1+i -1-i -2];

>> [num,den] = zp2tf(z,p,k) ; [num1,den1] = zp2tf(z,p1,k);

>> t = 0:0.01:4; y = impulse(num, den, t) ; y1 = impulse(num1,den1,t);

>> plot(t,y); hold on; plot(t,y1, ‘r’); ylim([0 0.5])

Page 28: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 28/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2828

Laplace Transform (9)

 – The result is given by:

 – Note that the first system (in blue) is not stable. The second system (in

red) is stable since all of its poles lie in the open LHP.

Page 29: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 29/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 2929

Laplace Transform (10)

• The partial fraction expansion (PFE) of a transfer function can bedetermined using the function “residue”

[r,p,k] = residue(num,den)

• In this case, the transfer function is written as:

where k(s) is different from zero when m ≥ n.

• Example: Determine the PFE of 

>> num = 1;

>> den = [1 7 14 8];

>> [r , p ,k] = residue(num,den);

The answer is k = [ ], ,

)()(1

1

0

0 sk  ps

 ps

bsb

asas H 

n

n

n

n

mm +

−++

−=

++++= L

L

L

81471)(

23 +++=

ssss H 

⎥⎥

⎢⎢

=

0.33

0.5-

0.16

⎥⎥

⎢⎢

=

1

2

4

 p

Page 30: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 30/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3030

Laplace Transform (11)

• Consequently:

• If p(j) = … = p(j+m-1) is a pole of multiplicity m, then the expansionincludes terms of the form:

• Example: Determine the PFE of:

>> z = -1 ; k = 1 ; p = [0 0 0 -2 -2];

>> [num,den] = zp2tf(z,p,k) ;

>> [r , p ,k] = residue(num,den);

1

1

3

1

2

1

2

1

4

1

6

1)(

++

+−

+=

ssss H 

( ) ( )m j

m j

 j

 j

 j

 j

 ps

 ps

 ps

−++

−+

−−++ 1

2

1L

23 )2(

1)(++=

ss

ss H 

Page 31: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 31/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3131

Laplace Transform (12)

• The result is:

• Consequently:

⎥⎥⎥

⎥⎥⎥

⎢⎢⎢

⎢⎢⎢

=

⎥⎥⎥

⎥⎥⎥

⎢⎢⎢

⎢⎢⎢

==

0.25

0

0.0625-

0.1250

0.0625

;

0

0

0

2

2

;][ r  pk 

32

25.00625.0

)2(

1250.0

2

0625.0)(

sssss H  +−

++

+=

Page 32: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 32/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3232

z-transform (1)

• To plot the zeros and poles of a transfer function H(z), we use thefunction “zplane”

zplane(num,den)

• Example: Plot the zeros and poles of 

H(z) can be written as:

>> num = [0 0 1 1];

>> den = [1 2 3 -2];

>> zplane(num,den)

232

1)(

23−++

+=

 z z z

 z z H 

321

32

2321)(

−−−

−−

−++

+=

 z z z

 z z z H 

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

-1.5

-1

-0.5

0

0.5

1

1.5

Real Part

   I  m  a  g   i  n  a  r  y   P  a  r   t

Page 33: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 33/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3333

z-transform (2)

• The step response can be obtained using the function “dstep”:y = dstep(num,den,n)

Where:

 – num and den stand for the numerator and denominator of H(z)

respectively.

 – The output is the sequence y[n].

• Example: Determine the step response of 

>> k = 1; % the gain

>> z = [ ]; %the zeros

>> p = [1/2 1/4]; %the poles

>> [num,den] = zp2tf(z,p,k);

>> n = 0:1:20;

>> y = dstep(num,den,n);

>> stem(n,y);

⎟ ⎠

 ⎞

⎜⎝ 

⎛ −

⎟ ⎠

 ⎞

⎜⎝ 

⎛ −

=

4

1

2

1

1)(

 z z

 z H 

Page 34: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 34/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3434

z-transform (3)

• The result is given by

 – we can directly conclude that the system is stable.

0 2 4 6 8 10 12 14 16 18 200

0.5

1

1.5

2

2.5

3

f (4)

Page 35: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 35/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3535

z-transform (4)

• The impulse response can be obtained using the function “dimpulse”:y = dimpulse(num,den,n)

Where:

 – num and den stand for the numerator and denominator of H(z)

respectively.

 – The output is the sequence y[n].

• Example: Determine the impulse response of 

>> k = 1; % the gain

>> z = [ ]; %the zeros

>> p = [1/2 1/4]; %the poles

>> [num,den] = zp2tf(z,p,k);

>> n = 0:1:20;

>> y = dimpulse(num,den,n);

>> stem(n,y);

⎟ ⎠

 ⎞⎜⎝ 

⎛ −⎟

 ⎠

 ⎞⎜⎝ 

⎛ −

=

4

1

2

1

1)(

 z z

 z H 

t f (5)

Page 36: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 36/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3636

z-transform (5)

• The result is given by

 – we can directly conclude that the system is stable.

0 2 4 6 8 10 12 14 16 18 200

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

F R (1)

Page 37: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 37/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3737

Frequency Responses (1)

• The function “freqs” is used to determine the frequency response of analog filters.

[h,w] = freqs(num,den)

Where:

 – num and den are the numerator and denominator of the transfer function

H(s) respectively.

 – H = H(jw) is the frequency response.

 – w is a row vector corresponding to the angular frequency.

• Example: Plot the amplitude and phase spectra of 

>> num = [0.2 0.3 1];

>> den = [1 0.4 1];

>> [h,w] = freqs(num,den);

14.0

13.02.0)(

2

2

++++=

ss

sss H 

F R (2)

Page 38: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 38/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3838

Frequency Responses (2)

• To plot the amplitude and phase spectra as a function of the logarithmof the angular frequency w:

>> subplot(2,1,1)

>> semilogx(w,20*log10(abs(h)));

>> xlabel(‘Frequency’) ; ylabel(‘Magnitude (dB)’) ; grid on

>> subplot(2,1,2)

>> semilogx(w,angle(h)*180/pi);

>> xlabel(‘Frequency’) ; ylabel(‘Phase (degrees)’) ; grid on

 – where the function semilogx(x,y) plots y as a function of x (in the same

way as the function “plot”). The difference is that a logarithmic scale is

used on the x-axis.

 – The function subplot divides a figure into sub-figures.

 – subplot(m,1,i) corresponds to the i-th subfigure of a figures composed

from m vertical subfigures.

Frequency Responses (3)

Page 39: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 39/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 3939

Frequency Responses (3)

• The result is given by:

10-1

100

101

-20

-15

-10

-5

0

5

10

Frequency

   M  a  g  n   i   t  u   d  e

   (   d   B   )

10-1

100

101

-120

-100

-80

-60

-40

-20

0

Frequency

   P   h  a  s  e   (   d

  e  g  r  e  e  s   )

Frequency Responses (4)

Page 40: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 40/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4040

Frequency Responses (4)

• The function “freqz” is used to determine the frequency response of DTfilters.

[h,w] = freqz(num,den)

Where:

 – H(z) = num(z)/den(z).

 – h = H(e jw) is the frequency response.

 – w is always included between 0 and π.

• Example (from lecture 17, example 2): Plot the amplitude and phasespectra of H(z) = 1+z-2.

>> num = [1 0 1];

>> den = [1 0 0];

>> [h,w] = freqz(num,den);

Frequency Responses (5)

Page 41: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 41/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4141

Frequency Responses (5)

• To plot the amplitude and phase spectra:

>> subplot(2,1,1)

>> plot(w,abs(h));

>> xlabel(‘Frequency’) ;>> ylabel(‘Magnitude’) ;

>> grid on

>> subplot(2,1,2)>> plot(w,angle(h)*180/pi);

>> xlabel(‘Frequency’) ;

>> ylabel(‘Phase (degrees)’) ;>> grid on

0 0.5 1 1.5 2 2.5 3 3.50

0.5

1

1.5

2

Frequency

   M  a  g  n   i   t  u   d  e

0 0.5 1 1.5 2 2.5 3 3.5-100

-50

0

50

100

Frequency

   P   h  a  s  e   (   d  e  g  r  e  e  s   )

Analog filters (1)

Page 42: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 42/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4242

 Analog filters (1)

• The function “buttap” is used for designing an analog lowpass

Butterworth filter with a cutoff angular frequency of 1 rad/s:

[z,p,k] = buttap(n)

Where:

 – n is the order of the filter.

 – The zeros, poles and gain of the filter will be stored in z, p and k

respectively.

 – By construction, the filter will be a lowpass filter with cutoff frequency 1

rad/s (angular frequency w=2πf).

• The cutoff frequency is defined as the frequency at which the gain of 

the filter is equal to -3dB.

Analog filters (2)

Page 43: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 43/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4343

 Analog filters (2)

• The function “bode” is used to plot the Bode diagram of analog filters:

bode(num,den)

where:

 – num and den correspond to the numerator and denominator of the

transfer function respectively.

• Example: Plot the Bode diagrams of the LP Butterworth filters of orders

2 and 9 having cutoff frequencies of 1 rad/s.

>> [z,p,k] = buttap(2);>> [num,den] = zp2tf(z,p,k);

>> [z1,p1,k1] = buttap(9);

>> [num1,den1] = zp2tf(z1,p1,k1);>> bode(num,den,‘b’);

>> hold on; grid on;

>> bode(num1,den1,‘r’);

Analog filters (3)

Page 44: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 44/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4444

 Analog filters (3)

• The result is given by:

-400

-350

-300

-250

-200

-150

-100

-50

0

   M  a  g  n   i   t  u   d

  e   (   d   B   )

10-2

10-1

100

101

102

-900

-720

-540

-360

-180

0

   P   h  a  s  e   (   d  e  g   )

Bode Diagram

Frequency (rad/sec)

Analog filters (4)

Page 45: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 45/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4545

 Analog filters (4)

• Limiting the range of the magnitude spectrum between -30 and 10:

>> ylim([-30 10]);

 – Butterworth filters are characterized by a magnitude response that is flat

in the passband. Note how the rolloff steepness enhances with the order 

of the filter.

-3 0

-2 5

-2 0

-1 5

-1 0

-5

0

5

10

   M  a  g  n   i   t  u   d  e   (   d   B   )

10-2

10-1

100

101

102

-900

-720

-540

-360

-180

0

   P   h  a  s  e   (   d  e  g   )

Bode Diagram

Frequency (rad/sec)

-3 dB at w = 1 rad/s 

Page 46: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 46/75

Analog filters (6)

Page 47: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 47/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4747

 Analog filters (6)

>> [z,p,k] = buttap(5);

>> [num,den] = zp2tf(z,p,k);

>> [num1,den1] = lp2lp(num,den,2*pi*100);

>> bode(num1,den1); grid on;

-200

-150

-100

-5 0

0

50

   M  a

  g  n   i   t  u   d  e   (   d   B   )

101

102

103

104

105

-450

-360

-270

-180

-9 0

0

   P   h  a  s  e   (   d  e  g   )

Bode Diagram

Frequency (rad/sec)

 Analog filters (7)

Page 48: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 48/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4848

g ( )

• To design a highpass Butterworth filter with cutoff angular frequency w

rad/s, the function “lp2hp” is used:

[num1,den1] = lp2hp(num,den,w)

Where:

 – num and den are the numerator and the denominator of the transfer function of a LP Butterworth filter with cutoff frequency 1.

 – w is the cutoff frequency of the filter (in rad/s).

 – num1 and den1 are the numerator and the denominator of the transfer 

function of a HP Butterworth filter with cutoff frequency w.

• Example: plot the Bode diagram of a HP Butterworth filter of order 5

having a cutoff frequency of 100 Hz.

 Analog filters (8)

Page 49: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 49/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 4949

g ( )

>> [z,p,k] = buttap(5);

>> [num,den] = zp2tf(z,p,k);

>> [num1,den1] = lp2hp(num,den,2*pi*100);

>> bode(num1,den1); grid on;

-250

-200

-150

-100

-50

0

   M  a  g

  n   i   t  u   d  e   (   d   B   )

101

102

103

104

105

0

90

180

270

360

450

   P   h  a  s  e   (   d  e  g

   )

Bode Diagram

Fr e q u e n c y ( r a d / s e c )

 Analog filters (9)

Page 50: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 50/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5050

g ( )

• To design bandpass and bandstop Butterworth filters with center 

frequency w and bandwidth B, the functions “lp2bp” and “lp2bs” are

used:

[num1,den1] = lp2bp(num,den,w,B)

[num1,den1] = lp2bs(num,den,w,B)

Where:

 – num and den are the numerator and the denominator of the transfer 

function of a LP Butterworth filter with cutoff frequency 1.

 – w is the center frequency of the filter (in rad/s).

 – B is the bandwidth of the filter.

 – num1 and den1 are the numerator and the denominator of the transfer 

function of a BP (resp. BS) Butterworth filter with center frequency w andbandwidth B.

• Example: plot the Bode diagrams of 2 BP Butterworth filters of order 5

having a center frequency of 200 Hz and whose bandwidths are equal

to 20 Hz and 100 Hz respectively.

 Analog filters (8)

Page 51: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 51/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5151

g ( )

>> [z,p,k] = buttap(5);

>> [num,den] = zp2tf(z,p,k);

>> [num1,den1] = lp2bp(num,den,2*pi*200,2*pi*20);

>> [num2,den2] = lp2bp(num,den,2*pi*200,2*pi*100);

>> bode(num1,den1); grid on; hold on;

>> bode(num2,den2,’r’); ylim([-100 5])

- 1 0 0

- 8 0

- 6 0

- 4 0

- 2 0

0

   M  a  g  n   i   t  u   d  e   (   d   B   )

1 0- 1

1 00

1 01

1 02

1 03

1 04

1 05

- 7 2 0

- 3 6 0

0

3 6 0

7 2 0

   P   h  a  s  e   (   d  e  g   )

B o d e D i a g r a m

Fre q u e n c y ( r a d / s e c )

 Analog filters (9)

Page 52: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 52/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5252

• Another type of filters is the Chebyshev filter. The function “cheb1ap” is

used for designing an analog lowpass Chebyshev filter (type 1) with

cutoff frequency 1 rad/s:

[z,p,k] = cheb1ap(n,R)

Where:

 – n is the order of the filter.

 – R is the ripple in the passband (in dB) .

 – The zeros, poles and gain of the filter will be stored in z, p and k

respectively.

 – By construction, the filter will be a lowpass filter with cutoff frequency 1

rad/s (angular frequency w=2πf).

• To design lowpass and highpass Chebyshev filters at other cutoff frequencies, the functions “lp2lp” and ‘lp2hp” are used.

• To design bandpass and bandstop Chebyshev filters, the functions

“lp2bp” and ‘lp2bs” are used.

 Analog filters (10)

Page 53: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 53/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5353

• Example: Compare 2 LP Chebyshev filters of order 4 having a cutoff 

frequency of 10 Hz. The first filter introduces 1 dB of ripple at low

frequencies while the second one introduces 3 dB of ripple.

>> [z,p,k] = cheb1ap(4,1);

>> [num,den] = zp2tf(z,p,k);

>> [NUM,DEN] = lp2lp(num,den,2*pi*10);

>> [z1,p1,k1] = cheb1ap(4,3);

>> [num1,den1] = zp2tf(z1,p1,k1);

>> [NUM1,DEN1] = lp2lp(num1,den1,2*pi*10);

>> bode(NUM,DEN);

>> hold on; grid on;

>> bode(NUM1,DEN1,’r’);

>> ylim([-30 5]);

 Analog filters (11)

Page 54: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 54/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5454

• The result is given by:

 – The filter with more ripple has a steeper rolloff. This enhanced filtering is

obtained at the expense of introducing more distortions to the signal.

-3 0

-2 5

-2 0

-1 5

-1 0

-5

0

5

   M  a  g  n

   i   t  u   d  e   (   d   B   )

100

101

102

103

-360

-270

-180

-9 0

0

   P   h  a  s  e   (   d  e  g   )

Bode Diagram

Frequency (rad/sec)

 Analog filters (12)

Page 55: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 55/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5555

• Example: Compare a BP Butterworth filter and a BP Chebyshev filter.

Both filters have an order of 4, center frequency of 100 rad/s and a

bandwidth of 50 rad/s. The Chebyshev filter introduces 1 dB of ripple.

>> w = 100 ; B = 50;

>> [z,p,k] = buttap(4);

>> [num,den] = zp2tf(z,p,k);

>> [NUM,DEN] = lp2bp(num,den,w,B);

>> [z1,p1,k1] = cheb1ap(4,1);

>> [num1,den1] = zp2tf(z1,p1,k1);

>> [NUM1,DEN1] = lp2bp(num1,den1,w,B);

>> bode(NUM,DEN);

>> hold on; grid on;

>> bode(NUM1,DEN1,’r’);

>> ylim([-30 5]) ; xlim([10 1e3]);

 Analog filters (13)

Page 56: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 56/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5656

• The result is given by:

• In the passband, the Chebyshev filters are not as flat as the Butterworth filters.

However, the Chebyshev filters have a better rolloff.

-20

-15

-10

-5

0

   M  a  g  n

   i   t  u   d  e   (   d   B   )

101 102 103

-360

-180

0

180

360

   P   h  a  s  e   (   d  e  g   )

Bode Diagram

Frequency (rad/sec)

Digital filters (1)

Page 57: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 57/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5757

• The function “butter” is used to design digital Butterworth filters:

[num,den] = butter(n,w,type)

Where:

 – n is the order of the filter.

 – type = ‘low’, ‘high’, ‘stop’ and ‘bandpass’ stands for the type of the filter.

 – num and den are the numerator and the denominator of the transfer 

function H(z) of the digital Butterworth filter.

 – For lowpass and highpass filters, w is a scalar:w = 2f c / f s

where f c is the cutoff frequency and f s is the frequency at which the data is

sampled.

 – For bandpass and bandstop filters, w is a 2-dimensional row vector:

w = 2 [f 1 f 2] / f s

where f 1 and f 2 correspond to the lower and higher frequencies respectively.

Digital filters (2)

Page 58: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 58/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5858

• Example: For data sampled at 1000 samples/s, design a bandstop

Butterworth filter of order 8 in the band [300 400] Hz.

>> [num,den] = butter(8, [300 400]*2/1000,‘stop’);

>> [h,w] = freqz(num,den);

>> f = w*(1000/2)/pi; % given that w is between 0 and π

>> plot(f,abs(h)) ; grid on % f is the frequency range in Hz. Based

%% on the sampling theorem, f can not exceed 1000/2 Hz = 500 Hz.

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

0

0 . 2

0 . 4

0 . 6

0 . 8

1

1 . 2

1 . 4

Digital filters (3)

Page 59: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 59/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 5959

• To filter discrete-time signals, the function “filtfilt” is used:

y = filtfilt(num,den,x)

Where:

 – x is the input signal (before filtering).

 – y is the output signal (after filtering).

 – num and den are the numerator and denominator of the DT filter 

respectively.

• Example: Filter the high frequency component of the signal x(t) =cos(2π50t) + cos(2π300t) sampled at 1000 Hz using a Butterworth filter 

of order 4.

>> [num,den] = butter(4,50*2/1000, ‘ low’);

>> t = 0:0.001:0.2;

>> x = cos(2*pi*50*t) + cos(2*pi*300*t);

>> y = filtfilt(num,den,x);

Digital filters (4)

Page 60: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 60/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6060

>> plot(t,x,’b’);

>> hold on;

>> plot(t,y,’r’);

0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Fourier transform (1)

Page 61: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 61/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6161

• The Fourier transform of discrete signals can be determined using the

“fft” function:

X = fft(x,N)

Where:

 – x is the DT signal.

 – X is the N-point DFT of x.

• Note that:

 – The k-th point of X corresponds to the frequencyk f s / N

where f s is the sampling rate.

Fourier transform (2)

Page 62: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 62/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6262

• Example: plot the 1024-point DFT of the signal x(t) = cos(2π50t)

sampled at the rate of 1000 samples/s.

>> N = 1024;

>> t = [0:1:N-1]*0.001;

>> x = cos(2*pi*50*t);

>> X = fft(x,N);

>> Y = X/N; % It is more significant to normalize the magnitude by N

>> f = [0:1:N-1]*1000/N;

>> plot(f,abs(Y));

>> xlabel(‘Frequency (Hz)’);

Fourier transform (3)

Page 63: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 63/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6363

• The result is given by:

0 100 200 300 400 500 600 700 800 900 10000

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

0.5

Frequency (Hz)

Fourier transform (4)

Page 64: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 64/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6464

• Note that since the points in the interval [N/2 N-1] are redundant, it is

more convenient to plot the first N/2 points of the DFT.

>> N = 1024;

>> t = [0:1:N-1]*0.001;

>> x = cos(2*pi*50*t);

>> X = fft(x,N);

>> Y = X/N;

>> f1 = [0:1:N/2-1]*1000/N;

>> Y1 = Y(1:1:N/2);

>> plot(f1,abs(Y1));

>> xlabel(‘Frequency (Hz)’); 0 50 100 150 200 250 300 350 400 450 5000

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

0.5

Frequency (Hz)

Fourier transform (5)

Page 65: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 65/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6565

• Example: Filter the high frequency component of the signal x(t) =

cos(2π50t) + cos(2π200t) sampled at 1000 Hz using a Butterworth filter of order 4. Plot the 1024-point DFT of x(t) and its filtered version.

>> N =1024;

>> t = [0:1:N-1]*0.001;

>> x = cos(2*pi*50*t) + cos(2*pi*200*t);

>> [num,den] = butter(4,50*2/1000, ‘ low’);

>> y = filtfilt(num,den,x);

>> X = fft(x,N)/N ; X1=X(1:1:N/2);

>> Y = fft(y,N)/N ; Y1=Y(1:1:N/2);

>> f = [0:1:N/2-1]*1000/N;

>> plot(f,abs(X1)) ; hold on;

>> plot(f,abs(Y1),‘r’);

Fourier transform (6)

Page 66: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 66/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6666

• The result is given by:

0 50 100 150 200 250 300 350 400 450 5000

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

0.5

Fourier transform (7)

Th I F i t f b l l t d i th f ti

Page 67: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 67/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6767

• The Inverse Fourier transform can be calculated using the function

“ifft”:

x = ifft(X,N)

Where:

 – x is the DT signal.

 – X is the N-point DFT of x.

Example: filtering the noise (1)

E l C id th i l

Page 68: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 68/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6868

• Example: Consider the signal

x(t) = 1 + cos(2π50t) + 0.5sin(2π70t)

sampled at the rate of 2000 samples/s. Let

y(t) = x(t) + n(t)

where n(t) stands for the noise term. Apply a convenient LP Butterworth

filter of order 3 that permits to reduce the impact of the noise.

%%% Design the Butterworth filter

>> [num,den] = butter(3,70*2/2000, ‘low’); % since the highest

% frequency component of the signal must not be filtered out.

%%% Generate x(t)

>> t = 0:0.0005:0.25;

>> x = 1 + cos(2*pi*50*t) + 0.5*sin(2*pi*70*t);

Example: filtering the noise (2)

%%% G t th i t (t)

Page 69: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 69/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 6969

%%% Generate the noise term n(t)

>> n = 2 * randn(size(t)); %% The noise here is considered to be

% Gaussian with a standard deviation of 2.

%%% Generate y(t)

>> y = x + n;

%%% filter the noise and plot the result

>> z = filtfilt(num,den,y);

>> plot(t,y);

>> hold on;

>> plot(t,z, ‘r’);

>> legend(‘signal corrupted by noise’, ‘ filtered signal’);

Example: filtering the noise (3)

• The result is given by:

Page 70: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 70/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 7070

• The result is given by:

Example: filtering the noise (4)

• Analyzing the result in the frequency domain

Page 71: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 71/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 7171

• Analyzing the result in the frequency domain

>> N = 1024;

>> f = [0:1:N/2-1]*2000/N;

>> Y = fft(y,N)/N ; Y1 = Y(1:1:N/2);

>> Z = fft(z,N)/N ; Z1 = Z(1:1:N/2);

>> plot(f,abs(Y1));

>> hold on;

>> plot(f,abs(Z1), ‘r’);

>> ylim([0 0.5]);

>> xlim([0 500]);

Example: filtering the noise (5)

• The result is given by:

Page 72: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 72/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 7272

• The result is given by:

Summary of functions (1)

• Basic functions:

Page 73: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 73/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 7373

Basic functions:

 – (+) , (-) , (*) , (/) , (^) , (‘).

 – (.*) , (./) , (.^) , (.‘).

• Math functions:

 – sin, cos, tan, asin, acos, atan.

 – exp, log, log10, sqrt, sin, abs, angle.

 – length.

• Plotting functions:

 – plot, hold on, xlabel, ylabel, title, legend.

 – grid on, xlim, ylim.

 – stem, figure. – semilogx, semilogy.

• Polynomials:

 – poly2str, polyval, roots, conv

Summary of functions (2)

• Laplace Transform:

Page 74: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 74/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 7474

Laplace Transform:

 – tf2zp, zp2tf, pzmap

 – step, impulse

 – residue

• z-transform:

 – zplane

 – dstep, dimpulse

• Frequency responses:

 – freqs, freqz

• Analog filters:

 – buttap, cheb1ap – lp2lp, lp2hp, lp2bp, lp2bs

 – bode

Summary of functions (3)

• Digital filters:

Page 75: Signal Processing Under Matlab

7/31/2019 Signal Processing Under Matlab

http://slidepdf.com/reader/full/signal-processing-under-matlab 75/75

Signal Processing under Matlab: IntroductionSignal Processing under Matlab: Introduction 7575

Digital filters:

 – butter 

 – filtfilt

• Fourier transform:

 – fft, ifft