-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrm_pll.pas
290 lines (248 loc) · 6.41 KB
/
rm_pll.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
unit rm_pll;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, RadioModule, UComplex, Math;
type
TPLLState = (psCapture, psLocked);
{ TPLLNode }
TPLLNode = class
private
FZero: Double;
FPreV: Double;
FRealAmp: Double;
FLPFAlpha: Double;
FBandwidth: Cardinal;
FErrorFilterTimeConst: Double;
FFreqRange: Cardinal;
FState: TPLLState;
FDefaultFrequency: Cardinal;
FSampleRate: Cardinal;
FTolerance: Double;
FNextPhase: Double;
FZeta: Double;
FError: Double;
FErrorAlpha: Double;
FError1MinusAlpha: Double;
FAlpha: Double;
FBeta: Double;
FPhaseDelta: Double;
FPhaseDeltaMin: Double;
FPhaseDeltaMax: Double;
function GetLocked: Boolean;
procedure SetBandwidth(AValue: Cardinal);
procedure SetDefaultFrequency(AValue: Cardinal);
procedure SetErrorFilterTimeConst(AValue: Double);
procedure SetFreqRange(AValue: Cardinal);
procedure SetSampleRate(AValue: Cardinal);
procedure Reset;
procedure SetTolerance(AValue: Double);
procedure SetZeta(AValue: Double);
public
constructor Create;
// Phase.re = Phase; Phase.im = phase error
procedure ProcessComplex(Data: PComplex; Phase: PComplex; const Len: Integer);
procedure ProcessReal(IO: PComplex; const Len: Integer);
property Locked: Boolean read GetLocked;
property DefaultFrequency: Cardinal read FDefaultFrequency write SetDefaultFrequency;
property SampleRate: Cardinal read FSampleRate write SetSampleRate;
property FreqRange: Cardinal read FFreqRange write SetFreqRange;
property Zeta: Double read FZeta write SetZeta; // damping factor
property Tolerance: Double read FTolerance write SetTolerance;
property ErrorFilterTimeConst: Double read FErrorFilterTimeConst write SetErrorFilterTimeConst;
property Bandwidth: Cardinal read FBandwidth write SetBandwidth;
end;
TRadioPLLModule = class(TRadioModule)
end;
implementation
uses
SignalBasic;
{ TPLLNode }
procedure TPLLNode.SetDefaultFrequency(AValue: Cardinal);
begin
if FDefaultFrequency = AValue then Exit;
FDefaultFrequency := AValue;
Reset;
end;
procedure TPLLNode.SetErrorFilterTimeConst(AValue: Double);
begin
if FErrorFilterTimeConst = AValue then Exit;
FErrorFilterTimeConst := AValue;
end;
procedure TPLLNode.SetFreqRange(AValue: Cardinal);
begin
if FFreqRange = AValue then Exit;
FFreqRange := AValue;
Reset;
end;
function TPLLNode.GetLocked: Boolean;
begin
Result := FState = psLocked;
end;
procedure TPLLNode.SetBandwidth(AValue: Cardinal);
begin
if FBandwidth = AValue then Exit;
FBandwidth := AValue;
Reset;
end;
procedure TPLLNode.SetSampleRate(AValue: Cardinal);
begin
if FSampleRate = AValue then Exit;
FSampleRate := AValue;
Reset;
end;
procedure TPLLNode.Reset;
var
PhasePerSample: Double;
begin
FState := psCapture;
FNextPhase := 0.0;
FError := 0.0;
if FSampleRate < 1 then Exit;
if FZeta <= 0.0 then Exit;
PhasePerSample := 2 * Pi / FSampleRate;
FPhaseDelta := FDefaultFrequency * PhasePerSample;
FPhaseDeltaMin := (FDefaultFrequency - FFreqRange) * PhasePerSample;
FPhaseDeltaMax := (FDefaultFrequency + FFreqRange) * PhasePerSample;
FErrorAlpha := 1 - Exp(-1/(FSampleRate * FErrorFilterTimeConst));
FError1MinusAlpha := 1 - FErrorAlpha;
FAlpha := 2.0 * FZeta * FBandwidth * PhasePerSample;
FBeta := (FAlpha * FAlpha) / (4.0 * FZeta * FZeta);
end;
procedure TPLLNode.SetZeta(AValue: Double);
begin
if FZeta = AValue then Exit;
FZeta := AValue;
Reset;
end;
procedure TPLLNode.SetTolerance(AValue: Double);
begin
if FTolerance = AValue then Exit;
FTolerance := AValue;
Reset;
end;
constructor TPLLNode.Create;
begin
inherited;
FSampleRate := 1024;
FDefaultFrequency := 10;
FZeta := 0.707;
FTolerance := 1.0;
FBandwidth := 10;
FFreqRange := 20;
FErrorFilterTimeConst := 0.5;
FRealAmp := 1.0;
FLPFAlpha := 0.1;
end;
procedure TPLLNode.ProcessComplex(Data: PComplex; Phase: PComplex;
const Len: Integer);
var
I: Integer;
T: Complex;
V: Double;
A: Double;
E: Double;
procedure Save;
var
F: TFileStream;
S: string;
I: Integer;
begin
F := TFileStream.Create('e:\pll.txt', fmCreate);
for I := 0 to Len - 1 do
begin
S := Format('%2.8f, %2.8f' + #13#10, [Phase[I].re, Phase[I].im]);
F.Write(S[1], Length(S));
end;
F.Free;
end;
begin
V := FNextPhase;
E := FError;
for I := 0 to Len - 1 do
begin
Phase[I].re := V;
T.re := Cos(V);
T.im := Sin(V);
T := T * Data[I];
A := -ArcTan2(T.im, T.re);
FPhaseDelta := EnsureRange(FPhaseDelta + FBeta * A, FPhaseDeltaMin, FPhaseDeltaMax);
V := V + FPhaseDelta + FAlpha * A;
if V > 2 * Pi then V := V - 2 * Pi;
E := E * FError1MinusAlpha + A * A * FErrorAlpha;
Phase[I].im := E;
end;
FNextPhase := V;
FError := E;
if FError < FTolerance then
FState := psLocked
else
FState := psCapture;
end;
procedure TPLLNode.ProcessReal(IO: PComplex; const Len: Integer);
var
I: Integer;
T: Complex;
V: Double;
A: Double;
P: Double;
E: Double;
Amp: Double = 0.0;
function arcsin(const X: Double): Double; inline;
begin
Result := Math.arcsin(EnsureRange(X, -1, 1));
end;
begin
V := FNextPhase;
E := FError;
for I := 0 to Len - 1 do
begin
T.re := IO[I].re - FZero;
FZero := (1 - FLPFAlpha) * FZero + FLPFAlpha * T.re;
if T.re > 0 then
begin
if FPrev <= 0 then
begin
// cross-zero, a new period starts
if Amp > 0 then FRealAmp := Amp;
Amp := 0;
end;
if T.re > FPreV then
begin
Amp := T.re;
P := ArcSin(T.re / FRealAmp);
end
else begin
P := Pi - ArcSin(T.re / FRealAmp);
end;
end
else begin
if T.re > FPreV then
begin
P := 2 * Pi + ArcSin(T.re / FRealAmp);
end
else begin
Amp := Max(Amp, -T.re);
P := Pi - ArcSin(T.re / FRealAmp);
end;
end;
FPreV := T.re;
// A in [-pi, pi]
A := P - V;
if A < -Pi then A := A + 2 * Pi;
if A > Pi then A := 2 * Pi - A;
FPhaseDelta := EnsureRange(FPhaseDelta + FBeta * A, FPhaseDeltaMin, FPhaseDeltaMax);
V := V + FPhaseDelta + FAlpha * A;
if V > 2 * Pi then V := V - 2 * Pi;
E := E * FError1MinusAlpha + A * A * FErrorAlpha;
IO[I].re := V;
IO[I].im := E;
end;
FNextPhase := V;
FError := E;
if FError < FTolerance then
FState := psLocked
else
FState := psCapture;
end;
end.