You are on page 1of 40
Lecture 4: Discrete Signal Analysis Prof. Dr. Salina A. Samad Mr. Iskandar Yahya

Lecture 4: Discrete Signal Analysis

Prof. Dr. Salina A. Samad Mr. Iskandar Yahya

Sequence Generation

Types of Sequences

Unit Sample Sequence

Create s single impulse between a finite number of zeros.

Having a function is useful, alternatively use “zeros”

function [x,n] = impseq(n0,n1,n2)

function.

%generates x(n) = delta(n-n0); n1 <= n <= n2

%--------------------------------------------

%[x,n] = impseq(n0,n1,n2)

%

n = [n1:n2]; x = [(n-n0) == 0]; %giving values of n and x

stem(n,x); %stem plot or impulse plotting function

title('Unit Sample Sequence') xlabel('n'); ylabel('x(n)');

x(n)

Sequence Generation

Unit Sample Sequence

The resulting plot looks like this

Unit Sample Sequence

2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 ­5 ­4 ­3 ­2 ­1
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0
­5
­4
­3
­2
­1
0
1
2
3
4
5
n

Sequence Generation

Unit Step Sequence

We can have another elegant way to produce a step function

Alternatively, we can use the “ones” function

function [x,n] = stepseq(n0,n1,n2) %generates x(n) = u(n-n0); n1 <= n <= n2

%--------------------------------------------

%[x,n] = stepseq(n0,n1,n2)

%

n = [n1:n2]; x = [(n-n0) >= 0];

stem(n,x); title('Unit Sample Sequence') xlabel('n'); ylabel('x(n)');

x(n)

Sequence Generation

Unit Step Sequence

Type “stepseq(0,-5,5)” we get:

Unit Sample Sequence

2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 ­5 ­4 ­3 ­2 ­1
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0
­5
­4
­3
­2
­1
0
1
2
3
4
5
n

Sequence Generation

Real-valued exponential Sequence

The operator “.^” is required to implement a real exponential sequence.

For example, to generate x(n) = (0.9) n , 0<= n<=10, we will need the following script:

>> n = [0:10]; x = (0.9).^n;

Example tutorial:

Create a function (M-file) to generate an exponential sequence.

Use the previous examples as your guide.

Sequence Generation

Complex-valued exponential Sequence

A matlab function “exp” is used to generate exponential sequences.

For example, to generate x(n) = exp[(2+j3)n], 0<= n<=10, we will need the following script:

>> n = [0:10]; x = exp((2+3j)*n);

Note that you need different plot commands for plotting real and imaginary components.

Sinusoidal sequence

A matlab function “cos” (or sin) is used to generate sinusoidal sequences.

To generate x(n) = 3cos(0.1πn + π/3) + 2sin(0.5 πn),

0<= n<=10, we will need the following script:

>> n = [0:10]; x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

Sequence Generation

Random Sequences

A random or stochastic sequences are characterised by parameters of the associated probability density functions or their statistical moments.

In matlab, 2 types of (psuedo ) random sequences are avalable:

“rand(1,N)” generates a length N random sequence whos elements are uniformly distributed between 0 and 1.

“randn(1,N) generates a length N gaussian random sequence with mean 0 and variance 1.

Other random sequences can be generated suing transformation of the above functions. (Check previous lecture slides)

Sequence Generation

Periodic sequence

A sequence is periodic if x(n) = x(n +N).

To generate P periods of x(n) from one period, we can copy x(n) P times:

>> xtilde = [x,x,x,x ...,x];

An elegant approach is to use matlab’s indexing capabilites:

Generate a matrix containing P rows of x(n) values.

Concatenate P rows into a long row vector using the construct (:).

Have to use matrix transposition operator (‘) to provide

>> xtilde = x' * ones(1,P); %P columns of x; x is a row vector

the same effect on rows:

>> xtilde = xtilde(:); %long coloumn vector >> xtilde = xtilde'; %long row vector

Sequence Operation

Signal addition

To add two sequences x1(n) and x2(n) requires the operator “+”.

But the length of both sequences must be equal and have the same sample position

We have to first augment both x1(n) and x2(n) so that they have the same position vector and hence length.

