Wed Oct 28 11:52:29 2009

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>

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().

00168 {
00169    float xS,xM;
00170 
00171    fskd->cola_in[fskd->pcola] = x;
00172    
00173    xS = filterS(fskd,x);
00174    xM = filterM(fskd,x);
00175 
00176    fskd->cola_filter[fskd->pcola] = xM-xS;
00177 
00178    x = filterL(fskd,xM*xM - xS*xS);
00179    
00180    fskd->cola_demod[fskd->pcola++] = x;
00181    fskd->pcola &=  (NCOLA-1);
00182 
00183    *retval = x;
00184    return 0;
00185 }

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().

00146 {
00147    int i, j;
00148    double s;
00149    double *pc;
00150    
00151    pc = &coef_out[fskd->bw][0];
00152    fskd->flxv[(fskd->flp + 6) & 7] = in * (*pc++); 
00153    
00154    s = (fskd->flxv[fskd->flp] + fskd->flxv[(fskd->flp+6)&7]) +
00155      6  * (fskd->flxv[(fskd->flp+1)&7] + fskd->flxv[(fskd->flp+5)&7]) +
00156      15 * (fskd->flxv[(fskd->flp+2)&7] + fskd->flxv[(fskd->flp+4)&7]) +
00157      20 *  fskd->flxv[(fskd->flp+3)&7]; 
00158    
00159    for (i = 0,j = fskd->flp;i<6;i++,j++)
00160       s += fskd->flyv[j&7]*(*pc++);
00161    fskd->flyv[j&7] = s;
00162    fskd->flp++;
00163    fskd->flp &= 7;
00164    return s;
00165 }

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().

00108 {
00109    int i, j;
00110    double s;
00111    double *pc;
00112    
00113    pc = &coef_in[fskd->f_mark_idx][fskd->bw][0];
00114    fskd->fmxv[(fskd->fmp+6)&7] = in*(*pc++);
00115    
00116    s = (fskd->fmxv[(fskd->fmp + 6) & 7] - fskd->fmxv[fskd->fmp]) + 3 * (fskd->fmxv[(fskd->fmp + 2) & 7] - fskd->fmxv[(fskd->fmp + 4) & 7]);
00117    for (i = 0, j = fskd->fmp; i < 6; i++, j++) 
00118       s += fskd->fmyv[j&7]*(*pc++);
00119    fskd->fmyv[j&7] = s;
00120    fskd->fmp++;
00121    fskd->fmp &= 7;
00122    return s;
00123 }

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().

00127 {
00128    int i, j;
00129    double s;
00130    double *pc;
00131    
00132    pc = &coef_in[fskd->f_space_idx][fskd->bw][0];
00133    fskd->fsxv[(fskd->fsp+6)&7] = in*(*pc++);
00134    
00135    s = (fskd->fsxv[(fskd->fsp + 6) & 7] - fskd->fsxv[fskd->fsp]) + 3 * (fskd->fsxv[(fskd->fsp + 2) & 7] - fskd->fsxv[(fskd->fsp + 4) & 7]);
00136    for (i = 0, j = fskd->fsp; i < 6; i++, j++) 
00137       s += fskd->fsyv[j&7]*(*pc++);
00138    fskd->fsyv[j&7] = s;
00139    fskd->fsp++;
00140    fskd->fsp &= 7;
00141    return s;
00142 }

int fsk_serial ( fsk_data fskd,
short *  buffer,
int *  len,
int *  outbyte 
)

Definition at line 222 of file fskmodem_float.c.

Referenced by callerid_feed(), callerid_feed_jp(), and tdd_feed().

