forked from paulh002/sdrberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMDemodulator.cpp
387 lines (348 loc) · 8.84 KB
/
AMDemodulator.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
#include <cstdio>
#include <cassert>
#include <cmath>
#include <unistd.h>
#include <complex>
#include <vector>
#include <liquid.h>
#include <algorithm>
#include <mutex>
#include "DataBuffer.h"
#include "AudioOutput.h"
#include "AMDemodulator.h"
#include "Waterfall.h"
#include "sdrberry.h"
#include "vfo.h"
#include "Agc_class.h"
void* am_demod_thread(void* ptr);
/** Compute RMS level over a small prefix of the specified sample vector. */
void AMDemodulator::init(demod_struct * ptr)
{
float As = 60.0f; // resampling filter stop-band attenuation [dB]
float mod_index = 0.03125f;
// resampler and band filter
m_r = (float)ptr->pcmrate / (float)ptr->ifrate;
m_audio_mean = m_audio_rms = m_audio_level = m_if_level = 0.0;
m_source_buffer = ptr->source_buffer;
if (m_r < 0.5)
{
printf("resample rate %f \n", m_r);
m_bresample = true;
m_q = msresamp_crcf_create(m_r, As);
msresamp_crcf_print(m_q);
}
m_demod = ampmodem_create(mod_index, ptr->mode, ptr->suppressed_carrier);
tune_offset(vfo.get_vfo_offset());
m_lowpass = iirfilt_crcf_create_lowpass(m_order, 0.03125);
m_init = true;
// create agc object
agc.set_bandwidth(0.01f);
//agc.set_enery_levels(0.1f, 1.0f);
agc.print();
}
void AMDemodulator::tune_offset(long offset)
{
// get lock on modulator process
unique_lock<mutex> lock(m_mutex);
if (m_upnco != nullptr)
nco_crcf_destroy(m_upnco);
m_offset = offset;
float rad_per_sample = ((2.0f * (float)M_PI * (float)(vfo.get_vfo_offset())) / (float)ifrate);
m_upnco = nco_crcf_create(LIQUID_NCO);
nco_crcf_set_phase(m_upnco, 0.0f);
nco_crcf_set_frequency(m_upnco, rad_per_sample);
}
void AMDemodulator::adjust_gain(IQSampleVector& samples_in, float vol)
{
for (auto& col : samples_in)
{
col.real(col.real() * vol);
col.imag(col.imag() * vol);
}
}
void AMDemodulator::exit_demod()
{
if (m_demod)
ampmodem_destroy(m_demod);
if (m_q)
msresamp_crcf_destroy(m_q);
iirfilt_crcf_destroy(m_lowpass);
if (m_upnco != nullptr)
nco_crcf_destroy(m_upnco);
}
AMDemodulator::~AMDemodulator()
{
}
void AMDemodulator::set_filter(double if_rate, int band_width)
{
double factor {0.0625};
unique_lock<mutex> lock(m_mutex);
iirfilt_crcf_destroy(m_lowpass);
switch (band_width)
{
case 0:
// 500hz
factor = 0.00520833333333333;
break;
case 1:
// 1Khz
factor = 0.010416667;
break;
case 2:
// 1.5 khz
factor = 0.015625;
break;
case 3:
// 2khz
factor = 0.0208333333333333;
break;
case 4:
// 2.5 khz
factor = 0.0260416666666667;
break;
case 5:
factor = 0.03125;
// 3 Khz
break;
case 6:
factor = 0.0364583333333333;
// 3.5Khz
break;
case 7:
factor = 0.0416666666666667;
// 4Khz
break;
}
m_lowpass = iirfilt_crcf_create_lowpass(m_order, factor);
iirfilt_crcf_print(m_lowpass);
}
void AMDemodulator::calc_if_level(const IQSampleVector& samples_in)
{
float y2 = 0.0;
for (auto& con : samples_in)
{
y2 += std::real(con * std::conj(con));
}
// smooth energy estimate using single-pole low-pass filter
y2 = y2 / samples_in.size();
accuf = (1.0 - alpha)* accuf + alpha*y2;
m_if_level = accuf;
}
void AMDemodulator::mono_to_left_right(const SampleVector& samples_mono,
SampleVector& audio)
{
unsigned int n = samples_mono.size();
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;
}
}
static int agc_counter = 0;
void AMDemodulator::process(const IQSampleVector& samples_in, SampleVector& audio)
{
unsigned int num_written;
SampleVector audio_tmp, audio_mono;
IQSampleVector filter, buf_mix;
IQSampleVector buf_iffiltered;
float bt = 0.1f;
unique_lock<mutex> lock(m_mutex);
agc.set_threshold(gagc.get_threshold());
agc.set_slope(gagc.get_slope());
int agcv = gagc.get_agc_mode();
if (agcv != m_iagc && agcv != 0)
{
m_iagc = agcv;
switch (agcv)
{
case 1:
bt = 0.1f;
break;
case 2:
bt = 0.01f;
break;
case 3:
bt = 0.001f;
break;
}
agc.set_bandwidth(bt);
agc.print();
}
// mixer to mix vfo offset
buf_mix.clear();
for (auto& col : samples_in)
{
complex<float> v;
nco_crcf_step(m_upnco);
nco_crcf_mix_down(m_upnco, col, &v);
buf_mix.push_back(v);
}
// Take the FFT of the original baseband
// The baseband for demodulation will be downmixed my vfo.setting.m_offset
Fft_calc.process_samples(samples_in);
Fft_calc.set_signal_strength(get_if_level());
// Downsample to pcmrate (pcmrate will be 44100 or 48000)
if (m_bresample)
{
float nx = (float)buf_mix.size() * m_r + 500;
buf_iffiltered.reserve((int)ceilf(nx));
buf_iffiltered.resize((int)ceilf(nx));
msresamp_crcf_execute(m_q, (complex<float> *)buf_mix.data(), buf_mix.size(), (complex<float> *)buf_iffiltered.data(), &num_written);
buf_iffiltered.resize(num_written);
filter.clear();
for (auto& col : buf_iffiltered)
{
complex<float> v;
iirfilt_crcf_execute(m_lowpass, col, &v);
filter.insert(filter.end(), v);
}
}
else
{
filter.clear();
for (auto& col : buf_mix)
{
complex<float> v;
iirfilt_crcf_execute(m_lowpass, col, &v);
filter.insert(filter.end(), v);
}
}
// apply audio filter set by user [2.2Khz, 2.4Khz, 2.6Khz, 3.0 Khz, ..]
calc_if_level(filter);
if (agcv)
{
//agc.init(filter);
agc.execute_vector(filter);
agc_counter++;
if(agc_counter > 100)
{
//agc.print();
agc_counter = 0;
}
}
for (auto& col : filter)
{
float z {0};
ampmodem_demodulate(get_am_demod(), (liquid_float_complex)col, &z);
try
{
audio_mono.insert(audio_mono.end(), z);
}
catch (const std::exception& e)
{
std::cout << e.what() << "audio_mono.insert" << std::endl;
}
}
mono_to_left_right(audio_mono, audio);
buf_iffiltered.clear();
filter.clear();
buf_mix.clear();
}
pthread_t am_thread;
int create_am_thread(demod_struct *demod)
{
return pthread_create(&am_thread, NULL, am_demod_thread, (void *)demod);
}
void* am_demod_thread(void* ptr)
{
unsigned int fft_block = 0;
bool inbuf_length_warning = false;
demod_struct *demod_ptr = (demod_struct *)ptr;
AudioOutput *audio_output = demod_ptr->audio_output;
SampleVector audiosamples, audioframes;
AMDemodulator ammod;
int ifilter {-1};
unique_lock<mutex> lock(am_finish);
//vfo.set_tuner_offset(0);
ammod.init(demod_ptr);
Fft_calc.plan_fft(nfft_samples); //
while (!stop_flag.load())
{
if (vfo.tune_flag == true)
{
vfo.tune_flag = false;
ammod.tune_offset(vfo.get_vfo_offset());
}
if (ifilter != filter)
{
ammod.set_filter(ifrate, filter);
ifilter = filter;
}
if (!inbuf_length_warning && ammod.m_source_buffer->queued_samples() > 10 * 530000) {
printf("\nWARNING: Input buffer is growing (system too slow) queued samples %u\n", ammod.m_source_buffer->queued_samples());
inbuf_length_warning = true;
}
if (ammod.m_source_buffer->queued_samples() == 0)
{
usleep(5000);
continue;
}
IQSampleVector iqsamples = ammod.m_source_buffer->pull();
if (iqsamples.empty())
{
usleep(5000);
continue;
}
//process
ammod.process(iqsamples, audiosamples);
// Measure audio level.
samples_mean_rms(audiosamples, ammod.m_audio_mean, ammod.m_audio_rms);
ammod.m_audio_level = 0.95 * ammod.m_audio_level + 0.05 * ammod.m_audio_rms;
// Set nominal audio volume.
audio_output->adjust_gain(audiosamples);
for (auto& col : audiosamples)
{
audioframes.insert(audioframes.end(), col);
if (audioframes.size() == (2 * audio_output->get_framesize()))
{
audio_output->write(audioframes);
}
}
iqsamples.clear();
audiosamples.clear();
}
ammod.exit_demod();
pthread_exit(NULL);
}
static demod_struct demod;
void start_dsb(int mode, double ifrate, int pcmrate, DataBuffer<IQSample> *source_buffer, AudioOutput *audio_output)
{
demod.source_buffer = source_buffer;
demod.audio_output = audio_output;
demod.pcmrate = pcmrate;
demod.ifrate = ifrate;
demod.tuner_offset = 0; // not used
demod.downsample = 0; //not used
vfo.set_step(10, 0);
printf("pcmrate %u\n", demod.pcmrate);
printf("ifrate %f\n", demod.ifrate);
switch (mode)
{
case mode_usb:
demod.suppressed_carrier = 1;
demod.mode = LIQUID_AMPMODEM_USB;
printf("mode LIQUID_AMPMODEM_USB carrier %d\n", demod.suppressed_carrier);
break;
case mode_lsb:
demod.suppressed_carrier = 1;
demod.mode = LIQUID_AMPMODEM_LSB;
printf("mode LIQUID_AMPMODEM_LSB carrier %d\n", demod.suppressed_carrier);
break;
case mode_am:
demod.suppressed_carrier = 0;
demod.mode = LIQUID_AMPMODEM_DSB;
printf("mode LIQUID_AMPMODEM_DSB carrier %d\n", demod.suppressed_carrier);
break;
case mode_dsb:
demod.suppressed_carrier = 1;
demod.mode = LIQUID_AMPMODEM_DSB;
printf("mode LIQUID_AMPMODEM_DSB carrier %d\n", demod.suppressed_carrier);
break;
default:
printf("Mode not correct\n");
return;
}
create_am_thread(&demod);
create_rx_streaming_thread(&soapy_devices[0]);
}