-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDemodulator.cpp
More file actions
836 lines (743 loc) · 21.5 KB
/
Copy pathDemodulator.cpp
File metadata and controls
836 lines (743 loc) · 21.5 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
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#include "lvgl_.h"
#include "Demodulator.h"
#include "gui_speech.h"
#include "gui_cal.h"
#include "Spectrum.h"
#include "vfo.h"
#include <tuple>
#include "gui_squelch.h"
#include "SdrberryTypeDefs.h"
#include "sdrberry.h"
#include "SecondScreen.h"
#include "SignalStrength.h"
#define dB2mag(x) pow(10.0, (x) / 20.0)
/*
*
** Basic class for processing radio data for bith RX and TX
**
**/
std::atomic<bool> Demodulator::dcBlockSwitch = true;
std::atomic<int> Demodulator::correction = 0;
std::atomic<double> correlationMeasurement, errorMeasurement;
std::atomic<int> Demodulator::noisefilter = 0;
std::atomic<float> Demodulator::noiseThresshold = 0.0;
std::atomic<int> Demodulator::filter_type = LIQUID_IIRDES_BUTTER;
std::atomic<int> Demodulator::filter_offset = 0;
std::atomic<int> Demodulator::filter_order = 6;
std::atomic<bool> Demodulator::filter_change = true;
Demodulator::Demodulator(AudioOutput *audio_output, AudioInput *audio_input)
{ // echo constructor
ifSampleRate = 0;
audioSampleRate = audio_output->get_samplerate();
transmitIQBuffer = nullptr;
audioInputBuffer = audio_input;
audioOutputBuffer = audio_output;
audioBufferSize = Settings_file.get_int(default_radio, "audiobuffer");
if (!audioBufferSize)
audioBufferSize = 4096;
correction = Settings_file.get_int(default_radio, "correction");
highfftquadrant = 0;
timeLastFlashGainSlider = std::chrono::high_resolution_clock::now();
noisefilter = 0;
lowPassAudioFilterCutOffFrequency = 0;
lowpasssamplerate = audioSampleRate;
// resampler and band filter assume pcmfrequency on the low side;
}
// Transmit mode contructor
Demodulator::Demodulator(double ifrate, DataBuffer<IQSample> *source_buffer, AudioInput *audio_input)
{ // Transmit constructor
ifSampleRate = ifrate;
audioSampleRate = audio_input->get_samplerate();
transmitIQBuffer = source_buffer;
audioInputBuffer = audio_input;
audioBufferSize = Settings_file.get_int(default_radio, "audiobuffertx");
if (!audioBufferSize)
audioBufferSize = 4096;
highfftquadrant = 0;
correction = Settings_file.get_int(default_radio, "correction");
timeLastFlashGainSlider = std::chrono::high_resolution_clock::now();
lowPassAudioFilterCutOffFrequency = 0;
lowpasssamplerate = audioSampleRate;
// resampler and band filter assume pcmfrequency on the low side
}
// Receive mode contructor
Demodulator::Demodulator(double ifrate, DataBuffer<IQSample> *source_buffer, AudioOutput *audio_output)
{ // Receive constructor
ifSampleRate = ifrate;
audioSampleRate = audio_output->get_samplerate();
receiveIQBuffer = source_buffer;
audioOutputBuffer = audio_output;
audioBufferSize = Settings_file.get_int(default_radio, "audiobuffer");
highfftquadrant = 0;
if (!audioBufferSize)
audioBufferSize = 4096;
// resampler and band filter assume pcmfrequency on the low side
tune_offset(vfo.get_vfo_offset());
dcBlockHandle = firfilt_crcf_create_dc_blocker(25, 30);
timeLastFlashGainSlider = std::chrono::high_resolution_clock::now();
correction = Settings_file.get_int(default_radio, "correction");
lowPassAudioFilterCutOffFrequency = 0;
lowpasssamplerate = audioSampleRate;
}
Demodulator::~Demodulator()
{
auto startTime = std::chrono::high_resolution_clock::now();
printf("destructor demod called\n");
if (resampleHandle)
msresamp_crcf_destroy(resampleHandle);
resampleHandle = nullptr;
if (tuneNCO != nullptr)
nco_crcf_destroy(tuneNCO);
tuneNCO = nullptr;
if (lowPassAudioFilterHandle)
iirfilt_crcf_destroy(lowPassAudioFilterHandle);
lowPassAudioFilterHandle = nullptr;
if (bandPassHandle)
firfilt_rrrf_destroy(bandPassHandle);
if (lowPassHandle)
firfilt_rrrf_destroy(lowPassHandle);
if (highPassHandle)
firfilt_rrrf_destroy(highPassHandle);
if (dcBlockHandle)
firfilt_crcf_destroy(dcBlockHandle);
if (lowPassParksMcClellanFilterHandle)
{
firfilt_crcf_destroy(lowPassParksMcClellanFilterHandle);
lowPassParksMcClellanFilterHandle = nullptr;
}
bandPassHandle = nullptr;
lowPassHandle = nullptr;
highPassHandle = nullptr;
dcBlockHandle = nullptr;
auto now = std::chrono::high_resolution_clock::now();
const auto timePassed = std::chrono::duration_cast<std::chrono::microseconds>(now - startTime);
std::cout << "Stoptime demodulator:" << timePassed.count() << std::endl;
}
void Demodulator::set_resample_rate(float resample_rate)
{
float As{60.0f};
if (resampleHandle)
msresamp_crcf_destroy(resampleHandle);
resampleRate = resample_rate;
resampleHandle = msresamp_crcf_create(resampleRate, As);
//msresamp_crcf_print(resampleHandle);
}
float Demodulator::adjust_resample_rate(float rateAjustFraction)
{
if ((resampleRate + resampleRate * rateAjustFraction) <= 0.0)
return resampleRate;
resampleRate = resampleRate + resampleRate * rateAjustFraction;
struct msresamp_rrrf_s
{
// user-defined parameters
float rate; // re-sampling rate
float As; // filter stop-band attenuation [dB]
// type: interpolator or decimator
int type; // run half-band resamplers as interp or decim
// half-band resampler parameters
unsigned int num_halfband_stages; // number of halfband stages
msresamp2_rrrf halfband_resamp; // multi-stage halfband resampler
float rate_halfband; // halfband rate
// arbitrary resampler parameters
resamp_rrrf arbitrary_resamp; // arbitrary resampling object
float rate_arbitrary; // clean-up resampling rate, in (0.5, 2.0)
// internal buffers (ping-pong)
unsigned int buffer_len; // length of each buffer
float *buffer; // buffer[0]
unsigned int buffer_index; // index of buffer
};
msresamp_rrrf_s *_q = (msresamp_rrrf_s *)resampleHandle;
if (_q != nullptr)
{
if (_q->type == LIQUID_RESAMP_DECIM)
{
float fraction = resampleRate / _q->rate , arbitraryRate;
resamp_rrrf_adjust_rate(_q->arbitrary_resamp, fraction);
_q->rate = resampleRate;
arbitraryRate = resampleRate;
for (int i = 0; i < _q->num_halfband_stages; i++)
arbitraryRate *= 2.0;
_q->rate_arbitrary = arbitraryRate;
}
//msresamp_crcf_print(resampleHandle);
}
std::vector<std::string> resamplerate_setting;
resamplerate_setting.push_back(std::to_string(resampleRate));
resamplerate_setting.push_back(std::to_string(Settings_file.get_int(default_radio, "samplerate", 0)));
resamplerate_setting.push_back(std::to_string(Settings_file.get_int(default_radio, "decimate", 0)));
Settings_file.set_array_string(default_radio, "resample_rate", resamplerate_setting);
Settings_file.write_settings();
return resampleRate;
}
float Demodulator::adjust_resample_rate1(float rateAjustFraction)
{
if ((abs(resampleRate - rateAjustFraction) / resampleRate) < 0.01)
return resampleRate;
resampleRate = rateAjustFraction;
struct msresamp_rrrf_s
{
// user-defined parameters
float rate; // re-sampling rate
float As; // filter stop-band attenuation [dB]
// type: interpolator or decimator
int type; // run half-band resamplers as interp or decim
// half-band resampler parameters
unsigned int num_halfband_stages; // number of halfband stages
msresamp2_rrrf halfband_resamp; // multi-stage halfband resampler
float rate_halfband; // halfband rate
// arbitrary resampler parameters
resamp_rrrf arbitrary_resamp; // arbitrary resampling object
float rate_arbitrary; // clean-up resampling rate, in (0.5, 2.0)
// internal buffers (ping-pong)
unsigned int buffer_len; // length of each buffer
float *buffer; // buffer[0]
unsigned int buffer_index; // index of buffer
};
msresamp_rrrf_s *_q = (msresamp_rrrf_s *)resampleHandle;
if (_q != nullptr)
{
if (_q->type == LIQUID_RESAMP_DECIM)
{
float fraction = resampleRate / _q->rate, arbitraryRate;
resamp_rrrf_adjust_rate(_q->arbitrary_resamp, fraction);
_q->rate = resampleRate;
arbitraryRate = resampleRate;
for (int i = 0; i < _q->num_halfband_stages; i++)
arbitraryRate *= 2.0;
_q->rate_arbitrary = arbitraryRate;
}
//msresamp_crcf_print(resampleHandle);
}
//std::string str1 = std::to_string(resampleRate);
//Settings_file.save_string(default_radio, "resamplerate", str1);
//Settings_file.write_settings();
return resampleRate;
}
void Demodulator::calc_af_signalstrength(std::span<Sample> samples_in)
{
signalstrength.calculateSignalStrength(samples_in);
}
void Demodulator::calc_iq_signalstrength(const IQSampleVector &samples_in)
{
signalstrength.calculateSignalStrength(samples_in);
}
void Demodulator::FlashGainSlider(float envelope)
{
auto now = std::chrono::high_resolution_clock::now();
if (timeLastFlashGainSlider + std::chrono::milliseconds(500) < now)
{ // toggle collor of gain slider when signal is limitted
if (envelope > 0.99)
guiQueue.push_back(GuiMessage(GuiMessage::action::blink, 1));
else
guiQueue.push_back(GuiMessage(GuiMessage::action::blink, 0));
timeLastFlashGainSlider = now;
}
}
// The vfo class calculates an offset within the bandwidth of the sdr radio
// tune offset configure the mixer to mix offset down to baseband
// use mix_down() to mix down to baseband during receive,mix_up() for transmit to mixup from baseband
void Demodulator::tune_offset(long offset)
{
if (offset)
{
tuneOffsetFrequency = offset;
float rad_per_sample = ((2.0f * (float)M_PI * (float)(tuneOffsetFrequency)) / (float)ifSampleRate);
if (tuneNCO == nullptr)
tuneNCO = nco_crcf_create(LIQUID_NCO);
nco_crcf_set_phase(tuneNCO, 0.0f);
nco_crcf_set_frequency(tuneNCO, rad_per_sample);
}
else
{
if (tuneNCO != nullptr)
nco_crcf_destroy(tuneNCO);
tuneNCO = nullptr;
}
}
void Demodulator::gain_phasecorrection(IQSampleVector &samples_in, float vol)
{
double error, correlation;
float autophase{0};
float autogain{1.0};
float gainManual = (float)gcal.getRxGain();
float phaseManual = (float)gcal.getRxPhase();
if (correction > 0)
{
ifEnergy.calculateEnergyLevel(samples_in);
std::tuple<float, float, float> result = ifEnergy.ResultsMoseleyIQ();
autophase = std::get<1>(result);
autogain = std::get<2>(result);
for (auto &col : samples_in)
{
if (correction > 1)
{
col.real(col.real() + col.imag() * autophase);
col.imag(col.imag() * autogain);
}
if (correction == 1 || correction == 3)
{
col.real(col.real() * gainManual);
if (phaseManual < 0.0)
col.real(col.real() + col.imag() * phaseManual);
if (phaseManual > 0.0)
col.imag(col.imag() + col.real() * phaseManual);
}
col.real(col.real() * vol);
col.imag(col.imag() * vol);
}
}
else
{
for (auto &col : samples_in)
{
col.real(col.real() * vol);
col.imag(col.imag() * vol);
}
}
}
void Demodulator::adjust_calibration(IQSampleVector &samples_in)
{
if (correction > 0)
{
float gain = (float)gcal.getTxGain();
for (auto &col : samples_in)
{
col.real(col.real() * gain);
}
float phase = (float)gcal.getTxPhase();
if (phase < 0.0)
{
for (auto &col : samples_in)
{
col.real(col.real() + col.imag() * phase);
}
}
if (phase > 0.0)
{
for (auto &col : samples_in)
{
col.imag(col.imag() + col.real() * phase);
}
}
}
}
// copy mono signal to both sereo channels
void Demodulator::mono_to_left_right(const SampleVector &samples_mono,
SampleVector &audio)
{
unsigned int n = samples_mono.size();
if (audio_output->get_channels() < 2)
{
audio = samples_mono;
return;
}
audio.resize(2 * n);
for (unsigned int i = 0; i < n; i++)
{
Sample m = samples_mono[i];
audio[2 * i] = m;
audio[2 * i + 1] = m;
}
}
void Demodulator::Resample(IQSampleVector &filter_in,
IQSampleVector &filter_out)
{
unsigned int num_written;
if (resampleHandle)
{
if (filter_out.size() == 0)
{
float nx = (float)filter_in.size() * resampleRate * 2;
filter_out.reserve((int)ceilf(nx));
filter_out.resize((int)ceilf(nx));
}
msresamp_crcf_execute(resampleHandle, (std::complex<float> *)filter_in.data(), filter_in.size(), (std::complex<float> *)filter_out.data(), &num_written);
filter_out.resize(num_written);
}
else
{
filter_out = std::move(filter_in);
}
}
void Demodulator::Resample_new(IQSampleVector &filter_in,
IQSampleVector &filter_out)
{
unsigned int num_written;
if (resampleHandle)
{
if (filter_out.size() == 0)
{
float nx = (float)filter_in.size() * resampleRate * 2;
filter_out.resize((int)ceilf(nx));
}
msresamp_crcf_execute(resampleHandle, (std::complex<float> *)filter_in.data(), filter_in.size(), (std::complex<float> *)filter_out.data(), &num_written);
filter_out.resize(num_written);
}
else
{
filter_out = filter_in;
}
}
IQSampleVector Demodulator::Resample(IQSampleVector &filter_in)
{
unsigned int num_written;
IQSampleVector filter_out;
if (resampleHandle)
{
if (filter_out.size() == 0)
{
float nx = (float)filter_in.size() * resampleRate * 2;
filter_out.reserve((int)ceilf(nx));
filter_out.resize((int)ceilf(nx));
}
msresamp_crcf_execute(resampleHandle, (std::complex<float> *)filter_in.data(), filter_in.size(), (std::complex<float> *)filter_out.data(), &num_written);
filter_out.resize(num_written);
}
else
{
return std::move(filter_in);
}
return filter_out;
}
// audio filter 500 hz - 4.0 Khz
void Demodulator::lowPassAudioFilter(const IQSampleVector &filter_in,
IQSampleVector &filter_out)
{
if (get_lowPassAudioFilterChange())
setLowPassAudioFilter(lowpasssamplerate, lowPassAudioFilterCutOffFrequency);
if (filter_type.load() <= (int)LIQUID_IIRDES_CHEBY2)
{
for (auto &col : filter_in)
{
std::complex<float> v, z;
iirfilt_crcf_execute(lowPassAudioFilterHandle, col, &v);
filter_out.insert(filter_out.end(), v);
}
}
else
{
for (auto &col : filter_in)
{
std::complex<float> v;
firfilt_crcf_push(lowPassParksMcClellanFilterHandle, col);
firfilt_crcf_execute(lowPassParksMcClellanFilterHandle, &v);
filter_out.insert(filter_out.end(), v);
}
}
}
void Demodulator::lowPassAudioFilter(IQSampleVector &filter_in)
{
if (get_lowPassAudioFilterChange())
setLowPassAudioFilter(lowpasssamplerate, lowPassAudioFilterCutOffFrequency);
if (filter_type.load() <= (int)LIQUID_IIRDES_CHEBY2)
{
for (auto &col : filter_in)
{
std::complex<float> v, z;
iirfilt_crcf_execute(lowPassAudioFilterHandle, col, &v);
col = v;
}
}
else
{
for (auto &col : filter_in)
{
std::complex<float> v;
firfilt_crcf_push(lowPassParksMcClellanFilterHandle, col);
firfilt_crcf_execute(lowPassParksMcClellanFilterHandle, &v);
col = v;
}
}
}
void Demodulator::dc_filter(IQSampleVector &filter_in)
{
if (dcBlockHandle && dcBlockSwitch)
{
for (auto &col : filter_in)
{
std::complex<float> v;
firfilt_crcf_push(dcBlockHandle, col);
firfilt_crcf_execute(dcBlockHandle, &v);
col = v;
}
}
}
void Demodulator::mix_down(IQSampleVector &filter_in)
{
if (tuneNCO)
{
for (auto &col : filter_in)
{
std::complex<float> v;
nco_crcf_step(tuneNCO);
nco_crcf_mix_down(tuneNCO, col, &v);
col = v;
}
}
}
void Demodulator::mix_up(const IQSampleVector &filter_in,
IQSampleVector &filter_out)
{
if (tuneNCO)
{
for (auto &col : filter_in)
{
std::complex<float> v;
nco_crcf_step(tuneNCO);
nco_crcf_mix_up(tuneNCO, col, &v);
filter_out.push_back(v);
}
}
else
{
filter_out = std::move(filter_in);
}
}
void Demodulator::mix_up(IQSampleVector &in)
{
if (tuneNCO)
{
for (auto &col : in)
{
std::complex<float> v;
nco_crcf_step(tuneNCO);
nco_crcf_mix_up(tuneNCO, col, &v);
col = v;
}
}
}
void Demodulator::setLowPassAudioFilter(float samplerate, int band_width)
{
lowPassAudioFilterCutOffFrequency = band_width;
if (lowPassAudioFilterHandle)
{
iirfilt_crcf_destroy(lowPassAudioFilterHandle);
lowPassAudioFilterHandle = nullptr;
}
if (filter_type.load() <= (int)LIQUID_IIRDES_CHEBY2)
{
int filtertype = LIQUID_IIRDES_LOWPASS;
float cutOffFrequency = band_width / samplerate;
float centerFrequency = 0.0;
lowpasssamplerate = samplerate;
if (filter_offset)
{
centerFrequency = filter_offset / samplerate;
filtertype = LIQUID_IIRDES_BANDPASS;
}
lowPassAudioFilterHandle = iirfilt_crcf_create_prototype((liquid_iirdes_filtertype)filter_type.load(), (liquid_iirdes_bandtype)filtertype, LIQUID_IIRDES_SOS, filter_order.load(), cutOffFrequency, centerFrequency, 0.1f, 60.0f);
//iirfilt_crcf_print(lowPassAudioFilterHandle);
}
else
{
if (lowPassParksMcClellanFilterHandle)
{
firfilt_crcf_destroy(lowPassParksMcClellanFilterHandle);
lowPassParksMcClellanFilterHandle = nullptr;
}
float cutOffFrequency = band_width / samplerate;
if (band_width < 500)
cutOffFrequency = 500.0 / samplerate;;
lowPassParksMcClellanFilterHandle = firfilt_crcf_create_firdespm(248, cutOffFrequency, 60.0f);
firfilt_crcf_set_scale(lowPassParksMcClellanFilterHandle, 2.0f * cutOffFrequency);
//firfilt_crcf_print(lowPassParksMcClellanFilterHandle);
}
}
void Demodulator::setLowPassAudioFilterCutOffFrequency(int band_width)
{
lowPassAudioFilterCutOffFrequency = band_width;
}
void Demodulator::perform_fft(const IQSampleVector &iqsamples)
{
SpectrumGraph.ProcessWaterfall(iqsamples);
if (secondscreen)
secondscreen->ProcessWaterfall(iqsamples);
}
float Demodulator::getSuppression()
{
return SpectrumGraph.getSuppression();
}
void Demodulator::setBandPassFilter(float high, float low)
{
StopBandAttenuation = 60.0f; // stop-band attenuation in db
float fc_high = high / (float)audioSampleRate;
float fc_low = low / (float)audioSampleRate;
float h_tmp1[EQ_NTABS];
float h_tmp2[EQ_NTABS];
liquid_firdes_kaiser(EQ_NTABS, fc_low, StopBandAttenuation, 0.0f, h_low);
// LPF at f_high
liquid_firdes_kaiser(EQ_NTABS, fc_high, StopBandAttenuation, 0.0f, h_tmp1);
// LPF at f_low (for band-pass difference)
liquid_firdes_kaiser(EQ_NTABS, fc_low, StopBandAttenuation, 0.0f, h_tmp2);
// High-pass = delta - LPF(f_high)
for (unsigned int i = 0; i < EQ_NTABS; i++)
{
h_high[i] = (i == (EQ_NTABS - 1) / 2 ? 1.0f : 0.0f) - h_tmp1[i];
}
//Mid band = LPF(f_high) - LPF(f_low)
for (unsigned int i = 0; i < EQ_NTABS; i++)
{
h_mid[i] = h_tmp1[i] - h_tmp2[i];
}
if (bandPassHandle)
firfilt_rrrf_destroy(bandPassHandle);
if (lowPassHandle)
firfilt_rrrf_destroy(lowPassHandle);
if (highPassHandle)
firfilt_rrrf_destroy(highPassHandle);
lowPassHandle = firfilt_rrrf_create(h_low, EQ_NTABS);
bandPassHandle = firfilt_rrrf_create(h_mid, EQ_NTABS);
highPassHandle = firfilt_rrrf_create(h_high, EQ_NTABS);
//firfilt_rrrf_print(lowPassHandle);
//firfilt_rrrf_print(bandPassHandle);
//firfilt_rrrf_print(highPassHandle);
}
void Demodulator::executeBandpassFilter(std::span<Sample> filter_in)
{
if (bandPassHandle != nullptr && lowPassHandle != nullptr && highPassHandle != nullptr)
{
float bass_gain = dB2mag(gspeech.get_bass() / 10.0);
float treble_gain = dB2mag(gspeech.get_treble() / 10.0);
for (auto &col : filter_in)
{
float yL, yM, yH;
firfilt_rrrf_push(lowPassHandle, col);
firfilt_rrrf_execute(lowPassHandle, &yL);
firfilt_rrrf_push(bandPassHandle, col);
firfilt_rrrf_execute(bandPassHandle, &yM);
firfilt_rrrf_push(highPassHandle, col);
firfilt_rrrf_execute(highPassHandle, &yH);
col = yM + yL * bass_gain + yH * treble_gain;
}
}
}
bool Demodulator::get_dc_filter()
{
if (Settings_file.get_int(default_radio, "dc"))
return true;
else
return false;
}
void Demodulator::set_dc_filter(bool state)
{
if (state)
dcBlockSwitch = true;
else
dcBlockSwitch = false;
}
void Demodulator::set_autocorrection(int state)
{
correction.store(state);
printf("auto correction %d\n", correction.load());
}
void Demodulator::set_noise_filter(int noise)
{
noisefilter = noise;
}
void Demodulator::set_filter_type(int type)
{
filter_change = true;
filter_type.store(type);
}
void Demodulator::set_filter_offset(int offset)
{
filter_change = true;
filter_offset.store(offset);
}
void Demodulator::set_filter_order(int order)
{
filter_change = true;
filter_order.store(order);
}
int Demodulator::get_lowPassAudioFilterCutOffFrequency()
{
return lowPassAudioFilterCutOffFrequency.load();
}
bool Demodulator::get_lowPassAudioFilterChange()
{
if (filter_change)
{
filter_change = false;
return true;
}
return false;
}
void Demodulator::set_noise_threshold(int threshold)
{
noiseThresshold = threshold;
}
void Demodulator::CreateSquelch(StreamMode mode)
{
AgcProc = std::make_unique<AGCUnifiedProcessor>(mode, 0.0001f);
}
void Demodulator::SquelchProcess(SampleVector &filter)
{
if (SquelchReadMode() > 0)
{
std::span<std::byte> raw_buf(
reinterpret_cast<std::byte *>(filter.data()),
filter.size() * sizeof(float));
AgcProc->Process(raw_buf, filter.size());
}
}
void Demodulator::SquelchIQProcess(IQSampleVector &filter)
{
if (SquelchReadMode() > 0)
{
std::span<std::byte> raw_buf(
reinterpret_cast<std::byte *>(filter.data()),
filter.size() * sizeof(std::complex<float>));
AgcProc->Process(raw_buf, filter.size());
}
}
int Demodulator::SquelchReadMode()
{
int s = guisquelch.get_mode();
if (s != squelch_mode || s > 0)
{
if (s == 2 && s != squelch_mode)
{
squelch_mode = s;
AgcProc->SetSquelch(true);
}
if ((s == 0 || s == 1) && s != squelch_mode)
{
squelch_mode = s;
AgcProc->SetSquelch(false);
}
int t = guisquelch.get_threshold();
if (t != threshold)
{
AgcProc->SetSquelchThreshold(t);
threshold = t;
}
int a = guisquelch.get_attack_release();
if (a!= attack_release && s == 2)
{
AgcProc->set_bandwidth((float)a / squelch_delay_div);
attack_release = a;
}
int d = guisquelch.get_agc_delay();
if (d != agc_delay && s == 1)
{
AgcProc->set_bandwidth((float)d / agc_delay_div);
agc_delay = d;
}
int g = guisquelch.get_agc_gain();
if (g != agc_gain)
{
AgcProc->set_scale(g);
agc_gain = g;
}
squelch_mode = s;
}
return s;
}
bool Demodulator::Squelch()
{
return AgcProc->squelch();
}
void Demodulator::SquelchPrint()
{
AgcProc->print();
}