Exp-2 - Difference Equation With Inbuilt Function - Filter
Exp-2 - Difference Equation With Inbuilt Function - Filter
Experiment #2: Find the response of a digital filter for a given input signal.
(Use “filter” command)
MATLAB code:
%Expt-2_ MATLAB program for solving difference equation using inbuilt function "filter"
clc; clear all; close all;
%% Solve a difference equation for given initial conditions using "filter" command
b=input('Enter the coefficients of x: ');
a=input('Enter the coefficients of y: ');
xinput=input('Enter the input x: ');
L=input('Enter the number of samples for y: ');
M=length(b)-1;
N=length(a)-1; % N is the order of the system
IC=input('Enter the initial conditions for y: '); %no. of ICs is equal to the order of the system
n=-N:L;%number of terms
xin=[xinput zeros(1,(L+1-length(xinput)))];
x=[zeros(1,N) xin];
ic=filtic(b,a,flip(IC));
%filtic finds the initial conditions for the delays in the transposed direct-form II filter
implementation given past outputs y and inputs x.
youtput=filter(b, a, xin, ic);
y=[IC youtput];
figure;
subplot(211);
stem(n,x);
title('input sequence x[n]');
xlabel('n');
ylabel('x[n]');
subplot(212);
stem(n,y);
title('output sequence y[n]');
xlabel('n');
ylabel('y[n]');
disp('y[n]=');
disp(y)
Program results:
The results have been obtained for the same difference equation:
y(n)-0.25y(n-1)-0.125y(n-2) = x(n)+0.5x(n-1)
x(n) = δ(n)
y(-1) = 0 and y(-2) = 0