-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuildPMatrix.m
132 lines (128 loc) · 4.42 KB
/
buildPMatrix.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
% Function to build the P matrix for the FROLS identification process
%
% written by: Renato Naville Watanabe
%
% [p, D] = buildPMatrix(u, y, degree, mu, my, delay)
%
% Inputs:
%
% u: vector of floats, input signal.
%
% y: vector of floats, output signal.
%
% degree: integer, maximal polynomial degree that you want the FROLS method to look for (it has been tested until the
% 9th degree).
%
% mu: integer, the maximal lag of the input signal.
%
% my: integer, the maximal lag of the output signal.
%
% delay: integer, how much lags you want to not consider in the input terms. It comes from a previous knowledge of your system
%
%
% Outputs:
%
% p: matrix of floats, the P matrix to be used in the identification process by the FROLS algorithm.
%
% D: cell, contains the strings with the candidate terms. Each element of D corresponds to a column of the P matrix.
function [p, D] = buildPMatrix(u, y, degree, mu, my, delay)
N=length(u);
p=zeros(N-max(mu,my), round(findPMatrixSize(mu - delay + 1, my, degree)));
%% build p Matrix
for k=1:N-max(mu, my)
xb = [u(k + max(mu,my) - delay:-1:k + max(mu,my) - mu)' y(k + max(mu,my) - 1:-1:k + max(mu,my) - my)'];
j=1;
for l=0:degree
if (l==0)
p(k, j)=1;
m=1;
len(l+1)=1;
else if (l==1)
p(k,j:j+(mu - delay + 1) + my - 1) = xb;
m=length(xb);
len(l+1)=m;
else
j1=j;
if (l==2)
subFactor = 0;
subsubFactor = 0;
else
subFactor = len(l-2);
if (l==3)
subsubFactor = 0;
else
subsubFactor = len(l-3);
end
end
numberFactor = len(l);
subSum = 0;
for i=1:length(xb)
p(k, j1:j1 + numberFactor - 1) = ...
kron(xb(i), p(k, j-numberFactor:j-1));
j1 = j1+numberFactor;
if (i >= 2)
subSum = subSum + subFactor - subsubFactor*(i-2);
end
numberFactor = numberFactor - len(l-1) + subSum;
end
len(l+1) = j1 - j;
end
end
j = j + len(l+1);
end
end
%% Build D dictionary
j=1;
for l=0:degree
if (l==0)
D{j}='1';
len(l+1)=1;
else if (l==1)
m = 1;
for i=delay:mu
D{j+m-1} = ['u(k-' num2str(i) ')'];
Db{m} = D{j+m-1};
m = m + 1;
end
for i = 1:my
D{j+m-1} = ['y(k-' num2str(i) ')'];
Db{m} = D{j+m-1};
m = m + 1;
end
len(l+1) = length(Db);
else
j1 = j;
if (l==2)
subFactor = 0;
subsubFactor = 0;
else
subFactor = len(l-2);
if (l==3)
subsubFactor = 0;
else
subsubFactor = len(l-3);
end
end
numberFactor = len(l);
subSum = 0;
for i=1:length(Db)
for q=j1:j1+numberFactor-1
D{q} = [Db{i} D{j-numberFactor+q-j1}];
end
j1=j1+numberFactor;
if (i>=2)
% subVector = 0;
% for n = 2:l-2
% subVector = subVector + (max((i-n)*(1+1*(n-1))/2, 0)*((-1)^(n - 1)))*len(l - (n + 1));
% end
% subSum = subSum + subFactor + subVector;
subSum = subSum + subFactor - subsubFactor*(i-2);
end
numberFactor = numberFactor - len(l-1) + subSum;
end
len(l+1)=j1-j;
end
end
j=j+len(l+1);
end
end