Let's also include aclocal.m4
[asterisk-bristuff.git] / main / fskmodem.c
blob3ab462886f67794bf640d3d7d5be830d9e20978f
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 *
8 * Includes code and algorithms from the Zapata library.
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.
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.
21 /*! \file
23 * \brief FSK Modulator/Demodulator
25 * \author Mark Spencer <markster@digium.com>
27 * \arg Includes code and algorithms from the Zapata library.
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <stdio.h>
36 #include "asterisk/fskmodem.h"
38 #define NBW 2
39 #define BWLIST {75,800}
40 #define NF 6
41 #define FLIST {1400,1800,1200,2200,1300,2100}
43 #define STATE_SEARCH_STARTBIT 0
44 #define STATE_SEARCH_STARTBIT2 1
45 #define STATE_SEARCH_STARTBIT3 2
46 #define STATE_GET_BYTE 3
48 static inline float get_sample(short **buffer, int *len)
50 float retval;
51 retval = (float) **buffer / 256;
52 (*buffer)++;
53 (*len)--;
54 return retval;
57 #define GET_SAMPLE get_sample(&buffer, len)
59 /* Coeficientes para filtros de entrada */
60 /* Tabla de coeficientes, generada a partir del programa "mkfilter" */
61 /* Formato: coef[IDX_FREC][IDX_BW][IDX_COEF] */
62 /* IDX_COEF=0 => 1/GAIN */
63 /* IDX_COEF=1-6 => Coeficientes y[n] */
65 static double coef_in[NF][NBW][8]={
66 #include "coef_in.h"
69 /* Coeficientes para filtro de salida */
70 /* Tabla de coeficientes, generada a partir del programa "mkfilter" */
71 /* Formato: coef[IDX_BW][IDX_COEF] */
72 /* IDX_COEF=0 => 1/GAIN */
73 /* IDX_COEF=1-6 => Coeficientes y[n] */
75 static double coef_out[NBW][8]={
76 #include "coef_out.h"
80 /*! Filtro pasa-banda para frecuencia de MARCA */
81 static inline float filtroM(fsk_data *fskd,float in)
83 int i,j;
84 double s;
85 double *pc;
87 pc=&coef_in[fskd->f_mark_idx][fskd->bw][0];
88 fskd->fmxv[(fskd->fmp+6)&7]=in*(*pc++);
90 s=(fskd->fmxv[(fskd->fmp+6)&7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp+2)&7] - fskd->fmxv[(fskd->fmp+4)&7]);
91 for (i=0,j=fskd->fmp;i<6;i++,j++) s+=fskd->fmyv[j&7]*(*pc++);
92 fskd->fmyv[j&7]=s;
93 fskd->fmp++; fskd->fmp&=7;
94 return s;
97 /*! Filtro pasa-banda para frecuencia de ESPACIO */
98 static inline float filtroS(fsk_data *fskd,float in)
100 int i,j;
101 double s;
102 double *pc;
104 pc=&coef_in[fskd->f_space_idx][fskd->bw][0];
105 fskd->fsxv[(fskd->fsp+6)&7]=in*(*pc++);
107 s=(fskd->fsxv[(fskd->fsp+6)&7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp+2)&7] - fskd->fsxv[(fskd->fsp+4)&7]);
108 for (i=0,j=fskd->fsp;i<6;i++,j++) s+=fskd->fsyv[j&7]*(*pc++);
109 fskd->fsyv[j&7]=s;
110 fskd->fsp++; fskd->fsp&=7;
111 return s;
114 /*! Filtro pasa-bajos para datos demodulados */
115 static inline float filtroL(fsk_data *fskd,float in)
117 int i,j;
118 double s;
119 double *pc;
121 pc=&coef_out[fskd->bw][0];
122 fskd->flxv[(fskd->flp + 6) & 7]=in * (*pc++);
124 s= (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
125 6 * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
126 15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
127 20 * fskd->flxv[(fskd->flp+3)&7];
129 for (i=0,j=fskd->flp;i<6;i++,j++) s+=fskd->flyv[j&7]*(*pc++);
130 fskd->flyv[j&7]=s;
131 fskd->flp++; fskd->flp&=7;
132 return s;
135 static inline int demodulador(fsk_data *fskd, float *retval, float x)
137 float xS,xM;
139 fskd->cola_in[fskd->pcola]=x;
141 xS=filtroS(fskd,x);
142 xM=filtroM(fskd,x);
144 fskd->cola_filtro[fskd->pcola]=xM-xS;
146 x=filtroL(fskd,xM*xM - xS*xS);
148 fskd->cola_demod[fskd->pcola++]=x;
149 fskd->pcola &= (NCOLA-1);
151 *retval = x;
152 return(0);
155 static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
157 /* Esta funcion implementa un DPLL para sincronizarse con los bits */
158 float x,spb,spb2,ds;
159 int f;
161 spb=fskd->spb;
162 if (fskd->spb == 7) spb = 8000.0 / 1200.0;
163 ds=spb/32.;
164 spb2=spb/2.;
166 for (f=0;;){
167 if (demodulador(fskd,&x, GET_SAMPLE)) return(-1);
168 if ((x*fskd->x0)<0) { /* Transicion */
169 if (!f) {
170 if (fskd->cont<(spb2)) fskd->cont+=ds; else fskd->cont-=ds;
171 f=1;
174 fskd->x0=x;
175 fskd->cont+=1.;
176 if (fskd->cont>spb) {
177 fskd->cont-=spb;
178 break;
181 f=(x>0)?0x80:0;
182 return(f);
185 int fsk_serie(fsk_data *fskd, short *buffer, int *len, int *outbyte)
187 int a;
188 int i,j,n1,r;
189 int samples=0;
190 int olen;
191 int beginlen=*len;
192 int beginlenx;
194 switch(fskd->state) {
195 /* Pick up where we left off */
196 case STATE_SEARCH_STARTBIT2:
197 goto search_startbit2;
198 case STATE_SEARCH_STARTBIT3:
199 goto search_startbit3;
200 case STATE_GET_BYTE:
201 goto getbyte;
203 /* Esperamos bit de start */
204 do {
205 /* this was jesus's nice, reasonable, working (at least with RTTY) code
206 to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
207 just start sending a start bit with nothing preceding it at the beginning
208 of a transmission (what a LOSING design), we cant do it this elegantly */
210 if (demodulador(zap,&x1)) return(-1);
211 for(;;) {
212 if (demodulador(zap,&x2)) return(-1);
213 if (x1>0 && x2<0) break;
214 x1=x2;
217 /* this is now the imprecise, losing, but functional code to detect the
218 beginning of a start bit in the TDD sceanario. It just looks for sufficient
219 level to maybe, perhaps, guess, maybe that its maybe the beginning of
220 a start bit, perhaps. This whole thing stinks! */
221 beginlenx=beginlen; /* just to avoid unused war warnings */
222 if (demodulador(fskd,&fskd->x1,GET_SAMPLE)) return(-1);
223 samples++;
224 for(;;)
226 search_startbit2:
227 if (*len <= 0) {
228 fskd->state = STATE_SEARCH_STARTBIT2;
229 return 0;
231 samples++;
232 if (demodulador(fskd,&fskd->x2,GET_SAMPLE)) return(-1);
233 #if 0
234 printf("x2 = %5.5f ", fskd->x2);
235 #endif
236 if (fskd->x2 < -0.5) break;
238 search_startbit3:
239 /* Esperamos 0.5 bits antes de usar DPLL */
240 i=fskd->spb/2;
241 if (*len < i) {
242 fskd->state = STATE_SEARCH_STARTBIT3;
243 return 0;
245 for(;i>0;i--) { if (demodulador(fskd,&fskd->x1,GET_SAMPLE)) return(-1);
246 #if 0
247 printf("x1 = %5.5f ", fskd->x1);
248 #endif
249 samples++; }
251 /* x1 debe ser negativo (confirmaciĆ³n del bit de start) */
253 } while (fskd->x1>0);
254 fskd->state = STATE_GET_BYTE;
256 getbyte:
258 /* Need at least 80 samples (for 1200) or
259 1320 (for 45.5) to be sure we'll have a byte */
260 if (fskd->nbit < 8) {
261 if (*len < 1320)
262 return 0;
263 } else {
264 if (*len < 80)
265 return 0;
267 /* Leemos ahora los bits de datos */
268 j=fskd->nbit;
269 for (a=n1=0;j;j--) {
270 olen = *len;
271 i=get_bit_raw(fskd, buffer, len);
272 buffer += (olen - *len);
273 if (i == -1) return(-1);
274 if (i) n1++;
275 a>>=1; a|=i;
277 j=8-fskd->nbit;
278 a>>=j;
280 /* Leemos bit de paridad (si existe) y la comprobamos */
281 if (fskd->paridad) {
282 olen = *len;
283 i=get_bit_raw(fskd, buffer, len);
284 buffer += (olen - *len);
285 if (i == -1) return(-1);
286 if (i) n1++;
287 if (fskd->paridad==1) { /* paridad=1 (par) */
288 if (n1&1) a|=0x100; /* error */
289 } else { /* paridad=2 (impar) */
290 if (!(n1&1)) a|=0x100; /* error */
294 /* Leemos bits de STOP. Todos deben ser 1 */
296 for (j=fskd->nstop;j;j--) {
297 r = get_bit_raw(fskd, buffer, len);
298 if (r == -1) return(-1);
299 if (!r) a|=0x200;
302 /* Por fin retornamos */
303 /* Bit 8 : Error de paridad */
304 /* Bit 9 : Error de Framming */
306 *outbyte = a;
307 fskd->state = STATE_SEARCH_STARTBIT;
308 return 1;