Thu Apr 28 2011 16:57:10

Asterisk developer's documentation


fskmodem_float.c File Reference

FSK Modulator/Demodulator. More...

#include "asterisk.h"
#include <stdio.h>
#include "asterisk/fskmodem.h"
Include dependency graph for fskmodem_float.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#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].
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].

Detailed Description

FSK Modulator/Demodulator.

Author:
Mark Spencer <markster@digium.com>
  • Includes code and algorithms from the Zapata library.

Definition in file fskmodem_float.c.


Define Documentation

#define BWLIST   {75,800}

Definition at line 40 of file fskmodem_float.c.

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

Definition at line 42 of file fskmodem_float.c.

#define GET_SAMPLE   get_sample(&buffer, len)

Definition at line 58 of file fskmodem_float.c.

Referenced by fsk_serial(), and get_bit_raw().

#define NBW   2

Definition at line 39 of file fskmodem_float.c.

#define NF   6

Definition at line 41 of file fskmodem_float.c.

#define STATE_GET_BYTE   3

Definition at line 47 of file fskmodem_float.c.

Referenced by fsk_serial().

#define STATE_SEARCH_STARTBIT   0

Definition at line 44 of file fskmodem_float.c.

Referenced by fsk_serial().

#define STATE_SEARCH_STARTBIT2   1

Definition at line 45 of file fskmodem_float.c.

Referenced by fsk_serial().

#define STATE_SEARCH_STARTBIT3   2

Definition at line 46 of file fskmodem_float.c.

Referenced by fsk_serial().


Function Documentation

static int demodulator ( fsk_data fskd,
float *  retval,
float  x 
) [inline, static]

Definition at line 167 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().

{
   float xS,xM;

   fskd->cola_in[fskd->pcola] = x;
   
   xS = filterS(fskd,x);
   xM = filterM(fskd,x);

   fskd->cola_filter[fskd->pcola] = xM-xS;

   x = filterL(fskd,xM*xM - xS*xS);
   
   fskd->cola_demod[fskd->pcola++] = x;
   fskd->pcola &=  (NCOLA-1);

   *retval = x;
   return 0;
}
static float filterL ( fsk_data fskd,
float  in 
) [inline, static]

Low-pass filter for demodulated data

Definition at line 145 of file fskmodem_float.c.

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

Referenced by demodulator().

{
   int i, j;
   double s;
   double *pc;
   
   pc = &coef_out[fskd->bw][0];
   fskd->flxv[(fskd->flp + 6) & 7] = in * (*pc++); 
   
   s = (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
     6  * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
     15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
     20 *  fskd->flxv[(fskd->flp+3)&7]; 
   
   for (i = 0,j = fskd->flp;i<6;i++,j++)
      s += fskd->flyv[j&7]*(*pc++);
   fskd->flyv[j&7] = s;
   fskd->flp++;
   fskd->flp &= 7;
   return s;
}
static float filterM ( fsk_data fskd,
float  in 
) [inline, static]

Band-pass filter for MARK frequency

Definition at line 107 of file fskmodem_float.c.

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

Referenced by demodulator().

{
   int i, j;
   double s;
   double *pc;
   
   pc = &coef_in[fskd->f_mark_idx][fskd->bw][0];
   fskd->fmxv[(fskd->fmp+6)&7] = in*(*pc++);
   
   s = (fskd->fmxv[(fskd->fmp + 6) & 7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp + 2) & 7] - fskd->fmxv[(fskd->fmp + 4) & 7]);
   for (i = 0, j = fskd->fmp; i < 6; i++, j++) 
      s += fskd->fmyv[j&7]*(*pc++);
   fskd->fmyv[j&7] = s;
   fskd->fmp++;
   fskd->fmp &= 7;
   return s;
}
static float filterS ( fsk_data fskd,
float  in 
) [inline, static]

Band-pass filter for SPACE frequency

Definition at line 126 of file fskmodem_float.c.

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

Referenced by demodulator().

{
   int i, j;
   double s;
   double *pc;
   
   pc = &coef_in[fskd->f_space_idx][fskd->bw][0];
   fskd->fsxv[(fskd->fsp+6)&7] = in*(*pc++);
   
   s = (fskd->fsxv[(fskd->fsp + 6) & 7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp + 2) & 7] - fskd->fsxv[(fskd->fsp + 4) & 7]);
   for (i = 0, j = fskd->fsp; i < 6; i++, j++) 
      s += fskd->fsyv[j&7]*(*pc++);
   fskd->fsyv[j&7] = s;
   fskd->fsp++;
   fskd->fsp &= 7;
   return s;
}
int fsk_serial ( fsk_data fskd,
short *  buffer,
int *  len,
int *  outbyte 
)

Definition at line 222 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.

