Wed Oct 28 11:52:30 2009

Asterisk developer's documentation


fskmodem_int.c File Reference

FSK Modulator/Demodulator. More...

#include "asterisk.h"
#include "asterisk/fskmodem.h"

Include dependency graph for fskmodem_int.c:

Go to the source code of this file.

Defines

#define BWLIST   {75,800}
#define FLIST   {1400,1800,1200,2200,1300,2100}
#define IGET_SAMPLE   iget_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

int fsk_serial (fsk_data *fskd, short *buffer, int *len, int *outbyte)
int fskmodem_init (fsk_data *fskd)
static int get_bit_raw (fsk_data *fskd, short *buffer, int *len)
static int ibpdfilter (struct filter_struct *fs, int in)
static int ibpfilter (struct filter_struct *fs, int in)
static int idemodulator (fsk_data *fskd, int *retval, int x)
static int iget_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_int.c.


Define Documentation

#define BWLIST   {75,800}

Definition at line 38 of file fskmodem_int.c.

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

Definition at line 40 of file fskmodem_int.c.

#define IGET_SAMPLE   iget_sample(&buffer, len)

Definition at line 56 of file fskmodem_int.c.

Referenced by fsk_serial(), and get_bit_raw().

#define NBW   2

Definition at line 37 of file fskmodem_int.c.

#define NF   6

Definition at line 39 of file fskmodem_int.c.

#define STATE_GET_BYTE   3

Definition at line 45 of file fskmodem_int.c.

#define STATE_SEARCH_STARTBIT   0

Definition at line 42 of file fskmodem_int.c.

#define STATE_SEARCH_STARTBIT2   1

Definition at line 43 of file fskmodem_int.c.

#define STATE_SEARCH_STARTBIT3   2

Definition at line 44 of file fskmodem_int.c.


Function Documentation

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

Definition at line 220 of file fskmodem_int.c.

References demodulator(), get_bit_raw(), GET_SAMPLE, idemodulator(), IGET_SAMPLE, fsk_data::instop, fsk_data::ispb, 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, fsk_data::x2, fsk_data::xi1, and fsk_data::xi2.

00221 {
00222    int a;
00223    int i, j, n1, r;
00224    int samples = 0;
00225    int olen;
00226    int beginlen = *len;
00227    int beginlenx;
00228    
00229    switch (fskd->state) {
00230       /* Pick up where we left off */
00231    case STATE_SEARCH_STARTBIT2:
00232       goto search_startbit2;
00233    case STATE_SEARCH_STARTBIT3:
00234       goto search_startbit3;
00235    case STATE_GET_BYTE:
00236       goto getbyte;
00237    }
00238    /* We await for start bit  */
00239    do {
00240       /* this was jesus's nice, reasonable, working (at least with RTTY) code
00241       to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
00242       just start sending a start bit with nothing preceding it at the beginning
00243       of a transmission (what a LOSING design), we cant do it this elegantly */
00244       /* NOT USED
00245             if (demodulator(zap,&x1)) 
00246                return -1;
00247             for(;;) {
00248                if (demodulator(zap,&x2)) 
00249                   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 (idemodulator(fskd, &fskd->xi1, IGET_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 (idemodulator(fskd, &fskd->xi2, IGET_SAMPLE)) 
00270             return -1;
00271 #if 0
00272          printf("xi2 = %d ", fskd->xi2);
00273 #endif         
00274          if (fskd->xi2 < 512) {
00275             break;
00276          }
00277       }
00278 search_startbit3:       
00279       /* We await for 0.5 bits before using DPLL */
00280       i = fskd->ispb / 2;
00281       if (*len < i) {
00282          fskd->state = STATE_SEARCH_STARTBIT3;
00283          return 0;
00284       }
00285       for (; i > 0; i--) {
00286          if (idemodulator(fskd, &fskd->xi1, IGET_SAMPLE))
00287             return(-1); 
00288 #if 0
00289          printf("xi1 = %d ", fskd->xi1);
00290 #endif
00291          samples++; 
00292       }
00293 
00294       /* x1 must be negative (start bit confirmation) */
00295 
00296    } while (fskd->xi1 > 0);
00297    fskd->state = STATE_GET_BYTE;
00298 
00299 getbyte:
00300 
00301    /* Need at least 80 samples (for 1200) or
00302       1320 (for 45.5) to be sure we'll have a byte */
00303    if (fskd->nbit < 8) {
00304       if (*len < 1320)
00305          return 0;
00306    } else {
00307       if (*len < 80)
00308          return 0;
00309    }
00310 
00311    /* Now we read the data bits */
00312    j = fskd->nbit;
00313    for (a = n1 = 0; j; j--) {
00314       olen = *len;
00315       i = get_bit_raw(fskd, buffer, len);
00316       buffer += (olen - *len);
00317       if (i == -1) 
00318          return -1;
00319       if (i) 
00320          n1++;
00321       a >>= 1; 
00322       a |= i;
00323    }
00324    j = 8 - fskd->nbit;
00325    a >>= j;
00326 
00327    /* We read parity bit (if exists) and check parity */
00328    if (fskd->parity) {
00329       olen = *len;
00330       i = get_bit_raw(fskd, buffer, len); 
00331       buffer += (olen - *len);
00332       if (i == -1) 
00333          return -1;
00334       if (i) 
00335          n1++;
00336       if (fskd->parity == 1) {   /* parity=1 (even) */
00337          if (n1 & 1) 
00338             a |= 0x100;       /* error */
00339       } else {             /* parity=2 (odd) */
00340          if (!(n1 & 1)) 
00341             a |= 0x100;       /* error */
00342       }
00343    }
00344    
00345    /* We read STOP bits. All of them must be 1 */
00346    
00347    for (j = fskd->instop; j; j--) {
00348       r = get_bit_raw(fskd, buffer, len);
00349       if (r == -1) 
00350          return -1;
00351       if (!r) 
00352          a |= 0x200;
00353    }
00354 
00355    /* And finally we return  
00356     * Bit 8 : Parity error 
00357     * Bit 9 : Framming error
00358    */
00359       
00360    *outbyte = a;
00361    fskd->state = STATE_SEARCH_STARTBIT;
00362    return 1;
00363 }