This requires careful attention to matlab’s indexing operations.

Sequence Operation

Signal addition

The following function, called “sigadd”, demonstrate this operation:

function [y,n] = sigadd(x1,n1,x2,n2) %generates y(n) = x1(n) + x2(n)

%--------------------------------------------

%[y,n] = sigadd(x1,n1,x2,n2)

% y = sum sequence over n, which includes n1 and n2 % x1 = first sequence over n1

% x2 = second sequence over n2 (n2 can be different from n1) %

n = min(min(n1),min(n2)):max(max(n1),max(n2)); y1 = zeros(1,length(n)); y2 = y1;

y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;

y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;

y = y1+y2;

% duration of y(n) % Initilization

% x1 with duration of y % x2 with duration of y % sequence addition

Sequence Operation

Signal Multiplication

Sample-to-sample multiplication (or “dot” multiplication) Implemented in matlab by the array operator “.*” Again, same restriction as using “+” We can create a function called “sigmult”:

Sequence Operation

function [y,n] = sigmult(x1,n1,x2,n2)

%generates y(n) = x1(n)*x2(n)

%--------------------------------------------

%[y,n] = sigmult(x1,n1,x2,n2) % y = product sequence over n, which includes n1 and n2 % x1 = first sequence over n1 % x2 = second sequence over n2 (n2 can be different from n1) %

n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)

y1 = zeros(1,length(n)); y2 = y1;

y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;

y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;

y = y1 .* y2;

% Initilization % x1 with duration of y % x2 with duration of y % sequence Multiplication

Sequence Operation

Signal Scaling

Each sample is multiplied by a scalar .

{ x( n )} = { x( n )}

Use the command “*” for scaling.

Signal Shifting

Each sample of x(n) is shifted by an amount k to

obtain a shifted sequence y(n)

y( n ) = { x( n k )}

if let m

=

n

k

n

=

m

+

k

then

y( m

+

k )

=

{ x( m )}

% MATLAB function function [y,n] = sigshift(x,m,n0) % implements y(n) = x(n-n0) % ---------------------- % [y,n] = sigshift(x,m,n0) % n = m+n0; y = x;

Sequence Operation

Folding

Each sample is of x(n) is flipped around n=0 to

obtain a folded sequence y(n)

y( n ) = { x( n )}

Implement “sigfold” by “fliplr(x)” & “-fliplr(x)”.

% MATLAB function function [y,n] = sigfold(x,n) % implements y(n) = x(-n) % -------------------- % [y,n] = sigfold(x,n) % y = fliplr(x); n = -fliplr(n);

Signal Summation

n

2

n n

=

1

x( n ) =

x( n )

1

+◊◊◊+

x( n )

2

Adds all sample values of x(n) between n1 and n2 Implement by “sum(x(n1:n2))” function

Sequence Operation

Sample Products

 

n

2

 

x( n )

=

 
 

n

1

x( n )

1

◊◊◊

x( n )

2

Multiplies all sample values of x(n) between n1 and

n2.

Implement by “prod(x(n1:n2))” function ..

Signal Energy

*

x

=

x( n )x ( n )

=

x( n )

2

Superscript * denotes the operation of complex conjugation

% MATLAB code

Implemented by

Ex = sum(x .* conj(x))

Ex = sum(abs(x) .^ 2)

% one approach % another approach

Sequence Operation

Signal Power

P

x

1

=

N

N

1

0

x( n )

2

Average power of a periodic sequence with fundamental period N

Sequence Operation

Example 1:

Let

x( n ) = {1,2,3,4,5,6,7,6,5,4,3,2,1}

Determine and plot the following sequences

x ( n )

x( n

  • a.

1

= 2

  • b.

x

2

( n )

=

x( 3

5

)

3

x( n

+

4

)

n )

+

x( n )x( n

 

2

)

Sequence Operation

Example 1 coding:

% MATLAB code for part (a) n = [-2:10]; x = [1:7,6:-1:1]; [x11,n11] = sigshift(x,n,5); [x12,n12] = sigshift(x,n,-4); [x1,n1] = sigadd(2*x11,n11,-3*x12,n12);

