Professional Documents
Culture Documents
LAB # 09 Difference Equation
LAB # 09 Difference Equation
LAB # 09 Difference Equation
DIFFERENCE EQUATION
LAB TASK:
Write a MATLAB code to find the output of the given difference equations .Also
attach the output waveform
1) y(n) - 5/3y(n-1) + 7/4 y(n-2) = 2x(n)
Given x(n) = (1/6)n * u(n). Assume initial conditions as y(-1) = 2, y(-2)=5
Output:
clear all;
close all;
clc;
% accept difference equation coefficients from input
b=input('enter the coefficient of input x(n):');
a=input('enter the coefficient of output y(n):');
y=input('enter the initial condition y(-1)), y(-2),...:');
% calculate initial condition using filtic
z=filtic(b,a,y);
%enter input sequence samples.
x= [1 1/6 1/36 1/2165 1/1296 1/7776];
n= 0:length(x) -1;
%time base for plotting
%calculate output using initial conditions
Yout=filter(b,a,x,z);
%display output sequence
disp('difference equation solution : y(n) :');
disp(Yout);
%plot input and output
subplot(2,1,1);
stem(n,x);
title('input sequence x(n)');
xlabel('n'); ylabel('x(n)');
subplot(2,1,2);
stem(n,Yout);
title('output sequence y(n)');
xlabel('n'); ylabel('y(n)');
x(n)
0.5
0.5
1.5
0.5
1.5
30
2.5
3
n
output sequence y(n)
3.5
4.5
3.5
4.5
y(n)
20
10
0
-10
2.5
n
Output:
clear all;
close all;
clc;
% accept difference equation coefficients from input
b=input('enter the coefficient of input x(n):');
a=input('enter the coefficient of output y(n):');
y=input('enter the initial condition y(-1):');
% calculate initial condition using filtic
z=filtic(b,a,y);
%enter input sequence samples.
x= [1 1 1 1 1 1];
n= 0:length(x) -1;
%time base for plotting
%calculate output using initial conditions
Yout=filter(b,a,x,z);
%display output sequence
disp('difference equation solution : y(n) :');
disp(Yout);
%plot input and output
subplot(2,1,1);
stem(n,x);
title('input sequence x(n)');
xlabel('n'); ylabel('x(n)');
subplot(2,1,2);
stem(n,Yout);
title('output sequence y(n)');
xlabel('n'); ylabel('y(n)');
1.5000
1.8750
1.9375
1.9688
x(n)
1.7500
0.5
0.5
1.5
0.5
1.5
2.5
3
n
output sequence y(n)
3.5
4.5
3.5
4.5
y(n)
1.5
1
0.5
0
2.5
n
FILTIC:
filtic Make initial conditions for 'filter' function.
Z = filtic( B, A, Y, X ) converts past input X and output Y
into initial conditions for the state variables Z needed in the
TRANSPOSED DIRECT FORM II filter structure.
The vectors of past inputs & outputs are stored with more recent
values first.