0% found this document useful (0 votes)
3 views3 pages

Exp-2 - Difference Equation With Inbuilt Function - Filter

This lab report details Experiment #2, which involves finding the response of a digital filter using MATLAB's 'filter' command. The experiment includes solving a difference equation with specified initial conditions and obtaining the impulse response of the system. Results are presented for both the difference equation and impulse response, demonstrating the output sequences for given input signals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Exp-2 - Difference Equation With Inbuilt Function - Filter

This lab report details Experiment #2, which involves finding the response of a digital filter using MATLAB's 'filter' command. The experiment includes solving a difference equation with specified initial conditions and obtaining the impulse response of the system. Results are presented for both the difference equation and impulse response, demonstrating the output sequences for given input signals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Digital Signal Processing Lab report

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)

%% Obtain impulse response of a system from the difference equation


% Impulse response can be obtained in the same way putting x(n)= delta(n) and all ICs = 0
xinput=[1];
IC=[0 0];
xin=[xinput zeros(1,(L+1-length(xinput)))];
x=[zeros(1,N) xin];
youtput=filter(b, a, xin, IC);
%Since ICs=0, we can also write youtput=filter(b,a,xin)
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('impulse response h[n]');
xlabel('n');
ylabel('y[n]');
disp('y[n]=');
disp(y)

Program results:

(i) Solving a difference equation with given initial conditions.


The results have been obtained for the following difference equation:
y(n)-0.25y(n-1)-0.125y(n-2) = x(n)+0.5x(n-1)
x(n)=[1 1 -1]
y(-1) = 1 and y(-2) = 2 (enter the values as [2 1])
No. of samples for y = 20
(ii) Obtaining impulse response:

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

You might also like