-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlogging_receiver_p25.cc
354 lines (257 loc) · 10.6 KB
/
logging_receiver_p25.cc
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
#include "logging_receiver_p25.h"
log_p25_sptr make_log_p25(float freq, float center, long t)
{
return gnuradio::get_initial_sptr(new log_p25(freq, center, t));
}
unsigned log_p25::GCD(unsigned u, unsigned v) {
while ( v != 0) {
unsigned r = u % v;
u = v;
v = r;
}
return u;
}
std::vector<float> log_p25::design_filter(double interpolation, double deci) {
float beta = 5.0;
float trans_width = 0.5 - 0.4;
float mid_transition_band = 0.5 - trans_width/2;
std::vector<float> result = gr_firdes::low_pass(
interpolation,
1,
mid_transition_band/interpolation,
trans_width/interpolation,
gr_firdes::WIN_KAISER,
beta
);
return result;
}
log_p25::log_p25(float f, float c, long t)
: gr_hier_block2 ("log_p25",
gr_make_io_signature (1, 1, sizeof(gr_complex)),
gr_make_io_signature (0, 0, sizeof(float)))
{
freq = f;
center = c;
talkgroup = t;
double capture_rate = 4000000;
float offset = center - (f*1000000);
float system_channel_rate = 48000; //125000;
float symbol_rate = 4800;
double symbol_deviation = 600.0;
double channel_decim = floor(capture_rate / system_channel_rate);
double channel_rate = floor(capture_rate / channel_decim);
double trans_width = 12500 / 2;
double trans_centre = trans_width + (trans_width / 2);
std::vector<float> sym_taps;
const double pi = M_PI; //boost::math::constants::pi<double>();
timestamp = time(NULL);
starttime = time(NULL);
std::cout << " Decim: " << channel_decim << " Rate: " << channel_rate << " trans center: " << trans_centre << std::endl;
/*
coeffs = gr.firdes.low_pass(1.0, capture_rate, trans_centre, trans_width, gr.firdes.WIN_HANN)
self.channel_filter = gr.freq_xlating_fir_filter_ccf(channel_decim, coeffs, 0.0, capture_rate)
self.set_channel_offset(0.0, 0, self.spectrum.win._units)
# power squelch
squelch_db = 0
self.squelch = gr.pwr_squelch_cc(squelch_db, 1e-3, 0, True)
self.set_squelch_threshold(squelch_db)
# FM demodulator
fm_demod_gain = channel_rate / (2.0 * pi * self.symbol_deviation)
fm_demod = gr.quadrature_demod_cf(fm_demod_gain)
# symbol filter
symbol_decim = 1
# symbol_coeffs = gr.firdes.root_raised_cosine(1.0, channel_rate, self.symbol_rate, 0.2, 500)
# boxcar coefficients for "integrate and dump" filter
samples_per_symbol = channel_rate // self.symbol_rate
symbol_coeffs = (1.0/samples_per_symbol,)*samples_per_symbol
symbol_filter = gr.fir_filter_fff(symbol_decim, symbol_coeffs)
# C4FM demodulator
autotuneq = gr.msg_queue(2)
self.demod_watcher = demod_watcher(autotuneq, self.adjust_channel_offset)
demod_fsk4 = op25.fsk4_demod_ff(autotuneq, channel_rate, self.symbol_rate)
# symbol slicer
levels = [ -2.0, 0.0, 2.0, 4.0 ]
slicer = op25.fsk4_slicer_fb(levels)*/
prefilter = gr_make_freq_xlating_fir_filter_ccf(int(channel_decim),
gr_firdes::low_pass(1.0, capture_rate, trans_centre, trans_width, gr_firdes::WIN_HANN),
offset,
capture_rate);
int squelch_db = 40;
squelch = gr_make_pwr_squelch_cc(squelch_db, 0.001, 0, true);
double fm_demod_gain = floor(channel_rate / (2.0 * pi * symbol_deviation));
demod = gr_make_quadrature_demod_cf(fm_demod_gain);
double symbol_decim = 1;
double samples_per_symbol = floor(channel_rate / symbol_rate);
std::cout << " FM Gain: " << fm_demod_gain << " Samples per sym: " << samples_per_symbol << std::endl;
for (int i=0; i < samples_per_symbol; i++) {
sym_taps.push_back(1.0 / samples_per_symbol);
}
//symbol_coeffs = (1.0/samples_per_symbol,)*samples_per_symbol
sym_filter = gr_make_fir_filter_fff(symbol_decim, sym_taps);
tune_queue = gr_make_msg_queue();
traffic_queue = gr_make_msg_queue();
const float l[] = { -2.0, 0.0, 2.0, 4.0 };
std::vector<float> levels( l,l + sizeof( l ) / sizeof( l[0] ) );
op25_demod = op25_make_fsk4_demod_ff(tune_queue, channel_rate, symbol_rate);
op25_decoder = op25_make_decoder_bf();
op25_slicer = op25_make_fsk4_slicer_fb(levels);
op25_decoder->set_msgq(traffic_queue);
/*
unsigned int d = GCD(channel_rate, pre_channel_rate);
channel_rate = floor(channel_rate / d);
pre_channel_rate = floor(pre_channel_rate / d);
downsample_sig = gr_make_rational_resampler_base_ccf(channel_rate, pre_channel_rate, design_filter(channel_rate, pre_channel_rate));
*/
/*
d = GCD(audio_rate, vocoder_rate);
audio_rate = floor(audio_rate / d);
vocoder_rate = floor(vocoder_rate / d);
upsample_audio = gr_make_rational_resampler_base_fff(audio_rate, vocoder_rate, design_filter(audio_rate, vocoder_rate));
*/
//demod = gr_make_quadrature_demod_cf(1.6);
//const float a[] = { 0.1, 0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1};
/*const float a[] = { 1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6};
std::vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );
sym_filter = gr_make_fir_filter_fff(1, data);
tm *ltm = localtime(&starttime);
std::stringstream path_stream;
path_stream << boost::filesystem::current_path().string() << "/" << 1900 + ltm->tm_year << "/" << 1 + ltm->tm_mon << "/" << ltm->tm_mday;
boost::filesystem::create_directories(path_stream.str());
//sprintf(filename, "%s/%ld-%ld.wav", path_stream.str().c_str(),talkgroup,timestamp);
*/
sprintf(filename, "%ld-%ld.wav",talkgroup,timestamp);
wav_sink = gr_make_wavfile_sink(filename,1,8000,16);
//gr_file_sink_sptr fs = gr_make_file_sink(sizeof(float),"sym_filter.dat");
connect(self(), 0, prefilter, 0);
muted = true;
connect(prefilter, 0, squelch, 0);
connect(squelch, 0, demod, 0);
//connect(demod, 0, wav_sink,0);
connect(demod, 0, sym_filter, 0);
connect(sym_filter, 0, op25_demod, 0);
connect(op25_demod,0, op25_slicer, 0);
connect(op25_slicer,0, op25_decoder,0);
connect(op25_decoder, 0, wav_sink,0);
/*
int samp_per_sym = 10; //6
double samp_rate = 5000000;
int decim = 80; //20
float xlate_bandwidth = 14000; //24260.0;
float channel_rate = 4800 * samp_per_sym;
double pre_channel_rate = double(samp_rate/decim);
double vocoder_rate = 8000;
double audio_rate = 44100;
int symbol_deviation = 600;
int symbol_rate = 4800;
const double pi = M_PI; //boost::math::constants::pi<double>();
timestamp = time(NULL);
starttime = time(NULL);
prefilter = gr_make_freq_xlating_fir_filter_ccf(decim,
gr_firdes::low_pass(1, samp_rate, xlate_bandwidth/2, 6000),
offset,
samp_rate);
tune_queue = gr_make_msg_queue();
traffic_queue = gr_make_msg_queue();
const float l[] = { -2.0, 0.0, 2.0, 4.0 };
std::vector<float> levels( l,l + sizeof( l ) / sizeof( l[0] ) );
op25_demod = op25_make_fsk4_demod_ff(tune_queue, channel_rate, 4800);
op25_decoder = op25_make_decoder_bf();
op25_slicer = op25_make_fsk4_slicer_fb(levels);
op25_decoder->set_msgq(traffic_queue);
demod = gr_make_quadrature_demod_cf(channel_rate/(2.0 * pi * symbol_deviation));
unsigned int d = GCD(channel_rate, pre_channel_rate);
channel_rate = floor(channel_rate / d);
pre_channel_rate = floor(pre_channel_rate / d);
downsample_sig = gr_make_rational_resampler_base_ccf(channel_rate, pre_channel_rate, design_filter(channel_rate, pre_channel_rate));
*/
/*
d = GCD(audio_rate, vocoder_rate);
audio_rate = floor(audio_rate / d);
vocoder_rate = floor(vocoder_rate / d);
upsample_audio = gr_make_rational_resampler_base_fff(audio_rate, vocoder_rate, design_filter(audio_rate, vocoder_rate));
*/
//demod = gr_make_quadrature_demod_cf(1.6);
//const float a[] = { 0.1, 0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1};
/* const float a[] = { 1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6};
std::vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );
sym_filter = gr_make_fir_filter_fff(1, data);
tm *ltm = localtime(&starttime);
std::stringstream path_stream;
path_stream << boost::filesystem::current_path().string() << "/" << 1900 + ltm->tm_year << "/" << 1 + ltm->tm_mon << "/" << ltm->tm_mday;
boost::filesystem::create_directories(path_stream.str());
//sprintf(filename, "%s/%ld-%ld.wav", path_stream.str().c_str(),talkgroup,timestamp);
sprintf(filename, "%ld-%ld.wav",talkgroup,timestamp);
wav_sink = gr_make_wavfile_sink(filename,1,8000,16);
//gr_file_sink_sptr fs = gr_make_file_sink(sizeof(float),"sym_filter.dat");
connect(self(), 0, prefilter, 0);
muted = true;
connect(prefilter, 0, downsample_sig, 0);
connect(downsample_sig, 0, demod, 0);
connect(demod, 0, sym_filter, 0);
connect(sym_filter, 0, op25_demod, 0);
connect(op25_demod,0, op25_slicer, 0);
connect(op25_slicer,0, op25_decoder,0);
connect(op25_decoder, 0, wav_sink,0);*/
//connect(sym_filter, 0, wav_sink,0);
//connect(dsd, 0, upsample_audio,0);
//connect(upsample_audio, 0, self(),0);
//connect(sym_filter,0,fs,0);
}
log_p25::~log_p25() {
}
// from: /gnuradio/grc/grc_gnuradio/blks2/selector.py
void log_p25::unmute() {
// this function gets called everytime their is a TG continuation command. This keeps the timestamp updated.
timestamp = time(NULL);
if (muted) {
/*disconnect(self(),0, null_sink,0);
disconnect(head_source,0, prefilter,0);
connect(head_source,0, null_sink,0);*/
/*connect(self(),0, copier,0);
connect(copier,0, prefilter,0);*/
//connect(self(),0, prefilter,0);
muted = false;
}
}
void log_p25::mute() {
if (!muted) {
//disconnect(self(),0, prefilter,0);
/*disconnect(self(),0, copier,0);
disconnect(copier,0, prefilter,0);*/
/*disconnect(head_source,0, null_sink,0);
connect(head_source,0,prefilter,0);
connect(self(),0, null_sink,0);
*/muted = true;
}
}
long log_p25::get_talkgroup() {
return talkgroup;
}
float log_p25::get_freq() {
return freq;
}
long log_p25::timeout() {
return time(NULL) - timestamp;
}
char *log_p25::get_filename() {
return filename;
}
void log_p25::close() {
mute();
wav_sink->close();
/*disconnect(prefilter, 0, demod, 0);
disconnect(demod, 0, sym_filter, 0);
disconnect(sym_filter, 0, op25_demod, 0);
disconnect(op25_demod,0, op25_slicer, 0);
disconnect(op25_slicer,0, op25_decoder,0);
disconnect(op25_decoder, 0, wav_sink,0);*/
}
/*
void log_p25::forecast(int noutput_items, gr_vector_int &ninput_items_required) {
ninput_items_required[0] = 4 * noutput_items;
}*/
void log_p25::tune_offset(float f) {
freq = f;
prefilter->set_center_freq(center - (f*1000000));
std::cout << "Offset set to: " << (f*1000000-center) << std::endl;
}