Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


fskmodem_float.c File Reference

FSK Modulator/Demodulator. More...

#include "asterisk.h"
#include <stdio.h>
#include "asterisk/fskmodem.h"

Go to the source code of this file.

Macros

#define BWLIST   {75,800}
 
#define FLIST   {1400,1800,1200,2200,1300,2100}
 
#define GET_SAMPLE   get_sample(&buffer, len)
 
#define NBW   2
 
#define NF   6
 
#define STATE_GET_BYTE   3
 
#define STATE_SEARCH_STARTBIT   0
 
#define STATE_SEARCH_STARTBIT2   1
 
#define STATE_SEARCH_STARTBIT3   2
 

Functions

static int demodulator (fsk_data *fskd, float *retval, float x)
 
static float filterL (fsk_data *fskd, float in)
 
static float filterM (fsk_data *fskd, float in)
 
static float filterS (fsk_data *fskd, float in)
 
int fsk_serial (fsk_data *fskd, short *buffer, int *len, int *outbyte)
 
static int get_bit_raw (fsk_data *fskd, short *buffer, int *len)
 
static float get_sample (short **buffer, int *len)
 

Variables

static double coef_in [NF][NBW][8]
 Coefficients for input filters Coefficients table, generated by program "mkfilter" mkfilter is part of the zapatatelephony.org distribution Format: coef[IDX_FREC][IDX_BW][IDX_COEF] IDX_COEF = 0 => 1/GAIN IDX_COEF = 1-6 => Coefficientes y[n]. More...
 
static double coef_out [NBW][8]
 Coefficients for output filter Coefficients table, generated by program "mkfilter" Format: coef[IDX_BW][IDX_COEF] IDX_COEF = 0 => 1/GAIN IDX_COEF = 1-6 => Coefficientes y[n]. More...
 

Detailed Description

FSK Modulator/Demodulator.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
  • Includes code and algorithms from the Zapata library.

Definition in file fskmodem_float.c.

Macro Definition Documentation

#define BWLIST   {75,800}

Definition at line 44 of file fskmodem_float.c.

#define FLIST   {1400,1800,1200,2200,1300,2100}

Definition at line 46 of file fskmodem_float.c.

#define GET_SAMPLE   get_sample(&buffer, len)

Definition at line 62 of file fskmodem_float.c.

Referenced by fsk_serial(), and get_bit_raw().

#define NBW   2

Definition at line 43 of file fskmodem_float.c.

#define NF   6

Definition at line 45 of file fskmodem_float.c.

#define STATE_GET_BYTE   3

Definition at line 51 of file fskmodem_float.c.

Referenced by fsk_serial().

#define STATE_SEARCH_STARTBIT   0

Definition at line 48 of file fskmodem_float.c.

Referenced by fsk_serial().

#define STATE_SEARCH_STARTBIT2   1

Definition at line 49 of file fskmodem_float.c.

Referenced by fsk_serial().

#define STATE_SEARCH_STARTBIT3   2

Definition at line 50 of file fskmodem_float.c.

Referenced by fsk_serial().

Function Documentation

static int demodulator ( fsk_data fskd,
float *  retval,
float  x 
)
inlinestatic

Definition at line 171 of file fskmodem_float.c.

References fsk_data::cola_demod, fsk_data::cola_filter, fsk_data::cola_in, filterL(), filterM(), filterS(), NCOLA, and fsk_data::pcola.

Referenced by fsk_serial(), and get_bit_raw().

172 {
173  float xS,xM;
174 
175  fskd->cola_in[fskd->pcola] = x;
176 
177  xS = filterS(fskd,x);
178  xM = filterM(fskd,x);
179 
180  fskd->cola_filter[fskd->pcola] = xM-xS;
181 
182  x = filterL(fskd,xM*xM - xS*xS);
183 
184  fskd->cola_demod[fskd->pcola++] = x;
185  fskd->pcola &= (NCOLA-1);
186 
187  *retval = x;
188  return 0;
189 }
#define NCOLA
static float filterM(fsk_data *fskd, float in)
static float filterL(fsk_data *fskd, float in)
float cola_in[NCOLA]
float cola_filter[NCOLA]
static float filterS(fsk_data *fskd, float in)
float cola_demod[NCOLA]
static float filterL ( fsk_data fskd,
float  in 
)
inlinestatic

