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