-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTouchStone.pas
174 lines (141 loc) · 4.57 KB
/
TouchStone.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
//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) 2013 Alex Shovkoplyas VE3NEA
unit TouchStone;
interface
uses
SysUtils, Classes, Forms, VnaCli, ComplMath;
procedure WriteSnPFile(AFileName: TFIleName; s11, s21: TScanArray);
procedure ReadSnPFile(AFileName: TFIleName; var s11: TScanArray; var s21: TScanArray);
implementation
procedure WriteSnPFile(AFileName: TFIleName; s11, s21: TScanArray);
var
Lines: TStringList;
i: integer;
begin
Lines := TStringList.Create;
try
Lines.Add('!Created with program: ' + Application.Title);
Lines.Add('!Creation time: ' + FormatDateTime('yyyy-nn-dd hh:nn:ss', Now));
Lines.Add('# GHz S MA R 50');
//two-port data
if s21 <> nil then
begin
if s11 = nil then SetLength(s11, Length(s21));
for i:=0 to High(s21) do
Lines.Add(Format('0.0%.8d %8.6f %7.2f %8.6f %7.2f 0.000000 0.00 0.000000 0.00',
[s21[i].Freq, s11[i].Value.Mag, s11[i].Value.Arg * 180/Pi,
s21[i].Value.Mag, s21[i].Value.Arg * 180/Pi]));
end
//one-port data
else if s11 <> nil then
for i:=0 to High(s11) do
Lines.Add(Format('0.0%.8d %8.6f %7.2f',
[s11[i].Freq, s11[i].Value.Mag, s11[i].Value.Arg * 180/Pi]));
Lines.SaveToFile(AFileName, TEncoding.ASCII);
finally
Lines.Free;
end;
end;
procedure ReadSnPFile(AFileName: TFIleName; var s11: TScanArray; var s21: TScanArray);
type
TReadPhase = (rfStart, rfOptions, rfS11, rfS21);
var
Lines, Fields: TStringList;
p, i, Cnt: integer;
S: string;
Phase: TReadPhase;
FreqMult: Single;
Arr: TScanArray;
Mag, Arg: Single;
C: TComplex;
procedure Err;
begin raise Exception.CreateFmt('Syntax error in %s on line %d', [ExtractFileName(AFileName), i+1]); end;
begin
{$IFDEF VER260}System.SysUtils.FormatSettings.{$ENDIF}DecimalSeparator := '.';
Lines := TStringList.Create;
Fields := TStringList.Create;
Cnt := 0;
SetLength(Arr, 100);
FreqMult := 1e9;
try
Lines.LoadFromFile(AFileName);
Phase := rfStart;
for i:=0 to Lines.Count-1 do
begin
//skip comments and blank lines
S := Lines[i];
p := Pos('!', S);
if p > 0 then Delete(S, p, MAXINT);
if Trim(S) = '' then Continue;
//options line '# GHz S MA R 50'
if S[1] = '#' then
begin
//multiple options lines not allowed
if Phase <> rfStart then Err;
//validate options
S := UpperCase(S);
p := Pos('HZ', S);
if p < 2 then Err;
Fields.CommaText := Copy(S, p+2, MAXINT);
if Fields.CommaText <> 'S,MA,R,50' then
raise Exception.CreateFmt('Unsupported options in %s, must be "S MA R 50"',
[ExtractFileName(AFileName)]);
//frequency format
case S[p-1] of
'G': FreqMult := 1e9;
'M': FreqMult := 1e6;
'K': FreqMult := 1e3;
'#', ' ': FreqMult := 1;
else Err;
end;
Phase := rfOptions;
Continue;
end;
//data line
Fields.CommaText := S;
case Phase of
//options line required before any data
rfStart: Err;
//first data line, see if S11 or S21
rfOptions:
if Fields.Count = 3 then Phase := rfS11
else if Fields.Count = 9 then Phase := rfS21
else Err;
//cannot switch between S11 and S21
rfS11: if Fields.Count <> 3 then Err;
rfS21: if Fields.Count <> 9 then Err;
end;
//read data
case Phase of
rfS11:
begin
Mag := StrToFloat(Fields[1]);
Arg := StrToFloat(Fields[2]);
C := POLAR_COMPL(Mag, Arg * Pi / 180);
C := (1 + C) / (1 - C) * 50;
end;
rfS21:
begin
Mag := StrToFloat(Fields[3]);
Arg := StrToFloat(Fields[4]);
C := POLAR_COMPL(Mag, Arg * Pi / 180);
end;
end;
//put data in array
if Cnt = Length(Arr) then SetLength(Arr, Cnt * 2);
Arr[Cnt].Freq := Round(StrToFloat(Fields[0]) * FreqMult);
Arr[Cnt].Value := C;
Inc(Cnt);
end;
finally
Lines.Free;
Fields.Free;
end;
//return data
SetLength(Arr, Cnt);
s11 := nil; s21 := nil;
if Phase = rfS11 then s11 := Arr else s21 := Arr;
end;
end.