-
Notifications
You must be signed in to change notification settings - Fork 7
/
rm_squelch.pas
224 lines (194 loc) · 5.18 KB
/
rm_squelch.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
unit rm_squelch;
{$mode objfpc}{$H+}
{
This module is for noise squelch, or rather voice/speech detection.
Hiss filter is suitable for FM demodulation output, while time domain alg
is suitable for AM.
}
interface
uses
Classes, SysUtils, UComplex, RadioModule, RadioSystem, RadioMessage, Math,
SignalBasic;
type
{ TRadioSquelch }
TRadioSquelch = class(TRadioModule)
private
FHissFilter1: TIIRFilter;
FHissFilter2: TIIRFilter;
FCenter: Cardinal;
FAlpha: Double;
FBw: Cardinal;
FRate: Cardinal;
FThreshold: Double;
FThresholdTime: Double;
FChannelNum: Integer;
FNoise1: Double;
FNoise2: Double;
FSig1: Double;
FSig2: Double;
FAlg: Integer;
procedure Reconfig;
procedure AlgHissFilter(const P: PComplex; O: PComplex; const Len: Integer);
procedure AlgTimeDomain(const P: PComplex; O: PComplex; const Len: Integer);
protected
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); override;
function RMSetSampleRate(const Msg: TRadioMessage; const Rate: Cardinal): Integer;
override;
procedure Describe(Strs: TStrings); override;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
procedure ReceiveData(const P: PComplex; const Len: Integer); override;
end;
implementation
{ TRadioSquelch }
procedure TRadioSquelch.Reconfig;
begin
if FRate < 1 then Exit;
FAlpha := 30 / FRate;
AudioEQFilterDesign(FCenter, FRate, FCenter / FBw, ftBPF, FHissFilter1.A, FHissFilter1.B);
AudioEQFilterDesign(FCenter, FRate, FCenter / FBw, ftBPF, FHissFilter2.A, FHissFilter2.B);
end;
procedure TRadioSquelch.AlgHissFilter(const P: PComplex; O: PComplex;
const Len: Integer);
var
I: Integer;
Noise: Double;
begin
IIRFilterReal(FHissFilter1, P, O, Len);
Exit;
Noise := FNoise1;
for I := 0 to Len - 1 do
begin
Noise := (1 - FAlpha) * Noise + FAlpha * Abs(O[I].re);
if Noise > FThreshold then
O[I].re := 0
else
O[I].re := P[I].re;
end;
FNoise1 := Noise;
if FChannelNum = 2 then
begin
IIRFilterReInIm(FHissFilter1, P, O, Len);
Noise := FNoise2;
for I := 0 to Len - 1 do
begin
Noise := (1 - FAlpha) * Noise + FAlpha * Abs(O[I].im);
if Noise > FThreshold then
O[I].im := 0
else
O[I].im := P[I].im;
end;
FNoise2 := Noise;
end;
end;
procedure TRadioSquelch.AlgTimeDomain(const P: PComplex; O: PComplex;
const Len: Integer);
var
I: Integer;
Sig: Double;
begin
Sig := FSig1;
for I := 0 to Len - 1 do
begin
Sig := 0.995 * Sig + 0.005 * 20 * log10(P[I].re);
if Sig > FThresholdTime then
O[I].re := P[I].re;
end;
FSig1 := Sig;
if FChannelNum < 2 then Exit;
Sig := FSig2;
for I := 0 to Len - 1 do
begin
Sig := 0.995 * Sig + 0.005 * 20 * log10(P[I].im);
if Sig > FThresholdTime then
O[I].im := P[I].im;
end;
FSig2 := Sig;
end;
procedure TRadioSquelch.ProccessCustomMessage(const Msg: TRadioMessage;
var Ret: Integer);
begin
case Msg.Id of
RM_SQUELCH_BPF_BAND:
begin
FCenter := Msg.ParamH;
FBw := Max(1, Msg.ParamL);
Reconfig;
GraphInvalidate;
end;
RM_SQUELCH_CFG:
begin
case Msg.ParamH of
SQUELCH_NOISE_THRESHOLD:
begin
if FAlg = SQUELCH_ALG_TIME_DOMAIN then
FThresholdTime := -150 + EnsureRange(Msg.ParamL, 0, 100)
else
FThreshold := 1 - EnsureRange(Msg.ParamL, 0, 100) / 100;
end;
SQUELCH_CHANNEL_NUM:
FChannelNum := EnsureRange(Msg.ParamL, 1, 2);
SQUELCH_ALG:
FAlg := Msg.ParamL;
end;
GraphInvalidate;
end;
end;
end;
function TRadioSquelch.RMSetSampleRate(const Msg: TRadioMessage;
const Rate: Cardinal): Integer;
begin
FRate := Rate;
Reconfig;
Result := inherited;
end;
procedure TRadioSquelch.Describe(Strs: TStrings);
begin
if FAlg = SQUELCH_ALG_HISS_FILTER then
begin
Strs.Add(Format('^bRef Band: ^n[%s, %s]', [FormatFreq(FCenter - FBw div 2), FormatFreq(FCenter + FBw div 2)]));
Strs.Add(Format('^bNoise Level: ^n%f', [FThreshold]));
end
else begin
Strs.Add(Format('^bSignal Threshold: ^n%fdB', [FThresholdTime]));
end;
end;
constructor TRadioSquelch.Create(RunQueue: TRadioRunQueue);
begin
inherited Create(RunQueue);
FChannelNum := 1;
FCenter := 5000;
FBw := 200;
FThreshold := 0.3;
FHasConfig := False;
FThresholdTime := -100;
SetIIROrders(FHissFilter1, 2, 2);
end;
destructor TRadioSquelch.Destroy;
begin
inherited Destroy;
end;
procedure TRadioSquelch.ReceiveData(const P: PComplex; const Len: Integer);
var
J: Integer;
O: PComplex;
begin
DefOutput.BufferSize := Len;
O := Alloc(DefOutput, J);
if not Assigned(O) then
begin
// it's better to forward input data
TRadioLogger.Report(llWarn, 'TRadioSquelch.ReceiveData: data lost');
Exit;
end;
FillByte(O^, Len * SizeOf(O^), 0);
if FAlg = SQUELCH_ALG_TIME_DOMAIN then
AlgTimeDomain(P, O, Len)
else
AlgHissFilter(P, O, Len);
DefOutput.Broadcast(J, FDataListeners);
end;
initialization
RegisterModule(TRadioModuleClass(TRadioSquelch.ClassType));
end.