Low-pass filter for demodulated data

Definition at line 149 of file fskmodem_float.c.

References fsk_data::bw, fsk_data::flp, fsk_data::flxv, and fsk_data::flyv.

Referenced by demodulator().

150 {
151  int i, j;
152  double s;
153  double *pc;
154 
155  pc = &coef_out[fskd->bw][0];
156  fskd->flxv[(fskd->flp + 6) & 7] = in * (*pc++);
157 
158  s = (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
159  6 * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
160  15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
161  20 * fskd->flxv[(fskd->flp+3)&7];
162 
163  for (i = 0,j = fskd->flp;i<6;i++,j++)
164  s += fskd->flyv[j&7]*(*pc++);
165  fskd->flyv[j&7] = s;
166  fskd->flp++;
167  fskd->flp &= 7;
168  return s;
169 }
static double coef_out[NBW][8]
Coefficients for output filter Coefficients table, generated by program &quot;mkfilter&quot; Format: coef[IDX_B...
double flyv[8]
struct ast_framehook_interface i
Definition: framehook.c:40
double flxv[8]
static float filterM ( fsk_data fskd,
float  in 
)
inlinestatic

Band-pass filter for MARK frequency

Definition at line 111 of file fskmodem_float.c.

References fsk_data::bw, fsk_data::f_mark_idx, fsk_data::fmp, fsk_data::fmxv, and fsk_data::fmyv.

Referenced by demodulator().

112 {
113  int i, j;
114  double s;
115  double *pc;
116 
117  pc = &coef_in[fskd->f_mark_idx][fskd->bw][0];
118  fskd->fmxv[(fskd->fmp+6)&7] = in*(*pc++);
119 
120  s = (fskd->fmxv[(fskd->fmp + 6) & 7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp + 2) & 7] - fskd->fmxv[(fskd->fmp + 4) & 7]);
121  for (i = 0, j = fskd->fmp; i < 6; i++, j++)
122  s += fskd->fmyv[j&7]*(*pc++);
123  fskd->fmyv[j&7] = s;
124  fskd->fmp++;
125  fskd->fmp &= 7;
126  return s;
127 }
double fmyv[8]
double fmxv[8]
static double coef_in[NF][NBW][8]
Coefficients for input filters Coefficients table, generated by program &quot;mkfilter&quot; mkfilter is part o...
struct ast_framehook_interface i
Definition: framehook.c:40
int f_mark_idx
static float filterS ( fsk_data fskd,
float  in 
)
inlinestatic

Band-pass filter for SPACE frequency

Definition at line 130 of file fskmodem_float.c.

References fsk_data::bw, fsk_data::f_space_idx, fsk_data::fsp, fsk_data::fsxv, and fsk_data::fsyv.

Referenced by demodulator().

131 {
132  int i, j;
133  double s;
134  double *pc;
135 
136  pc = &coef_in[fskd->f_space_idx][fskd->bw][0];
137  fskd->fsxv[(fskd->fsp+6)&7] = in*(*pc++);
138 
139  s = (fskd->fsxv[(fskd->fsp + 6) & 7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp + 2) & 7] - fskd->fsxv[(fskd->fsp + 4) & 7]);
140  for (i = 0, j = fskd->fsp; i < 6; i++, j++)
141  s += fskd->fsyv[j&7]*(*pc++);
142  fskd->fsyv[j&7] = s;
143  fskd->fsp++;
144  fskd->fsp &= 7;
145  return s;
146 }
double fsyv[8]
double fsxv[8]
static double coef_in[NF][NBW][8]
Coefficients for input filters Coefficients table, generated by program &quot;mkfilter&quot; mkfilter is part o...
int f_space_idx
struct ast_framehook_interface i
Definition: framehook.c:40
int fsk_serial ( fsk_data fskd,
short *  buffer,
int *  len,
int *  outbyte 
)

