Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


fskmodem_float.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Includes code and algorithms from the Zapata library.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20 
21 /*! \file
22  *
23  * \brief FSK Modulator/Demodulator
24  *
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \arg Includes code and algorithms from the Zapata library.
28  *
29  */
30 
31 /*** MODULEINFO
32  <support_level>core</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
38 
39 #include <stdio.h>
40 
41 #include "asterisk/fskmodem.h"
42 
43 #define NBW 2
44 #define BWLIST {75,800}
45 #define NF 6
46 #define FLIST {1400,1800,1200,2200,1300,2100}
47 
48 #define STATE_SEARCH_STARTBIT 0
49 #define STATE_SEARCH_STARTBIT2 1
50 #define STATE_SEARCH_STARTBIT3 2
51 #define STATE_GET_BYTE 3
52 
53 static inline float get_sample(short **buffer, int *len)
54 {
55  float retval;
56  retval = (float) **buffer / 256;
57  (*buffer)++;
58  (*len)--;
59  return retval;
60 };
61 
62 #define GET_SAMPLE get_sample(&buffer, len)
63 
64 /*! \brief Coefficients for input filters
65  * Coefficients table, generated by program "mkfilter"
66  * mkfilter is part of the zapatatelephony.org distribution
67  * Format: coef[IDX_FREC][IDX_BW][IDX_COEF]
68  * IDX_COEF = 0 => 1/GAIN
69  * IDX_COEF = 1-6 => Coefficientes y[n]
70 */
71 static double coef_in[NF][NBW][8] = {
72  {
73  { 1.8229206611e-04,-7.8997325866e-01,2.2401819940e+00,-4.6751353581e+00,5.5080745712e+00,-5.0571565772e+00,2.6215820004e+00,0.0000000000e+00, },
74  { 9.8532175289e-02,-5.6297236492e-02,3.3146713415e-01,-9.2239200436e-01,1.4844365184e+00,-2.0183258642e+00,2.0074154497e+00,0.0000000000e+00, },
75  },
76  {
77  { 1.8229206610e-04,-7.8997325866e-01,7.7191410839e-01,-2.8075643964e+00,1.6948618347e+00,-3.0367273700e+00,9.0333559408e-01,0.0000000000e+00, } ,
78  { 9.8531161839e-02,-5.6297236492e-02,1.1421579050e-01,-4.8122536483e-01,4.0121072432e-01,-7.4834487567e-01,6.9170822332e-01,0.0000000000e+00, },
79  },
80  {
81  { 1.8229206611e-04,-7.8997325866e-01,2.9003821430e+00,-6.1082779024e+00,7.7169345751e+00,-6.6075999680e+00,3.3941838836e+00,0.0000000000e+00, },
82  { 9.8539686961e-02,-5.6297236492e-02,4.2915323820e-01,-1.2609358633e+00,2.2399213250e+00,-2.9928879142e+00,2.5990173742e+00,0.0000000000e+00, },
83  },
84  {
85  { 1.8229206610e-04,-7.8997325866e-01,-7.7191410839e-01,-2.8075643964e+00,-1.6948618347e+00,-3.0367273700e+00,-9.0333559408e-01,0.0000000000e+00, },
86  { 9.8531161839e-02,-5.6297236492e-02,-1.1421579050e-01,-4.8122536483e-01,-4.0121072432e-01,-7.4834487567e-01,-6.9170822332e-01,0.0000000000e+00, },
87  },
88  {
89  { 1.8229206611e-04,-7.8997325866e-01,2.5782298908e+00,-5.3629717478e+00,6.5890882172e+00,-5.8012914776e+00,3.0171839130e+00,0.0000000000e+00, },
90  { 9.8534230718e-02,-5.6297236492e-02,3.8148618075e-01,-1.0848760410e+00,1.8441165168e+00,-2.4860666655e+00,2.3103384142e+00,0.0000000000e+00, },
91  },
92  {
93  { 1.8229206610e-04,-7.8997325866e-01,-3.8715051001e-01,-2.6192408538e+00,-8.3977994034e-01,-2.8329897913e+00,-4.5306444352e-01,0.0000000000e+00, },
94  { 9.8531160936e-02,-5.6297236492e-02,-5.7284484199e-02,-4.3673866734e-01,-1.9564766257e-01,-6.2028156584e-01,-3.4692356122e-01,0.0000000000e+00, },
95  },
96 };
97 
98 /*! \brief Coefficients for output filter
99  * Coefficients table, generated by program "mkfilter"
100  * Format: coef[IDX_BW][IDX_COEF]
101  * IDX_COEF = 0 => 1/GAIN
102  * IDX_COEF = 1-6 => Coefficientes y[n]
103  */
104 static double coef_out[NBW][8] = {
105  { 1.3868644653e-08,-6.3283665042e-01,4.0895057217e+00,-1.1020074592e+01,1.5850766191e+01,-1.2835109292e+01,5.5477477340e+00,0.0000000000e+00, },
106  { 3.1262119724e-03,-7.8390522307e-03,8.5209627801e-02,-4.0804129163e-01,1.1157139955e+00,-1.8767603680e+00,1.8916395224e+00,0.0000000000e+00, },
107 };
108 
109 
110 /*! Band-pass filter for MARK frequency */
111 static inline float filterM(fsk_data *fskd,float in)
112 {
113  int i, j;
114  double s;
115  double *pc;
116 
117  pc = &coef_in[fskd->f_mark_idx][fskd->bw][0];
118  fskd->fmxv[(fskd->fmp+6)&7] = in*(*pc++);
119 
120  s = (fskd->fmxv[(fskd->fmp + 6) & 7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp + 2) & 7] - fskd->fmxv[(fskd->fmp + 4) & 7]);
121  for (i = 0, j = fskd->fmp; i < 6; i++, j++)
122  s += fskd->fmyv[j&7]*(*pc++);
123  fskd->fmyv[j&7] = s;
124  fskd->fmp++;
125  fskd->fmp &= 7;
126  return s;
127 }
128 
129 /*! Band-pass filter for SPACE frequency */
130 static inline float filterS(fsk_data *fskd,float in)
131 {
132  int i, j;
133  double s;
134  double *pc;
135 
136  pc = &coef_in[fskd->f_space_idx][fskd->bw][0];
137  fskd->fsxv[(fskd->fsp+6)&7] = in*(*pc++);
138 
139  s = (fskd->fsxv[(fskd->fsp + 6) & 7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp + 2) & 7] - fskd->fsxv[(fskd->fsp + 4) & 7]);
140  for (i = 0, j = fskd->fsp; i < 6; i++, j++)
141  s += fskd->fsyv[j&7]*(*pc++);
142  fskd->fsyv[j&7] = s;
143  fskd->fsp++;
144  fskd->fsp &= 7;
145  return s;
146 }
147 
148 /*! Low-pass filter for demodulated data */
149 static inline float filterL(fsk_data *fskd,float in)
150 {
151  int i, j;
152  double s;
153  double *pc;
154 
155  pc = &coef_out[fskd->bw][0];
156  fskd->flxv[(fskd->flp + 6) & 7] = in * (*pc++);
157 
158  s = (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
159  6 * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
160  15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
161  20 * fskd->flxv[(fskd->flp+3)&7];
162 
163  for (i = 0,j = fskd->flp;i<6;i++,j++)
164  s += fskd->flyv[j&7]*(*pc++);
165  fskd->flyv[j&7] = s;
166  fskd->flp++;
167  fskd->flp &= 7;
168  return s;
169 }
170 
171 static inline int demodulator(fsk_data *fskd, float *retval, float x)
172 {
173  float xS,xM;
174 
175  fskd->cola_in[fskd->pcola] = x;
176 
177  xS = filterS(fskd,x);
178  xM = filterM(fskd,x);
179 
180  fskd->cola_filter[fskd->pcola] = xM-xS;
181 
182  x = filterL(fskd,xM*xM - xS*xS);
183 
184  fskd->cola_demod[fskd->pcola++] = x;
185  fskd->pcola &= (NCOLA-1);
186 
187  *retval = x;
188  return 0;
189 }
190 
191 static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
192 {
193  /* This function implements a DPLL to synchronize with the bits */
194  float x,spb,spb2,ds;
195  int f;
196 
197  spb = fskd->spb;
198  if (fskd->spb == 7)
199  spb = 8000.0 / 1200.0;
200  ds = spb/32.;
201  spb2 = spb/2.;
202 
203  for (f = 0;;) {
204  if (demodulator(fskd, &x, GET_SAMPLE))
205  return -1;
206  if ((x * fskd->x0) < 0) { /* Transition */
207  if (!f) {
208  if (fskd->cont<(spb2))
209  fskd->cont += ds;
210  else
211  fskd->cont -= ds;
212  f = 1;
213  }
214  }
215  fskd->x0 = x;
216  fskd->cont += 1.;
217  if (fskd->cont > spb) {
218  fskd->cont -= spb;
219  break;
220  }
221  }
222  f = (x > 0) ? 0x80 : 0;
223  return f;
224 }
225 
226 int fsk_serial(fsk_data *fskd, short *buffer, int *len, int *outbyte)
227 {
228  int a;
229  int i,j,n1,r;
230  int samples = 0;
231  int olen;
232 
233  switch (fskd->state) {
234  /* Pick up where we left off */
236  goto search_startbit2;
238  goto search_startbit3;
239  case STATE_GET_BYTE:
240  goto getbyte;
241  }
242  /* We await for start bit */
243  do {
244  /* this was jesus's nice, reasonable, working (at least with RTTY) code
245  to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
246  just start sending a start bit with nothing preceding it at the beginning
247  of a transmission (what a LOSING design), we cant do it this elegantly */
248  /*
249  if (demodulator(zap,&x1)) return(-1);
250  for (;;) {
251  if (demodulator(zap,&x2)) return(-1);
252  if (x1>0 && x2<0) break;
253  x1 = x2;
254  }
255  */
256  /* this is now the imprecise, losing, but functional code to detect the
257  beginning of a start bit in the TDD sceanario. It just looks for sufficient
258  level to maybe, perhaps, guess, maybe that its maybe the beginning of
259  a start bit, perhaps. This whole thing stinks! */
260  if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
261  return -1;
262  samples++;
263  for (;;) {
264 search_startbit2:
265  if (*len <= 0) {
267  return 0;
268  }
269  samples++;
270  if (demodulator(fskd, &fskd->x2, GET_SAMPLE))
271  return(-1);
272 #if 0
273  printf("x2 = %5.5f ", fskd->x2);
274 #endif
275  if (fskd->x2 < -0.5)
276  break;
277  }
278 search_startbit3:
279  /* We await for 0.5 bits before using DPLL */
280  i = fskd->spb/2;
281  if (*len < i) {
283  return 0;
284  }
285  for (; i>0; i--) {
286  if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
287  return(-1);
288 #if 0
289  printf("x1 = %5.5f ", fskd->x1);
290 #endif
291  samples++;
292  }
293 
294  /* x1 must be negative (start bit confirmation) */
295 
296  } while (fskd->x1 > 0);
297  fskd->state = STATE_GET_BYTE;
298 
299 getbyte:
300 
301  /* Need at least 80 samples (for 1200) or
302  1320 (for 45.5) to be sure we'll have a byte */
303  if (fskd->nbit < 8) {
304  if (*len < 1320)
305  return 0;
306  } else {
307  if (*len < 80)
308  return 0;
309  }
310  /* Now we read the data bits */
311  j = fskd->nbit;
312  for (a = n1 = 0; j; j--) {
313  olen = *len;
314  i = get_bit_raw(fskd, buffer, len);
315  buffer += (olen - *len);
316  if (i == -1)
317  return(-1);
318  if (i)
319  n1++;
320  a >>= 1;
321  a |= i;
322  }
323  j = 8-fskd->nbit;
324  a >>= j;
325 
326  /* We read parity bit (if exists) and check parity */
327  if (fskd->parity) {
328  olen = *len;
329  i = get_bit_raw(fskd, buffer, len);
330  buffer += (olen - *len);
331  if (i == -1)
332  return(-1);
333  if (i)
334  n1++;
335  if (fskd->parity == 1) { /* parity=1 (even) */
336  if (n1&1)
337  a |= 0x100; /* error */
338  } else { /* parity=2 (odd) */
339  if (!(n1&1))
340  a |= 0x100; /* error */
341  }
342  }
343 
344  /* We read STOP bits. All of them must be 1 */
345 
346  for (j = fskd->nstop;j;j--) {
347  r = get_bit_raw(fskd, buffer, len);
348  if (r == -1)
349  return(-1);
350  if (!r)
351  a |= 0x200;
352  }
353 
354  /* And finally we return */
355  /* Bit 8 : Parity error */
356  /* Bit 9 : Framming error*/
357 
358  *outbyte = a;
360  return 1;
361 }
#define NCOLA
Asterisk main include file. File version handling, generic pbx functions.
static float filterM(fsk_data *fskd, float in)
double fmyv[8]
double fsyv[8]
#define STATE_SEARCH_STARTBIT3
static float filterL(fsk_data *fskd, float in)
double fsxv[8]
static float get_sample(short **buffer, int *len)
float cont
double fmxv[8]
float nstop
#define STATE_SEARCH_STARTBIT2
static int demodulator(fsk_data *fskd, float *retval, float x)
#define STATE_GET_BYTE
float cola_in[NCOLA]
#define NF
static double coef_out[NBW][8]
Coefficients for output filter Coefficients table, generated by program &quot;mkfilter&quot; Format: coef[IDX_B...
float cola_filter[NCOLA]
static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
static double coef_in[NF][NBW][8]
Coefficients for input filters Coefficients table, generated by program &quot;mkfilter&quot; mkfilter is part o...
#define NBW
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
double flyv[8]
static float filterS(fsk_data *fskd, float in)
int f_space_idx
static struct ast_format f[]
Definition: format_g726.c:181
#define STATE_SEARCH_STARTBIT
float cola_demod[NCOLA]
#define GET_SAMPLE
int f_mark_idx
FSK Modem Support.
int fsk_serial(fsk_data *fskd, short *buffer, int *len, int *outbyte)
double flxv[8]
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150