Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id.
No: 2000040230
Name: [Link] Lab Section: B Id No:2000040230
Pre-lab Session In-Lab Session Post Lab session Viva Total Marks
work (15M) work (15M) work (10M) (10M) 50M
Remarks if any:
Date: Signature of the Instructor Marks awarded
Digital Signal Processing, 19EC2208
Solution of Linear Constant Coefficient Difference
Equations -Lab-4
Lab Report
Introduction: The approach to solving linear constant coefficient difference equations
is to find the general form of all possible solutions to the equation and then apply a
number of conditions to find the appropriate solution.
The two main types of problems are initial value problems, which involve constraints
on the solution at several consecutive points, and boundary value problems, which
involve constraints on the solution at nonconsecutive points.
The number of initial conditions needed for an Nth order difference equation, which is
the order of the highest order difference or the largest delay parameter of the output in
the equation, is N, and a unique solution is always guaranteed if these are supplied.
Objectives:
To find the impulse and step response of the system.
To test whether the given system is stable or not using the impulse response.
To assess the solution for a system described by linear constant coefficient
difference equations, with initial conditions for an arbitrary input sequences.
Requirements: Digital Computer with MATLAB software.
Basic theory: Difference Equations: An LTI system can also be described by a Nth
order linear constant Coefficient difference equation of the form
1
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
Examples:
Ex1: Consider a linear constant coefficient difference equation described by
,
(a) Determine analytically Impulse Response of the system. Check the system is
stable or not.
(b) Determine analytically Step Response of the system
(c) Find the response of the system for an input sequence
Case1: Assume that the initial conditions are zero
Case2: Consider the initial conditions and .
(d) Develop Matlab codes for the above and compare with analytical results
Solution
(a) Impulse response using impz.m and filter.m functions
%%% Program for Impulse response of a system
%%% Developed by Dr. M. Venu Gopala Rao,
%%% Email: mvgr03@[Link], mvgr03@[Link],
%----------------------------------------------------------
% Demo1: Impulse response of the system
%%% Solution of linear constant co-efficient difference %
equations
2
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
% Solve y[n] - (3/2)y[n-1]+(1/2)y[n-2] = x[n], n >= 0
clear; close all; clc;
format long;
b = [1]; a = [1, -1.5 +0.5];
% Impulse response of the system using impz.m
N = 21; % Choose number of samples
n = [0:N-1];
h1 = impz(b,a,n);
%% define in put impulse sequence
s = [ones(1,1) zeros(1,N-1)];
% Impulse response of the system using filter.m
h2 = filter(b,a,s);
% Impulse response of the system by analytical solution
% h = 2u[n]-(1/2)^n u[n]
ha = 2*ones(size(n))-(1/2).^n;
figure();
subplot(4,1,1);stem(n,s,'r','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Input Impulse Sequence');grid on
axis([-2 21 -0.05 1.2]);
subplot(4,1,2);stem(n,h1,'r','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Impulse Response using impz.m');grid on
axis([-2 21 min(h1) max(h1)+0.25]);
subplot(4,1,3);stem(n,h2,'b','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Impulse Response using filter.m');grid on
axis([-2 21 min(h2) max(h2)+0.25]);
subplot(4,1,4);stem(n,ha,'k','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Impulse Response Analytical Solution');grid on
axis([-2 21 min(ha) max(ha)+0.25]);
3
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
% Stability Test
% T = sum(abs(h1))
% T = sum(abs(h2))
T = sum(abs(ha))
if (T < 1000)
disp (' Stable ')
else
disp (' Unstable ')
end
(b) Step Response of the system:
4
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
%%% Program for Step response of the system
%%% Developed by Dr. M. Venu Gopala Rao,
%%% Email: mvgr03@[Link], mvgr03@[Link],
%----------------------------------------------------------
% Demo: Step response
%%% Solution of linear constant co-efficient difference %
equations
% Solve y[n] - (3/2)y[n-1]+(1/2)y[n-2] = x[n], n >= 0
clear; close all; clc;
format long;
b = [1]; a = [1, -1.5 +0.5];
n = 0:20;
% Step response of the system
s = ones(1,21);
ys = filter(b,a,s);
% Impulse response of the system by analytical solution
ya = (1/2).^n + 2*n.*ones(size(n));
figure();
subplot(3,1,1);stem(n,s,'b','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Step Sequence s[n]');grid on
axis([-2 21 -0.1 1.2]);
subplot(3,1,2);stem(n,ys,'r','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Step Response ys[n]');grid on
5
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
axis([-2 21 min(ys) max(ys)+1]);
subplot(3,1,3);stem(n,ya,'k','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Step Response ya[n]');grid on
axis([-2 21 min(ya) max(ya)+1]);
(c) Case1: Analytical solution for the response of system with zero initial conditions for
an input sequence
6
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
%%% Program for Solution of linear constant co-efficient
% difference equationswith zero initial conditions
%%% Developed by Dr. M. Venu Gopala Rao,
%%% Email: mvgr03@[Link], mvgr03@[Link],
%----------------------------------------------------------
% Solve y[n] - (3/2)y[n-1]+(1/2)y[n-2] = x[n], n >= 0
% where x[n] = (1/4)^n u[n], with zero initial conditions
clear; close all; clc;
format long;
b = [1]; a = [1, -1.5 +0.5];
n = 0:20;
% Solution of LCCDE for an input sequence x = (1/4).^n with
%zero initial conditions.
x = (1/4).^n ;
y = filter(b,a,x);
figure();
subplot(3,1,1);stem(n,x,'b','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Input Sequence');grid on
axis([-1 21 -0.1 1.2]);
subplot(3,1,2);stem(n,y,'m','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Response of the System');grid on
7
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
axis([-1 21 min(y) max(y)+0.5]);
%%% Manual solution
% y[n] = (1/3)*(1/4).^n -2*(1/2).^n+(8/3)*ones(size(n));
y1 = (1/3)*(1/4).^n - 2*(1/2).^n+(8/3)*ones(size(n));
subplot(3,1,3);stem(n,y1,'b','fill','LineWidth', 1.5);
axis([-1 21 min(y1) max(y1)+0.5]);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Response of the System (Analytical)');grid on
Case2: Analytical solution for the response of system with initial conditions
and , for an input sequence
% Program for Solution of linear constant co-efficient
% difference equations with initial conditions
%%% Developed by Dr. M. Venu Gopala Rao,
8
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
%%% Email: mvgr03@[Link], mvgr03@[Link].
%%% Here the function filter(b,a,x,xic) is used, where xic %
%% is an equivalent initial condition input array
clear; close all; clc;
format long;
n = [0:20]; x = (1/4).^n ; xic = [1, -2];
b = [1]; a = [1, -1.5 +0.5];
y1 = filter(b,a,x,xic);
figure();
subplot(3,1,1);stem(n,x,'b','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Input Sequence');grid on
axis([-1 21 -0.1 1.2]);
subplot(3,1,2);stem(n,y1,'r','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Response of the system');grid on
axis([-2 22 min(y1) max(y1)+0.25]);
%%% Manual solution
y = (1/3)*(1/4).^n+(1/2).^n+(2/3)*ones(1,21);
subplot(3,1,3);stem(n,y,'b','fill','LineWidth', 1.5);
axis([-2 22 min(y) max(y)+0.25]);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Response of the System (Analytical)');grid on
9
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
Laboratory Exercise - 4
Exercise 1: Consider a LCCDE described by the equation
.
(a) Determine analytically Impulse Response of the system. Using this impulse response
test whether this system is stable or not.
Code:
clear; close all; clc;
format long;
b = [0.45 0.4]; a = [1, -0.4 -0.45];
% Impulse response of the system using impz.m
N = 21; % Choose number of samples
n = [0:N-1];
h1 = impz(b,a,n);
%% define in put impulse sequence
s = [ones(1,1) zeros(1,N-1)];
% Impulse response of the system using filter.m
h2 = filter(b,a,s);
% Impulse response of the system by analytical solution
10
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
% h = 2u[n]-(1/2)^n u[n]
figure();
subplot(4,1,1);stem(n,s,'r','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Input Impulse Sequence');grid on
axis([-2 21 -0.05 1.2]);
subplot(4,1,2);stem(n,h1,'r','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Impulse Response using impz.m');grid on
axis([-2 21 min(h1) max(h1)+0.25]);
subplot(4,1,3);stem(n,h2,'b','fill','LineWidth', 1.5);
xlabel('Time-index n---->');ylabel('Amplitude');
title('Impulse Response using filter.m');grid on
axis([-2 21 min(h2) max(h2)+0.25]);
% Stability Test
% T = sum(abs(h1))
% T = sum(abs(h2))
T = sum(abs(ha))
if (T < 1000)
disp (' Stable ')
else
disp (' Unstable ')
end
RESULT:
11
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
(b) Determine analytically Step Response of the system
Code:
clear all;
close all;
clc;
format long;
b=[0.45 0.4];
a=[1,-0.4,-0.45];
n=[0:20];
x=2+(1/2).^n;
y=filter(b,a,x);
figure();
stem(n,y);
xlable('Time-index n------>');
ylable('Amplitude');
title('Impulse Response using filter.m');
grid on;
RESULT:
(c) Find the response of the system for an input sequence
Case1: Assume that the initial conditions are zero
Case2: Consider the initial conditions
12
Digital Signal Processing Lab, Dept. of ECE, KL University, A.P., India. Id. No: 2000040230
(d) Develop Matlab codes for the above and compare with analytical results
Exercise 2: A linear and time-invariant system is described by the difference equation
(a) Using the impz.m and filter functions, compute and plot the impulse response of the
system over .
(b) Determine the stability of the system from this impulse response.
(c) If the input to this system is , determine
the response of the system over using the filter function.
(d) Develop the Matlab codes for the above tasks and plot the results.
13