Definition at line 226 of file fskmodem_float.c.

References demodulator(), get_bit_raw(), GET_SAMPLE, len(), fsk_data::nbit, fsk_data::nstop, fsk_data::parity, ast_frame::samples, fsk_data::spb, fsk_data::state, STATE_GET_BYTE, STATE_SEARCH_STARTBIT, STATE_SEARCH_STARTBIT2, STATE_SEARCH_STARTBIT3, fsk_data::x1, and fsk_data::x2.

227 {
228  int a;
229  int i,j,n1,r;
230  int samples = 0;
231  int olen;
232 
233  switch (fskd->state) {
234  /* Pick up where we left off */
236  goto search_startbit2;
238  goto search_startbit3;
239  case STATE_GET_BYTE:
240  goto getbyte;
241  }
242  /* We await for start bit */
243  do {
244  /* this was jesus's nice, reasonable, working (at least with RTTY) code
245  to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
246  just start sending a start bit with nothing preceding it at the beginning
247  of a transmission (what a LOSING design), we cant do it this elegantly */
248  /*
249  if (demodulator(zap,&x1)) return(-1);
250  for (;;) {
251  if (demodulator(zap,&x2)) return(-1);
252  if (x1>0 && x2<0) break;
253  x1 = x2;
254  }
255  */
256  /* this is now the imprecise, losing, but functional code to detect the
257  beginning of a start bit in the TDD sceanario. It just looks for sufficient
258  level to maybe, perhaps, guess, maybe that its maybe the beginning of
259  a start bit, perhaps. This whole thing stinks! */
260  if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
261  return -1;
262  samples++;
263  for (;;) {
264 search_startbit2:
265  if (*len <= 0) {
267  return 0;
268  }
269  samples++;
270  if (demodulator(fskd, &fskd->x2, GET_SAMPLE))
271  return(-1);
272 #if 0
273  printf("x2 = %5.5f ", fskd->x2);
274 #endif
275  if (fskd->x2 < -0.5)
276  break;
277  }
278 search_startbit3:
279  /* We await for 0.5 bits before using DPLL */
280  i = fskd->spb/2;
281  if (*len < i) {
283  return 0;
284  }
285  for (; i>0; i--) {
286  if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
287  return(-1);
288 #if 0
289  printf("x1 = %5.5f ", fskd->x1);
290 #endif
291  samples++;
292  }
293 
294  /* x1 must be negative (start bit confirmation) */
295 
296  } while (fskd->x1 > 0);
297  fskd->state = STATE_GET_BYTE;
298 
299 getbyte:
300 
301  /* Need at least 80 samples (for 1200) or
302  1320 (for 45.5) to be sure we'll have a byte */
303  if (fskd->nbit < 8) {
304  if (*len < 1320)
305  return 0;
306  } else {
307  if (*len < 80)
308  return 0;
309  }
310  /* Now we read the data bits */
311  j = fskd->nbit;
312  for (a = n1 = 0; j; j--) {
313  olen = *len;
314  i = get_bit_raw(fskd, buffer, len);
315  buffer += (olen - *len);
316  if (i == -1)
317  return(-1);
318  if (i)
319  n1++;
320  a >>= 1;
321  a |= i;
322  }
323  j = 8-fskd->nbit;
324  a >>= j;
325 
326  /* We read parity bit (if exists) and check parity */
327  if (fskd->parity) {
328  olen = *len;
329  i = get_bit_raw(fskd, buffer, len);
330  buffer += (olen - *len);
331  if (i == -1)
332  return(-1);
333  if (i)
334  n1++;
335  if (fskd->parity == 1) { /* parity=1 (even) */
336  if (n1&1)
337  a |= 0x100; /* error */
338  } else { /* parity=2 (odd) */
339  if (!(n1&1))
340  a |= 0x100; /* error */
341  }
342  }
343 
344  /* We read STOP bits. All of them must be 1 */
345 
346  for (j = fskd->nstop;j;j--) {
347  r = get_bit_raw(fskd, buffer, len);
348  if (r == -1)
349  return(-1);
350  if (!r)
351  a |= 0x200;
352  }
353 
354  /* And finally we return */
355  /* Bit 8 : Parity error */
356  /* Bit 9 : Framming error*/
357 
358  *outbyte = a;
360  return 1;
361 }
#define STATE_SEARCH_STARTBIT3
float nstop
#define STATE_SEARCH_STARTBIT2
static int demodulator(fsk_data *fskd, float *retval, float x)
#define STATE_GET_BYTE
static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define STATE_SEARCH_STARTBIT
struct ast_framehook_interface i
Definition: framehook.c:40
#define GET_SAMPLE
static int get_bit_raw ( fsk_data fskd,
short *  buffer,
int *  len 
)
static

