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
|
/*
Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty
the Queen in Right of Canada (Communications Research Center Canada)
Copyright (C) 2015
Matthias P. Braendli, matthias.braendli@mpb.li
http://opendigitalradio.org
*/
/*
This file is part of ODR-DabMod.
ODR-DabMod 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 3 of the
License, or (at your option) any later version.
ODR-DabMod 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 ODR-DabMod. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TII.h"
#include "PcDebug.h"
#include <stdio.h>
#include <stdexcept>
#include <string.h>
typedef std::complex<float> complexf;
/* TII pattern for TM I, II, IV */
const int pattern_tm1_2_4[][8] = { // {{{
{0,0,0,0,1,1,1,1},
{0,0,0,1,0,1,1,1},
{0,0,0,1,1,0,1,1},
{0,0,0,1,1,1,0,1},
{0,0,0,1,1,1,1,0},
{0,0,1,0,0,1,1,1},
{0,0,1,0,1,0,1,1},
{0,0,1,0,1,1,0,1},
{0,0,1,0,1,1,1,0},
{0,0,1,1,0,0,1,1},
{0,0,1,1,0,1,0,1},
{0,0,1,1,0,1,1,0},
{0,0,1,1,1,0,0,1},
{0,0,1,1,1,0,1,0},
{0,0,1,1,1,1,0,0},
{0,1,0,0,0,1,1,1},
{0,1,0,0,1,0,1,1},
{0,1,0,0,1,1,0,1},
{0,1,0,0,1,1,1,0},
{0,1,0,1,0,0,1,1},
{0,1,0,1,0,1,0,1},
{0,1,0,1,0,1,1,0},
{0,1,0,1,1,0,0,1},
{0,1,0,1,1,0,1,0},
{0,1,0,1,1,1,0,0},
{0,1,1,0,0,0,1,1},
{0,1,1,0,0,1,0,1},
{0,1,1,0,0,1,1,0},
{0,1,1,0,1,0,0,1},
{0,1,1,0,1,0,1,0},
{0,1,1,0,1,1,0,0},
{0,1,1,1,0,0,0,1},
{0,1,1,1,0,0,1,0},
{0,1,1,1,0,1,0,0},
{0,1,1,1,1,0,0,0},
{1,0,0,0,0,1,1,1},
{1,0,0,0,1,0,1,1},
{1,0,0,0,1,1,0,1},
{1,0,0,0,1,1,1,0},
{1,0,0,1,0,0,1,1},
{1,0,0,1,0,1,0,1},
{1,0,0,1,0,1,1,0},
{1,0,0,1,1,0,0,1},
{1,0,0,1,1,0,1,0},
{1,0,0,1,1,1,0,0},
{1,0,1,0,0,0,1,1},
{1,0,1,0,0,1,0,1},
{1,0,1,0,0,1,1,0},
{1,0,1,0,1,0,0,1},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,1,0,0},
{1,0,1,1,0,0,0,1},
{1,0,1,1,0,0,1,0},
{1,0,1,1,0,1,0,0},
{1,0,1,1,1,0,0,0},
{1,1,0,0,0,0,1,1},
{1,1,0,0,0,1,0,1},
{1,1,0,0,0,1,1,0},
{1,1,0,0,1,0,0,1},
{1,1,0,0,1,0,1,0},
{1,1,0,0,1,1,0,0},
{1,1,0,1,0,0,0,1},
{1,1,0,1,0,0,1,0},
{1,1,0,1,0,1,0,0},
{1,1,0,1,1,0,0,0},
{1,1,1,0,0,0,0,1},
{1,1,1,0,0,0,1,0},
{1,1,1,0,0,1,0,0},
{1,1,1,0,1,0,0,0},
{1,1,1,1,0,0,0,0} }; // }}}
TII::TII(unsigned int dabmode, const tii_config_t& tii_config) :
ModCodec(ModFormat(0), ModFormat(0)),
RemoteControllable("tii"),
m_dabmode(dabmode),
m_enable(tii_config.enable),
m_comb(tii_config.comb),
m_pattern(tii_config.pattern),
m_insert(true)
{
PDEBUG("TII::TII(%u) @ %p\n", dabmode, this);
RC_ADD_PARAMETER(enable, "enable TII [0-1]");
RC_ADD_PARAMETER(comb, "TII comb number [0-23]");
RC_ADD_PARAMETER(pattern, "TII pattern number [0-69]");
switch (m_dabmode) {
case 1:
m_carriers = 1536;
if (not(0 <= m_pattern and m_pattern <= 69) ) {
throw std::runtime_error(
"TII::TII pattern not valid!");
}
break;
case 2:
m_carriers = 384;
if (not(0 <= m_pattern and m_pattern <= 69) ) {
throw std::runtime_error(
"TII::TII pattern not valid!");
}
break;
/* unsupported
case 3:
m_carriers = 192;
break;
case 4:
d_dabmode = 0;
case 0:
*/
default:
std::stringstream ss_exception;
ss_exception <<
"TII::TII DAB mode " << m_dabmode << " not valid!";
throw std::runtime_error(ss_exception.str());
}
if (not(0 <= m_comb and m_comb <= 23) ) {
throw std::runtime_error(
"TII::TII comb not valid!");
}
m_dataIn.clear();
m_dataIn.resize(m_carriers);
prepare_pattern();
myOutputFormat.size(m_carriers * sizeof(complexf));
}
TII::~TII()
{
PDEBUG("TII::~TII() @ %p\n", this);
}
const char* TII::name()
{
// Calculate name on demand because comb and pattern are
// modifiable through RC
std::stringstream ss;
ss << "TII(comb:" << m_comb << ", pattern:" << m_pattern << ")";
m_name = ss.str();
return m_name.c_str();
}
int TII::process(Buffer* const dataIn, Buffer* dataOut)
{
PDEBUG("TII::process(dataIn: %p, dataOut: %p)\n",
dataIn, dataOut);
if ((dataIn != NULL) && (dataIn->getLength() != 0)) {
throw std::runtime_error(
"TII::process input size not valid!");
}
if (m_enable and m_insert) {
boost::mutex::scoped_lock lock(m_dataIn_mutex);
dataOut->setData(&m_dataIn[0], m_carriers * sizeof(complexf));
}
else {
dataOut->setLength(m_carriers * sizeof(complexf));
bzero(dataOut->getData(), dataOut->getLength());
}
// TODO wrong! Must align with frames containing the right data
m_insert = not m_insert;
return 1;
}
void TII::enable_carrier(int k) {
int ix = m_carriers/2 + k;
if (ix < 0 or ix+1 >= (ssize_t)m_dataIn.size()) {
throw std::runtime_error(
"TII::enable_carrier invalid k!");
}
// TODO power of the carrier ?
m_dataIn.at(ix) = 1.0;
m_dataIn.at(ix+1) = 1.0; // TODO verify if +1 is really correct
}
void TII::prepare_pattern() {
int comb = m_comb; // Convert from unsigned to signed
boost::mutex::scoped_lock lock(m_dataIn_mutex);
// Clear previous pattern
for (size_t i = 0; i < m_dataIn.size(); i++) {
m_dataIn[i] = 0.0;
}
// This could be written more efficiently, but since it is
// not performance-critial, it makes sense to write it
// in the same way as the specification in
// ETSI EN 300 401 Clause 14.8
if (m_dabmode == 1) {
for (int k = -768; k < -384; k++) {
for (int b = 0; b < 8; b++) {
if ( k == -768 + 2 * comb + 48 * b and
pattern_tm1_2_4[m_pattern][b]) {
enable_carrier(k);
}
}
}
for (int k = -384; k < -0; k++) {
for (int b = 0; b < 8; b++) {
if ( k == -384 + 2 * comb + 48 * b and
pattern_tm1_2_4[m_pattern][b]) {
enable_carrier(k);
}
}
}
for (int k = 1; k <= 384; k++) {
for (int b = 0; b < 8; b++) {
if ( k == 1 + 2 * comb + 48 * b and
pattern_tm1_2_4[m_pattern][b]) {
enable_carrier(k);
}
}
}
for (int k = 384; k <= 768; k++) {
for (int b = 0; b < 8; b++) {
if ( k == 385 + 2 * comb + 48 * b and
pattern_tm1_2_4[m_pattern][b]) {
enable_carrier(k);
}
}
}
}
else if (m_dabmode == 2) {
for (int k = -192; k <= 192; k++) {
for (int b = 0; b < 4; b++) {
if ( k == -192 + 2 * comb + 48 * b and
pattern_tm1_2_4[m_pattern][b]) {
enable_carrier(k);
}
}
for (int b = 4; b < 8; b++) {
if ( k == -191 + 2 * comb + 48 * b and
pattern_tm1_2_4[m_pattern][b]) {
enable_carrier(k);
}
}
}
}
else {
throw std::runtime_error(
"TII::TII DAB mode not valid!");
}
}
void TII::set_parameter(const std::string& parameter, const std::string& value)
{
using namespace std;
stringstream ss(value);
ss.exceptions ( stringstream::failbit | stringstream::badbit );
if (parameter == "enable") {
ss >> m_enable;
}
else if (parameter == "pattern") {
int new_pattern;
ss >> new_pattern;
if ( (m_dabmode == 1 or m_dabmode == 2) and
not(0 <= new_pattern and new_pattern <= 69) ) {
throw std::runtime_error(
"TII pattern not valid!");
}
m_pattern = new_pattern;
prepare_pattern();
}
else if (parameter == "comb") {
int new_comb;
ss >> new_comb;
if (not(0 <= new_comb and new_comb <= 23) ) {
throw std::runtime_error(
"TII comb not valid!");
}
m_comb = new_comb;
prepare_pattern();
}
else {
stringstream ss;
ss << "Parameter '" << parameter << "' is not exported by controllable " << get_rc_name();
throw ParameterError(ss.str());
}
}
const std::string TII::get_parameter(const std::string& parameter) const
{
using namespace std;
stringstream ss;
if (parameter == "enable") {
ss << (m_enable ? 1 : 0);
}
else if (parameter == "pattern") {
ss << m_pattern;
}
else if (parameter == "comb") {
ss << m_comb;
}
else {
ss << "Parameter '" << parameter << "' is not exported by controllable " << get_rc_name();
throw ParameterError(ss.str());
}
return ss.str();
}
#ifdef TII_TEST
int main(int argc, char** argv)
{
const unsigned int mode = 2;
const unsigned int comb = 4;
const unsigned int pattern = 16;
TII tii(mode, comb, pattern);
return 0;
}
#endif
|