-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchedFilter.cs
More file actions
424 lines (356 loc) · 11.9 KB
/
Copy pathMatchedFilter.cs
File metadata and controls
424 lines (356 loc) · 11.9 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
using System;
using System.Linq;
namespace SDRSharp.TimeDomainScope
{
public enum PulseShape
{
Square,
RaisedCosine,
Gaussian
}
public enum CarrierWaveform
{
None, // No carrier - envelope only
Sine, // Sine wave carrier
Square, // Square wave carrier
Triangle, // Triangle wave carrier
Sawtooth // Sawtooth wave carrier
}
public enum FilterMode
{
FilterInputSignal,
FilterEnvelopeOnly
}
public enum FilterQuality
{
Fast, // Downsample for speed
High // No downsampling, full quality
}
public class MatchedFilter
{
private float[] _template;
private float[] _filterOutput;
private readonly object _lock = new object();
// Signal parameters - SAME AS SIGNAL GENERATOR
private double _sampleRate = 250000;
private double _pulseWidth = 50.0; // milliseconds (ON time)
private double _dutyCycle = 50.0; // percentage (0-100)
private double _carrierFrequency = 0; // Hz
private PulseShape _pulseShape = PulseShape.Square;
private CarrierWaveform _carrierWaveform = CarrierWaveform.None;
// Filter parameters
private FilterMode _filterMode = FilterMode.FilterEnvelopeOnly;
private FilterQuality _quality = FilterQuality.Fast;
private bool _enabled = false;
// Template generation mode
private bool _useOOKPattern = false;
public bool Enabled
{
get => _enabled;
set => _enabled = value;
}
public double SampleRate
{
get => _sampleRate;
set
{
if (_sampleRate != value)
{
_sampleRate = value;
GenerateTemplate();
}
}
}
public double PulseWidthMs
{
get => _pulseWidth;
set
{
if (_pulseWidth != value)
{
_pulseWidth = value;
GenerateTemplate();
}
}
}
public double DutyCycle
{
get => _dutyCycle;
set
{
if (_dutyCycle != value && value >= 0 && value <= 100)
{
_dutyCycle = value;
GenerateTemplate();
}
}
}
public double PulsePeriod
{
get
{
if (_dutyCycle > 0 && _dutyCycle < 100)
return _pulseWidth / (_dutyCycle / 100.0);
return _pulseWidth * 2;
}
}
public double OffTimeMs
{
get { return PulsePeriod - _pulseWidth; }
}
public double CarrierFrequency
{
get => _carrierFrequency;
set
{
if (_carrierFrequency != value)
{
_carrierFrequency = value;
GenerateTemplate();
}
}
}
public PulseShape Shape
{
get => _pulseShape;
set
{
if (_pulseShape != value)
{
_pulseShape = value;
GenerateTemplate();
}
}
}
public CarrierWaveform Carrier
{
get => _carrierWaveform;
set
{
if (_carrierWaveform != value)
{
_carrierWaveform = value;
GenerateTemplate();
}
}
}
public bool UseOOKPattern
{
get => _useOOKPattern;
set
{
if (_useOOKPattern != value)
{
_useOOKPattern = value;
GenerateTemplate();
}
}
}
public FilterMode Mode
{
get => _filterMode;
set => _filterMode = value;
}
public FilterQuality Quality
{
get => _quality;
set => _quality = value;
}
public MatchedFilter()
{
GenerateTemplate();
}
private void GenerateTemplate()
{
lock (_lock)
{
if (_sampleRate <= 0 || _pulseWidth <= 0)
return;
int templateLength;
if (_useOOKPattern)
{
double periodSec = PulsePeriod / 1000.0;
templateLength = (int)(_sampleRate * periodSec);
}
else
{
double pulseWidthSec = _pulseWidth / 1000.0;
templateLength = (int)(_sampleRate * pulseWidthSec);
}
if (templateLength <= 0)
return;
_template = new float[templateLength];
int onSamples = (int)(_sampleRate * _pulseWidth / 1000.0);
for (int i = 0; i < templateLength; i++)
{
float sample;
if (_useOOKPattern)
{
if (i < onSamples)
{
float t = (float)i / onSamples;
float envelope = GenerateEnvelope(t);
float carrier = GenerateCarrier(i);
sample = envelope * carrier;
}
else
{
sample = 0.0f;
}
}
else
{
float t = (float)i / templateLength;
float envelope = GenerateEnvelope(t);
float carrier = GenerateCarrier(i);
sample = envelope * carrier;
}
_template[i] = sample;
}
float maxVal = _template.Max(Math.Abs);
if (maxVal > 0)
{
for (int i = 0; i < _template.Length; i++)
{
_template[i] /= maxVal;
}
}
}
}
private float GenerateEnvelope(float t)
{
switch (_pulseShape)
{
case PulseShape.Square:
return 1.0f;
case PulseShape.RaisedCosine:
return 0.5f * (1.0f - (float)Math.Cos(2 * Math.PI * t));
case PulseShape.Gaussian:
float sigma = 0.3f;
float x = (t - 0.5f) / sigma;
return (float)Math.Exp(-0.5 * x * x);
default:
return 1.0f;
}
}
private float GenerateCarrier(int sampleIndex)
{
if (_carrierWaveform == CarrierWaveform.None || _carrierFrequency == 0)
return 1.0f;
float phase = (float)(2.0 * Math.PI * _carrierFrequency * sampleIndex / _sampleRate);
while (phase < 0) phase += 2.0f * (float)Math.PI;
while (phase >= 2.0f * Math.PI) phase -= 2.0f * (float)Math.PI;
switch (_carrierWaveform)
{
case CarrierWaveform.None:
return 1.0f;
case CarrierWaveform.Sine:
return (float)Math.Cos(phase);
case CarrierWaveform.Square:
return phase < Math.PI ? 1.0f : -1.0f;
case CarrierWaveform.Triangle:
if (phase < Math.PI)
return -1.0f + (2.0f * phase / (float)Math.PI);
else
return 3.0f - (2.0f * phase / (float)Math.PI);
case CarrierWaveform.Sawtooth:
return -1.0f + (phase / (float)Math.PI);
default:
return (float)Math.Cos(phase);
}
}
public unsafe float[] ApplyFilter(float[] input)
{
if (input == null || _template == null)
return null;
if (!_enabled)
{
float[] passthrough = new float[input.Length];
Array.Copy(input, passthrough, input.Length);
return passthrough;
}
lock (_lock)
{
int inputLength = input.Length;
int templateLength = _template.Length;
if (inputLength < templateLength)
{
return new float[inputLength];
}
float[] output = new float[inputLength];
int step = (_quality == FilterQuality.Fast) ? 2 : 1;
if (_quality == FilterQuality.Fast && inputLength > 10000)
{
step = Math.Min(4, inputLength / 5000);
}
fixed (float* pInput = input, pTemplate = _template, pOutput = output)
{
int halfTemplate = templateLength / 2;
for (int i = halfTemplate; i < inputLength - halfTemplate; i += step)
{
float sum = 0;
float* inputPtr = pInput + i - halfTemplate;
float* templatePtr = pTemplate;
for (int j = 0; j < templateLength; j++)
{
sum += inputPtr[j] * templatePtr[j];
}
pOutput[i] = sum;
if (step > 1 && i + step < inputLength - halfTemplate)
{
float nextSum = 0;
float* nextInputPtr = pInput + i + step - halfTemplate;
for (int j = 0; j < templateLength; j++)
{
nextSum += nextInputPtr[j] * templatePtr[j];
}
for (int k = 1; k < step && i + k < inputLength; k++)
{
float alpha = (float)k / step;
pOutput[i + k] = sum * (1 - alpha) + nextSum * alpha;
}
}
}
}
_filterOutput = output;
return output;
}
}
public float[] GetFilterOutput()
{
lock (_lock)
{
if (_filterOutput == null)
return null;
float[] copy = new float[_filterOutput.Length];
Array.Copy(_filterOutput, copy, _filterOutput.Length);
return copy;
}
}
public float[] GetTemplate()
{
lock (_lock)
{
if (_template == null)
return null;
float[] copy = new float[_template.Length];
Array.Copy(_template, copy, _template.Length);
return copy;
}
}
public string GetTemplateDescription()
{
string desc = $"{_pulseShape} envelope";
if (_carrierWaveform != CarrierWaveform.None)
{
desc += $" × {_carrierWaveform} carrier @ {_carrierFrequency / 1000:F1}kHz";
}
desc += $", PW:{_pulseWidth:F1}ms, Duty:{_dutyCycle:F0}%";
if (_useOOKPattern)
{
desc += $" (Full OOK: {PulsePeriod:F1}ms period)";
}
return desc;
}
}
}