DSP Using Matlab® - 4
DSP Using Matlab® - 4
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)
%
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:
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
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)
%
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
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:
% 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:
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
xe ( n ) = 1 [ x( n ) + x( − n )], xo ( n ) = 1 [ x( n ) − x( − n )]
2 2
Sequence Synthesis
Implement “evenodd” function
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
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
>>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
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]
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.