-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuildPNoiseMatrix.m
150 lines (143 loc) · 5.38 KB
/
buildPNoiseMatrix.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
% Function to build the P matrix for the FROLS identification process of the residue from the system identification
% of the signals
%
% written by: Renato Naville Watanabe
%
% [p, D] = buildPNoiseMatrix(u, y, e, degree, mu, my, me, delay)
%
%
% Inputs:
%
% u: vector of floats, input signal.
%
% y: vector of floats, output signal.
%
% e: vector of floats, residue 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.
%
% me: integer, the maximal lag of the residue 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 matrix used in the identification process of the residue of the system identification by the FROLS algorithm.
%
% D: cell, contains the strings with candidate terms. Each element of D corresponds to a column of the P matrix.
function [p, D] = buildPNoiseMatrix(u, y, e, degree, mu, my, me, delay)
N=length(u);
%p=zeros(N-max([mu my me]), round(findPMatrixSize((mu), my+me, degree)-findPMatrixSize((mu), my, degree)));
%% build p Matrix
for k=1:N-max([mu my me])
xb = [u(k + max([mu my me]) - delay:-1:k + max([mu my me]) - mu)' ...
y(k + max([mu my me]) - 1:-1:k + max([mu my me]) - my)'...
e(k + max([mu my me]) - 1:-1:k + max([mu my me]) - me)'];
eb = e(k + max([mu my me]) - 1:-1:k + max([mu my me]) - me)';
j=1;
for l=1:degree
if (l==1)
p(k,j:j+me-1) = eb;
m=length(eb);
len(l)=m;
else if (l==2)
j1=j;
for i=1:length(xb)
p(k, j1:j1+length(eb)-max(0, i - (mu -delay + 1) - my - 1)-1) = ...
kron(xb(i), eb(max(0, i - (mu -delay + 1) - my - 1)+1:end));
j1=j1+length(eb)-max(0, i - (mu -delay + 1) - my - 1);
end
len(l)=j1-j;
else
j1=j;
if (l==3)
subFactor = 0;
subsubFactor = 0;
else
subFactor = len(l-3);
if (l==4)
subsubFactor = 0;
else
subsubFactor = len(l-4);
end
end
numberFactor = len(l-1);
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-2) + subSum;
end
len(l) = j1 - j;
end
end
j=j+len(l);
end
end
%% Build D dictionary
j=1;
for l=1:degree
if (l==1)
for i=delay:mu+my+me
if (i<=mu)
Db{i-delay + 1} = ['u(k-' num2str(i) ')'];
else if (i<=mu+my)
Db{i - delay + 1} = ['y(k-' num2str(i-mu) ')'];
else
Db{i - delay + 1} = ['e(k-' num2str(i-mu-my) ')'];
Deb{i-mu-my} = ['e(k-' num2str(i-mu-my) ')'];
D{j+i-mu-my-1} = ['e(k-' num2str(i-mu-my) ')'];
end
end
end
len(l) = length(Deb);
else if (l==2)
j1=j;
for i=1:length(Db)
for q = j1:j1+length(Deb)-max(0, i - (mu - (delay - 1)) - my - 1)-1
D{q} = [Db{i} Deb{max(0, i - (mu - (delay - 1)) - my - 1)+q-j1+1}];
end
j1=j1+length(Deb)-max(0, i - (mu - (delay - 1)) - my - 1);
end
len(l)=j1-j;
else
j1=j;
if (l==3)
subFactor = 0;
subsubFactor = 0;
else
subFactor = len(l-3);
if (l==4)
subsubFactor = 0;
else
subsubFactor = len(l-4);
end
end
numberFactor = len(l-1);
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)
subSum = subSum + subFactor - subsubFactor*(i-2);
end
numberFactor = numberFactor - len(l-2) + subSum;
end
len(l) = j1 - j;
end
end
j=j+len(l);
end
end