-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvectorFitting.m
196 lines (179 loc) · 5.62 KB
/
vectorFitting.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function model = vectorFitting(s, fVals, order, nIters, isPeaks, ...
poleDistribution)
%% vectorFitting searches for the VF model
% This function searches for the VF model based on complex frequency,
% complex function samples and fully specified settings.
%
% INPUTS
% s: complex frequency, double [1 x nS]
% fun: function samples, double [1 x nS]
% order: approximation order (number of poles), double [1 x 1]
% nIters: number of iterations per VF run, double [1 x 1]
% isPeaks: are peaks in the function, logical [1 x 1]
% poleDistribution: ioit pole distribution ('lin'/'log'), char [1 x 3]
%
% OUTPUTS
% model: VF model, struct
% .poles: complex poles, double [nP x 1]
% .residues: complex rsidues, double [nP x 1]
% .d: VF coeff., double [1 x 1]
% .d: VF coeff., double [1 x 1]
%
% SYNTAX
%
% model = vectorFitting(s, fun, order, nIters, isPeaks, poleDistribution)
%
% Function vectorFitting searches for the VF model.
%
% © 2019, Petr Kadlec, BUT, kadlecp@feec.vutbr.cz
model.poles = [];
model.residues = [];
model.d = [];
model.e = [];
if nargin < 6
poleDistribution = 'logarithmic';
if nargin < 5
isPeaks = true;
if nargin < 4
nIters = 1;
end
end
end
% take only even number of poles
order = floor(order/2)*2;
[nS, c] = size(s);
if nS == 1
s = s.';
nS = c;
end
nF = size(fVals, 1);
if nF == 1
fVals = fVals.';
end
%% poles
% get initial poles
[realPoles, complexPoles] = createPoles(s, order, isPeaks, poleDistribution);
for iIter = 1:nIters
if isempty(realPoles)
realRatios = [];
else
realRatios = 1./(s - realPoles);
end
oddComplexPoles = complexPoles(1:2:end);
temp1 = [...
1./(s - oddComplexPoles) + 1./(s - (oddComplexPoles').'), ...
1j./(s - oddComplexPoles) - 1j./(s - (oddComplexPoles').')];
complexRatios = temp1;
complexRatios(:, 1:2:end) = temp1(:, 1:end/2);
complexRatios(:, 2:2:end) = temp1(:, end/2+1:end);
if isempty(realPoles)
A = [complexRatios, ...
ones(nS,1), ...
s, ...
-repmat(fVals, 1, size(complexRatios, 2)).*complexRatios];
else
A = [realRatios, ...
complexRatios, ...
ones(nS,1), ...
s, ...
-repmat(fVals, 1, size(realRatios, 2)).*realRatios, ...
-repmat(fVals, 1, size(complexRatios, 2)).*complexRatios];
end
%% solve least squares problem
scale = norm(fVals);
scale = scale/nS;
lastRowA = [zeros(1, order+2), real(scale*sum(A(:, 1:order)))];
A = [real(A), -real(fVals); imag(A), -imag(fVals); lastRowA, scale*nS];
[Q, R] = qr(A,0);
b = Q(end, order + 3:end)*nS*scale;
Rpart = R(order + 3:end, order + 3:end);
nR = size(Rpart, 2);
eScale = zeros(1, nR);
for iC = 1:nR
eScale(iC) = 1/norm(Rpart(:,iC));
end
Rpart = Rpart.*repmat(eScale, order + 1, 1);
x = Rpart\b';
x = x.*eScale';
C = x(1:end-1);
d = x(end);
% make C complex again
nR = size(realPoles, 2);
nC = size(complexPoles,2);
cReal = C(nR+1:2:end).';
cImag = C(nR+2:2:end).';
C = [C(1:nR); reshape([cReal + 1j*cImag; cReal - 1j*cImag], [], 1)];
%% zeros
A = diag(realPoles);
for iC = 1:nC/2
curP = complexPoles((iC-1)*2+1);
curC = C(nR + (iC - 1)*2 + 1);
A = blkdiag(A, [real(curP), imag(curP); ...
-imag(curP), real(curP)]);
C(nR + (iC-1)*2 + 1) = real(curC);
C(nR + (iC-1)*2 + 2) = imag(curC);
end
b = [ones(nR, 1); repmat([2; 0], nC/2, 1)];
if abs(d) < 1e-8 % check for sovability
d = 1e-8*d/abs(d);
end
vals = eig(A - b*C.'/d);
% make all zeros stable
notStable = real(vals) > 0;
vals(notStable) = vals(notStable) - 2*real(vals(notStable));
% sort so that real come first
vals = sort(vals);
isReal = abs(imag(vals)) <= 1e-14;
realPoles = real(vals(isReal))';
complexPoles = vals(~isReal).';
complexPoles = complexPoles - 1i*2*imag(complexPoles);
vals = [realPoles, complexPoles].';
%% save results
model.poles = vals;
if iIter == nIters
% sort poles so that [-a - 1j*b, -a + 1j*b]
nR = numel(realPoles);
nC = order - nR;
isReal = false(1, order);
isReal(1:nR) = true;
realRatios = 1./(s - realPoles);
if nC > 0
% complex pole found
oddComplexPoles = complexPoles(1:2:end);
temp1 = [...
1./(s - oddComplexPoles) + 1./(s - (oddComplexPoles').'), ...
1j./(s - oddComplexPoles) - 1j./(s - (oddComplexPoles').')];
complexRatios = temp1;
complexRatios(:, 1:2:end) = temp1(:, 1:end/2);
complexRatios(:, 2:2:end) = temp1(:, end/2+1:end);
A = [realRatios, ...
complexRatios, ...
ones(nS,1), ...
s];
else
% no complex poles
A = [realRatios, ...
ones(nS,1), ...
s];
end
A = [real(A); imag(A)];
b = [real(fVals); imag(fVals)];
nA = size(A, 2);
eScale = zeros(1, nA);
for iC = 1:nA
eScale(iC) = norm(A(:,iC));
end
A = A./repmat(eScale, 2*nS, 1);
x = (A\b)./eScale';
cOut = x(1:order);
% make complex again
complexC = cOut(~isReal);
cReal = complexC(1:2:end).';
cImag = complexC(2:2:end).';
model.residues = [cOut(isReal); ...
reshape([cReal + 1j*cImag; cReal - 1j*cImag], [], 1)];
model.d = x(order + 1);
model.e = x(order + 2);
end
end
end