-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriftProcessor.cpp
648 lines (531 loc) · 15.3 KB
/
DriftProcessor.cpp
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//
// DriftProcessor.cpp: description
// Copyright (C) 2023 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include "DriftProcessor.h"
#include <UIMediator.h>
#include <SuWidgetsHelpers.h>
#include <Suscan/AnalyzerRequestTracker.h>
using namespace SigDigger;
DriftProcessor::DriftProcessor(UIMediator *mediator, QObject *parent)
: QObject{parent}
{
m_mediator = mediator;
m_tracker = new Suscan::AnalyzerRequestTracker(this);
this->connectAll();
this->setState(DRIFT_PROCESSOR_IDLE, "Idle");
}
DriftProcessor::~DriftProcessor()
{
if (m_cfgTemplate != nullptr)
suscan_config_destroy(m_cfgTemplate);
}
void
DriftProcessor::connectAll()
{
connect(
this->m_tracker,
SIGNAL(opened(Suscan::AnalyzerRequest const &)),
this,
SLOT(onOpened(Suscan::AnalyzerRequest const &)));
connect(
this->m_tracker,
SIGNAL(cancelled(Suscan::AnalyzerRequest const &)),
this,
SLOT(onCancelled(Suscan::AnalyzerRequest const &)));
connect(
this->m_tracker,
SIGNAL(error(Suscan::AnalyzerRequest const &, const std::string &)),
this,
SLOT(onError(Suscan::AnalyzerRequest const &, const std::string &)));
}
qreal
DriftProcessor::adjustBandwidth(qreal desired) const
{
if (m_decimation == 0)
return desired;
return m_chanRBW * ceil(desired / m_chanRBW);
}
void
DriftProcessor::disconnectAnalyzer()
{
disconnect(m_analyzer, nullptr, this, nullptr);
this->setState(DRIFT_PROCESSOR_IDLE, "Analyzer closed");
}
void
DriftProcessor::connectAnalyzer()
{
connect(
m_analyzer,
SIGNAL(inspector_message(const Suscan::InspectorMessage &)),
this,
SLOT(onInspectorMessage(const Suscan::InspectorMessage &)));
connect(
m_analyzer,
SIGNAL(samples_message(const Suscan::SamplesMessage &)),
this,
SLOT(onInspectorSamples(const Suscan::SamplesMessage &)));
}
void
DriftProcessor::closeChannel()
{
if (m_analyzer != nullptr && m_inspHandle != -1)
m_analyzer->closeInspector(m_inspHandle);
m_inspHandle = -1;
}
// Depending on the state, a few things must be initialized
void
DriftProcessor::setState(DriftProcessorState state, QString const &msg)
{
if (m_state != state) {
m_state = state;
switch (state) {
case DRIFT_PROCESSOR_IDLE:
if (m_inspHandle != -1)
this->closeChannel();
m_inspId = 0xffffffff;
m_equivSampleRate = 0;
m_fullSampleRate = 0;
m_decimation = 0;
m_chanRBW = 0;
m_settingParams = false;
break;
case DRIFT_PROCESSOR_CONFIGURING:
m_settingParams = true;
break;
case DRIFT_PROCESSOR_STREAMING:
m_rawSampleCount = 0;
m_currSmoothDrift = 0;
m_currSmoothShift = 0;
m_prevSmoothShift = 0;
m_lock = false;
m_stabilized = false;
break;
default:
break;
}
emit stateChanged(state, msg);
}
}
void
DriftProcessor::resetPLL()
{
Suscan::Config cfg(m_cfgTemplate);
if (m_state == DRIFT_PROCESSOR_STREAMING) {
cfg.set("drift.pll-reset", true);
m_analyzer->setInspectorConfig(m_inspHandle, cfg);
}
}
void
DriftProcessor::configureInspector()
{
Suscan::Config cfg(m_cfgTemplate);
cfg.set("drift.feedback-interval", SCAST(SUFLOAT, m_desiredFeedback));
cfg.set("drift.lock-threshold", SCAST(SUFLOAT, m_desiredThreshold));
m_analyzer->setInspectorConfig(m_inspHandle, cfg);
this->setState(DRIFT_PROCESSOR_CONFIGURING, "Configuring params...");
}
bool
DriftProcessor::openChannel()
{
Suscan::Channel ch;
ch.bw = m_desiredBandwidth;
ch.fc = m_desiredFrequency - m_analyzer->getFrequency();
ch.fLow = -.5 * m_desiredBandwidth;
ch.fHigh = +.5 * m_desiredBandwidth;
if (!m_tracker->requestOpen("drift", ch))
return false;
this->setState(DRIFT_PROCESSOR_OPENING, "Opening inspector...");
return true;
}
///////////////////////////////// Public API //////////////////////////////////
DriftProcessorState
DriftProcessor::state() const
{
return m_state;
}
void
DriftProcessor::setFFTSizeHint(unsigned int fftSize)
{
m_fftSize = fftSize;
}
void
DriftProcessor::setAnalyzer(Suscan::Analyzer *analyzer)
{
if (m_analyzer != nullptr)
this->disconnectAnalyzer();
m_analyzer = nullptr;
if (analyzer == nullptr)
this->setState(DRIFT_PROCESSOR_IDLE, "Capture stopped");
else
this->setState(DRIFT_PROCESSOR_IDLE, "Analyzer changed");
m_analyzer = analyzer;
if (m_analyzer != nullptr)
connectAnalyzer();
m_tracker->setAnalyzer(analyzer);
}
bool
DriftProcessor::isRunning() const
{
return m_state != DRIFT_PROCESSOR_IDLE;
}
bool
DriftProcessor::cancel()
{
if (isRunning()) {
if (m_state == DRIFT_PROCESSOR_OPENING)
m_tracker->cancelAll();
this->setState(DRIFT_PROCESSOR_IDLE, "Cancelled by user");
return true;
}
return false;
}
bool
DriftProcessor::hasLock() const
{
return m_state == DRIFT_PROCESSOR_STREAMING && m_lock;
}
void
DriftProcessor::setFeedbackInterval(qreal desiredInterval)
{
m_desiredFeedback = desiredInterval;
if (m_state > DRIFT_PROCESSOR_OPENING)
configureInspector();
}
qreal
DriftProcessor::getMaxBandwidth() const
{
return m_maxBandwidth;
}
qreal
DriftProcessor::getMinBandwidth() const
{
return m_chanRBW;
}
qreal
DriftProcessor::getTrueBandwidth() const
{
return m_trueBandwidth;
}
qreal
DriftProcessor::getTrueFeedbackInterval() const
{
return m_trueFeedback;
}
qreal
DriftProcessor::getTrueCutOff() const
{
if (m_state == DRIFT_PROCESSOR_STREAMING)
return m_trueCutOff;
else
return 0;
}
qreal
DriftProcessor::getTrueThreshold() const
{
if (m_state == DRIFT_PROCESSOR_STREAMING)
return m_trueThreshold;
else
return m_desiredThreshold;
}
qreal
DriftProcessor::setBandwidth(qreal desired)
{
qreal ret;
m_desiredBandwidth = desired;
if (m_state > DRIFT_PROCESSOR_OPENING) {
m_trueBandwidth = adjustBandwidth(m_desiredBandwidth);
m_analyzer->setInspectorBandwidth(m_inspHandle, m_trueBandwidth);
ret = m_trueBandwidth;
} else {
ret = desired;
}
return ret;
}
void
DriftProcessor::setFrequency(qreal fc)
{
m_desiredFrequency = fc;
if (m_state > DRIFT_PROCESSOR_OPENING) {
m_analyzer->setInspectorFreq(
m_inspHandle,
m_desiredFrequency - m_analyzer->getFrequency());
}
}
unsigned
DriftProcessor::getDecimation() const
{
return m_decimation;
}
qreal
DriftProcessor::getEquivFs() const
{
if (m_state > DRIFT_PROCESSOR_OPENING)
return m_equivSampleRate;
else
return 0;
}
quint64
DriftProcessor::getSamplesPerUpdate() const
{
return m_samplesPerUpdate;
}
qreal
DriftProcessor::getCurrShift() const
{
if (hasLock())
return m_currSmoothShift;
else
return 0;
}
qreal
DriftProcessor::getCurrDrift() const
{
if (hasLock())
return m_currSmoothDrift;
else
return 0;
}
bool
DriftProcessor::isStable() const
{
return hasLock() && m_stabilized;
}
bool
DriftProcessor::startStreaming(SUFREQ fc, SUFLOAT bw)
{
if (this->isRunning())
return false;
if (m_analyzer == nullptr)
return false;
this->setFrequency(fc);
this->setBandwidth(SCAST(qreal, bw));
return openChannel();
}
void
DriftProcessor::useConfigAsTemplate(const suscan_config_t *cfg)
{
if (m_cfgTemplate != nullptr) {
suscan_config_destroy(m_cfgTemplate);
m_cfgTemplate = nullptr;
}
m_cfgTemplate = suscan_config_dup(cfg);
}
bool
DriftProcessor::setParamsFromConfig(const suscan_config_t *cfg)
{
const struct suscan_field_value *cutoff, *threshold, *interval, *samps;
useConfigAsTemplate(cfg);
cutoff = suscan_config_get_value(cfg, "drift.cutoff");
threshold = suscan_config_get_value(cfg, "drift.lock-threshold");
interval = suscan_config_get_value(cfg, "drift.feedback-interval");
samps = suscan_config_get_value(cfg, "drift.feedback-samples");
// Value is the same as requested? Go ahead
if (cutoff != nullptr && threshold != nullptr && interval != nullptr) {
m_trueCutOff = SCAST(qreal, cutoff->as_float);
m_trueThreshold = SCAST(qreal, threshold->as_float);
m_trueFeedback = interval->as_float;
m_samplesPerUpdate = samps->as_int;
// Stabilization proportioinal to PLL cutoff
m_trueStabilization = 30 / m_trueCutOff;
// The goal is calculated in updates
m_stabilGoal = SCAST(SUSCOUNT, ceil(m_trueStabilization / m_trueFeedback));
// Smoothing should happen at a speed proportional to the goal
m_alpha = SU_SPLPF_ALPHA(m_trueStabilization / m_trueFeedback);
return true;
}
return false;
}
void
DriftProcessor::setCutOff(qreal cutoff)
{
Suscan::Config cfg(m_cfgTemplate);
if (m_state == DRIFT_PROCESSOR_STREAMING) {
cfg.set("drift.cutoff", SU_ASFLOAT(cutoff));
m_analyzer->setInspectorConfig(m_inspHandle, cfg);
}
}
void
DriftProcessor::setThreshold(qreal threshold)
{
Suscan::Config cfg(m_cfgTemplate);
m_desiredThreshold = threshold;
if (m_state == DRIFT_PROCESSOR_STREAMING) {
cfg.set("drift.lock-threshold", SU_ASFLOAT(threshold));
m_analyzer->setInspectorConfig(m_inspHandle, cfg);
}
}
struct timeval
DriftProcessor::getLastLock() const
{
return m_lastLock;
}
///////////////////////////// Analyzer slots //////////////////////////////////
void
DriftProcessor::onInspectorMessage(Suscan::InspectorMessage const &msg)
{
bool configuring = m_state == DRIFT_PROCESSOR_CONFIGURING;
if (msg.getInspectorId() == m_inspId) {
// This refers to us!
if (configuring) {
switch (msg.getKind()) {
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_SET_CONFIG:
// Check if this is the acknowledgement of a "Setting rate" message
// If this is the case, we transition to our final state
if (m_settingParams) {
m_settingParams = false;
if (setParamsFromConfig(msg.getCConfig())) {
this->setState(DRIFT_PROCESSOR_STREAMING, "Channel opened");
} else {
// This should never happen, but just in case the server is not
// behaving as expected
SU_ERROR("Some of the required parameters of the drift inspector were missing\n");
cancel();
}
}
break;
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_CLOSE:
m_inspHandle = -1;
this->setState(DRIFT_PROCESSOR_IDLE, "Inspector closed");
break;
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_WRONG_KIND:
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_WRONG_OBJECT:
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_WRONG_HANDLE:
this->setState(DRIFT_PROCESSOR_IDLE, "Error during channel opening");
break;
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_SET_TLE:
break;
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_ORBIT_REPORT:
break;
default:
break;
}
} else {
switch (msg.getKind()) {
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_SIGNAL:
if (msg.getSignalName() == "lock") {
m_lock = msg.getSignalValue() > 0.;
if (!m_lock) {
m_rawSampleCount = 0;
m_stabilized = false;
} else {
m_lastLock = m_analyzer->getSourceTimeStamp();
}
emit lockState(m_lock);
}
break;
case SUSCAN_ANALYZER_INSPECTOR_MSGKIND_SET_CONFIG:
setParamsFromConfig(msg.getCConfig());
break;
default:
break;
}
}
}
}
void
DriftProcessor::onInspectorSamples(Suscan::SamplesMessage const &msg)
{
SUSCOUNT sampCount = m_rawSampleCount;
SUSCOUNT stabilizationGoal = m_stabilGoal;
bool stable = m_stabilized;
qreal carrier, channel;
qreal currShift;
qreal prevShift = m_prevSmoothShift;
bool reset = false;
// Data delivered by the drift inspector is of the form:
//
// - Frequency of the carrier, relative to the channel center (in Hz)
// - Frequency of the channel center, relative to the tuner (in Hz)
//
if (msg.getInspectorId() == m_inspId && hasLock()) {
const SUCOMPLEX *samples = msg.getSamples();
unsigned int count = msg.getCount();
unsigned int i;
for (i = 0; i < count; ++i) {
carrier = SCAST(qreal, SU_C_REAL(samples[i]));
channel = SCAST(qreal, SU_C_IMAG(samples[i]));
currShift = carrier + channel;
//
// Locked to an alias, leave
//
if (!reset && fabs(carrier) > m_trueBandwidth) {
resetPLL();
reset = true;
}
// If we are stabilizing, do not put these noisy samples into the
// smoothed variable
prevShift = m_currSmoothShift;
if (!stable) {
m_currSmoothShift = currShift;
} else {
SU_SPLPF_FEED(m_currSmoothShift, currShift, m_alpha);
SU_SPLPF_FEED(m_currSmoothDrift, (m_currSmoothShift - prevShift) / m_trueFeedback, m_alpha);
}
emit measurement(sampCount, carrier, channel);
++sampCount;
if (!stable && sampCount >= stabilizationGoal)
stable = true;
}
m_rawSampleCount = sampCount;
m_prevSmoothShift = prevShift;
m_stabilized = stable;
}
}
////////////////////////////// Processor slots ////////////////////////////////
void
DriftProcessor::onOpened(Suscan::AnalyzerRequest const &req)
{
// Async step 2: update state
if (m_analyzer != nullptr) {
// We do a lazy initialization of the audio channel parameters. Instead of
// creating our own audio configuration template in the constructor, we
// wait for the channel to provide the current configuration and
// duplicate that one.
useConfigAsTemplate(req.config);
if (m_cfgTemplate == nullptr) {
m_analyzer->closeInspector(req.handle);
this->setState(DRIFT_PROCESSOR_IDLE, "Failed to duplicate configuration");
return;
}
// Async step 3: set parameters
m_inspHandle = req.handle;
m_inspId = req.inspectorId;
m_fullSampleRate = SCAST(qreal, req.basebandRate);
m_equivSampleRate = SCAST(qreal, req.equivRate);
m_decimation = SCAST(unsigned, m_fullSampleRate / m_equivSampleRate);
m_maxBandwidth = m_equivSampleRate;
m_chanRBW = m_fullSampleRate / m_fftSize;
m_trueBandwidth = adjustBandwidth(m_desiredBandwidth);
// Adjust bandwidth to something that is physical and determined by the FFT
m_analyzer->setInspectorBandwidth(m_inspHandle, m_trueBandwidth);
// Enter in configuring state
this->configureInspector();
}
}
void
DriftProcessor::onCancelled(Suscan::AnalyzerRequest const &)
{
this->setState(DRIFT_PROCESSOR_IDLE, "Cancelled");
}
void
DriftProcessor::onError(Suscan::AnalyzerRequest const &, std::string const &err)
{
this->setState(
DRIFT_PROCESSOR_IDLE,
"Failed to open inspector: " + QString::fromStdString(err));
}