% MATLAB code for part (b) n = [-2:10]; x = [1:7,6:-1:1]; [x21,n21] = sigfold(x,n); [x21,n21] = sigshift(x21,n21,3); [x22,n22] = sigshift(x,n,2); [x22,n22] = sigmult(x,n,x22,n22); [x2,n2] = sigadd(x21,n21,x22,n22);

% Plotting coding subplot(2,1,1); stem(n1,x1); title('Sequence in Example 1a') xlabel('n'); ylabel('x1(n)'); subplot(2,1,2); stem(n2,x2); title('Sequence in Example 1b') xlabel('n'); ylabel('x2(n)');

Sequence Operation

Example 1 plots:

Sequence in Example 1a

20 10 0 ­10 ­20 ­30 x1(n)
20
10
0
­10
­20
­30
x1(n)
­10 ­5 0 5 10 15 n Sequence in Example 1b 40 30 20 10 0
­10
­5
0
5
10
15
n
Sequence in Example 1b
40
30
20
10
0
­8
­6
­4
­2
0
2
4
6
8
10
12
n
x2(n)

Sequence Operation

Example 2:

Generate the complex-valued signal

x( n )

=

e

(
(

0 1

.

+

j . )n

0 3

,

10

n

10

and plot its magnitude, phase, the real part, and the imaginary part in four separate subplots

Sequence Operation

Example 2 coding:

% MATLAB code exmple2 n = [-10:1:10]; alpha = -0.1+0.3j; x = exp(alpha*n); subplot(2,2,1); stem(n,real(x)); title('real part');xlabel('n') subplot(2,2,2); stem(n,imag(x)); title('imaginary part');xlabel('n') subplot(2,2,3); stem(n,abs(x)); title('magnitude part');xlabel('n') subplot(2,2,4); stem(n,(180/pi)*angle(x)); title('phase part');xlabel('n')

Sequence Operation

Example 2 plots:

real part

imaginary part

2

2 1 0 ­1 ­2
2 1 0 ­1 ­2
1 0
1
0

­1

­2

1

0

­1

­2

­3

­10

­5

0

5

10

­10

­5

0

5

10

n

n

magnitude part

phase part

3

3 200 0 100 ­100 ­200

200

200 0 100 ­100 ­200

0

100

­100

­200

2

1

0

­10

­5

0

5

10

­10

­5

0

5

10

n

n

Sequence Synthesis

Unit Sample Synthesis

x( n ) =

n

  • 2 x( k ) ( n

k n

=

1

k )

Arbitrary sequence x(n) can be synthesized as a weighted sum of delayed and scaled unit sample sequences

if

e

Even and odd synthesis

( n )

=

x ( n )

e

  • x

then x ( n )is called even( symmetric )

e

if

( n )

  • x

o

=

x ( n )

o

then x ( n )is called odd( antisymmetric )

o

Any arbitrary real-valued sequence x(n) can be decomposed into its even and odd component

x( n )

=

x ( n )

e

+

x ( n )

o

  • x e

( n )

=

1 2
1
2

[ x( n )

+

x( n )], x ( n )

o

=

1 2
1
2

[ x( n )

x( n )]

Sequence Synthesis

Implement evenoddfunction

function [xe, xo, m] = evenodd(x,n) % Real signal decomposition into even and odd parts % --------------------------------------------- % [xe, xo, m] = evenodd(x,n) % if any(imag(x) ~= 0) error('x is not a real sequence')

end m = -fliplr(n); m1 = min([m,n]); m2 = max([m,n]); m = m1:m2; nm = n(1)-m(1); n1 = 1:length(n); x1 = zeros(1,length(m)); x1(n1+nm) = x; x = x1; xe = 0.5*(x + fliplr(x)); xo = 0.5*(x - fliplr(x));

Sequence Synthesis

Example 2: Even-Odd Components:

% MATLAB code example 2 n = [0:10]; x = stepseq(0,0,10)-stepseq(10,0,10); [xe,xo,m] = evenodd(x,n);

subplot(1,1,1)

subplot(2,2,1); stem(n,x); title('Rectangular pulse') xlabel('n'); ylabel('x(n)'); axis([-10,10,0,1.2]) subplot(2,2,2); stem(m,xe); title('Even Part') xlabel('n'); ylabel('xe(n)'); axis([-10,10,0,1.2]) subplot(2,2,4); stem(m,xo); title('Odd Part') xlabel('n'); ylabel('xo(n)'); axis([-10,10,-0.6,0.6])

Sequence Synthesis

Example 2: The plots

Rectangular pulse

1 0.8 0.6 0.4 0.2 0 x(n)
1
0.8
0.6
0.4
0.2
0
x(n)

Even Part

1 0.8 0.6 0.4 0.2 0 xe(n)
1
0.8
0.6
0.4
0.2
0
xe(n)

­10

­5

0

5

10

­10

­5

0

5

10

 

n

n

Odd Part

0.6 0.4 0.2 0 ­0.2 ­0.4 ­10 ­5 0 5 10 n xo(n)
0.6
0.4
0.2
0
­0.2
­0.4
­10
­5
0
5
10
n
xo(n)

Sequence Synthesis

The Geometric Series

A one-sided exponential sequence of the form

{

n

,n

0

},

is arbitrary constant

n = 0

n

N 1

n = 0

n

1

1

,

for

< 1

1

N

1

,

for

Sequence Synthesis

Correlation of sequences

Measure of the degree to which two sequences are similar Crosscorrelation

If) x(n), y(n) is finite energy Then) crosscorrelation of x(n), y(n)

