2019ee76 - LAB4 DSP
2019ee76 - LAB4 DSP
LAB 4
Instructions:
All tasks must be completed in this lab session for full credit.
Students must show the output and MATLAB code for each task to the instructor
before proceeding to the next task.
References:
Task 1
Write a MATLAB script file to verify the commutativity for an LTI systems having impulse response ℎ[𝑛] =
0.2𝑛𝑢[𝑛 − 2], 0 ≤ 𝑛 ≤ 10. Follow the steps given below to complete this task.
Task 2
Write a MATLAB script file to verify the associative law for the LTI systems having impulse response ℎ1[𝑛] =
0.2𝑛𝑢[𝑛], 0 ≤ 𝑛 ≤ 10 and ℎ2[𝑛] = 𝑢[𝑛 − 1] − 𝑢[𝑛 + 3]. Follow the steps given below to complete this task.
x = [1,1,1,1,1,1,1,1,0,0,0,0,0,0];
n = [-3:10];
h1 = [0,0,0,1,0.2^1,0.2^2,0.2^3,0.2^4,0.2^5,0.2^6,0.2^7,0.2^8,0.2^9,0.2^10];
h2 = [-1,-1,-1,-1,1,1,1,1,1,1,1,1,1,1,];
[y_d1, n_d1] = conv_m(h1, n, h2, n);
[y_1, n_1] = conv_m(x, n, y_d1, n_d1);
[y_d2, n_d2] = conv_m(x, n, h1, n);
[y_2, n_2] = conv_m(y_d2, n_d2, h2, n);
subplot(2,1,1); stem(n_1, y_1);
title('y1[n] = x[n]*(h1[n]*h2[n])')
xlabel('n_1'); ylabel('y_1[n]');
subplot(2,1,2); stem(n_2, y_2);
5
title('y2[n] = (x[n]*h1[n])*h2[n]')
xlabel('n_2'); ylabel('y_2[n]');
ℎ[𝑛] = 0, 𝑛 < 0.
An LTI system is said to be stable, in the bounded input-bounded output (BIBO) sense, if its impulse
response is absolutely summable, i.e.,
∞
∑ |ℎ[𝑛]| ≤ 𝑀ℎ < ∞,
where 𝑀ℎ is the upper bound on the absolute summation of the impulse response. It can be observed that
the impulse response sequence of BIBO stable systems is a decaying sequence, i.e., ℎ[𝑛] → 0 as 𝑛 →
∞.
Task 3
Write a MATLAB function, using if-else control statements, that takes impulse response as its input and outputs
1 (when the system is causal) and 0 (otherwise). Call the function with the impulse responses defined in tasks 1
and 2 to find out which are causal. Record your observation below.
6
function [status] = causality_check (x, n)
%
%
% --------------------------------------------------------------
d = 0;
for i = 1:length(n)
if n(i) < 0
d = d + x(i);
end
end
if d == 0
status = 1;
else
status = 0;
end
h1 = [0,0,0,0,0,0.2^2,0.2^3,0.2^4,0.2^5,0.2^6,0.2^7,0.2^8,0.2^9,0.2^10];
n1 = [-3:10];
n23 = [-3:10];
h2 = [0,0,0,1,0.2^1,0.2^2,0.2^3,0.2^4,0.2^5,0.2^6,0.2^7,0.2^8,0.2^9,0.2^10];
h3 = [-1,-1,-1,-1,1,1,1,1,1,1,1,1,1,1,];
x1 = causality_check (h1, n1)
x2 = causality_check (h2, n23)
x3 = causality_check (h3, n23)
Task 4
Find whether the three impulse response sequences, defined in Tasks 1 and 2, are BIBO stable or not ? Justify
your answer through MATLAB by writing a one statement code.
h1 = [0,0,0,0,0,0.2^2,0.2^3,0.2^4,0.2^5,0.2^6,0.2^7,0.2^8,0.2^9,0.2^10];
n1 = [-3:10];
n23 = [-3:10];
h2 = [0,0,0,1,0.2^1,0.2^2,0.2^3,0.2^4,0.2^5,0.2^6,0.2^7,0.2^8,0.2^9,0.2^10];
h3 = [-1,-1,-1,-1,1,1,1,1,1,1,1,1,1,1,];
x1 = isstable (h1)
x2 = isstable (h2)
x3 = isstable (h3)
7
2.1 CORRELATION OF SEQUENCES
An operation that is closely related to the discrete convolution of two sequences is the correlation of two
sequences, which is a measure of similarity of the sequences. Given two real-valued sequences 𝑥[𝑛] and 𝑦[𝑛] of
finite energy, the cross-correlation of 𝑥[𝑛] and 𝑦[𝑛]is a sequence 𝑟𝑥𝑦[𝑙] defined as
∞
It provides a measure of self-similarity between different alignments of the sequence. MATLAB functions xcorr
and to compute auto- and cross-correlations for sequences In its simplest form,
>> xcorr(x,y)
>> xcorr(x)
8
computes the autocorrelation of vector x. However, like the conv function, the xcorr function cannot provide the
timing (or lag) information, which then must be obtained in a similar manner that was used to obtain the timing
information for the convolution of two sequences.
Task 5
Write a function my_corr.m to find the correlation of two sequences 𝑥[𝑛] and 𝑦[𝑛], where 𝑥[𝑛] is the
input and 𝑦[𝑛] = 𝑥[𝑛 − 5] + 𝑤[𝑛], in which 𝑤[𝑛] is a zero-mean, unit-variance Gaussian noise signal.
Verify its output by writing a MATLAB script file named my_corr_check.m, in which define the input
sequence 𝑥[𝑛] = {−2, −1, 0, 1, 2} and the Gaussian noise sequence by the following piece of code
w = randn(1, length(x));
nw = nx;
Call the function my_corr in the script file and write commands to plot the signals 𝑥[𝑛], 𝑤[𝑛] and 𝑦[𝑛].
The signals must be plotted on a single figure window using the stem command.
For your convenience, structure of the function my_corr is given below.
x = [-2:2];
nx = [-2:2];
w = randn(1, length(x));
nw = nx;
[x_delay, n_delay] = sigshift(x,nx,5);
[y,ny] = sigadd(x_delay,n_delay,w,nw);
9
[r_xy, n_xy] = my_corr(x,nx,y,ny);
subplot(3,1,1); stem(nx, x);
title('1st Input Signal')
xlabel('nx'); ylabel('x[n]');
subplot(3,1,2); stem(ny, y);
title('2nd Input Signal')
xlabel('ny'); ylabel('y[n]');
subplot(3,1,3); stem(n_xy, r_xy);
title('Correlaion Output')
xlabel('n_xy'); ylabel('r_xy[n]');
A discrete causal LTI system can also be described by a linear constant coefficient difference equation of the
form
𝑁 𝑀
If 𝑎𝑁 ≠ 0, then the difference equation is of order 𝑁. This equation describes a recursive approach for
computing the current output, given the input values and previously computed output values. In practice, this
the equation is computed forward in time, from 𝑛 = −∞ to 𝑛 = ∞. Therefore, another form of this equation is
𝑁 𝑀
1
0
𝑦[𝑛] = 𝑦𝑝[𝑛] + 𝑦ℎ[𝑛],
where 𝑦𝑝[𝑛] and 𝑦ℎ[𝑛] are called particular and homogeneous solutions respectively. The homogeneous
solution is given by
𝑁
𝑦ℎ[𝑛] = ∑ 𝐶𝑘𝜆𝑛𝑘
𝑘=1
where 𝜆𝑘, 𝑘 = 1,2, … , 𝑁 are the 𝑁 roots of the characteristic polynomial of the system, given by
∑ 𝑎𝑘𝜆𝑁−𝑘 = 0.
𝑘=0
This characteristic polynomial is important in determining the stability of system. If the roots 𝜆𝑘 satisfy the
condition
then a causal system described by the linear constant-coefficient difference equation in (4.2) is stable. The
particular solution, 𝑦𝑝[𝑛], is determined from the right-hand side of (4.1).
where
are the coefficient arrays from the equation given in (4.1) and x is the input sequence array. The output y has the
same length as input x. One must ensure that the coefficient a0 is not zero. To compute and plot impulse
response, MATLAB provides the function impz. When invoked by
it computes samples of the impulse response of the filter at the sample indices given in n with numerator
coefficients in b and denominator coefficients in a. When no output arguments are given, the impz function
plots the response in the current figure window using the stem function.
where 𝑦𝑧𝑖[𝑛] is called the zero-input response, which is a solution due to the initial conditions alone (assuming
they exist), while the zero-state response, 𝑦𝑧𝑠[𝑛], is a solution due to input x(n) alone (or assuming that the
initial conditions are zero).
FIR filter
If the unit impulse response of an LTI system is of finite duration, then the system is called a finite-duration
impulse response (or FIR) filter. Hence, for an FIR filter, ℎ[𝑛] = 0 for 𝑛1 ≤ 𝑛 ≤ 𝑛2. The following part of the
difference equation (4.1) describes a causal FIR filter:
𝑀
Furthermore, ℎ[0] = 𝑏0, ℎ[1] = 𝑏1, … , ℎ[𝑀] = 𝑏𝑀, while all other ℎ[𝑛]’s are 0.
FIR filters are also called non-recursive or moving average (MA) filters. In MATLAB, FIR filters are
represented either as impulse response values ℎ[𝑛] or as difference equation coefficients {𝑏𝑘} and 𝑎0 = 1.
Therefore, to implement FIR filters, we can use either the conv(x, h) function (and its modification that we
discussed) or the filter (b, 1, x) function. It should be noted that the output sequence from the conv(x, h)
function has a longer length than both the 𝑥[𝑛] and ℎ[𝑛] sequences. On the other hand, the output sequence
from the filter (b, 1, x) function has exactly the same length as the input sequence 𝑥[𝑛].
IIR filter
If the impulse response of an LTI system is of infinite duration, then the system is called an infinite-duration
impulse response (or IIR) filter. The part of difference equation (4.1)
∑ 𝑎𝑘𝑦[𝑛 − 𝑘] = 𝑥[𝑛], ∀𝑛
𝑘=0
describes a recursive filter in which the output 𝑦[𝑛]is recursively computed from its previously computed
values and is called an autoregressive (AR) filter. The impulse response of such a filter is of infinite duration
and hence, represents an IIR filter. The general equation (4.1) also describes an IIR filter. It has two parts: an
AR part and an MA part. Such an IIR filter is called an autoregressive moving average, or an ARMA, filter. In
1
2
MATLAB, IIR filters are described by the difference equation coefficients {𝑏𝑘} and {𝑎𝑘} and are implemented
by the filter(b ,a ,x) function.
Exercises:
1. Linear Constant-Coefficient Difference Equation
A backward difference system, given by
is a digital differentiator. Implement this differentiator on the following sequences and plot the results
using stem command.
𝑥[𝑛] = 3𝑢[𝑛 − 5] − 3𝑢[𝑛 − 25]: a rectangular pulse
𝑥[𝑛] = 𝑛(𝑢[𝑛] − 𝑢[𝑛 − 15]) + (30 − 𝑛)(𝑢[𝑛 − 15] − 𝑢[𝑛 − 30]): a triangular pulse
𝜋𝑛
𝑥[𝑛] = sin ( ) (𝑢[𝑛] − 𝑢[𝑛 − 100]): a sinusoidal pulse
20
1
3
[x21,nx21] = stepseq(0,0,31);[x22,nx22] = stepseq(15,0,31); [x23,nx23] =
stepseq(30,0,31);
x2 = n2.*(x21 - x22) + (30 - n2).*(x22 - x23);
y2 = filter(b,a,x2);
stem(n2,y2)
1
4
2. Correlation of Sequences
In a certain concert hall, echoes of the original audio signal x(n) are generated due to the reflections at
the walls and ceiling. The audio signal experienced by the listener y(n) is a combination of x(n) and its
echoes. Let