Sei sulla pagina 1di 40

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)');
Sequence Generation
Unit Sample Sequence
 The resulting plot looks like this
Unit Sample Sequence
2

1.8

1.6

1.4

1.2
x(n)

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)');
Sequence Generation
Unit Step Sequence
 Type “stepseq(0,-5,5)” we get:
Unit Sample Sequence
2

1.8

1.6

1.4

1.2
x(n)

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
the same = x' * ones(1,P);
effect %P columns of x; x is a row vector
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)); % duration of y(n)


y1 = zeros(1,length(n)); y2 = y1; % Initilization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1+y2; % 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; % Initilization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1 .* y2; % sequence Multiplication
Sequence Operation
Signal Scaling
 α{ x(. n )} = { αx( n )}
Each sample is multiplied by a scalar
 Use the command “*” for scaling.

Signal Shifting
 Each sample of x(n) is shifted by an amount k to
y( n ) = {y(n)
obtain a shifted sequence x( n − k )}
% MATLAB function
if let m = n − k function [y,n] = sigshift(x,m,n0)
n = m+k % implements y(n) = x(n-n0)
% ----------------------
then y( m + k ) = { x( m )} % [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 n ) = { − x( n )}y(n)
y( sequence
 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 ∑
n2
n = n1
x( n ) = x( n1 ) + ⋅ ⋅ ⋅ + x( n2 )
 Adds all sample values of x(n) between n1 and n2
 Implement by “sum(x(n1:n2))” function
Sequence Operation
Sample Products
n2

∏ x( n ) = x( n1 ) × ⋅ ⋅ ⋅ × x( n2 )
n1
 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)) % one approach
Ex = sum(abs(x) .^ 2) % another approach
Sequence Operation
Signal Power
1 N −1
Px = ∑0 x( n )
2

N
 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

a . x1( n ) = 2 x( n − 5 ) − 3 x( n + 4 )
b. x2 ( n ) = x( 3 − 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
x1(n)

­10

­20

­30
­10 ­5 0 5 10 15
n

Sequence in Example 1b
40

30
x2(n)

20

10

0
­8 ­6 ­4 ­2 0 2 4 6 8 10 12
n
Sequence Operation
Example 2:
 Generate the complex-valued signal

x( n ) = e( −0.1+ j 0.3 )n , − 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 1

1
0
0

­1
­1
­2

­3 ­2
­10 ­5 0 5 10 ­10 ­5 0 5 10
n n
magnitude part phase part
3 200

100
2
0

1
­100

0 ­200
­10 ­5 0 5 10 ­10 ­5 0 5 10
n n
Sequence Synthesis
Unit Sample Synthesis
x( n ) = ∑k 2= n x( k )δ( n − k )
n
1

 Arbitrary sequence x(n) can be synthesized as a


weighted sum of delayed and scaled unit sample
sequences

 if x Even and odd synthesis


e ( − n ) = xe ( n )  if xo ( −n ) = − xo ( n )
 
then xe ( n ) is called even( symmetric ) then xo ( n ) is called odd ( antisymmetric )

 Any arbitrary real-valued sequence x(n) can be


decomposed
x( n ) = x ( n ) + x into
( n ) its even and odd component
e o

xe ( n ) = 1 [ x( n ) + x( − n )], xo ( n ) = 1 [ x( n ) − x( − n )]
2 2
Sequence Synthesis
 Implement “evenodd” function

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 Even Part

1 1
0.8 0.8

xe(n)
x(n)

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

0
­0.2
­0.4

­10 ­5 0 5 10
n
Sequence Synthesis
The Geometric Series
 A one-sided exponential sequence of the form

{ α n , n ≥ 0 }, α is arbitrary cons tan t


1
∑n =0 α n →

, for α < 1
1− α
1− αN
∑n=0 α → 1 − α , for ∀α
N −1 n
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)

rx ,y ( l ) = ∑n = −∞ x( n ) y( n − l ), l : shift or log parameter


n =∞

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

rx ,x ( l ) = ∑n = −∞ x( n )x( n − l )
n =∞
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)
0:
Convolution
x(n) = [3,11,7,0,-1,4,2]
Example of a simple convolution:
h(n) = [2, 3,0,-5,2,1]

>>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

x(m)
y(n)

5
­50
0

­100 ­5
0 5 10 15 0 2 4 6 8
n m
Sequence y(o)
4

2
Is This Correct???
0
y(o)

­2

­4

­6
0 2 4 6
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}

More info? HELP convolution (conv)


Convolution
Lets plot y(n):
Convolved Signal y(n)
60

40

20
y(n)

­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]

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]; % given signal x(n)
[y,ny] = sigshift(x,nx,2); % Obtain x(n-2)
w = rand(1,length(y)); nw = ny; % generate w(n)
[y,ny] = sigadd(y,ny,w,nw); % obtain y(n) = x(n-2) + w(n)
[x,nx] = sigfold(x,nx); % obtain x(-n)
[rxy,nrxy] = conv_m(y,ny,x,nx); % 1st crosscorrelation
%
% noise sequence 2
x = [3,11,7,0,-1,4,2]; nx=[-3:3]; % given signal x(n)
[y,ny] = sigshift(x,nx,2); % Obtain x(n-2)
w = rand(1,length(y)); nw = ny; % generate w(n)
[y,ny] = sigadd(y,ny,w,nw); % obtain y(n) = x(n-2) + w(n)
[x,nx] = sigfold(x,nx); % obtain x(-n)
[rxy2,nrxy2] = conv_m(y,ny,x,nx); % 2nd 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')
ylabel('rxy');title('Crosscorrelation: noise sequence 2')
Correlations
 The plots:
Crosscorrelation: noise sequence 1
250

200

150
rxy

100

50

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

200

150
rxy

100

50

­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

Potrebbero piacerti anche