num = [0 1 1];
den86 = [1 -1.4 0.48]
roots(den86)
den75 = [1 -1.2 .35];
roots(den75)

[Dado1,RPos] = LeDadoIt (5);
[Dado2,RPos] = LeDadoIt (15);

Dado1(2,:) = dimpulse(num, den75,50)';
Dado2(2,:) = dimpulse(num, den86, 50)';
%c = a+b;

%y12 = [Dado1(2,:) Dado2(2,:)];
n = 50;%size(y12',1);

MatrizAD1 = diag(ones(n-2,1),-1);
MatrizBD1 = zeros(n-1,1); MatrizBD1(1) = 1;
MatrizCD1 = Dado1(2,2:n);

MatrizAD2 = diag(ones(n-2,1),-1);
MatrizBD2 = zeros(n-1,1); MatrizBD2(1) = 1;
MatrizCD2 = Dado2(2,2:n);

MatrizA = [MatrizAD1 zeros(n-1); zeros(n-1) MatrizAD2];
MatrizB = [MatrizBD1; MatrizBD2];
MatrizC = [MatrizCD1 MatrizCD2];

Wc = ctrb(MatrizA,MatrizB);
Wo = obsv(MatrizA, MatrizC);
H12 = Wo*Wc;
[u,s,v] = svd(H12);

k =4;

Ak = v(2:2*n-2,1:k)'*v(1:2*n-3,1:k);
Bk = v(1,1:k)';
Ck = MatrizC*v(1:2*n-2,1:k);


Wck = ctrb(Ak,Bk);
Wok = obsv(Ak,Ck);
Hk = Wok*Wck;
[uk,sk,vk] = svd(Hk);

singk12 = diag(sk);
singk12(1:k)'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y1M2 = Dado1(2,:) +  Dado2(2,:);
n = size(y1M2',1);

MatrizAM = diag(ones(n-2,1),-1);
MatrizBM = zeros(n-1,1); MatrizBM(1) = 1;
MatrizCM = 0.6*Dado1(2,2:n) + Dado2(2,2:n);

WcM = ctrb(MatrizAM,MatrizBM);
WoM = obsv(MatrizAM, MatrizCM);
H12M = WoM*WcM;
[uM,sM,vM] = svd(H12M);


AkM = vM(2:n-2,1:k)'*vM(1:n-3,1:k);
BkM = vM(1,1:k)';
CkM = MatrizCM*vM(1:n-1,1:k);


WckM = ctrb(AkM,BkM);
WokM = obsv(AkM,CkM);
HkM = WokM*WckM;
[ukM,skM,vkM] = svd(HkM);

singk12M = diag(skM);
singk12M(1:k)'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

[atf86,btf86,ctf86,dtf86] = tf2ss(num,den86);
[atf75,btf75,ctf75,dtf75] = tf2ss(0.6*num,den75);
Ak8675 = [atf86 zeros(2); zeros(2) atf75];
Bk8675 = [btf86;btf75];
ck8675 = [ctf86 ctf75];
svd(testeHW(Ak8675,Bk8675,ck8675))'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555

[Num2, Den2] = ss2tf(Ak, Bk, Ck, 0,1)