r

x,y

(l )

=

n =•

n =

x( n )y( n

l ), l : shift or log parameter

Autocorrelation

Special case of crosscorrelation when y(n)=x(n)

r

x,x

(l )

=

n =•

n =

x( n )x( n

l )

Convolution

Convolution can be evaluated in many different ways.

If arbitrary sequences are of infinite duration, then MATLAB cannot be used directly to compute the convolution.

As you already know, there are 3 conditions (cases) for evaluation:

No overlap Partially overlap Complete overlap

A built in function to compute convolution of 2 finite duration is called “conv”.

It assumes that the two sequences begin at n =

>> y = conv(x,h)

Convolution

x(n) = [3,11,7,0,-1,4,2]

x(n) = [3,11,7,0,-1,4,2] h(n) = [2, 3,0,-5,2,1]

h(n) = [2, 3,0,-5,2,1]

x(n) = [3,11,7,0,-1,4,2] h(n) = [2, 3,0,-5,2,1]

Example of a simple convolution:

>>x = [3,11,7,0,-1,4,2]; >> h = [2,3,0,-5,2,1]; >> y = conv(x,h) y =

6

31

47

6

-51

-5

41

18

-22

-3

8

2

>> n = [1:length(y)]; >> m = [1:length(x)]; o = [1:length(h)]; >> figure(1); clf >> subplot(2,2,2); stem(m,x) >>title('Sequence x(m)') >>xlabel('m'); ylabel('x(m)'); >> subplot(2,2,3); stem(o,h) >> title('Sequence y(o)') >>xlabel('o'); ylabel('y(o)'); >> subplot(2,2,1); stem(n,y); >> title('Convolved sequence') >> xlabel('n'); ylabel('y(n)');

Convolution

Example of a simple convolution:

Convolved sequence

Sequence x(m)

50 15 10 0 5 ­50 0 ­100 ­5 0 5 10 15 0 2 4
50
15
10
0
5
­50
0
­100
­5
0
5
10
15
0
2
4
6
8
n
Sequence y(o)
m
4
2
Is This Correct???
0
­2
­4
­6
0
2
4
6
y(n)
y(o)
x(m)

o

Convolution

Based on the plots, “conv” function neither provides nor accepts any timing information.

We need the beginning and the end point of y(n).

A simple extension of the “conv” function, called “conv_m”, can be written to perform the convolution of arbitrary support sequences:

function [y,ny] = conv_m(x,nx,h,nh)

% Modified convolution routine for signal processing % ----------------------------------------------------- % [y,ny] = conv_m(x,nx,h,nh)

%

[y,ny] = convolution result

%

[x,nx] = first signal

%

[h,nh] = second signal

