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(nn0); n1 <= n <= n2
%
%[x,n] = impseq(n0,n1,n2)
%
n = [n1:n2]; x = [(nn0) == 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
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(nn0); n1 <= n <= n2
%
%[x,n] = stepseq(n0,n1,n2)
%
n = [n1:n2]; x = [(nn0) >= 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:
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
Realvalued 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 (Mfile) to generate an exponential sequence.
Use the previous examples as your guide.
Sequence Generation
Complexvalued 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
Sampletosample 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(nn0) %  % [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
x( n ) =
x( n )
+◊◊◊+
x( n )
Adds all sample values of x(n) between n1 and n2 Implement by “sum(x(n1:n2))” function
Sequence Operation
Sample Products
x( n )
◊◊◊
x( n )
Multiplies all sample values of x(n) between n1 and
n2.
Implement by “prod(x(n1:n2))” function ..
Signal Energy
=
x( n )x ( n )
=
x( n )
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
1
=
N
x( 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
x ( n )
x( n
= 2
x
( n )
=
x( 3
5

)

3

x( n

+
4

)

n )

+

x( n )x( n


2

)

Sequence Operation
Example 2:
Generate the complexvalued signal
x( n )
=
e
,
10
n
10
and plot its magnitude, phase, the real part, and the imaginary part in four separate subplots
Sequence Synthesis
Unit Sample Synthesis
x( n ) =
k )
Arbitrary sequence x(n) can be synthesized as a weighted sum of delayed and scaled unit sample sequences
if
Even and odd synthesis
( n )
=
x ( n )
then x ( n )is called even( symmetric )
if
( n )
=
x ( n )
then x ( n )is called odd( antisymmetric )
Any arbitrary realvalued sequence x(n) can be decomposed into its even and odd component
x( n )
=
x ( n )
+
x ( n )
( n )
=
[ x( n )
+
x( n )], x ( n )
=
[ x( n )
x( n )]
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: EvenOdd 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
The Geometric Series
A onesided exponential sequence of the form
{
,n
≥ 0
},
is arbitrary constant
1
1
,
for
< 1
1
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
(l )
=
x( n )y( n
l ), l : shift or log parameter
Autocorrelation
Special case of crosscorrelation when y(n)=x(n)
r
(l )
=
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]
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:
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)
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)
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(n2)+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(n2)
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(n2) % generate w(n) % obtain y(n) = x(n2) + w(n) % obtain x(n) % 1 ^{s}^{t} crosscorrelation
% given signal x(n) % Obtain x(n2) % generate w(n) % obtain y(n) = x(n2) + w(n) % obtain x(n) % 2 ^{n}^{d} 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')
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 signalprocessing 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
Much more than documents.
Discover everything Scribd has to offer, including books and audiobooks from major publishers.
Cancel anytime.