00223 {
00224    int a;
00225    int i,j,n1,r;
00226    int samples = 0;
00227    int olen;
00228    int beginlen=*len;
00229    int beginlenx;
00230    
00231    switch (fskd->state) {
00232       /* Pick up where we left off */
00233    case STATE_SEARCH_STARTBIT2:
00234       goto search_startbit2;
00235    case STATE_SEARCH_STARTBIT3:
00236       goto search_startbit3;
00237    case STATE_GET_BYTE:
00238       goto getbyte;
00239    }
00240    /* We await for start bit  */
00241    do {
00242       /* this was jesus's nice, reasonable, working (at least with RTTY) code
00243       to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
00244       just start sending a start bit with nothing preceding it at the beginning
00245       of a transmission (what a LOSING design), we cant do it this elegantly */
00246       /*
00247       if (demodulator(zap,&x1)) return(-1);
00248       for (;;) {
00249          if (demodulator(zap,&x2)) return(-1);
00250          if (x1>0 && x2<0) break;
00251          x1 = x2;
00252       }
00253       */
00254       /* this is now the imprecise, losing, but functional code to detect the
00255       beginning of a start bit in the TDD sceanario. It just looks for sufficient
00256       level to maybe, perhaps, guess, maybe that its maybe the beginning of
00257       a start bit, perhaps. This whole thing stinks! */
00258       beginlenx=beginlen; /* just to avoid unused war warnings */
00259       if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
00260          return -1;
00261       samples++;
00262       for (;;) {
00263 search_startbit2:       
00264          if (*len <= 0) {
00265             fskd->state  =  STATE_SEARCH_STARTBIT2;
00266             return 0;
00267          }
00268          samples++;
00269          if (demodulator(fskd, &fskd->x2, GET_SAMPLE))
00270             return(-1);
00271 #if 0
00272          printf("x2  =  %5.5f ", fskd->x2);
00273 #endif         
00274          if (fskd->x2 < -0.5)
00275             break; 
00276       }
00277 search_startbit3:       
00278       /* We await for 0.5 bits before using DPLL */
00279       i = fskd->spb/2;
00280       if (*len < i) {
00281          fskd->state = STATE_SEARCH_STARTBIT3;
00282          return 0;
00283       }
00284       for (; i>0; i--) {
00285          if (demodulator(fskd, &fskd->x1, GET_SAMPLE))
00286             return(-1); 
00287 #if 0
00288          printf("x1 = %5.5f ", fskd->x1);
00289 #endif         
00290          samples++;
00291       }
00292 
00293       /* x1 must be negative (start bit confirmation) */
00294 
00295    } while (fskd->x1 > 0);
00296    fskd->state = STATE_GET_BYTE;
00297 
00298 getbyte:
00299 
00300    /* Need at least 80 samples (for 1200) or
00301       1320 (for 45.5) to be sure we'll have a byte */
00302    if (fskd->nbit < 8) {
00303       if (*len < 1320)
00304          return 0;
00305    } else {
00306       if (*len < 80)
00307          return 0;
00308    }
00309    /* Now we read the data bits */
00310    j = fskd->nbit;
00311    for (a = n1 = 0; j; j--) {
00312       olen = *len;
00313       i = get_bit_raw(fskd, buffer, len);
00314       buffer += (olen - *len);
00315       if (i == -1)
00316          return(-1);
00317       if (i)
00318          n1++;
00319       a >>= 1;
00320       a |= i;
00321    }
00322    j = 8-fskd->nbit;
00323    a >>= j;
00324 
00325    /* We read parity bit (if exists) and check parity */
00326    if (fskd->parity) {
00327       olen = *len;
00328       i = get_bit_raw(fskd, buffer, len); 
00329       buffer += (olen - *len);
00330       if (i == -1)
00331          return(-1);
00332       if (i)
00333          n1++;
00334       if (fskd->parity == 1) {   /* parity=1 (even) */
00335          if (n1&1)
00336             a |= 0x100;    /* error */
00337       } else {       /* parity=2 (odd) */
00338          if (!(n1&1))
00339             a |= 0x100; /* error */
00340       }
00341    }
00342    
00343    /* We read STOP bits. All of them must be 1 */
00344    
00345    for (j = fskd->nstop;j;j--) {
00346       r = get_bit_raw(fskd, buffer, len);
00347       if (r == -1)
00348          return(-1);
00349       if (!r)
00350          a |= 0x200;
00351    }
00352 
00353    /* And finally we return  */
00354    /* Bit 8 : Parity error */
00355    /* Bit 9 : Framming error*/
00356 
00357    *outbyte = a;
00358    fskd->state = STATE_SEARCH_STARTBIT;
00359    return 1;
00360 }

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().

00188 {
00189    /* This function implements a DPLL to synchronize with the bits */
00190    float x,spb,spb2,ds;
00191    int f;
00192 
00193    spb = fskd->spb; 
00194    if (fskd->spb == 7)
00195       spb = 8000.0 / 1200.0;
00196    ds = spb/32.;
00197    spb2 = spb/2.;
00198 
00199    for (f = 0;;) {
00200       if (demodulator(fskd, &x, GET_SAMPLE))
00201          return -1;
00202       if ((x * fskd->x0) < 0) {  /* Transition */
00203          if (!f) {
00204             if (fskd->cont<(spb2))
00205                fskd->cont += ds;
00206             else
00207                fskd->cont -= ds;
00208             f = 1;
00209          }
00210       }
00211       fskd->x0 = x;
00212       fskd->cont += 1.;
00213       if (fskd->cont > spb) {
00214          fskd->cont -= spb;
00215          break;
00216       }
00217    }
00218    f = (x > 0) ? 0x80 : 0;
00219    return f;
00220 }

static float get_sample ( short **  buffer,
int *  len 
) [inline, static]

Definition at line 49 of file fskmodem_float.c.

00050 {
00051    float retval;
00052    retval = (float) **buffer / 256;
00053    (*buffer)++;
00054    (*len)--;
00055    return retval;
00056 };


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.


Generated on Wed Oct 28 11:52:30 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6