MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INSTITUTE OF INFORMATION & COMMUNICATION TECHNOLOGIES
ADVANCED DIGITAL SIGNAL PROCESSING
Lab 4: Convolution Sum and Correlation
Objective 1.2 Convolution of an impulse and a
square wave
The objective of this lab is to compute and plot unit im-
pulse response, unit step response, unit ramp response To have better insight, let’s examine the result of a con-
of discrete time systems, convolution sum of finite se- volution sum of an impulse and a square, e.g. y[n] =
quences, autocorrelation and cross correlation of signals. δ[n − 1] ∗ xs [n]. Where,
1, 0 ≤ n ≤ 5
xs [n] =
1 Convolution Sum 0, elsewhere
The convolution of two discrete time sequences x[n] and To create the signals,
h[n] can be computed as follows,
≫ x = [0 1 0 0 0 0];
∞
X
y[n] = x[n] ∗ h[n] = x[k]h[n − k] ≫ xs = [1 1 1 1 1 1];
k=−∞
Now, convolve the signals and create a time vector
or, equivalently,
∞
≫ y = conv(x,xs);
X
y[n] = h[n] ∗ x[n] = h[k]x[n − k] ≫ t = 0:length(y)-1;
k=−∞
This sum of products (or convolution sum) is in fact And, to plot the output.
a function of n that represent the overlap between x[n]
≫ stem(t,y);
and the time-reversed and shifted version of h[n]. The
number of samples N in the output signal y[n] will be What can you observe about the output? Try shifting
N = M1 + M2 –1. Where, M1 is the number of samples the time square wave and/or the impulse. How does
in sequence x[n] and M2 is the number of samples in this change the convolution output? Try to convolve
sequence h[n]. two square waves y[n] = xs [n] ∗ xs [n] and observe the
In MATLAB, the function y = conv(x, h) implements result.
the convolution of two finite length sequences x(n) and
h(n), generating the finite length sequence y. The pro-
cess is illustrated in the following example. 1.3 Example
Determine and sketch the convolution sum of,
1.1 Convolution of two impulses x(n) = [−2 0 1 −1 3] and h(n) = [1 2 0 −1]
clc , clear all , close all
Lets say we want to examine the convolution sum of two x = [ -2 0 1 -1 3]; % x ( n )
impulses, e.g. y[n] = δ[n] ∗ δ[n − 1] using the time axis h = [1 2 0 -1]; % h ( n )
n = [0, 1, 2] y = conv (x , h ) ; % convoluti o n
n1 = length ( h ) -1;
≫ x1 = [1 0 0]; n2 = length ( x ) -1;
k = - n1 :1: n2 ;
disp ([ ’ Output Sequence y [ n ] = ’ , num2str ( y ) ]) ;
≫ x2 = [0 1 0];
figure ;
subplot (311)
Now, we can convolve them, and create the proper stem (x , ’ . ’) , title ( ’ x [ n ] ’)
time vector the output. subplot (312)
stem (h , ’ . ’) , title ( ’ h [ n ] ’)
≫ y = conv(x1,x2); subplot (313)
stem (k ,y , ’. ’) , title ( ’y [ n ] ’)
≫ t = 0:length(y)-1; To have better insight, lets do another example where,
And, to see the result graphically, let’s plot the out- x(n) = { 1 ↑2 3 } and h(n) = { 2 4 ↑3 5 }. Using the MAT-
put. LAB script.
clc , clear all , close all
≫ stem(t,y); x = [1 ,2 ,3]; % x ( n )
nx = -1:1;
h = [2 ,4 ,3 ,5]; % h ( n )
What can you observe about the output of this sys- nh = -2:1;
tem? Try changing the order of convolution, i.e. try nyb = nx (1) + nh (1) ; nye = nx ( length ( x ) ) + nh (
conv(x2,x1). Does this change the output? length ( h )) ;
1/3
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INSTITUTE OF INFORMATION & COMMUNICATION TECHNOLOGIES
ADVANCED DIGITAL SIGNAL PROCESSING
Lab 4: Convolution Sum and Correlation
ny = nyb : nye ; y = conv (x , h ) ; A measure of similarity between a pair of energy sig-
disp ([ ’ Output Sequence y [ n ] = ’ , num2str ( y ) ]) ; nals, x[n] and h[n], is given by the cross-correlation se-
figure ;
subplot (311)
quence Γxh (k). Where the parameter k is called lag,
stem ( nx ,x , ’ . ’) , title ( ’x [ n ] ’ ) indicating the time-shift between the pair of signals.
subplot (312) The cross correlation sequence Γxh (k) of two discrete
stem ( nh ,h , ’ . ’) , title ( ’h [ n ] ’ ) time signals x[n] and h[n] is defined as,
subplot (313)
stem ( ny ,y , ’ . ’) , title ( ’y [ n ] ’ ) ∞
X
Γxh (k) = x[k]h[n − k]
n=−∞
2 Responses of LTI Systems or equivalently,
∞
The response of a discrete time system to a unit im- X
pulse sequence is called the “unit impulse response” or Γhx (k) = h[k]x[n − k]
n=−∞
simply the “impulse response”. Correspondingly, the
response of a discrete time system to a unit step se- Similarly, the number of samples N in the output sig-
quence is its “unit step response”. The output y[n] of a nal will be N = M1 + M2 –1.
causal LTI system can be simulated in MATLAB using In MATLAB, the function conv(x, f lipr(h)) may
the function “filter”. In one of its forms, the function, be used to compute this sequence. Alternatively,
y = f ilter(p, d, x) processes the input data vector x us- xcorr(x, h) and xcorr2(x, h) may also be used.
ing the system characterized by the coefficient vectors p
and d to generate the output vector y assuming zero ini- 3.1 Example
tial conditions. The length of y is the same as the length
of x. The following example illustrates the computation Compute and sketch the correlation sequence for the fol-
of the impulse and step responses of an LTI system. lowing signals.
x(n) = [ 0↑ 1 −2 3 −4 ] and h(n) = [ 0.5 1 ↑2 1 0.5 ]
2.1 Example clc , clear all , close all
t = [ -5 -4 -3 -2 -1 0 1 2 3 4 5]; % x - axis
Find impulse and step response of the following LTI sys- x = [ 0 0 0 0 0 0 1 -2 3 -4 0]; % input
tem h = [ 0 0 0 0.5 1 2 1 0.5 0 0 0]; % response
y = xcorr2 (x , h );
n1 = length ( x ) -1;
n2 = length ( h ) -1;
y[n] + 0.7y[n − 1]–0.45y[n − 2]–0.6y[n − 3] = k = - n1 :1: n2 ;
0.8x[n]–0.44x[n − 1] + 0.36x[n − 2] + 0.02x[n − 3] disp ([ ’ Output Sequence y [ n ] = ’ , num2str ( y ) ]) ;
figure ;
subplot (311)
clc , clear all , close all stem (t ,x , ’m . ’) , title ( ’ x [ n ] ’)
p = [0.8 -0.44 0.36 0.02]; % x ( n ) c o e f f i c i e n t s subplot (312)
d = [1 0.7 -0.45 -0.6]; % c o e f f i c i e n t s of y ( n ) stem (t ,h , ’r . ’) , title ( ’ h [ n ] ’)
N = 41; % desired impulse response length subplot (313)
x = [1 zeros (1 , N -1) ]; % impulse input stem (k ,y , ’. ’) , title ( ’y [ n ] ’)
y = filter (p ,d , x ) ; % impulse response ( output ) axis ([ -10 10 -7 2])
k = 0:1: N -1;
stem (k , y ) A different code to achieve same outcome,
title ( ’ Impulse response ’) clc , clear all , close all
xlabel ( ’ Time index ( n ) ’) t = [ -5 -4 -3 -2 -1 0 1 2 3 4 5]; % x - axis
ylabel ( ’ Amplitude ’) x = [ 0 0 0 0 0 0 1 -2 3 -4 0]; % input
h = [ 0 0 0 0.5 1 2 1 0.5 0 0 0]; % response
To determine the step response, we replace in the y = [ 0 0 0 0 0 0 0 0 0 0 0];
above program the statement x = [1 zeros(1,N-1)]
with the statement x = ones(1,N) yc = 1;
for n = min ( t ): max ( t ) ,
pause (3) ;
3 Cross Correlation Sequence % flip h
ht = fliplr ( h ) ;
There are applications where it is necessary to compare
if n <0 ,
one reference signal with one or more signals to determ- % shift to the left
ine the similarity between the pair and to determine ad- ht = [ ht ( - n +1: length ( h ) ) zeros (1 , - n ) ];
ditional information based on the similarity. else
2/3
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INSTITUTE OF INFORMATION & COMMUNICATION TECHNOLOGIES
ADVANCED DIGITAL SIGNAL PROCESSING
Lab 4: Convolution Sum and Correlation
% shift to the right 5 Exercise
ht = [ zeros (1 , n ) ht (1: length ( h ) - n ) ];
end 1. Compute the convolution sum for the following sig-
y ( yc ) = sum ( x .* ht ) ;
nals,
yc = yc + 1;
(a) x = [ 1↑ 2 1 −1 ] and h = [ 1 ↑2 3 1 ]
subplot (2 ,1 ,1) ;
stem (t , x ) ;
(b)
hold on ;
2, n=0
stem (t , ht , ’ filled ’ , ’r ’) ; 1, n = ±1
3, n=1
hold off ; x(n) = 2, n=1 h(n) =
0 −2, n=2
elsewhere
xlabel ( ’t ’ ) ; 0 elsewhere
legend ( ’x [ n ] ’ , ’h [n - k ] ’ ,0) ;
title ([ ’n = ’ num2str ( n ) ]) ; 2. Determine and sketch the unit ramp response of the
subplot (2 ,1 ,2) ; system given in Example 2.1. Hint: x=k
stem (t , y ) ;
xlabel ( ’t ’ ) ; 3. Determine the impulse response, unit step response
ylabel ( ’y [ n ] ’ ) ; and unit ramp response of the system described by
end the difference equation. y[n] = 0.7y[n−1]−0.1y[n−
2] + 2x[n] − x[n − 2]
4. Sketch the response of the system characterized by
4 Autocorrelation Sequence the impulse response h[n] = (1/2)n u[n] to the input
signals
Autocorrelation indicates how the signal energy (power)
is distributed within the signal, and as such is used to 1, 0 ≤ n ≤ 10
x(n) =
measure the signal power. 0 elsewhere
The auto correlation sequence Γxx (k) of discrete time
signals x(n) is computed as, 5. Compute and sketch the cross correlation for the
following signals,
∞
X (a) x(n) = [ 1 ↑2 3 4 ] and h(n) = [ 4 3 ↑2 1 ]
Γxx (k) = x(n)x(n − k)
n=−∞ (b) x(n) = [ 4↑ 2 1 5 ] and h(n) = [ 3↑ 5 2 ]
The number of samples N in the output signal will be 6. Compute and sketch the autocorrelation sequence
N = 2 × M –1. Where, M is the number of samples in for the following signals,
the sequence x[n].
The Matlab function xcorr2(x) is used to find the (a) x(n) = [2 3 5 6]
autocorrelation function. (b) m(n) = [2 1 1 1 2]
4.1 Example
Find the autocorrelation function of the signal,
x(n) = [1 2 1 1]
clc , clear all , close all
x = [1 2 1 1];
y = xcorr2 ( x ) ;
n = length ( x ) -1;
k = - n :1: n ;
disp ([ ’ Output Sequence y [ n ] = ’ , num2str ( y ) ]) ;
figure ;
subplot (211)
stem (x , ’ . ’) , title ( ’ x [ n ] ’ )
subplot (212)
stem (k ,y , ’. ’)
title ( ’y [ n ] ’)
xlabel ( ’ Lag index k ’ )
ylabel ( ’ Amplitude ’)
3/3