{
   int a;
   int i,j,n1,r;
   int samples = 0;
   int olen;
   int beginlen=*len;
   int beginlenx;
   
   switch (fskd->state) {
      /* Pick up where we left off */
   case STATE_SEARCH_STARTBIT2:
      goto search_startbit2;
   case STATE_SEARCH_STARTBIT3:
      goto search_startbit3;
   case STATE_GET_BYTE:
      goto getbyte;
   }
   /* We await for start bit  */
   do {
      /* this was jesus's nice, reasonable, working (at least with RTTY) code
      to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
      just start sending a start bit with nothing preceding it at the beginning
      of a transmission (what a LOSING design), we cant do it this elegantly */
      /*
      if (demodulator(zap,&x1)) return(-1);
      for (;;) {
         if (demodulator(zap,&x2)) return(-1);
         if (x1>0 && x2<0) break;
         x1 = x2;
      }
      */
      /* this is now the imprecise, losing, but functional code to detect the
      beginning of a start bit in the TDD sceanario. It just looks for sufficient
      level to maybe, perhaps, guess, maybe that its maybe the beginning of
      a start bit, perhaps. This whole thing stinks! */
      beginlenx=beginlen; /* just to avoid unused war warnings */
      if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
         return -1;
      samples++;
      for (;;) {
search_startbit2:       
         if (*len <= 0) {
            fskd->state  =  STATE_SEARCH_STARTBIT2;
            return 0;
         }
         samples++;
         if (demodulator(fskd, &fskd->x2, GET_SAMPLE))
            return(-1);
#if 0
         printf("x2  =  %5.5f ", fskd->x2);
#endif         
         if (fskd->x2 < -0.5)
            break; 
      }
search_startbit3:       
      /* We await for 0.5 bits before using DPLL */
      i = fskd->spb/2;
      if (*len < i) {
         fskd->state = STATE_SEARCH_STARTBIT3;
         return 0;
      }
      for (; i>0; i--) {
         if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
            return(-1); 
#if 0
         printf("x1 = %5.5f ", fskd->x1);
#endif         
         samples++;
      }

      /* x1 must be negative (start bit confirmation) */

   } while (fskd->x1 > 0);
   fskd->state = STATE_GET_BYTE;

getbyte:

   /* Need at least 80 samples (for 1200) or
      1320 (for 45.5) to be sure we'll have a byte */
   if (fskd->nbit < 8) {
      if (*len < 1320)
         return 0;
   } else {
      if (*len < 80)
         return 0;
   }
   /* Now we read the data bits */
   j = fskd->nbit;
   for (a = n1 = 0; j; j--) {
      olen = *len;
      i = get_bit_raw(fskd, buffer, len);
      buffer += (olen - *len);
      if (i == -1)
         return(-1);
      if (i)
         n1++;
      a >>= 1;
      a |= i;
   }
   j = 8-fskd->nbit;
   a >>= j;

   /* We read parity bit (if exists) and check parity */
   if (fskd->parity) {
      olen = *len;
      i = get_bit_raw(fskd, buffer, len); 
      buffer += (olen - *len);
      if (i == -1)
         return(-1);
      if (i)
         n1++;
      if (fskd->parity == 1) {   /* parity=1 (even) */
         if (n1&1)
            a |= 0x100;    /* error */
      } else {       /* parity=2 (odd) */
         if (!(n1&1))
            a |= 0x100; /* error */
      }
   }
   
   /* We read STOP bits. All of them must be 1 */
   
   for (j = fskd->nstop;j;j--) {
      r = get_bit_raw(fskd, buffer, len);
      if (r == -1)
         return(-1);
      if (!r)
         a |= 0x200;
   }

   /* And finally we return  */
   /* Bit 8 : Parity error */
   /* Bit 9 : Framming error*/

   *outbyte = a;
   fskd->state = STATE_SEARCH_STARTBIT;
   return 1;
}
static int get_bit_raw ( fsk_data fskd,
short *  buffer,
int *  len 
) [static]

Definition at line 187 of file fskmodem_float.c.

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

Referenced by fsk_serial().

{
   /* This function implements a DPLL to synchronize with the bits */
   float x,spb,spb2,ds;
   int f;

   spb = fskd->spb; 
   if (fskd->spb == 7)
      spb = 8000.0 / 1200.0;
   ds = spb/32.;
   spb2 = spb/2.;

   for (f = 0;;) {
      if (demodulator(fskd, &x, GET_SAMPLE))
         return -1;
      if ((x * fskd->x0) < 0) {  /* Transition */
         if (!f) {
            if (fskd->cont<(spb2))
               fskd->cont += ds;
            else
               fskd->cont -= ds;
            f = 1;
         }
      }
      fskd->x0 = x;
      fskd->cont += 1.;
      if (fskd->cont > spb) {
         fskd->cont -= spb;
         break;
      }
   }
   f = (x > 0) ? 0x80 : 0;
   return f;
}
static float get_sample ( short **  buffer,
int *  len 
) [inline, static]

Definition at line 49 of file fskmodem_float.c.

{
   float retval;
   retval = (float) **buffer / 256;
   (*buffer)++;
   (*len)--;
   return retval;
};

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 67 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 100 of file fskmodem_float.c.