Contents

% simulation of 2nd order filter in state-space representation with
% respect to direct form II / normal form with l2 scaling of the
% system and the states
% lab4_5.m * mw * 02/21/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

K2 = [1+a(3) -a(2); -a(2) 1+a(3)]/((1-a(3))*(1-a(2)+a(3))*(1+a(2)+a(3)));
T2 = [sqrt(K2(1,1)) 0; 0 sqrt(K2(2,2))];
ssr = ssr_trans(ssr,T2);

Simulation parameter

h2d = [40,-1,1,40,-1,1];      % 2d histrogram
V  = [1e-4 1e-3 1e-2 1e-1];   % contiour lines
N  = 1e6;                     % number of simulation cycles = N + 1
rand('state',0);              % return generator to its default initial state
x = 2*rand(1,N)-1;            % input signal (impulse)
x = x*sqrt(3)/5;              % crest factor 5

Filtering

s = zeros(2,length(x)+1); % state variables
y = zeros(1,length(x));   % output signal
for n=1:length(x)
     y(n)     = ssr.C*s(:,n) + ssr.D*x(n); % compute output signal
     s(:,n+1) = ssr.A*s(:,n) + ssr.B*x(n); % update state variables
end
% mean and variance
mean_s1 = mean(s(1,:)); var_s1 = var(s(1,:));
mean_s2 = mean(s(2,:)); var_s2 = var(s(2,:));
min1 = min(s(1,:)); max1 = max(s(1,:));
min2 = min(s(2,:)); max2 = max(s(2,:));
fprintf('lab4_5\n')
fprintf('state-space representation - %s\n',ssr.form)
fprintf('mean(s1) = %g : var(s1) = %g\n',mean_s1,var_s1)
fprintf('mean(s2) = %g : var(s2) = %g\n',mean_s2,var_s2)
fprintf('min(s1)  = %g : max(s1) = %g\n',min1,max1)
fprintf('min(s2)  = %g : max(s2) = %g\n',min2,max2)
% graphics
% 2d histogramm
[c1,c2,f2d] = hist2D(s(1,:),s(2,:),h2d);
FIG1 = figure('Name',['lab4_5 : bidim. pdf of state variables - ',ssr.form],'NumberTitle',...
  'off','Units','normal','Position',[.4 .3 .45 .55]);
delta1 = (h2d(3)-h2d(2))/h2d(1); delta2 =(h2d(6)-h2d(5))/h2d(4);
f2d = f2d/(N*delta1*delta2); % pdf
surfl(c1,c2,f2d)
xlabel('s_1 \rightarrow'), ylabel('\leftarrow s_2')
zlabel('f(s_1,s_2) \rightarrow')
title('2D histogram of state variables')
% contour lines
FIG2 = figure('Name',['lab4_5 : contour lines of bidim. pdf - ',ssr.form],...
  'NumberTitle','off','Units','normal','Position',[.4 .27 .45 .55]);
[CS,CH] = contour(c1,c2,f2d,V); grid % contour lines
clabel(CS,CH,V); axis('square');
xlabel('s_1 \rightarrow'), ylabel('s_2 \rightarrow')
title('Contour lines of 2D histogram')
% scatter plot
h = [100 h2d(2) h2d(3) 100 h2d(5) h2d(6)];
[c1,c2,f2d] = hist2D(s(1,:),s(2,:),h);
s_sc = [];
for n=1:100
    for m=1:100
        if f2d(n,m)>0
          si = [c1(n); c2(m)];
          s_sc = [s_sc si];
        end
    end
end
FIG3 = figure('Name',['lab4_5 : state variables - ',ssr.form],...
    'NumberTitle','off','Units','normal','Position',[.4 .24 .45 .55]);
plot(s_sc(1,:),s_sc(2,:),'.'),grid
axis([h2d(2) h2d(3) h2d(5) h2d(6)],'square');
ylabel('s_2 \rightarrow'), xlabel('s_1 \rightarrow')
title('Scatter plot of state variables')
lab4_5
state-space representation - DF2_T
mean(s1) = 0.000313248 : var(s1) = 0.0400357
mean(s2) = 0.000313173 : var(s2) = 0.0400357
min(s1)  = -0.731114 : max(s1) = 0.758897
min(s2)  = -0.731114 : max(s2) = 0.758897