int fskmodem_init ( fsk_data fskd  ) 

Definition at line 193 of file fskmodem_int.c.

References fsk_data::bw, fsk_data::demod_filter, fsk_data::f_mark_idx, fsk_data::f_space_idx, filter_struct::icoefs, filter_struct::ip, filter_struct::ixv, filter_struct::iyv, fsk_data::mark_filter, and fsk_data::space_filter.

Referenced by callerid_new(), and tdd_new().

00194 {
00195    int i;
00196 
00197    fskd->space_filter.ip  = 0;
00198    fskd->mark_filter.ip   = 0;
00199    fskd->demod_filter.ip  = 0;
00200 
00201    for ( i = 0 ; i < 7 ; i++ ) {
00202       fskd->space_filter.icoefs[i] = 
00203          coef_in[fskd->f_space_idx][fskd->bw][i] * 256;
00204       fskd->space_filter.ixv[i] = 0;;
00205       fskd->space_filter.iyv[i] = 0;;
00206 
00207       fskd->mark_filter.icoefs[i] = 
00208          coef_in[fskd->f_mark_idx][fskd->bw][i] * 256;
00209       fskd->mark_filter.ixv[i] = 0;;
00210       fskd->mark_filter.iyv[i] = 0;;
00211 
00212       fskd->demod_filter.icoefs[i] = 
00213          coef_out[fskd->bw][i] * 1024;
00214       fskd->demod_filter.ixv[i] = 0;;
00215       fskd->demod_filter.iyv[i] = 0;;
00216    }
00217    return 0;
00218 }

static int get_bit_raw ( fsk_data fskd,
short *  buffer,
int *  len 
) [static]

Definition at line 163 of file fskmodem_int.c.

References f, fsk_data::icont, idemodulator(), IGET_SAMPLE, fsk_data::pllids, fsk_data::pllispb, fsk_data::pllispb2, and fsk_data::xi0.

