-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRlcFit.pas
333 lines (266 loc) · 7.34 KB
/
RlcFit.pas
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//Copyright (c) 2014 Alex Shovkoplyas VE3NEA
unit RlcFit;
interface
uses
SysUtils, Classes, Math, ComplMath, VnaCli, IOUtils, SndTypes0,
VectFitt, Generics.Collections;
const
INFINITY: Single = 1e9;
MIN_R = 0.03;
MAX_R = 1E5;
type
TTank = class
C, G, L, R: Single;
constructor CreateLR;
constructor CreateCG;
constructor CreateOpen;
constructor CreateShort;
function IsShortCircuit: boolean;
function IsOpenCircuit: boolean;
end;
TTanks = TObjectList<TTank>;
TRlcFitter = class
private
Ft: TVectorFitter;
function GetWeight(Freq: Single): Single;
function ComputeRms(AFittedZ: TScanArray): Single;
procedure ComputeFittedData;
procedure ComputeLumpedElements;
public
Complexity, IterCount: integer;
Tanks: TTanks;
FittedZ: TScanArray;
constructor Create;
destructor Destroy; override;
procedure Fit(AData: TScanArray);
end;
implementation
{ TRlcFitter }
constructor TRlcFitter.Create;
begin
Ft := TVectorFitter.Create;
Tanks := TTanks.Create;
Complexity := 2;
IterCount := 6;
end;
destructor TRlcFitter.Destroy;
begin
Ft.Free;
Tanks.Free;
inherited;
end;
procedure TRlcFitter.Fit(AData: TScanArray);
var
i: integer;
Sum, Weight, Freq, Err: Double;
IdxD, IdxF: integer; //index of data point, index of basis fun
Coeff: TVector;
begin
try
FittedZ := nil;
SetLength(Ft.Freq, Length(AData));
SetLength(Ft.Weights, Length(AData));
SetLength(Ft.MeasuredZ, Length(AData));
for i:=0 to High(AData) do
begin
Ft.Freq[i] := AData[i].Freq;
Ft.MeasuredZ[i] := AData[i].Value;
Ft.Weights[i] := GetWeight(AData[i].Freq);
end;
Ft.InitializePoles(Complexity);
for i:=1 to IterCount do Ft.Iterate;
ComputeLumpedElements;
ComputeFittedData;
except
Tanks.Clear;
FittedZ := nil;
end;
end;
procedure TRlcFitter.ComputeLumpedElements;
var
p: integer;
MinS, MaxS: Single;
ZMin, ZMax: Single;
Tank: TTank;
begin
Tanks.Clear;
//R0, L0
Tank := TTank.CreateLR;
Tank.R := Ft.D;
Tank.L := Ft.E;
Tanks.Add(Tank);
//poles/residues to RLCG
for p:=0 to High(Ft.Poles) do
case Ft.PoleTypes[p] of
ptReal:
begin
Tank := TTank.CreateCG;
if Ft.Residues[p].Re = 0
then
begin Tank.C := INFINITY; Tank.G := INFINITY; end
else
begin
Tank.C := 1 / Ft.Residues[p].Re;
Tank.G := -Ft.Poles[p].Re / Ft.Residues[p].Re;
end;
Tanks.Add(Tank);
end;
ptComplex:
begin
Tank := TTank.Create;
if Ft.Residues[p].Re = 0
then
begin
Tank.C := INFINITY;
Tank.G := INFINITY;
end
else
begin
Tank.C := 1 / (2 * Ft.Residues[p].Re);
Tank.G := (-Ft.Poles[p].Re * Ft.Residues[p].Re + Ft.Poles[p].Im * Ft.Residues[p].Im)
/ (2 * Sqr(Ft.Residues[p].Re));
end;
if Ft.Residues[p].SqrMag * Sqr(Ft.Poles[p].Im) = 0
then
begin
Tank.L := INFINITY;
Tank.R := INFINITY;
end
else
begin
Tank.L := 2 * IntPower(Ft.Residues[p].Re, 3)
/ (Ft.Residues[p].SqrMag * Sqr(Ft.Poles[p].Im));
Tank.R:= -2 * Sqr(Ft.Residues[p].Re)
* (Ft.Poles[p].Re * Ft.Residues[p].Re + Ft.Poles[p].Im * Ft.Residues[p].Im)
/ (Ft.Residues[p].SqrMag * Sqr(Ft.Poles[p].Im));
end;
Tanks.Add(Tank);
end;
end;
//ignore too large and too small values
MinS := 2*Pi * Ft.Freq[0];
MaxS := 2*Pi * Ft.Freq[High(Ft.Freq)];
for p:=0 to Tanks.Count-1 do
with Tanks[p] do
begin
if C <> 0 then
begin
ZMax := 1 / (MinS * C); ZMin := 1 / (MaxS * C);
if Abs(ZMax) < MIN_R then C := INFINITY
else if Abs(ZMin) > MAX_R then C := 0
else if C < 0 then C := 0;
end;
if G <> 0 then
begin
ZMax := 1 / G; ZMin := 1 / G;
if Abs(ZMax) < MIN_R then G := INFINITY
else if Abs(ZMin) > MAX_R then G := 0
else if G < 0 then G := 0;
end;
ZMax := MaxS * L; ZMin := MinS * L;
if Abs(ZMax) < MIN_R then L := 0
else if Abs(ZMin) > MAX_R then L := INFINITY
else if L < 0 then L := 0;
if Abs(R) < MIN_R then R := 0
else if Abs(R) > MAX_R then R := INFINITY
else if R < 0 then R := 0;
//merge R and G
if (L = 0) and (R <> 0) and (R <> INFINITY) then
begin
G := G + 1/R;
R := INFINITY;
end
end;
//delete short circuit tanks
for p:=Tanks.Count-1 downto 0 do
if Tanks[p].IsShortCircuit then Tanks.Delete(p)
else if Tanks[p].IsOpenCircuit then
begin Tanks.Clear; Tanks.Add(TTank.CreateOpen); Break; end;
if Tanks.Count = 0 then Tanks.Add(TTank.CreateShort);
end;
procedure TRlcFitter.ComputeFittedData;
var
i, p: integer;
Y, Z: TComplex;
function s(idx: integer): TComplex;
begin Result := COMPL(0, 2*Pi* FittedZ[i].Freq); end;
begin
SetLength(FittedZ, Length(Ft.MeasuredZ));
for i:=0 to High(Ft.MeasuredZ) do
begin
FittedZ[i].Freq := Round(Ft.Freq[i]);
Z := 0;
for p:=0 to Tanks.Count-1 do
with Tanks[p] do
if not IsShortCircuit then
begin
Y := C * s(i) + G + 1 / (L * s(i) + R);
Z := Z + 1 / Y;
end;
FittedZ[i].Value := Z;
end;
end;
//empirical weight function, reduces the effect of imperfect hardware response
//below 1.5 MHz and above 58.5 MHz
function TRlcFitter.GetWeight(Freq: Single): Single;
var
x: Single;
begin
x := Freq * 1e-6;
if x < 2 then x := 2 - x
else if x > 58 then x := x - 58
else Exit(1);
Result := Exp(-(Sqr(Sqr(x))));
end;
function TRlcFitter.ComputeRms(AFittedZ: TScanArray): Single;
var
i: integer;
W: Single;
begin
Result := 0; W := 0;
for i:=0 to High(AFittedZ) do
begin
Result := Result + (Ft.Weights[i] * (AFittedZ[i].Value - Ft.MeasuredZ[i])).SqrMag;
W := W + Ft.Weights[i];
end;
Result := Sqrt(Result / W);
end;
//------------------------------------------------------------------------------
// TTank
//------------------------------------------------------------------------------
constructor TTank.CreateLR;
begin
C := 0;
G := 0;
end;
constructor TTank.CreateCG;
begin
L := INFINITY;
R := INFINITY;
end;
constructor TTank.CreateShort;
begin
L := 0;
R := 0;
C := INFINITY;
G := INFINITY;
end;
constructor TTank.CreateOpen;
begin
L := INFINITY;
R := INFINITY;
C := 0;
G := 0;
end;
function TTank.IsOpenCircuit: boolean;
begin
Result := ((L = INFINITY) or (R = INFINITY)) and (C = 0) and (G = 0);
end;
function TTank.IsShortCircuit: boolean;
begin
Result := ((L = 0) and (R = 0)) or (C = INFINITY) or (G = INFINITY);
end;
end.