To implement a causal IIR filter implemented in the Direct Form II structure, the function
direct2 given below can be employed.
function [y,sf] = direct2(p,d,x,si);
% Y = DIRECT2(P,D,X) filters input data vector X with
% the filter described by vectors P and D to create the
% filtered data Y. The filter is a "Direct Form II"
% implementation of the difference equation:
% y(n) = p(1)*x(n) + p(2)*x(n-1) + ... + p(np+1)*x(n-np)
% - d(2)*y(n-1) - ... - d(nd+1)*y(n-nd)
% [Y,SF] = DIRECT2(P,D,X,SI) gives access to initial and
% final conditions, SI and SF, of the delays.
dlen = length(d); plen = length(p);
N = max(dlen,plen); M = length(x);
sf = zeros(1,N-1); y = zeros(1,M);
if nargin ~= 3,
sf = si;
end
if dlen <>
d = [d zeros(1,plen - dlen)];
else
p = [p zeros(1, dlen - plen)];
end
p = p/d(1); d = d/d(1);
for n = 1:M;
wnew = [1 -d(2:N)]*[x(n) sf]’;
K = [wnew sf];
y(n) = K*p’;
sf = [wnew sf(1:N-2)];
end
The structure being simulated can be verified in MATLAB by computing its transfer function with the aid of the function strucver given below.
function [p,d] = strucver(ir,N);
H = zeros(2*N+1,N+1);
H(:,1) = ir’;
for n = 2:N+1;
H(:,n) = [zeros(1,n-1) ir(1:2*(N+1)-n)]’;
end
H1 = zeros(N+1,N+1);
for k = 1:N+1;
H1(k,:) = H(k,:);
end
H3 = zeros(N,N+1);
for k = 1:N;
H3(k,:) = H(k+N+1,:);
end
H2 = H3(:,2:N+1);
hf = H3(:,1);
% Compute the denominator coefficients
d = -(inv(H2))*hf;
% Compute the numerator coefficients
p = H1*[1;d];
d = [1; d];
Program to illustrate the design of a causal IIR filter and its simulation in Direct Form
II. It employs the functions strucver and direct2 described earlier.
% Program
Wp = [0.4 0.5]; Ws = [0.1 0.8]; Rp = 1; Rs = 30;
[N1, Wn1] = buttord(Wp, Ws, Rp, Rs);
[num,den] = butter(N1,Wn1);
disp(’Numerator coefficients are ’);disp(num);
disp(’Denominator coefficients are ’);disp(den);
impres = direct2(num,den,[1 zeros(1,4*N1)]);
[p,d] = strucver(impres,2*N1);
disp(’Actual numerator coeffs are ’); disp(p’);
disp(’Actual denominator coeffs are ’); disp(d’);