Definition at line 191 of file fskmodem_float.c.

References fsk_data::cont, demodulator(), f, GET_SAMPLE, fsk_data::spb, and fsk_data::x0.

Referenced by fsk_serial().

192 {
193  /* This function implements a DPLL to synchronize with the bits */
194  float x,spb,spb2,ds;
195  int f;
196 
197  spb = fskd->spb;
198  if (fskd->spb == 7)
199  spb = 8000.0 / 1200.0;
200  ds = spb/32.;
201  spb2 = spb/2.;
202 
203  for (f = 0;;) {
204  if (demodulator(fskd, &x, GET_SAMPLE))
205  return -1;
206  if ((x * fskd->x0) < 0) { /* Transition */
207  if (!f) {
208  if (fskd->cont<(spb2))
209  fskd->cont += ds;
210  else
211  fskd->cont -= ds;
212  f = 1;
213  }
214  }
215  fskd->x0 = x;
216  fskd->cont += 1.;
217  if (fskd->cont > spb) {
218  fskd->cont -= spb;
219  break;
220  }
221  }
222  f = (x > 0) ? 0x80 : 0;
223  return f;
224 }
float cont
static int demodulator(fsk_data *fskd, float *retval, float x)
static struct ast_format f[]
Definition: format_g726.c:181
#define GET_SAMPLE
static float get_sample ( short **  buffer,
int *  len 
)
inlinestatic

Definition at line 53 of file fskmodem_float.c.

54 {
55  float retval;
56  retval = (float) **buffer / 256;
57  (*buffer)++;
58  (*len)--;
59  return retval;
60 };

Variable Documentation

double coef_in[NF][NBW][8]
static

Coefficients for input filters Coefficients table, generated by program "mkfilter" mkfilter is part of the zapatatelephony.org distribution Format: coef[IDX_FREC][IDX_BW][IDX_COEF] IDX_COEF = 0 => 1/GAIN IDX_COEF = 1-6 => Coefficientes y[n].

Definition at line 71 of file fskmodem_float.c.

double coef_out[NBW][8]
static
Initial value:
= {
{ 1.3868644653e-08,-6.3283665042e-01,4.0895057217e+00,-1.1020074592e+01,1.5850766191e+01,-1.2835109292e+01,5.5477477340e+00,0.0000000000e+00, },
{ 3.1262119724e-03,-7.8390522307e-03,8.5209627801e-02,-4.0804129163e-01,1.1157139955e+00,-1.8767603680e+00,1.8916395224e+00,0.0000000000e+00, },
}

Coefficients for output filter Coefficients table, generated by program "mkfilter" Format: coef[IDX_BW][IDX_COEF] IDX_COEF = 0 => 1/GAIN IDX_COEF = 1-6 => Coefficientes y[n].

Definition at line 104 of file fskmodem_float.c.