d35b83603854f0d6b1ebff9e61c60f05dcd44b15
[asterisk/asterisk.git] / main / fskmodem.c
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  * \todo - REMOVE ALL SPANISH COMMENTS AND TRANSLATE THEM TO ENGLISH. Thank you.
30  *      Swedish will work too :-)
31  */
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <stdio.h>
38
39 #include "asterisk/fskmodem.h"
40
41 #define NBW     2
42 #define BWLIST  {75,800}
43 #define NF      6
44 #define FLIST {1400,1800,1200,2200,1300,2100}
45
46 #define STATE_SEARCH_STARTBIT   0
47 #define STATE_SEARCH_STARTBIT2  1
48 #define STATE_SEARCH_STARTBIT3  2
49 #define STATE_GET_BYTE                  3
50
51 static inline float get_sample(short **buffer, int *len)
52 {
53         float retval;
54         retval = (float) **buffer / 256;
55         (*buffer)++;
56         (*len)--;
57         return retval;
58 };
59
60 #define GET_SAMPLE get_sample(&buffer, len)
61
62 /* Coeficientes para filtros de entrada                                 */
63 /* Tabla de coeficientes, generada a partir del programa "mkfilter"     */
64 /* Formato: coef[IDX_FREC][IDX_BW][IDX_COEF]                            */
65 /* IDX_COEF = 0 =>      1/GAIN                                          */
66 /* IDX_COEF = 1-6       =>      Coeficientes y[n]                       */
67
68 static double coef_in[NF][NBW][8] = {
69  {
70         { 1.8229206611e-04,-7.8997325866e-01,2.2401819940e+00,-4.6751353581e+00,5.5080745712e+00,-5.0571565772e+00,2.6215820004e+00,0.0000000000e+00, },  
71         { 9.8532175289e-02,-5.6297236492e-02,3.3146713415e-01,-9.2239200436e-01,1.4844365184e+00,-2.0183258642e+00,2.0074154497e+00,0.0000000000e+00, }, 
72  }, 
73  { 
74         { 1.8229206610e-04,-7.8997325866e-01,7.7191410839e-01,-2.8075643964e+00,1.6948618347e+00,-3.0367273700e+00,9.0333559408e-01,0.0000000000e+00, } ,
75         { 9.8531161839e-02,-5.6297236492e-02,1.1421579050e-01,-4.8122536483e-01,4.0121072432e-01,-7.4834487567e-01,6.9170822332e-01,0.0000000000e+00, }, 
76  },
77  {
78         { 1.8229206611e-04,-7.8997325866e-01,2.9003821430e+00,-6.1082779024e+00,7.7169345751e+00,-6.6075999680e+00,3.3941838836e+00,0.0000000000e+00, }, 
79         { 9.8539686961e-02,-5.6297236492e-02,4.2915323820e-01,-1.2609358633e+00,2.2399213250e+00,-2.9928879142e+00,2.5990173742e+00,0.0000000000e+00, },
80   },
81   {
82         { 1.8229206610e-04,-7.8997325866e-01,-7.7191410839e-01,-2.8075643964e+00,-1.6948618347e+00,-3.0367273700e+00,-9.0333559408e-01,0.0000000000e+00, },
83         { 9.8531161839e-02,-5.6297236492e-02,-1.1421579050e-01,-4.8122536483e-01,-4.0121072432e-01,-7.4834487567e-01,-6.9170822332e-01,0.0000000000e+00, },
84   },
85   {
86         { 1.8229206611e-04,-7.8997325866e-01,2.5782298908e+00,-5.3629717478e+00,6.5890882172e+00,-5.8012914776e+00,3.0171839130e+00,0.0000000000e+00, }, 
87         { 9.8534230718e-02,-5.6297236492e-02,3.8148618075e-01,-1.0848760410e+00,1.8441165168e+00,-2.4860666655e+00,2.3103384142e+00,0.0000000000e+00, },  
88   },
89   {
90         { 1.8229206610e-04,-7.8997325866e-01,-3.8715051001e-01,-2.6192408538e+00,-8.3977994034e-01,-2.8329897913e+00,-4.5306444352e-01,0.0000000000e+00, },
91         { 9.8531160936e-02,-5.6297236492e-02,-5.7284484199e-02,-4.3673866734e-01,-1.9564766257e-01,-6.2028156584e-01,-3.4692356122e-01,0.0000000000e+00, },
92   }, 
93 };
94
95 /* Coeficientes para filtro de salida                                   */
96 /* Tabla de coeficientes, generada a partir del programa "mkfilter"     */
97 /* Formato: coef[IDX_BW][IDX_COEF]                                      */
98 /* IDX_COEF = 0 =>      1/GAIN                                          */
99 /* IDX_COEF = 1-6       =>      Coeficientes y[n]                       */
100
101 static double coef_out[NBW][8] = {
102         { 1.3868644653e-08,-6.3283665042e-01,4.0895057217e+00,-1.1020074592e+01,1.5850766191e+01,-1.2835109292e+01,5.5477477340e+00,0.0000000000e+00, },
103         { 3.1262119724e-03,-7.8390522307e-03,8.5209627801e-02,-4.0804129163e-01,1.1157139955e+00,-1.8767603680e+00,1.8916395224e+00,0.0000000000e+00, }, 
104 };
105
106
107 /*! Filtro pasa-banda para frecuencia de MARCA */
108 static inline float filtroM(fsk_data *fskd,float in)
109 {
110         int i, j;
111         double s;
112         double *pc;
113         
114         pc = &coef_in[fskd->f_mark_idx][fskd->bw][0];
115         fskd->fmxv[(fskd->fmp+6)&7] = in*(*pc++);
116         
117         s = (fskd->fmxv[(fskd->fmp + 6) & 7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp + 2) & 7] - fskd->fmxv[(fskd->fmp + 4) & 7]);
118         for (i = 0, j = fskd->fmp; i < 6; i++, j++) 
119                 s += fskd->fmyv[j&7]*(*pc++);
120         fskd->fmyv[j&7] = s;
121         fskd->fmp++;
122         fskd->fmp &= 7;
123         return s;
124 }
125
126 /*! Filtro pasa-banda para frecuencia de ESPACIO */
127 static inline float filtroS(fsk_data *fskd,float in)
128 {
129         int i, j;
130         double s;
131         double *pc;
132         
133         pc = &coef_in[fskd->f_space_idx][fskd->bw][0];
134         fskd->fsxv[(fskd->fsp+6)&7] = in*(*pc++);
135         
136         s = (fskd->fsxv[(fskd->fsp + 6) & 7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp + 2) & 7] - fskd->fsxv[(fskd->fsp + 4) & 7]);
137         for (i = 0, j = fskd->fsp; i < 6; i++, j++) 
138                 s += fskd->fsyv[j&7]*(*pc++);
139         fskd->fsyv[j&7] = s;
140         fskd->fsp++;
141         fskd->fsp &= 7;
142         return s;
143 }
144
145 /*! Filtro pasa-bajos para datos demodulados */
146 static inline float filtroL(fsk_data *fskd,float in)
147 {
148         int i, j;
149         double s;
150         double *pc;
151         
152         pc = &coef_out[fskd->bw][0];
153         fskd->flxv[(fskd->flp + 6) & 7] = in * (*pc++); 
154         
155         s = (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
156           6  * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
157           15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
158           20 *  fskd->flxv[(fskd->flp+3)&7]; 
159         
160         for (i = 0,j = fskd->flp;i<6;i++,j++)
161                 s += fskd->flyv[j&7]*(*pc++);
162         fskd->flyv[j&7] = s;
163         fskd->flp++;
164         fskd->flp &= 7;
165         return s;
166 }
167
168 static inline int demodulador(fsk_data *fskd, float *retval, float x)
169 {
170         float xS,xM;
171
172         fskd->cola_in[fskd->pcola] = x;
173         
174         xS = filtroS(fskd,x);
175         xM = filtroM(fskd,x);
176
177         fskd->cola_filtro[fskd->pcola] = xM-xS;
178
179         x = filtroL(fskd,xM*xM - xS*xS);
180         
181         fskd->cola_demod[fskd->pcola++] = x;
182         fskd->pcola &=  (NCOLA-1);
183
184         *retval = x;
185         return 0;
186 }
187
188 static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
189 {
190         /* Esta funcion implementa un DPLL para sincronizarse con los bits */
191         float x,spb,spb2,ds;
192         int f;
193
194         spb = fskd->spb; 
195         if (fskd->spb == 7)
196                 spb = 8000.0 / 1200.0;
197         ds = spb/32.;
198         spb2 = spb/2.;
199
200         for (f = 0;;) {
201                 if (demodulador(fskd, &x, GET_SAMPLE))
202                         return -1;
203                 if ((x * fskd->x0) < 0) {       /* Transicion */
204                         if (!f) {
205                                 if (fskd->cont<(spb2))
206                                         fskd->cont += ds;
207                                 else
208                                         fskd->cont -= ds;
209                                 f = 1;
210                         }
211                 }
212                 fskd->x0 = x;
213                 fskd->cont += 1.;
214                 if (fskd->cont > spb) {
215                         fskd->cont -= spb;
216                         break;
217                 }
218         }
219         f = (x > 0) ? 0x80 : 0;
220         return f;
221 }
222
223 int fsk_serie(fsk_data *fskd, short *buffer, int *len, int *outbyte)
224 {
225         int a;
226         int i,j,n1,r;
227         int samples = 0;
228         int olen;
229
230         switch(fskd->state) {
231                 /* Pick up where we left off */
232         case STATE_SEARCH_STARTBIT2:
233                 goto search_startbit2;
234         case STATE_SEARCH_STARTBIT3:
235                 goto search_startbit3;
236         case STATE_GET_BYTE:
237                 goto getbyte;
238         }
239         /* Esperamos bit de start       */
240         do {
241                 /* this was jesus's nice, reasonable, working (at least with RTTY) code
242                 to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
243                 just start sending a start bit with nothing preceding it at the beginning
244                 of a transmission (what a LOSING design), we cant do it this elegantly */
245                 /*
246                 if (demodulador(zap,&x1)) return(-1);
247                 for(;;) {
248                         if (demodulador(zap,&x2)) return(-1);
249                         if (x1>0 && x2<0) break;
250                         x1 = x2;
251                 }
252                 */
253                 /* this is now the imprecise, losing, but functional code to detect the
254                 beginning of a start bit in the TDD sceanario. It just looks for sufficient
255                 level to maybe, perhaps, guess, maybe that its maybe the beginning of
256                 a start bit, perhaps. This whole thing stinks! */
257                 if (demodulador(fskd,&fskd->x1,GET_SAMPLE))
258                         return -1;
259                 samples++;
260                 for(;;) {
261 search_startbit2:                  
262                         if (!*len) {
263                                 fskd->state  =  STATE_SEARCH_STARTBIT2;
264                                 return 0;
265                         }
266                         samples++;
267                         if (demodulador(fskd,&fskd->x2,GET_SAMPLE))
268                                 return(-1);
269 #if 0
270                         printf("x2  =  %5.5f ", fskd->x2);
271 #endif                  
272                         if (fskd->x2 < -0.5)
273                                 break; 
274                 }
275 search_startbit3:                  
276                 /* Esperamos 0.5 bits antes de usar DPLL */
277                 i = fskd->spb/2;
278                 if (*len < i) {
279                         fskd->state = STATE_SEARCH_STARTBIT3;
280                         return 0;
281                 }
282                 for(;i;i--) {
283                         if (demodulador(fskd,&fskd->x1,GET_SAMPLE))
284                                 return(-1); 
285 #if 0
286                         printf("x1 = %5.5f ", fskd->x1);
287 #endif                  
288                         samples++;
289                 }
290
291                 /* x1 debe ser negativo (confirmaciĆ³n del bit de start) */
292
293         } while (fskd->x1 > 0);
294         fskd->state = STATE_GET_BYTE;
295
296 getbyte:
297
298         /* Need at least 80 samples (for 1200) or
299                 1320 (for 45.5) to be sure we'll have a byte */
300         if (fskd->nbit < 8) {
301                 if (*len < 1320)
302                         return 0;
303         } else {
304                 if (*len < 80)
305                         return 0;
306         }
307         /* Leemos ahora los bits de datos */
308         j = fskd->nbit;
309         for (a = n1 = 0; j; j--) {
310                 olen = *len;
311                 i = get_bit_raw(fskd, buffer, len);
312                 buffer += (olen - *len);
313                 if (i == -1)
314                         return(-1);
315                 if (i)
316                         n1++;
317                 a >>= 1;
318                 a |= i;
319         }
320         j = 8-fskd->nbit;
321         a >>= j;
322
323         /* Leemos bit de paridad (si existe) y la comprobamos */
324         if (fskd->paridad) {
325                 olen = *len;
326                 i = get_bit_raw(fskd, buffer, len); 
327                 buffer += (olen - *len);
328                 if (i == -1)
329                         return(-1);
330                 if (i)
331                         n1++;
332                 if (fskd->paridad == 1) {       /* paridad = 1 (par) */
333                         if (n1&1)
334                                 a |= 0x100;             /* error */
335                 } else {                        /* paridad = 2 (impar) */
336                         if (!(n1&1))
337                                 a |= 0x100;     /* error */
338                 }
339         }
340         
341         /* Leemos bits de STOP. Todos deben ser 1 */
342         
343         for (j = fskd->nstop;j;j--) {
344                 r = get_bit_raw(fskd, buffer, len);
345                 if (r == -1)
346                         return(-1);
347                 if (!r)
348                         a |= 0x200;
349         }
350
351         /* Por fin retornamos  */
352         /* Bit 8 : Error de paridad */
353         /* Bit 9 : Error de Framming */
354
355         *outbyte = a;
356         fskd->state = STATE_SEARCH_STARTBIT;
357         return 1;
358 }