% nyb = nx(1)+nh(1); nye = nx(length(x)) + nh(length(h)); ny = [nyb:nye]; y = conv(x,h);

Convolution

Lets do the convolution again from the previous example:

>> x = [3,11,7,0,-1,4,2]; nx = [-3:3]; >> h = [2,3,0,-5,2,1]; nh = [-1:4]; >> [y,ny] = conv_m(x,nx,h,nh)

y =

 

6

31

47

6

-51

-5

41

18

-22

-3

8

2

ny =

 

-4

-3

-2

-1

0

1

2

3

4

5

6

7

Hence we get the correct convolution:

Y(n) = {6,31,47,6,-51,-5,41,18,-22,-3,8,2}

Convolution  Lets do the convolution again from the previous example: >> x = [3,11,7,0,-1,4,2]; nx

More info? HELP convolution (conv)

y(n)

Convolution

Lets plot y(n):

Convolved Signal y(n)

60 40 20 0 ­20 ­40 ­60 ­4 ­2 0 2 4 6 8 ny
60
40
20
0
­20
­40
­60
­4
­2
0
2
4
6
8
ny

Correlations

There is a close resemblance in convolution and correlation.

Crosscorrelation and autocorrelation can be computed using the “conv” function if sequences are of finite duration.

Example:

In this example let x(n) = [3,11,7, 0,-1,4,2]

Correlations  There is a close resemblance in convolution and correlation.  Crosscorrelation and autocorrelation can

be a prototype sequence, and let y(n) be its noise corrupted and shifted version:

y(n) = x(n-2)+w(n)

where w(n) is Gaussian sequence with mean 0 and variance 1.

Let’s compute the cross correlation between y(n)

Correlations

From the construction of y(n), it follows that y(n) is “similar” to x(n-2)

Hence their strongest similarity is when l = 2. Compute the crosscorrelation using two different noise sequences in Matlab.

Correlations

Matlab commands:

% Noise sequence 1

  • x = [3,11,7,0,-1,4,2]; nx=[-3:3];

[y,ny] = sigshift(x,nx,2); w = rand(1,length(y)); nw = ny; [y,ny] = sigadd(y,ny,w,nw); [x,nx] = sigfold(x,nx); [rxy,nrxy] = conv_m(y,ny,x,nx); % % noise sequence 2

  • x = [3,11,7,0,-1,4,2]; nx=[-3:3];

[y,ny] = sigshift(x,nx,2); w = rand(1,length(y)); nw = ny;

[y,ny] = sigadd(y,ny,w,nw); [x,nx] = sigfold(x,nx); [rxy2,nrxy2] = conv_m(y,ny,x,nx);

% given signal x(n) % Obtain x(n-2) % generate w(n) % obtain y(n) = x(n-2) + w(n) % obtain x(-n) % 1 st crosscorrelation

% given signal x(n) % Obtain x(n-2) % generate w(n) % obtain y(n) = x(n-2) + w(n) % obtain x(-n) % 2 nd crosscorrelation

% figure(1); clf % clear figure(1) subplot(1,1,1), subplot(2,1,1); stem(nrxy,rxy) axis([-4,8,-50,250]); xlabel('lag variable 1') ylabel('rxy');title('Crosscorrelation: noise sequence 1') subplot(2,1,2); stem(nrxy2,rxy2) axis([-4,8,-50,250]); xlabel('lag variable 1')

rxy

rxy

Correlations

The plots:

Crosscorrelation: noise sequence 1

250 200 150 100 50 0 ­50 ­4 ­2 0 2 4 6 8 lag variable
250
200
150
100
50
0
­50
­4
­2
0
2
4
6
8
lag variable 1
Crosscorrelation: noise sequence 2
250
200
150
100
50
0
­50
­4
­2
0
2
4
6
8

lag variable 1

Correlations

Based on the plots, the correlation is proven to peak at l = 2. This implies that y(n) is similar to x(n) shifted by 2.

We can use the signal-processing toolbox in matlab for crosscorrelation:

>> xcorr(x,y) computes the crosscorrelation between vectors x and y, while, >> xcorr(x) computes the autocorrelation of vector x.

This function does not provide timing