fskmodem.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * FSK Modulator/Demodulator
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License.
  12. *
  13. * Includes code and algorithms from the Zapata library.
  14. *
  15. */
  16. #include <asterisk/fskmodem.h>
  17. #include <stdio.h>
  18. #define NBW 2
  19. #define BWLIST {75,800}
  20. #define NF 6
  21. #define FLIST {1400,1800,1200,2200,1300,2100}
  22. #define STATE_SEARCH_STARTBIT 0
  23. #define STATE_SEARCH_STARTBIT2 1
  24. #define STATE_SEARCH_STARTBIT3 2
  25. #define STATE_GET_BYTE 3
  26. static inline float get_sample(short **buffer, int *len)
  27. {
  28. float retval;
  29. retval = (float) **buffer / 256;
  30. (*buffer)++;
  31. (*len)--;
  32. return retval;
  33. }
  34. #define GET_SAMPLE get_sample(&buffer, len)
  35. /* Coeficientes para filtros de entrada */
  36. /* Tabla de coeficientes, generada a partir del programa "mkfilter" */
  37. /* Formato: coef[IDX_FREC][IDX_BW][IDX_COEF] */
  38. /* IDX_COEF=0 => 1/GAIN */
  39. /* IDX_COEF=1-6 => Coeficientes y[n] */
  40. static double coef_in[NF][NBW][8]={
  41. #include "coef_in.h"
  42. };
  43. /* Coeficientes para filtro de salida */
  44. /* Tabla de coeficientes, generada a partir del programa "mkfilter" */
  45. /* Formato: coef[IDX_BW][IDX_COEF] */
  46. /* IDX_COEF=0 => 1/GAIN */
  47. /* IDX_COEF=1-6 => Coeficientes y[n] */
  48. static double coef_out[NBW][8]={
  49. #include "coef_out.h"
  50. };
  51. /* Filtro pasa-banda para frecuencia de MARCA */
  52. static inline float filtroM(fsk_data *fskd,float in)
  53. {
  54. int i,j;
  55. double s;
  56. double *pc;
  57. pc=&coef_in[fskd->f_mark_idx][fskd->bw][0];
  58. fskd->fmxv[(fskd->fmp+6)&7]=in*(*pc++);
  59. s=(fskd->fmxv[(fskd->fmp+6)&7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp+2)&7] - fskd->fmxv[(fskd->fmp+4)&7]);
  60. for (i=0,j=fskd->fmp;i<6;i++,j++) s+=fskd->fmyv[j&7]*(*pc++);
  61. fskd->fmyv[j&7]=s;
  62. fskd->fmp++; fskd->fmp&=7;
  63. return s;
  64. }
  65. /* Filtro pasa-banda para frecuencia de ESPACIO */
  66. static inline float filtroS(fsk_data *fskd,float in)
  67. {
  68. int i,j;
  69. double s;
  70. double *pc;
  71. pc=&coef_in[fskd->f_space_idx][fskd->bw][0];
  72. fskd->fsxv[(fskd->fsp+6)&7]=in*(*pc++);
  73. s=(fskd->fsxv[(fskd->fsp+6)&7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp+2)&7] - fskd->fsxv[(fskd->fsp+4)&7]);
  74. for (i=0,j=fskd->fsp;i<6;i++,j++) s+=fskd->fsyv[j&7]*(*pc++);
  75. fskd->fsyv[j&7]=s;
  76. fskd->fsp++; fskd->fsp&=7;
  77. return s;
  78. }
  79. /* Filtro pasa-bajos para datos demodulados */
  80. static inline float filtroL(fsk_data *fskd,float in)
  81. {
  82. int i,j;
  83. double s;
  84. double *pc;
  85. pc=&coef_out[fskd->bw][0];
  86. fskd->flxv[(fskd->flp + 6) & 7]=in * (*pc++);
  87. s= (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
  88. 6 * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
  89. 15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
  90. 20 * fskd->flxv[(fskd->flp+3)&7];
  91. for (i=0,j=fskd->flp;i<6;i++,j++) s+=fskd->flyv[j&7]*(*pc++);
  92. fskd->flyv[j&7]=s;
  93. fskd->flp++; fskd->flp&=7;
  94. return s;
  95. }
  96. static inline int demodulador(fsk_data *fskd, float *retval, float x)
  97. {
  98. float xS,xM;
  99. fskd->cola_in[fskd->pcola]=x;
  100. xS=filtroS(fskd,x);
  101. xM=filtroM(fskd,x);
  102. fskd->cola_filtro[fskd->pcola]=xM-xS;
  103. x=filtroL(fskd,xM*xM - xS*xS);
  104. fskd->cola_demod[fskd->pcola++]=x;
  105. fskd->pcola &= (NCOLA-1);
  106. *retval = x;
  107. return(0);
  108. }
  109. static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
  110. {
  111. /* Esta funcion implementa un DPLL para sincronizarse con los bits */
  112. float x,spb,spb2,ds;
  113. int f;
  114. spb=fskd->spb;
  115. if (fskd->spb == 7) spb = 8000.0 / 1200.0;
  116. ds=spb/32.;
  117. spb2=spb/2.;
  118. for (f=0;;){
  119. if (demodulador(fskd,&x, GET_SAMPLE)) return(-1);
  120. if ((x*fskd->x0)<0) { /* Transicion */
  121. if (!f) {
  122. if (fskd->cont<(spb2)) fskd->cont+=ds; else fskd->cont-=ds;
  123. f=1;
  124. }
  125. }
  126. fskd->x0=x;
  127. fskd->cont+=1.;
  128. if (fskd->cont>spb) {
  129. fskd->cont-=spb;
  130. break;
  131. }
  132. }
  133. f=(x>0)?0x80:0;
  134. return(f);
  135. }
  136. int fsk_serie(fsk_data *fskd, short *buffer, int *len, int *outbyte)
  137. {
  138. int a;
  139. int i,j,n1,r;
  140. int samples=0;
  141. int olen;
  142. switch(fskd->state) {
  143. /* Pick up where we left off */
  144. case STATE_SEARCH_STARTBIT2:
  145. goto search_startbit2;
  146. case STATE_SEARCH_STARTBIT3:
  147. goto search_startbit3;
  148. case STATE_GET_BYTE:
  149. goto getbyte;
  150. }
  151. /* Esperamos bit de start */
  152. do {
  153. /* this was jesus's nice, reasonable, working (at least with RTTY) code
  154. to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
  155. just start sending a start bit with nothing preceding it at the beginning
  156. of a transmission (what a LOSING design), we cant do it this elegantly */
  157. /*
  158. if (demodulador(zap,&x1)) return(-1);
  159. for(;;) {
  160. if (demodulador(zap,&x2)) return(-1);
  161. if (x1>0 && x2<0) break;
  162. x1=x2;
  163. }
  164. */
  165. /* this is now the imprecise, losing, but functional code to detect the
  166. beginning of a start bit in the TDD sceanario. It just looks for sufficient
  167. level to maybe, perhaps, guess, maybe that its maybe the beginning of
  168. a start bit, perhaps. This whole thing stinks! */
  169. if (demodulador(fskd,&fskd->x1,GET_SAMPLE)) return(-1);
  170. samples++;
  171. for(;;)
  172. {
  173. search_startbit2:
  174. if (!*len) {
  175. fskd->state = STATE_SEARCH_STARTBIT2;
  176. return 0;
  177. }
  178. samples++;
  179. if (demodulador(fskd,&fskd->x2,GET_SAMPLE)) return(-1);
  180. #if 0
  181. printf("x2 = %5.5f ", fskd->x2);
  182. #endif
  183. if (fskd->x2 < -0.5) break;
  184. }
  185. search_startbit3:
  186. /* Esperamos 0.5 bits antes de usar DPLL */
  187. i=fskd->spb/2;
  188. if (*len < i) {
  189. fskd->state = STATE_SEARCH_STARTBIT3;
  190. return 0;
  191. }
  192. for(;i;i--) { if (demodulador(fskd,&fskd->x1,GET_SAMPLE)) return(-1);
  193. #if 0
  194. printf("x1 = %5.5f ", fskd->x1);
  195. #endif
  196. samples++; }
  197. /* x1 debe ser negativo (confirmación del bit de start) */
  198. } while (fskd->x1>0);
  199. fskd->state = STATE_GET_BYTE;
  200. getbyte:
  201. /* Need at least 80 samples (for 1200) or
  202. 1320 (for 45.5) to be sure we'll have a byte */
  203. if (fskd->nbit < 8) {
  204. if (*len < 1320)
  205. return 0;
  206. } else {
  207. if (*len < 80)
  208. return 0;
  209. }
  210. /* Leemos ahora los bits de datos */
  211. j=fskd->nbit;
  212. for (a=n1=0;j;j--) {
  213. olen = *len;
  214. i=get_bit_raw(fskd, buffer, len);
  215. buffer += (olen - *len);
  216. if (i == -1) return(-1);
  217. if (i) n1++;
  218. a>>=1; a|=i;
  219. }
  220. j=8-fskd->nbit;
  221. a>>=j;
  222. /* Leemos bit de paridad (si existe) y la comprobamos */
  223. if (fskd->paridad) {
  224. olen = *len;
  225. i=get_bit_raw(fskd, buffer, len);
  226. buffer += (olen - *len);
  227. if (i == -1) return(-1);
  228. if (i) n1++;
  229. if (fskd->paridad==1) { /* paridad=1 (par) */
  230. if (n1&1) a|=0x100; /* error */
  231. } else { /* paridad=2 (impar) */
  232. if (!(n1&1)) a|=0x100; /* error */
  233. }
  234. }
  235. /* Leemos bits de STOP. Todos deben ser 1 */
  236. for (j=fskd->nstop;j;j--) {
  237. r = get_bit_raw(fskd, buffer, len);
  238. if (r == -1) return(-1);
  239. if (!r) a|=0x200;
  240. }
  241. /* Por fin retornamos */
  242. /* Bit 8 : Error de paridad */
  243. /* Bit 9 : Error de Framming */
  244. *outbyte = a;
  245. fskd->state = STATE_SEARCH_STARTBIT;
  246. return 1;
  247. }