fskmodem_float.c 9.6 KB

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