Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/src/v17tx.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * v17tx.c - ITU V.17 modem transmit part | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2004 Steve Underwood | |
9 * | |
10 * All rights reserved. | |
11 * | |
12 * This program is free software; you can redistribute it and/or modify | |
13 * it under the terms of the GNU General Public License version 2, as | |
14 * published by the Free Software Foundation. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 * | |
25 * $Id: v17tx.c,v 1.46 2006/11/28 16:59:57 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \file */ | |
29 | |
30 #ifdef HAVE_CONFIG_H | |
31 #include <config.h> | |
32 #endif | |
33 | |
34 #include <stdio.h> | |
35 #include <inttypes.h> | |
36 #include <stdlib.h> | |
37 #include <string.h> | |
38 #if defined(HAVE_TGMATH_H) | |
39 #include <tgmath.h> | |
40 #endif | |
41 #if defined(HAVE_MATH_H) | |
42 #include <math.h> | |
43 #endif | |
44 | |
45 #include "spandsp/telephony.h" | |
46 #include "spandsp/logging.h" | |
47 #include "spandsp/complex.h" | |
48 #include "spandsp/vector_float.h" | |
49 #include "spandsp/complex_vector_float.h" | |
50 #include "spandsp/async.h" | |
51 #include "spandsp/dds.h" | |
52 #include "spandsp/power_meter.h" | |
53 | |
54 #include "spandsp/v17tx.h" | |
55 | |
56 #define CARRIER_NOMINAL_FREQ 1800.0f | |
57 | |
58 /* Segments of the training sequence */ | |
59 #define V17_TRAINING_SEG_TEP_A 0 | |
60 #define V17_TRAINING_SEG_TEP_B (V17_TRAINING_SEG_TEP_A + 480) | |
61 #define V17_TRAINING_SEG_1 (V17_TRAINING_SEG_TEP_B + 48) | |
62 #define V17_TRAINING_SEG_2 (V17_TRAINING_SEG_1 + 256) | |
63 #define V17_TRAINING_SEG_3 (V17_TRAINING_SEG_2 + 2976) | |
64 #define V17_TRAINING_SEG_4 (V17_TRAINING_SEG_3 + 64) | |
65 #define V17_TRAINING_END (V17_TRAINING_SEG_4 + 48) | |
66 #define V17_TRAINING_SHUTDOWN_A (V17_TRAINING_END + 32) | |
67 #define V17_TRAINING_SHUTDOWN_END (V17_TRAINING_SHUTDOWN_A + 48) | |
68 | |
69 #define V17_TRAINING_SHORT_SEG_4 (V17_TRAINING_SEG_2 + 38) | |
70 | |
71 #define V17_BRIDGE_WORD 0x8880 | |
72 | |
73 const complexf_t v17_14400_constellation[128] = | |
74 { | |
75 {-8, -3}, /* 0x00 */ | |
76 { 9, 2}, /* 0x01 */ | |
77 { 2, -9}, /* 0x02 */ | |
78 {-3, 8}, /* 0x03 */ | |
79 { 8, 3}, /* 0x04 */ | |
80 {-9, -2}, /* 0x05 */ | |
81 {-2, 9}, /* 0x06 */ | |
82 { 3, -8}, /* 0x07 */ | |
83 {-8, 1}, /* 0x08 */ | |
84 { 9, -2}, /* 0x09 */ | |
85 {-2, -9}, /* 0x0A */ | |
86 { 1, 8}, /* 0x0B */ | |
87 { 8, -1}, /* 0x0C */ | |
88 {-9, 2}, /* 0x0D */ | |
89 { 2, 9}, /* 0x0E */ | |
90 {-1, -8}, /* 0x0F */ | |
91 {-4, -3}, /* 0x10 */ | |
92 { 5, 2}, /* 0x11 */ | |
93 { 2, -5}, /* 0x12 */ | |
94 {-3, 4}, /* 0x13 */ | |
95 { 4, 3}, /* 0x14 */ | |
96 {-5, -2}, /* 0x15 */ | |
97 {-2, 5}, /* 0x16 */ | |
98 { 3, -4}, /* 0x17 */ | |
99 {-4, 1}, /* 0x18 */ | |
100 { 5, -2}, /* 0x19 */ | |
101 {-2, -5}, /* 0x1A */ | |
102 { 1, 4}, /* 0x1B */ | |
103 { 4, -1}, /* 0x1C */ | |
104 {-5, 2}, /* 0x1D */ | |
105 { 2, 5}, /* 0x1E */ | |
106 {-1, -4}, /* 0x1F */ | |
107 { 4, -3}, /* 0x20 */ | |
108 {-3, 2}, /* 0x21 */ | |
109 { 2, 3}, /* 0x22 */ | |
110 {-3, -4}, /* 0x23 */ | |
111 {-4, 3}, /* 0x24 */ | |
112 { 3, -2}, /* 0x25 */ | |
113 {-2, -3}, /* 0x26 */ | |
114 { 3, 4}, /* 0x27 */ | |
115 { 4, 1}, /* 0x28 */ | |
116 {-3, -2}, /* 0x29 */ | |
117 {-2, 3}, /* 0x2A */ | |
118 { 1, -4}, /* 0x2B */ | |
119 {-4, -1}, /* 0x2C */ | |
120 { 3, 2}, /* 0x2D */ | |
121 { 2, -3}, /* 0x2E */ | |
122 {-1, 4}, /* 0x2F */ | |
123 { 0, -3}, /* 0x30 */ | |
124 { 1, 2}, /* 0x31 */ | |
125 { 2, -1}, /* 0x32 */ | |
126 {-3, 0}, /* 0x33 */ | |
127 { 0, 3}, /* 0x34 */ | |
128 {-1, -2}, /* 0x35 */ | |
129 {-2, 1}, /* 0x36 */ | |
130 { 3, 0}, /* 0x37 */ | |
131 { 0, 1}, /* 0x38 */ | |
132 { 1, -2}, /* 0x39 */ | |
133 {-2, -1}, /* 0x3A */ | |
134 { 1, 0}, /* 0x3B */ | |
135 { 0, -1}, /* 0x3C */ | |
136 {-1, 2}, /* 0x3D */ | |
137 { 2, 1}, /* 0x3E */ | |
138 {-1, 0}, /* 0x3F */ | |
139 { 8, -3}, /* 0x40 */ | |
140 {-7, 2}, /* 0x41 */ | |
141 { 2, 7}, /* 0x42 */ | |
142 {-3, -8}, /* 0x43 */ | |
143 {-8, 3}, /* 0x44 */ | |
144 { 7, -2}, /* 0x45 */ | |
145 {-2, -7}, /* 0x46 */ | |
146 { 3, 8}, /* 0x47 */ | |
147 { 8, 1}, /* 0x48 */ | |
148 {-7, -2}, /* 0x49 */ | |
149 {-2, 7}, /* 0x4A */ | |
150 { 1, -8}, /* 0x4B */ | |
151 {-8, -1}, /* 0x4C */ | |
152 { 7, 2}, /* 0x4D */ | |
153 { 2, -7}, /* 0x4E */ | |
154 {-1, 8}, /* 0x4F */ | |
155 {-4, -7}, /* 0x50 */ | |
156 { 5, 6}, /* 0x51 */ | |
157 { 6, -5}, /* 0x52 */ | |
158 {-7, 4}, /* 0x53 */ | |
159 { 4, 7}, /* 0x54 */ | |
160 {-5, -6}, /* 0x55 */ | |
161 {-6, 5}, /* 0x56 */ | |
162 { 7, -4}, /* 0x57 */ | |
163 {-4, 5}, /* 0x58 */ | |
164 { 5, -6}, /* 0x59 */ | |
165 {-6, -5}, /* 0x5A */ | |
166 { 5, 4}, /* 0x5B */ | |
167 { 4, -5}, /* 0x5C */ | |
168 {-5, 6}, /* 0x5D */ | |
169 { 6, 5}, /* 0x5E */ | |
170 {-5, -4}, /* 0x5F */ | |
171 { 4, -7}, /* 0x60 */ | |
172 {-3, 6}, /* 0x61 */ | |
173 { 6, 3}, /* 0x62 */ | |
174 {-7, -4}, /* 0x63 */ | |
175 {-4, 7}, /* 0x64 */ | |
176 { 3, -6}, /* 0x65 */ | |
177 {-6, -3}, /* 0x66 */ | |
178 { 7, 4}, /* 0x67 */ | |
179 { 4, 5}, /* 0x68 */ | |
180 {-3, -6}, /* 0x69 */ | |
181 {-6, 3}, /* 0x6A */ | |
182 { 5, -4}, /* 0x6B */ | |
183 {-4, -5}, /* 0x6C */ | |
184 { 3, 6}, /* 0x6D */ | |
185 { 6, -3}, /* 0x6E */ | |
186 {-5, 4}, /* 0x6F */ | |
187 { 0, -7}, /* 0x70 */ | |
188 { 1, 6}, /* 0x71 */ | |
189 { 6, -1}, /* 0x72 */ | |
190 {-7, 0}, /* 0x73 */ | |
191 { 0, 7}, /* 0x74 */ | |
192 {-1, -6}, /* 0x75 */ | |
193 {-6, 1}, /* 0x76 */ | |
194 { 7, 0}, /* 0x77 */ | |
195 { 0, 5}, /* 0x78 */ | |
196 { 1, -6}, /* 0x79 */ | |
197 {-6, -1}, /* 0x7A */ | |
198 { 5, 0}, /* 0x7B */ | |
199 { 0, -5}, /* 0x7C */ | |
200 {-1, 6}, /* 0x7D */ | |
201 { 6, 1}, /* 0x7E */ | |
202 {-5, 0} /* 0x7F */ | |
203 }; | |
204 | |
205 const complexf_t v17_12000_constellation[64] = | |
206 { | |
207 { 7, 1}, /* 0x00 */ | |
208 {-5, -1}, /* 0x01 */ | |
209 {-1, 5}, /* 0x02 */ | |
210 { 1, -7}, /* 0x03 */ | |
211 {-7, -1}, /* 0x04 */ | |
212 { 5, 1}, /* 0x05 */ | |
213 { 1, -5}, /* 0x06 */ | |
214 {-1, 7}, /* 0x07 */ | |
215 { 3, -3}, /* 0x08 */ | |
216 {-1, 3}, /* 0x09 */ | |
217 { 3, 1}, /* 0x0A */ | |
218 {-3, -3}, /* 0x0B */ | |
219 {-3, 3}, /* 0x0C */ | |
220 { 1, -3}, /* 0x0D */ | |
221 {-3, -1}, /* 0x0E */ | |
222 { 3, 3}, /* 0x0F */ | |
223 { 7, -7}, /* 0x10 */ | |
224 {-5, 7}, /* 0x11 */ | |
225 { 7, 5}, /* 0x12 */ | |
226 {-7, -7}, /* 0x13 */ | |
227 {-7, 7}, /* 0x14 */ | |
228 { 5, -7}, /* 0x15 */ | |
229 {-7, -5}, /* 0x16 */ | |
230 { 7, 7}, /* 0x17 */ | |
231 {-1, -7}, /* 0x18 */ | |
232 { 3, 7}, /* 0x19 */ | |
233 { 7, -3}, /* 0x1A */ | |
234 {-7, 1}, /* 0x1B */ | |
235 { 1, 7}, /* 0x1C */ | |
236 {-3, -7}, /* 0x1D */ | |
237 {-7, 3}, /* 0x1E */ | |
238 { 7, -1}, /* 0x1F */ | |
239 { 3, 5}, /* 0x20 */ | |
240 {-1, -5}, /* 0x21 */ | |
241 {-5, 1}, /* 0x22 */ | |
242 { 5, -3}, /* 0x23 */ | |
243 {-3, -5}, /* 0x24 */ | |
244 { 1, 5}, /* 0x25 */ | |
245 { 5, -1}, /* 0x26 */ | |
246 {-5, 3}, /* 0x27 */ | |
247 {-1, 1}, /* 0x28 */ | |
248 { 3, -1}, /* 0x29 */ | |
249 {-1, -3}, /* 0x2A */ | |
250 { 1, 1}, /* 0x2B */ | |
251 { 1, -1}, /* 0x2C */ | |
252 {-3, 1}, /* 0x2D */ | |
253 { 1, 3}, /* 0x2E */ | |
254 {-1, -1}, /* 0x2F */ | |
255 {-5, 5}, /* 0x30 */ | |
256 { 7, -5}, /* 0x31 */ | |
257 {-5, -7}, /* 0x32 */ | |
258 { 5, 5}, /* 0x33 */ | |
259 { 5, -5}, /* 0x34 */ | |
260 {-7, 5}, /* 0x35 */ | |
261 { 5, 7}, /* 0x36 */ | |
262 {-5, -5}, /* 0x37 */ | |
263 {-5, -3}, /* 0x38 */ | |
264 { 7, 3}, /* 0x39 */ | |
265 { 3, -7}, /* 0x3A */ | |
266 {-3, 5}, /* 0x3B */ | |
267 { 5, 3}, /* 0x3C */ | |
268 {-7, -3}, /* 0x3D */ | |
269 {-3, 7}, /* 0x3E */ | |
270 { 3, -5} /* 0x3F */ | |
271 }; | |
272 | |
273 const complexf_t v17_9600_constellation[32] = | |
274 { | |
275 {-8, 2}, /* 0x00 */ | |
276 {-6, -4}, /* 0x01 */ | |
277 {-4, 6}, /* 0x02 */ | |
278 { 2, 8}, /* 0x03 */ | |
279 { 8, -2}, /* 0x04 */ | |
280 { 6, 4}, /* 0x05 */ | |
281 { 4, -6}, /* 0x06 */ | |
282 {-2, -8}, /* 0x07 */ | |
283 { 0, 2}, /* 0x08 */ | |
284 {-6, 4}, /* 0x09 */ | |
285 { 4, 6}, /* 0x0A */ | |
286 { 2, 0}, /* 0x0B */ | |
287 { 0, -2}, /* 0x0C */ | |
288 { 6, -4}, /* 0x0D */ | |
289 {-4, -6}, /* 0x0E */ | |
290 {-2, 0}, /* 0x0F */ | |
291 { 0, -6}, /* 0x10 */ | |
292 { 2, -4}, /* 0x11 */ | |
293 {-4, -2}, /* 0x12 */ | |
294 {-6, 0}, /* 0x13 */ | |
295 { 0, 6}, /* 0x14 */ | |
296 {-2, 4}, /* 0x15 */ | |
297 { 4, 2}, /* 0x16 */ | |
298 { 6, 0}, /* 0x17 */ | |
299 { 8, 2}, /* 0x18 */ | |
300 { 2, 4}, /* 0x19 */ | |
301 { 4, -2}, /* 0x1A */ | |
302 { 2, -8}, /* 0x1B */ | |
303 {-8, -2}, /* 0x1C */ | |
304 {-2, -4}, /* 0x1D */ | |
305 {-4, 2}, /* 0x1E */ | |
306 {-2, 8} /* 0x1F */ | |
307 }; | |
308 | |
309 const complexf_t v17_7200_constellation[16] = | |
310 { | |
311 { 6, -6}, /* 0x00 */ | |
312 {-2, 6}, /* 0x01 */ | |
313 { 6, 2}, /* 0x02 */ | |
314 {-6, -6}, /* 0x03 */ | |
315 {-6, 6}, /* 0x04 */ | |
316 { 2, -6}, /* 0x05 */ | |
317 {-6, -2}, /* 0x06 */ | |
318 { 6, 6}, /* 0x07 */ | |
319 {-2, 2}, /* 0x08 */ | |
320 { 6, -2}, /* 0x09 */ | |
321 {-2, -6}, /* 0x0A */ | |
322 { 2, 2}, /* 0x0B */ | |
323 { 2, -2}, /* 0x0C */ | |
324 {-6, 2}, /* 0x0D */ | |
325 { 2, 6}, /* 0x0E */ | |
326 {-2, -2} /* 0x0F */ | |
327 }; | |
328 | |
329 /* Raised root cosine pulse shaping; Beta = 0.25; 4 symbols either | |
330 side of the centre. */ | |
331 /* Created with mkshape -r 0.05 0.25 91 -l and then split up */ | |
332 #define PULSESHAPER_GAIN (9.9888356312f/10.0f) | |
333 #define PULSESHAPER_COEFF_SETS 10 | |
334 | |
335 static const float pulseshaper[PULSESHAPER_COEFF_SETS][V17_TX_FILTER_STEPS] = | |
336 { | |
337 { | |
338 -0.0029426223f, /* Filter 0 */ | |
339 -0.0183060118f, | |
340 0.0653192857f, | |
341 -0.1703207714f, | |
342 0.6218069936f, | |
343 0.6218069936f, | |
344 -0.1703207714f, | |
345 0.0653192857f, | |
346 -0.0183060118f | |
347 }, | |
348 { | |
349 0.0031876922f, /* Filter 1 */ | |
350 -0.0300884145f, | |
351 0.0832744718f, | |
352 -0.1974255221f, | |
353 0.7664229820f, | |
354 0.4670580725f, | |
355 -0.1291107519f, | |
356 0.0424189243f, | |
357 -0.0059810465f | |
358 }, | |
359 { | |
360 0.0097229236f, /* Filter 2 */ | |
361 -0.0394811291f, | |
362 0.0931039664f, | |
363 -0.2043906784f, | |
364 0.8910868760f, | |
365 0.3122713836f, | |
366 -0.0802880559f, | |
367 0.0179050490f, | |
368 0.0052057308f | |
369 }, | |
370 { | |
371 0.0156117223f, /* Filter 3 */ | |
372 -0.0447125347f, | |
373 0.0922040267f, | |
374 -0.1862939416f, | |
375 0.9870942864f, | |
376 0.1669790517f, | |
377 -0.0301581072f, | |
378 -0.0051358510f, | |
379 0.0139350286f | |
380 }, | |
381 { | |
382 0.0197702545f, /* Filter 4 */ | |
383 -0.0443470335f, | |
384 0.0789538534f, | |
385 -0.1399184160f, | |
386 1.0476130256f, | |
387 0.0393903028f, | |
388 0.0157339854f, | |
389 -0.0241879599f, | |
390 0.0193774571f | |
391 }, | |
392 { | |
393 0.0212455717f, /* Filter 5 */ | |
394 -0.0375307894f, | |
395 0.0530516472f, | |
396 -0.0642195521f, | |
397 1.0682849922f, | |
398 -0.0642195521f, | |
399 0.0530516472f, | |
400 -0.0375307894f, | |
401 0.0212455717f | |
402 }, | |
403 { | |
404 0.0193774571f, /* Filter 6 */ | |
405 -0.0241879599f, | |
406 0.0157339854f, | |
407 0.0393903028f, | |
408 1.0476130256f, | |
409 -0.1399184160f, | |
410 0.0789538534f, | |
411 -0.0443470335f, | |
412 0.0197702545f | |
413 }, | |
414 { | |
415 0.0139350286f, /* Filter 7 */ | |
416 -0.0051358510f, | |
417 -0.0301581072f, | |
418 0.1669790517f, | |
419 0.9870942864f, | |
420 -0.1862939416f, | |
421 0.0922040267f, | |
422 -0.0447125347f, | |
423 0.0156117223f | |
424 }, | |
425 { | |
426 0.0052057308f, /* Filter 8 */ | |
427 0.0179050490f, | |
428 -0.0802880559f, | |
429 0.3122713836f, | |
430 0.8910868760f, | |
431 -0.2043906784f, | |
432 0.0931039664f, | |
433 -0.0394811291f, | |
434 0.0097229236f | |
435 }, | |
436 { | |
437 -0.0059810465f, /* Filter 9 */ | |
438 0.0424189243f, | |
439 -0.1291107519f, | |
440 0.4670580725f, | |
441 0.7664229820f, | |
442 -0.1974255221f, | |
443 0.0832744718f, | |
444 -0.0300884145f, | |
445 0.0031876922f | |
446 }, | |
447 }; | |
448 | |
449 static __inline__ int scramble(v17_tx_state_t *s, int in_bit) | |
450 { | |
451 int out_bit; | |
452 | |
453 out_bit = (in_bit ^ (s->scramble_reg >> 17) ^ (s->scramble_reg >> 22)) & 1; | |
454 s->scramble_reg = (s->scramble_reg << 1) | out_bit; | |
455 return out_bit; | |
456 } | |
457 /*- End of function --------------------------------------------------------*/ | |
458 | |
459 static __inline__ complexf_t training_get(v17_tx_state_t *s) | |
460 { | |
461 static const complexf_t abcd[4] = | |
462 { | |
463 {-6.0f, -2.0f}, | |
464 { 2.0f, -6.0f}, | |
465 { 6.0f, 2.0f}, | |
466 {-2.0f, 6.0f} | |
467 }; | |
468 static const int cdba_to_abcd[4] = | |
469 { | |
470 2, 3, 1, 0 | |
471 }; | |
472 static const int dibit_to_step[4] = | |
473 { | |
474 1, 0, 2, 3 | |
475 }; | |
476 int bits; | |
477 int shift; | |
478 | |
479 if (++s->training_step <= V17_TRAINING_SEG_3) | |
480 { | |
481 if (s->training_step <= V17_TRAINING_SEG_2) | |
482 { | |
483 if (s->training_step <= V17_TRAINING_SEG_TEP_B) | |
484 { | |
485 /* Optional segment: Unmodulated carrier (talker echo protection) */ | |
486 return abcd[0]; | |
487 } | |
488 if (s->training_step <= V17_TRAINING_SEG_1) | |
489 { | |
490 /* Optional segment: silence (talker echo protection) */ | |
491 return complex_setf(0.0f, 0.0f); | |
492 } | |
493 /* Segment 1: ABAB... */ | |
494 return abcd[(s->training_step & 1) ^ 1]; | |
495 } | |
496 /* Segment 2: CDBA... */ | |
497 /* Apply the scrambler */ | |
498 bits = scramble(s, 1); | |
499 bits = (bits << 1) | scramble(s, 1); | |
500 s->constellation_state = cdba_to_abcd[bits]; | |
501 if (s->short_train && s->training_step == V17_TRAINING_SHORT_SEG_4) | |
502 { | |
503 /* Go straight to the ones test. */ | |
504 s->training_step = V17_TRAINING_SEG_4; | |
505 } | |
506 return abcd[s->constellation_state]; | |
507 } | |
508 /* Segment 3: Bridge... */ | |
509 shift = ((s->training_step - V17_TRAINING_SEG_3 - 1) & 0x7) << 1; | |
510 span_log(&s->logging, SPAN_LOG_FLOW, "Seg 3 shift %d\n", shift); | |
511 bits = scramble(s, V17_BRIDGE_WORD >> shift); | |
512 bits = (bits << 1) | scramble(s, V17_BRIDGE_WORD >> (shift + 1)); | |
513 s->constellation_state = (s->constellation_state + dibit_to_step[bits]) & 3; | |
514 return abcd[s->constellation_state]; | |
515 } | |
516 /*- End of function --------------------------------------------------------*/ | |
517 | |
518 static __inline__ int diff_and_convolutional_encode(v17_tx_state_t *s, int q) | |
519 { | |
520 static const int diff_code[16] = | |
521 { | |
522 0, 1, 2, 3, 1, 2, 3, 0, 2, 3, 0, 1, 3, 0, 1, 2 | |
523 }; | |
524 int y1; | |
525 int y2; | |
526 int this1; | |
527 int this2; | |
528 | |
529 /* Differentially encode */ | |
530 s->diff = diff_code[((q & 0x03) << 2) | s->diff]; | |
531 | |
532 /* Convolutionally encode the redundant bit */ | |
533 y2 = s->diff >> 1; | |
534 y1 = s->diff; | |
535 this2 = y2 ^ y1 ^ (s->convolution >> 2) ^ ((y2 ^ (s->convolution >> 1)) & s->convolution); | |
536 this1 = y2 ^ (s->convolution >> 1) ^ (y1 & s->convolution); | |
537 s->convolution = ((s->convolution & 1) << 2) | ((this2 & 1) << 1) | (this1 & 1); | |
538 return ((q << 1) & 0x78) | (s->diff << 1) | ((s->convolution >> 2) & 1); | |
539 } | |
540 /*- End of function --------------------------------------------------------*/ | |
541 | |
542 static int fake_get_bit(void *user_data) | |
543 { | |
544 return 1; | |
545 } | |
546 /*- End of function --------------------------------------------------------*/ | |
547 | |
548 static __inline__ complexf_t getbaud(v17_tx_state_t *s) | |
549 { | |
550 int i; | |
551 int bit; | |
552 int bits; | |
553 | |
554 if (s->in_training) | |
555 { | |
556 if (s->training_step <= V17_TRAINING_END) | |
557 { | |
558 /* Send the training sequence */ | |
559 if (s->training_step < V17_TRAINING_SEG_4) | |
560 return training_get(s); | |
561 /* The last step in training is to send some 1's */ | |
562 if (++s->training_step > V17_TRAINING_END) | |
563 { | |
564 /* Training finished - commence normal operation. */ | |
565 s->current_get_bit = s->get_bit; | |
566 s->in_training = FALSE; | |
567 } | |
568 } | |
569 else | |
570 { | |
571 if (++s->training_step > V17_TRAINING_SHUTDOWN_A) | |
572 { | |
573 /* The shutdown sequence is 32 bauds of all 1's, then 48 bauds | |
574 of silence */ | |
575 return complex_setf(0.0f, 0.0f); | |
576 } | |
577 } | |
578 } | |
579 bits = 0; | |
580 for (i = 0; i < s->bits_per_symbol; i++) | |
581 { | |
582 if ((bit = s->current_get_bit(s->user_data)) == PUTBIT_END_OF_DATA) | |
583 { | |
584 printf("End of real data\n"); | |
585 /* End of real data. Switch to the fake get_bit routine, until we | |
586 have shut down completely. */ | |
587 s->current_get_bit = fake_get_bit; | |
588 s->in_training = TRUE; | |
589 bit = 1; | |
590 } | |
591 bits |= (scramble(s, bit) << i); | |
592 } | |
593 return s->constellation[diff_and_convolutional_encode(s, bits)]; | |
594 } | |
595 /*- End of function --------------------------------------------------------*/ | |
596 | |
597 int v17_tx(v17_tx_state_t *s, int16_t amp[], int len) | |
598 { | |
599 complexf_t x; | |
600 complexf_t z; | |
601 int i; | |
602 int sample; | |
603 | |
604 if (s->training_step >= V17_TRAINING_SHUTDOWN_END) | |
605 { | |
606 /* Once we have sent the shutdown sequence, we stop sending completely. */ | |
607 return 0; | |
608 } | |
609 for (sample = 0; sample < len; sample++) | |
610 { | |
611 if ((s->baud_phase += 3) >= 10) | |
612 { | |
613 s->baud_phase -= 10; | |
614 s->rrc_filter[s->rrc_filter_step] = | |
615 s->rrc_filter[s->rrc_filter_step + V17_TX_FILTER_STEPS] = getbaud(s); | |
616 if (++s->rrc_filter_step >= V17_TX_FILTER_STEPS) | |
617 s->rrc_filter_step = 0; | |
618 } | |
619 /* Root raised cosine pulse shaping at baseband */ | |
620 x.re = 0.0f; | |
621 x.im = 0.0f; | |
622 for (i = 0; i < V17_TX_FILTER_STEPS; i++) | |
623 { | |
624 x.re += pulseshaper[9 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].re; | |
625 x.im += pulseshaper[9 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].im; | |
626 } | |
627 /* Now create and modulate the carrier */ | |
628 z = dds_complexf(&(s->carrier_phase), s->carrier_phase_rate); | |
629 /* Don't bother saturating. We should never clip. */ | |
630 amp[sample] = (int16_t) lrintf((x.re*z.re - x.im*z.im)*s->gain); | |
631 } | |
632 return sample; | |
633 } | |
634 /*- End of function --------------------------------------------------------*/ | |
635 | |
636 void v17_tx_power(v17_tx_state_t *s, float power) | |
637 { | |
638 /* The constellation design seems to keep the average power the same, regardless | |
639 of which bit rate is in use. */ | |
640 s->gain = 0.223f*powf(10.0f, (power - DBM0_MAX_POWER)/20.0f)*32768.0f/PULSESHAPER_GAIN; | |
641 } | |
642 /*- End of function --------------------------------------------------------*/ | |
643 | |
644 void v17_tx_set_get_bit(v17_tx_state_t *s, get_bit_func_t get_bit, void *user_data) | |
645 { | |
646 if (s->get_bit == s->current_get_bit) | |
647 s->current_get_bit = get_bit; | |
648 s->get_bit = get_bit; | |
649 s->user_data = user_data; | |
650 } | |
651 /*- End of function --------------------------------------------------------*/ | |
652 | |
653 int v17_tx_restart(v17_tx_state_t *s, int rate, int tep, int short_train) | |
654 { | |
655 switch (rate) | |
656 { | |
657 case 14400: | |
658 s->bits_per_symbol = 6; | |
659 s->constellation = v17_14400_constellation; | |
660 break; | |
661 case 12000: | |
662 s->bits_per_symbol = 5; | |
663 s->constellation = v17_12000_constellation; | |
664 break; | |
665 case 9600: | |
666 s->bits_per_symbol = 4; | |
667 s->constellation = v17_9600_constellation; | |
668 break; | |
669 case 7200: | |
670 s->bits_per_symbol = 3; | |
671 s->constellation = v17_7200_constellation; | |
672 break; | |
673 default: | |
674 return -1; | |
675 } | |
676 /* NB: some modems seem to use 3 instead of 1 for long training */ | |
677 s->diff = (short_train) ? 0 : 1; | |
678 s->bit_rate = rate; | |
679 cvec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0])); | |
680 s->rrc_filter_step = 0; | |
681 s->convolution = 0; | |
682 s->scramble_reg = 0x2ECDD5; | |
683 s->in_training = TRUE; | |
684 s->short_train = short_train; | |
685 s->training_step = (tep) ? V17_TRAINING_SEG_TEP_A : V17_TRAINING_SEG_1; | |
686 s->carrier_phase = 0; | |
687 s->baud_phase = 0; | |
688 s->constellation_state = 0; | |
689 s->current_get_bit = fake_get_bit; | |
690 return 0; | |
691 } | |
692 /*- End of function --------------------------------------------------------*/ | |
693 | |
694 v17_tx_state_t *v17_tx_init(v17_tx_state_t *s, int rate, int tep, get_bit_func_t get_bit, void *user_data) | |
695 { | |
696 if (s == NULL) | |
697 { | |
698 if ((s = (v17_tx_state_t *) malloc(sizeof(*s))) == NULL) | |
699 return NULL; | |
700 } | |
701 memset(s, 0, sizeof(*s)); | |
702 s->get_bit = get_bit; | |
703 s->user_data = user_data; | |
704 s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ); | |
705 v17_tx_power(s, -14.0f); | |
706 v17_tx_restart(s, rate, tep, FALSE); | |
707 return s; | |
708 } | |
709 /*- End of function --------------------------------------------------------*/ | |
710 | |
711 int v17_tx_release(v17_tx_state_t *s) | |
712 { | |
713 free(s); | |
714 return 0; | |
715 } | |
716 /*- End of function --------------------------------------------------------*/ | |
717 /*- End of file ------------------------------------------------------------*/ |