/
Ddr.cpp
757 lines (646 loc) · 17.7 KB
/
Ddr.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/**
@file Ddr.cpp
@brief A receiver class to handle digital drop receivers
@author Tobias Blomberg / SM0SVX
@date 2014-07-16
This file contains a class that handle local digital drop receivers.
\verbatim
SvxLink - A Multi Purpose Voice Services System for Ham Radio Use
Copyright (C) 2004-2014 Tobias Blomberg / SM0SVX
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\endverbatim
*/
/****************************************************************************
*
* System Includes
*
****************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sigc++/sigc++.h>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <complex>
#include <fstream>
#include <algorithm>
#include <iterator>
/****************************************************************************
*
* Project Includes
*
****************************************************************************/
#include <AsyncConfig.h>
#include <AsyncAudioPassthrough.h>
#include <AsyncTcpClient.h>
/****************************************************************************
*
* Local Includes
*
****************************************************************************/
#include "Ddr.h"
#include "WbRxRtlTcp.h"
/****************************************************************************
*
* Namespaces to use
*
****************************************************************************/
using namespace std;
using namespace sigc;
using namespace Async;
/****************************************************************************
*
* Defines & typedefs
*
****************************************************************************/
#define FILTER_COEFF(name, coeffs...) \
static const float name[] = { \
coeffs \
}; \
static const int name ## _cnt = sizeof(name) / sizeof(*name);
/****************************************************************************
*
* Local class definitions
*
****************************************************************************/
/**
* fs=960000;
* a=[1 1 0 0];
* f=[0 10000/(fs/2) 96000/(fs/2) 1];
* b=firpm(30,f,a);
*
* Lowpass filter of order 30 for first stage decimation from 960kHz to 192kHz
* sampling frequency. Below -50dB over 96kHz.
*/
FILTER_COEFF(nbfm_iq_dec_coeff1,
-0.0028713422063345,
-0.0041769139545598,
-0.0062171808745743,
-0.0078031238748803,
-0.0081233271798609,
-0.0062743621398142,
-0.0014205292313401,
0.0070048711105898,
0.0191247988093941,
0.0344897867015570,
0.0520570673062483,
0.0702711557447386,
0.0872655777710594,
0.1011208091483218,
0.1101849356054080,
0.1133410547020278,
0.1101849356054080,
0.1011208091483218,
0.0872655777710594,
0.0702711557447386,
0.0520570673062483,
0.0344897867015570,
0.0191247988093941,
0.0070048711105898,
-0.0014205292313401,
-0.0062743621398142,
-0.0081233271798609,
-0.0078031238748803,
-0.0062171808745743,
-0.0041769139545598,
-0.0028713422063345
)
/**
* fs=192000;
* a=[1 1 0 0];
* f=[0 10000/(fs/2) 24000/(fs/2) 1];
* b=firpm(38,f,a);
*
* Lowpass filter of order 38 for second stage decimation from 192kHz to 48kHz
* sampling frequency. Below -50dB over 24kHz.
*/
FILTER_COEFF(nbfm_iq_dec_coeff2,
-0.0022054057399946,
-0.0013759144555157,
-0.0003560235137129,
0.0019402788096753,
0.0050318463119633,
0.0077118103385981,
0.0083196771915043,
0.0053564859260711,
-0.0017223951190513,
-0.0118310814176461,
-0.0220269740210023,
-0.0279535379185214,
-0.0249640367446503,
-0.0096365156797809,
0.0187997839077061,
0.0576385308284084,
0.1008804142901618,
0.1404539524894698,
0.1682431134370760,
0.1782444890349868,
0.1682431134370760,
0.1404539524894698,
0.1008804142901618,
0.0576385308284084,
0.0187997839077061,
-0.0096365156797809,
-0.0249640367446503,
-0.0279535379185214,
-0.0220269740210023,
-0.0118310814176461,
-0.0017223951190513,
0.0053564859260711,
0.0083196771915043,
0.0077118103385981,
0.0050318463119633,
0.0019402788096753,
-0.0003560235137129,
-0.0013759144555157,
-0.0022054057399946
)
/**
* fs=48000;
* a=[1 1 0 0];
* f=[0 10000/(fs/2) 12500/(fs/2) 1];
* b=firpm(52,f,a);
*
* Lowpass filter of order 52 for channel filter to create a channel that start
* falling off at 20kHz bandwidth and at -50dB over 25kHz bandwidth.
*/
FILTER_COEFF(nbfm_iq_ch_filt_coeff,
0.0008241697920589,
-0.0021070026154187,
-0.0018283827176036,
0.0016310471394501,
0.0028118758642382,
-0.0019396699761340,
-0.0046522841665745,
0.0017070490502376,
0.0070035913525649,
-0.0007830014477388,
-0.0098758879175436,
-0.0011783591096547,
0.0131725633282163,
0.0045849076900695,
-0.0167379346321622,
-0.0099840064006532,
0.0203645795200879,
0.0182368672779849,
-0.0238117005219107,
-0.0310369079128660,
0.0268255689484280,
0.0527102597060804,
-0.0291747144820595,
-0.0992601034613792,
0.0306674034773647,
0.3159828500981747,
0.4688223043097902,
0.3159828500981747,
0.0306674034773647,
-0.0992601034613792,
-0.0291747144820595,
0.0527102597060804,
0.0268255689484280,
-0.0310369079128660,
-0.0238117005219107,
0.0182368672779849,
0.0203645795200879,
-0.0099840064006532,
-0.0167379346321622,
0.0045849076900695,
0.0131725633282163,
-0.0011783591096547,
-0.0098758879175436,
-0.0007830014477388,
0.0070035913525649,
0.0017070490502376,
-0.0046522841665745,
-0.0019396699761340,
0.0028118758642382,
0.0016310471394501,
-0.0018283827176036,
-0.0021070026154187,
0.0008241697920589
)
FILTER_COEFF(wbfm_iq_dec_coeff,
0.0194679922087664,
-0.0005977805219536,
-0.0061383768321768,
-0.0125104713279058,
-0.0160872817947965,
-0.0138809700545037,
-0.0052351956520739,
0.0072710975817097,
0.0183426114801711,
0.0220231641013175,
0.0144027836146915,
-0.0036725021706061,
-0.0260775629116080,
-0.0425493300881981,
-0.0422802568440652,
-0.0182490019178980,
0.0293433807630875,
0.0920107509228422,
0.1549430768095743,
0.2014808299049397,
0.2186376583168589,
0.2014808299049397,
0.1549430768095743,
0.0920107509228422,
0.0293433807630875,
-0.0182490019178980,
-0.0422802568440652,
-0.0425493300881981,
-0.0260775629116080,
-0.0036725021706061,
0.0144027836146915,
0.0220231641013175,
0.0183426114801711,
0.0072710975817097,
-0.0052351956520739,
-0.0138809700545037,
-0.0160872817947965,
-0.0125104713279058,
-0.0061383768321768,
-0.0005977805219536,
0.0194679922087664
)
FILTER_COEFF(audio_dec_coeff,
0.0018440287120654,
0.0101759828785910,
0.0099946080524450,
-0.0016951247788318,
-0.0254220310953511,
-0.0423576044725107,
-0.0248714560096571,
0.0431946872471736,
0.1473022777557578,
0.2432684435456421,
0.2823051451694801,
0.2432684435456421,
0.1473022777557578,
0.0431946872471736,
-0.0248714560096571,
-0.0423576044725107,
-0.0254220310953511,
-0.0016951247788318,
0.0099946080524450,
0.0101759828785910,
0.0018440287120654
)
namespace {
template <class T>
class Decimator
{
public:
Decimator(int dec_fact, const float *coeff, int taps)
: dec_fact(dec_fact), taps(taps)
{
this->coeff.assign(coeff, coeff+taps);
p_Z = new T[taps];
memset(p_Z, 0, taps * sizeof(*p_Z));
}
void decimate(vector<T> &out, const vector<T> &in)
{
int orig_count = in.size();
/*
cout << "### in.size()=" << in.size()
<< " dec_fact=" << dec_fact
<< endl;
*/
// this implementation assumes in.size() is a multiple of factor_M
assert(in.size() % dec_fact == 0);
assert(taps >= dec_fact);
int num_out = 0;
typename vector<T>::const_iterator src = in.begin();
out.clear();
out.reserve(in.size() / dec_fact);
while (src != in.end())
{
// shift Z delay line up to make room for next samples
memmove(p_Z + dec_fact, p_Z, (taps - dec_fact) * sizeof(T));
// copy next samples from input buffer to bottom of Z delay line
for (int tap = dec_fact - 1; tap >= 0; tap--)
{
assert(src != in.end());
p_Z[tap] = *src++;
}
// calculate FIR sum
T sum;
memset(&sum, 0, sizeof(T));
for (int tap = 0; tap < taps; tap++)
{
sum += coeff[tap] * p_Z[tap];
}
out.push_back(sum); /* store sum */
num_out++;
}
//printf("out.size()=%d in.size()=%d dec_fact=%d\n", out.size(), in.size(), dec_fact);
assert(num_out == orig_count / dec_fact);
}
private:
const int dec_fact;
T *p_Z;
int taps;
vector<float> coeff;
};
class FmDemod
{
public:
FmDemod(AudioSink &audio_sink)
: iold(1.0f), qold(1.0f), audio_sink(audio_sink),
iq_dec1(5, nbfm_iq_dec_coeff1, nbfm_iq_dec_coeff1_cnt),
iq_dec2(4, nbfm_iq_dec_coeff2, nbfm_iq_dec_coeff2_cnt),
ch_filt(1, nbfm_iq_ch_filt_coeff, nbfm_iq_ch_filt_coeff_cnt),
sql_open(false)
{
//Decimator<float> audio_dec(20, audio_dec_coeff, audio_dec_coeff_cnt);
outfile.open("out.bin", ios::out | ios::binary);
}
~FmDemod()
{
outfile.close();
}
void iq_received(vector<WbRxRtlTcp::Sample> samples)
{
//cout << "### Received " << samples.size() << " samples\n";
#if 0
outfile.write(reinterpret_cast<char*>(&samples[0]),
samples.size() * sizeof(samples[0]));
#endif
vector<WbRxRtlTcp::Sample> dec_samp1, dec_samp2, ch_samp;
iq_dec1.decimate(dec_samp1, samples);
iq_dec2.decimate(dec_samp2, dec_samp1);
ch_filt.decimate(ch_samp, dec_samp2);
preDemod(ch_samp);
//dec_samp = samples;
#if 0
outfile.write(reinterpret_cast<char*>(&ch_samp[0]),
ch_samp.size() * sizeof(ch_samp[0]));
#endif
// From article-sdr-is-qs.pdf: Watch your Is and Qs:
// FM = (Qn.In-1 - In.Qn-1)/(In.In-1 + Qn.Qn-1)
vector<float> audio;
//double sumE = 0.0;
for (size_t idx=0; idx<ch_samp.size(); ++idx)
{
//sumE += pow(abs(ch_samp[idx]), 2.0);
// Normalize signal amplitude
ch_samp[idx] = ch_samp[idx] / abs(ch_samp[idx]);
float i = ch_samp[idx].real();
float q = ch_samp[idx].imag();
//outfile.write(reinterpret_cast<char*>(&i), sizeof(float));
//outfile.write(reinterpret_cast<char*>(&q), sizeof(float));
#if 1
float demod = (q*iold - i*qold)/(i*iold + q*qold);
demod = atanf(demod);
//demod = FastArcTan(demod);
#endif
/*
demod /= 5;
*/
// Complex baseband delay demodulator
#if 0
float demod = arg(ch_samp[idx] * conj(prev_samp));
prev_samp = ch_samp[idx];
#endif
audio.push_back(demod);
//int16_t samp = static_cast<int16_t>(demod * 4096);
//outfile.write(reinterpret_cast<char*>(&samp), sizeof(samp));
iold = i;
qold = q;
}
//double meanE = sumE / ch_samp.size();
//if (meanE > 5.0E-6)
//if (meanE > 0.01)
{
//cout << "meanE=" << 10*log10(meanE/1.0E-3) << endl;
sql_open = true;
vector<float> dec_audio;
//audio_dec.decimate(dec_audio, audio);
dec_audio = audio;
audio_sink.writeSamples(&dec_audio[0], dec_audio.size());
}
/*
else if (sql_open)
{
audio_sink.flushSamples();
sql_open = false;
}
*/
}
sigc::signal<void, const std::vector<RtlTcp::Sample>&> preDemod;
private:
float iold;
float qold;
WbRxRtlTcp::Sample prev_samp;
AudioSink &audio_sink;
Decimator<complex<float> > iq_dec1;
Decimator<complex<float> > iq_dec2;
Decimator<complex<float> > ch_filt;
bool sql_open;
ofstream outfile;
// Maximum error 0.0015 radians (0.085944 degrees)
// Produced another result and did not affect overall CPU% much
double FastArcTan(double x)
{
return M_PI_4*x - x*(fabs(x) - 1)*(0.2447 + 0.0663*fabs(x));
}
};
class Translate
{
public:
Translate(float samp_rate, float offset)
: n(0)
{
unsigned N = gcd(samp_rate, abs(offset));
cout << "### Translate: offset=" << offset << " N=" << N << endl;
exp_lut.resize(N);
for (int i=0; i<N; ++i)
{
complex<float> e(0.0f, -2.0*M_PI*offset*i/samp_rate);
exp_lut[i] = exp(e);
}
}
void iq_received(vector<WbRxRtlTcp::Sample> &out,
const vector<WbRxRtlTcp::Sample> &in)
{
out.clear();
out.reserve(in.size());
vector<WbRxRtlTcp::Sample>::const_iterator it;
for (it = in.begin(); it != in.end(); ++it)
{
out.push_back(*it * exp_lut[n]);
if (++n == exp_lut.size())
{
n = 0;
}
}
}
private:
vector<complex<float> > exp_lut;
unsigned n;
/**
* @brief Find the greatest common divisor for two numbers
* @param dividend The larger number
* @param divisor The lesser number
*
* This function will return the greatest common divisor of the two given
* numbers. This implementation requires that the dividend is larger than
* the divisor.
*/
unsigned gcd(unsigned dividend, unsigned divisor)
{
unsigned reminder = dividend % divisor;
if (reminder == 0)
{
return divisor;
}
return gcd(divisor, reminder);
}
};
}; /* anonymous namespace */
class Ddr::Channel : public sigc::trackable
{
public:
Channel(AudioSink &audio_sink, int fq_offset)
: fm_demod(audio_sink), trans(960000, fq_offset)
{
fm_demod.preDemod.connect(preDemod.make_slot());
}
void iq_received(vector<WbRxRtlTcp::Sample> samples)
{
vector<WbRxRtlTcp::Sample> translated;
trans.iq_received(translated, samples);
fm_demod.iq_received(translated);
};
sigc::signal<void, const std::vector<RtlTcp::Sample>&> preDemod;
private:
FmDemod fm_demod;
Translate trans;
}; /* Channel */
/****************************************************************************
*
* Prototypes
*
****************************************************************************/
/****************************************************************************
*
* Exported Global Variables
*
****************************************************************************/
/****************************************************************************
*
* Local Global Variables
*
****************************************************************************/
Ddr::DdrMap Ddr::ddr_map;
/****************************************************************************
*
* Public member functions
*
****************************************************************************/
Ddr *Ddr::find(const std::string &name)
{
DdrMap::iterator it = ddr_map.find(name);
if (it != ddr_map.end())
{
return (*it).second;
}
return 0;
} /* Ddr::find */
Ddr::Ddr(Config &cfg, const std::string& name)
: LocalRxBase(cfg, name), cfg(cfg), audio_pipe(0), channel(0), rtl(0)
{
} /* Ddr::Ddr */
Ddr::~Ddr(void)
{
DdrMap::iterator it = ddr_map.find(name());
if (it != ddr_map.end())
{
ddr_map.erase(it);
}
delete channel;
delete rtl;
delete audio_pipe;
} /* Ddr::~Ddr */
bool Ddr::initialize(void)
{
DdrMap::iterator it = ddr_map.find(name());
if (it != ddr_map.end())
{
cout << "*** ERROR: The name for a Digital Drop Receiver (DDR) must be "
<< "unique. There already is a receiver named \"" << name()
<< "\".\n";
return false;
}
ddr_map[name()] = this;
double fq = 0.0;
if (!cfg.getValue(name(), "FQ", fq))
{
cerr << "*** ERROR: Config variable " << name() << "/FQ not set\n";
return false;
}
string wbrx;
if (!cfg.getValue(name(), "WBRX", wbrx))
{
cerr << "*** ERROR: Config variable " << name()
<< "/WBRX not set\n";
return false;
}
audio_pipe = new AudioPassthrough;
rtl = WbRxRtlTcp::instance(cfg, wbrx);
if (rtl == 0)
{
cout << "*** ERROR: Could not create WBRX " << wbrx
<< " specified in receiver " << name() << endl;
return false;
}
channel = new Channel(*audio_pipe, fq-rtl->centerFq());
channel->preDemod.connect(preDemod.make_slot());
rtl->iqReceived.connect(mem_fun(*channel, &Channel::iq_received));
if (!LocalRxBase::initialize())
{
return false;
}
return true;
} /* Ddr:initialize */
/****************************************************************************
*
* Protected member functions
*
****************************************************************************/
bool Ddr::audioOpen(void)
{
return true;
} /* Ddr::audioOpen */
void Ddr::audioClose(void)
{
} /* Ddr::audioClose */
int Ddr::audioSampleRate(void)
{
return 48000;
} /* Ddr::audioSampleRate */
Async::AudioSource *Ddr::audioSource(void)
{
return audio_pipe;
} /* Ddr::audioSource */
/****************************************************************************
*
* Private member functions
*
****************************************************************************/
/*
* This file has not been truncated
*/