00164 {
00165    /* This function implements a DPLL to synchronize with the bits */
00166    int f;
00167 
00168    int ix;
00169    /* PLL coeffs are set up in callerid_new */
00170    for (f = 0;;) {
00171       if (idemodulator(fskd, &ix, IGET_SAMPLE)) return(-1);
00172       if ((ix * fskd->xi0) < 0) { /* Transicion */
00173          if (!f) {
00174             if (fskd->icont < (fskd->pllispb2)) {
00175                fskd->icont += fskd->pllids; 
00176             } else {
00177                fskd->icont -= fskd->pllids;
00178             }
00179             f = 1;
00180          }
00181       }
00182       fskd->xi0 = ix;
00183       fskd->icont += 32;
00184       if (fskd->icont > fskd->pllispb) {
00185          fskd->icont -= fskd->pllispb;
00186          break;
00187       }
00188    }
00189    f = (ix > 0) ? 0x80 : 0;
00190    return f;
00191 }

static int ibpdfilter ( struct filter_struct fs,
int  in 
) [inline, static]

Integer Pass Band demodulator filter

Definition at line 93 of file fskmodem_int.c.

References filter_struct::icoefs, filter_struct::ip, filter_struct::ixv, filter_struct::iyv, and s.

Referenced by idemodulator().

00094 {
00095    int i,j;
00096    int s;
00097    int64_t s_interim;
00098    
00099    /* integer filter */
00100    s =  in * fs->icoefs[0];
00101    fs->ixv[(fs->ip + 6) & 7] = s;
00102    
00103    s =      (fs->ixv[fs->ip]           + fs->ixv[(fs->ip + 6) & 7]) +
00104       6  * (fs->ixv[(fs->ip + 1) & 7] + fs->ixv[(fs->ip + 5) & 7]) +
00105       15 * (fs->ixv[(fs->ip + 2) & 7] + fs->ixv[(fs->ip + 4) & 7]) +
00106       20 *  fs->ixv[(fs->ip + 3) & 7];
00107    
00108    for (i = 1, j = fs->ip; i < 7; i++, j++) {
00109       /* Promote operation to 64 bit to prevent overflow that occurred in 32 bit) */
00110       s_interim = (int64_t)(fs->iyv[j & 7]) * 
00111             (int64_t)(fs->icoefs[i]) /
00112             (int64_t)(1024);
00113       s += (int) s_interim;
00114    }
00115    fs->iyv[j & 7] = s;
00116    fs->ip++;
00117    fs->ip &= 7;
00118    return s;
00119 }

static int ibpfilter ( struct filter_struct fs,
int  in 
) [inline, static]

Integer Band Pass filter

Definition at line 122 of file fskmodem_int.c.

References filter_struct::icoefs, filter_struct::ip, filter_struct::ixv, filter_struct::iyv, and s.

Referenced by idemodulator().

00123 {
00124    int i, j;
00125    int s;
00126    int64_t s_interim;
00127    
00128    /* integer filter */
00129    s =  in * fs->icoefs[0] / 256;
00130    fs->ixv[(fs->ip + 6) & 7] = s;
00131    
00132    s = (fs->ixv[(fs->ip + 6) & 7] - fs->ixv[fs->ip])
00133       + 3 * (fs->ixv[(fs->ip + 2) & 7] - fs->ixv[(fs->ip + 4) & 7]);
00134    
00135    for (i = 1, j = fs->ip; i < 7; i++, j++) { 
00136       s_interim = (int64_t)(fs->iyv[j & 7]) * 
00137             (int64_t)(fs->icoefs[i]) / 
00138             (int64_t)(256);
00139       s += (int) s_interim;
00140    }
00141    fs->iyv[j & 7] = s;
00142    fs->ip++;
00143    fs->ip &= 7;
00144    return s;
00145 }

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

Definition at line 147 of file fskmodem_int.c.

References fsk_data::demod_filter, ibpdfilter(), ibpfilter(), id, fsk_data::mark_filter, and fsk_data::space_filter.

Referenced by fsk_serial(), and get_bit_raw().

00148 {
00149    int is, im, id;
00150    int ilin2;
00151  
00152    is = ibpfilter(&fskd->space_filter, x);
00153    im = ibpfilter(&fskd->mark_filter, x);
00154    
00155    ilin2 = ((im * im) - (is * is)) / (256 * 256);
00156    
00157    id = ibpdfilter(&fskd->demod_filter, ilin2);
00158    
00159    *retval = id;
00160    return 0;
00161 }

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

Definition at line 47 of file fskmodem_int.c.

00048 {
00049    int retval;
00050    retval = (int) **buffer;
00051    (*buffer)++;
00052    (*len)--;
00053    return retval;
00054 }


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 64 of file fskmodem_int.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 86 of file fskmodem_int.c.


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