-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHo_Kalman.m
65 lines (56 loc) · 1.57 KB
/
Ho_Kalman.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function [A,B,C,D,sigma] = Ho_Kalman(G,T1,T2,n,m,p,flag)
% A naive implementation of the Ho-Kalman algorithm to find (A,B,C,D)
% Input:
% G: markov paramter
% T1,T2: dimension of Hankel matrix -- T = T1 + T2 + 1
% n,m,p: system dimension
% Output: State space realization A, B, C, D
if nargin <=6
flag = 0; % a naive adaptation to choose system order.
end
% step 1: Hankel matrix
H = zeros(p*T1,m*(T2+1)); % Hankel matrix
for i = 1:T1
for j = 1:T2+1
H((i-1)*p+1:i*p,(j-1)*m+1:j*m) = G(:,(i+j-1)*m+1:(i+j)*m);
end
end
% step 2: first mT2 columns of H
Hneg = H(:,1:m*T2);
% step 3: rank-n-approximation of hH
[U,S,V] = svd(Hneg);
hS = zeros(size(S));
if flag == 0
for i = 1:n
hS(i,i) = S(i,i);
end
L = U*hS*V';
else
totalSig = sum(diag(S));
tmp = 0;
for i = 1:min(size(S))
hS(i,i) = S(i,i);
tmp = tmp + hS(i,i);
if tmp/totalSig > 0.9
n = i; % system order
break;
end
end
L = U*hS*V';
end
% SVD decomposition
[U,Sig,V] = svd(L);
Sigma = Sig(1:n,1:n); %% only keep the non-zero elements
Uc = U(:,1:n);
Vc = V(:,1:n);
hO = Uc*Sigma^(0.5);
hQ = Sigma^(0.5)*Vc';
C = hO(1:p,:);
B = hQ(:,1:m);
Hplu = H(:,m+1:end);
A = pinv(hO)*Hplu*pinv(hQ);
%hOi = (hO'*hO)^(-1)*hO';
%hQi = hQ'*(hQ*hQ')^(-1); % psedoinverse
%A = hOi*Hplu*hQi;
D = G(:,1:m);
end