Contents

% simulation of 2nd order filter in state-space representation
% in direct form II with l2 scaling of states
% lab5_1.m * mw * 02/22/2007

2nd order system

b = [1 2 1]; a = [1 -1.4 .74];    % numerator and denominator
h = filter(b,a,[1 zeros(1,100)]); % impulse response
b = b / sqrt(sum(h.^2));          % l2 scaling
ssr = ssr_ABCD(b,a,'DF2');        % direct form II

l2 scaling of states

if strcmp(ssr.form,'DF2')     % direct form II
    K = [1+a(3) -a(2); -a(2) 1+a(3)]/((1-a(3))*(1-a(2)+a(3))*(1+a(2)+a(3)));
elseif strcmp(ssr.form,'NF2') % normal form
    p = roots(a); z = p(1);   % poles
    K11 = 1/(1-abs(z)^2) - real(1/(1-z^2)); K12 = - imag(1/(1-z^2));
    K22 = 1/(1-abs(z)^2) + real(1/(1-z^2));
    K = .5*[K11 K12; K12 K22];
else
    fprintf('lab5_1 : no match - ssr.form = %s\n',ssr.form)
end
T = [sqrt(K(1,1)) 0; 0 sqrt(K(2,2))]; T_1 = inv(T);
ssr.A = T_1*ssr.A*T; ssr.B = T_1*ssr(1).B; ssr.C = ssr.C*T;

Quantization

w = 8;                       % word length
ssr = ssr_quant2c(ssr,w);    % quantized coeffizients

Simulation parameter

N  = 1e6;                     % number of simulation cycles = N + 1
rand('state',0);
x = 2*rand(1,N)-1;            % input signal
x = x*sqrt(3)/5;              % crest factor 5
% x=[1 zeros(1,200)];         % impulse
xq = quant2c(x,w,'r');        % quantization (rounding)

Filtering

[y,S]       = ssr_filter(ssr,xq);     % ideal filter
[yq,Sq,OC]  = ssr_filter2c(ssr,xq,w); % quantized filter (rounding)
fprintf('lab5_1 (%s)\n',ssr.form)
fprintf('Wordlength : %g bit\n',w)
fprintf('Overflow counter : %g\n',OC)
Ni = var(y-yq); % variance of inner noise
fprintf('Variance %g dB\n',10*log10(Ni))
lab5_1 (DF2q)
Wordlength : 8 bit
Overflow counter : 0
Variance -44.2791 dB

Graphics - Output signal

FIG = figure('Name',['lab5_1 : Output signal (',ssr.form,')'],...
  'NumberTitle','off','Units','normal','Position',[.3 .3 .65 .55]);
plot(0:200,y(1:201),':',0:200,y(1:201),'+',0:200,yq(1:201),'o'), grid
axis([0 200 -.6 .6]);
xlabel('n \rightarrow'), ylabel('y[n], y_q[n] \rightarrow')
legend('MATLAB','MATLAB',['quantized, w = ',num2str(w),' bit'],'Location','Best')
title(['Output signal (',ssr.form,')'])