You are on page 1of 2

ADSP Assignment # 2

Sumaiyya Farooq
6557 (MS-81)
Implementation of a System using Difference Equation
function [ Y, x, y ] = IIR_filter ( a, b, x, y, x_in )
%
%
%
%
%

b = numerator coefficients
a = denominator coefficients
x_in = input sample
Y = output sample
x,y = input & output buffers

N_b = length(b);
N_a = length(a);
% shift right input buffer
for n = N_b-1:-1:1
x(n+1) = x(n);
end
% new sample
x(1) = x_in;
% convolution of b(n) and x(n)
y1 = 0;
for k=1:1:N_b
y1 = y1 + b(k)*x(k);
end
% convolution of a(n) and y(n)
y2 = 0;
for k=1:1:N_a
y2 = y2 + a(k)*y(k);
end
% shift right output buffer
for n = N_a-1:-1:1
y(n+1) = y(n);
end
y(1) = y1+y2;
Y = y(1);
end

Main Method
clc
clear
% b = numerator coefficients
% a = denominator coefficients
a = [0.3 0.4];
b = [2 4 6 5];
% an arbitrary signal
x_in = 2:2:10;
x_in = [x_in, zeros(1,5)];
x = zeros(1,length(b));
y = zeros(1,length(a));
% output
output = zeros(1,length(x_in));
for i = 1:length(x_in)
[ Y, x, y ] = IIR_filter ( a, b, x, y, x_in(i));
output(i) = Y;
end
% MATLAB filter command
a = [1 -1*a];
Y = filter(b,a,x_in);
error = output Y
ERROR
error =
1.0e-013 *
0

0.1421

You might also like