-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrm_filter.pas
393 lines (351 loc) · 9.25 KB
/
rm_filter.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
unit rm_filter;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, RadioSystem, RadioModule, RadioNode, SignalBasic, UComplex, formfilter,
rm_spectrum, Math, radiomessage;
type
{ TFilterModule }
TFilterModule = class(TRadioModule)
private
FCoeffDomain: Integer;
FSampleRate: Cardinal;
FNode: TDataFlowNode;
FFIRNode: TFIRNode;
FTaps: Integer;
FType: TFilterType;
FWnd: TWindowFunction;
FOmega: Integer;
FBandwidth: Integer;
FWndParam: Double;
FConfig: TFilterForm;
FBandIndex: Integer;
FDirect: Boolean;
procedure Redesign;
procedure RedesignReal;
procedure RedesignComplex;
procedure ReceiveFIRData(const P: PComplex; const Len: Integer);
procedure DesignBPF(LowFreq, HighFreq: Integer);
procedure DesignBPFReal(LowFreq, HighFreq: Integer);
procedure DesignBPFComplex(LowFreq, HighFreq: Integer);
public
class procedure DesignBPFComplex(FIR: TFIRNode; const Taps, SampleRate: Integer; LowFreq, HighFreq: Integer);
class procedure DesignBPFReal(FIR: TFIRNode; const Taps, SampleRate: Integer; LowFreq, HighFreq: Integer);
protected
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); override;
function RMSetSampleRate(const Msg: TRadioMessage; const Rate: Cardinal): Integer;
override;
procedure DoConfigure; override;
procedure Describe(Strs: TStrings); override;
procedure DoSyncDestroy; override;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
procedure ReceiveData(const P: PComplex; const Len: Integer); override;
end;
implementation
{ TFilterModule }
procedure TFilterModule.Redesign;
begin
if FCoeffDomain = FILTER_COEFF_DOMAIN_REAL then
RedesignReal
else
RedesignComplex;
end;
procedure TFilterModule.RedesignReal;
var
Omega: Double;
Bw: Double;
Coeff: array of Double;
N: Integer;
begin
N := FTaps;
if FSampleRate < 1 then Exit;
if N < 5 then Exit;
if (FType = ftHPF) and (not Odd(N)) then Inc(N);
SetLength(Coeff, N);
Omega := FOmega / FSampleRate * 2;
Bw := FBandwidth / FSampleRate * 2;
FIRDesign(@Coeff[0], N, FType,
Omega, Bw,
FWnd,
FWndParam);
FFIRNode.SetFIR(PDouble(@Coeff[0]), N);
end;
procedure TFilterModule.RedesignComplex;
var
Omega: Double;
Coeff: array of Double;
C: array of Complex;
N: Integer;
I: Integer;
P: Complex = (re: 0; im: 0);
begin
case FType of
ftLPF, ftHPF:
begin
RedesignReal;
Exit;
end;
end;
N := FTaps;
if FSampleRate < 1 then Exit;
if N < 5 then Exit;
SetLength(Coeff, N);
SetLength(C, N);
Omega := FBandwidth / FSampleRate;
if Omega >= 1 then
begin
case FType of
ftBPF:
begin
Omega := 1;
FFIRNode.SetFIR(PDouble(@Omega), 1);
end;
ftBSF:
begin
Omega := 0;
FFIRNode.SetFIR(PDouble(@Omega), 1);
end;
end;
Exit;
end;
case FType of
ftBPF:
FIRDesign(@Coeff[0], N, ftLPF,
Omega, 0,
FWnd,
FWndParam);
ftBSF:
FIRDesign(@Coeff[0], N, ftHPF,
Omega, 0,
FWnd,
FWndParam);
end;
Omega := FOmega;
for I := 0 to N - 1 do
begin
P.im := 2 * Pi * FOmega * I / FSampleRate;
C[I] := Coeff[I] * cexp(P);
end;
FFIRNode.SetFIR(PComplex(@C[0]), N);
end;
procedure TFilterModule.ReceiveFIRData(const P: PComplex; const Len: Integer);
var
I: Integer;
J: Integer;
X: PComplex;
begin
if Len <> DefOutput.BufferSize then
begin
TRadioLogger.Report(llWarn,
'TFilterModule.ReceiveFIRData: Len(%d) <> DefOutput.BufferSize(%d)',
[Len, DefOutput.BufferSize]);
I := Min(Len, DefOutput.BufferCount);
end
else
I := Len;
X := Alloc(DefOutput, J);
if not Assigned(X) then
begin
TRadioLogger.Report(llWarn,
'TFilterModule.ReceiveFIRData: Alloc failed, data lost');
Exit;
end;
Move(P^, X^, I * SizeOf(X^));
DefOutput.Broadcast(J, FDataListeners);
end;
procedure TFilterModule.DesignBPF(LowFreq, HighFreq: Integer);
begin
if FCoeffDomain = FILTER_COEFF_DOMAIN_REAL then
DesignBPFReal(LowFreq, HighFreq)
else
DesignBPFComplex(LowFreq, HighFreq);
end;
procedure TFilterModule.DesignBPFReal(LowFreq, HighFreq: Integer);
var
K: Double = 1.0;
begin
FType := ftBPF;
FOmega := (LowFreq + HighFreq) div 2;
FBandwidth := HighFreq - LowFreq;
if LowFreq / FSampleRate < 1e-3 then
begin
FType := ftLPF;
FOmega := HighFreq;
end;
if HighFreq / FSampleRate > (1 - 1e-3) then
begin
if FType = ftBPF then
begin
FType := ftHPF;
FOmega := LowFreq;
end
else begin
// full pass
FFIRNode.SetFIR(PDouble(@K), 1);
Exit;
end;
end;
RedesignReal;
end;
procedure TFilterModule.DesignBPFComplex(LowFreq, HighFreq: Integer);
begin
FType := ftBPF;
FOmega := (LowFreq + HighFreq) div 2;
FBandwidth := HighFreq - LowFreq;
RedesignComplex;
end;
class procedure TFilterModule.DesignBPFComplex(FIR: TFIRNode; const Taps,
SampleRate: Integer; LowFreq, HighFreq: Integer);
var
Omega: Double;
Coeff: array of Double;
C: array of Complex;
N: Integer;
I: Integer;
P: Complex = (re: 0; im: 0);
begin
N := Taps;
SetLength(Coeff, N);
SetLength(C, N);
Omega := (HighFreq - LowFreq) / SampleRate;
FIRDesign(@Coeff[0], N, ftLPF,
Omega, 0,
wfKaiser,
0);
Omega := (HighFreq + LowFreq) / SampleRate / 2;
for I := 0 to N - 1 do
begin
P.im := 2 * Pi * Omega * I / SampleRate;
C[I] := Coeff[I] * cexp(P);
end;
FIR.SetFIR(PComplex(@C[0]), N);
end;
class procedure TFilterModule.DesignBPFReal(FIR: TFIRNode; const Taps,
SampleRate: Integer; LowFreq, HighFreq: Integer);
var
Coeff: array of Double;
N: Integer;
begin
N := Taps;
SetLength(Coeff, N);
FIRDesign(@Coeff[0], N, ftBPF,
(HighFreq + LowFreq) / SampleRate / 2, (HighFreq - LowFreq) / SampleRate,
wfKaiser,
0);
FIR.SetFIR(PDouble(@Coeff[0]), N);
end;
procedure TFilterModule.ProccessCustomMessage(const Msg: TRadioMessage;
var Ret: Integer);
begin
if Msg.Id = RM_SPECTRUM_BAND_SELECT_1 + FBandIndex then
begin
DesignBPF(Integer(Msg.ParamH), Integer(Msg.ParamL));
FDirect := False;
GraphInvalidate;
Exit;
end;
case Msg.Id of
RM_FILTER_USE_BAND_SELECT: FBandIndex := Msg.ParamH;
RM_FILTER_SET:
begin
FFIRNode.SetFIR(PComplex(Msg.ParamH), Msg.ParamL, False);
FDirect := True;
GraphInvalidate;
end;
RM_FILTER_REDESIGN:
begin
Redesign;
FDirect := False;
GraphInvalidate;
end;
RM_FILTER_CONFIG:
begin
case Msg.ParamH of
FILTER_TYPE: FType := TFilterType(Msg.ParamL);
FILTER_OMEGA: FOmega := Msg.ParamL;
FILTER_BANDWIDTH: FBandwidth := Msg.ParamL;
FILTER_TAPS: FTaps := Msg.ParamL;
FILTER_WINDOW: FWnd := TWindowFunction(Msg.ParamL);
FILTER_WINDOW_PARAM: FWndParam := PSingle(@Msg.ParamL)^;
FILTER_COEFF_DOMAIN: FCoeffDomain := Msg.ParamL;
end;
end;
else
inherited;
end;
end;
function TFilterModule.RMSetSampleRate(const Msg: TRadioMessage;
const Rate: Cardinal): Integer;
begin
FSampleRate := Rate;
FConfig.EditRate.Text := IntToStr(Rate);
Result := inherited;
end;
procedure TFilterModule.DoConfigure;
begin
FConfig.EditRate.Text := IntToStr(FSampleRate);
FConfig.Show;
end;
procedure TFilterModule.Describe(Strs: TStrings);
const
FT: array [TFilterType] of string = ('Low Pass', 'Band Pass', 'Band Stop', 'High Pass');
begin
if FDirect then
begin
Strs.Add('^bDirectly Designed');
Strs.Add('^bFeatures Unknown');
Exit;
end;
Strs.Add('^bType: ^n' + FT[FType]);
if FCoeffDomain = FILTER_COEFF_DOMAIN_REAL then
Strs.Add('^bCoeff.: ^nReal')
else
Strs.Add('^bCoeff.: ^nComplex');
Strs.Add(Format('^bTaps: ^n%d', [FTaps]));
case FType of
ftLPF, ftHPF:
Strs.Add(Format('^bCutoff Freq: ^n%sHz', [FormatFreq(FOmega)]));
ftBPF, ftBSF:
begin
Strs.Add(Format('^bBand Center: ^n%sHz', [FormatFreq(FOmega)]));
Strs.Add(Format('^bBandwidth: ^n%sHz', [FormatFreq(FBandwidth)]));
end;
end;
Strs.Add(Format('^bWindow: ^n%s', [gWindowFunctionNames[FWnd]]));
end;
procedure TFilterModule.DoSyncDestroy;
begin
FConfig.Free;
inherited DoSyncDestroy;
end;
constructor TFilterModule.Create(RunQueue: TRadioRunQueue);
var
R: TRegulatorNode;
begin
inherited Create(RunQueue);
FFIRNode := TFIRNode.Create;
R := TRegulatorNode.Create;
FFIRNode.Connect(R);
FNode := FFIRNode;
R.Regulator.Size := DefOutput.BufferSize;
R.OnSendToNext := @ReceiveFIRData;
FWnd := wfKaiser;
FWndParam := -1;
FTaps := 64;
FConfig := TFilterForm.Create(nil);
FConfig.Module := Self;
end;
destructor TFilterModule.Destroy;
begin
FNode.Free;
inherited Destroy;
end;
procedure TFilterModule.ReceiveData(const P: PComplex; const Len: Integer);
begin
FNode.ReceiveData(P, Len);
end;
initialization
RegisterModule(TRadioModuleClass(TFilterModule.ClassType));
end.