⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reid_c.htm

📁 这是Half rate speech(GSM 06.20)
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0075)http://ece.ut.ac.ir/classpages/VLSI/GSM%20Vocoder/Half-Rate/C-Source/REID.C -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY><PRE>/*___________________________________________________________________________
 |                                                                           |
 |                                                                           |
 |                 Residual Error Insertion Device                           |
 |                                                                           |
 |                                                                           |
 |      File  :  REID.C                                                      |
 |                                                                           |
 |      Date  :  February 03, 1995                                           |
 |                                                                           |
 |      Version: 4.1                                                         |
 |                                                                           |
 |                                                                           |
 |   Description:                                                            |
 |   ------------                                                            |
 |      This routine transforms the output file format of the GSM Half       |
 |      Rate Encoder module consisting of:                                   |
 |           * 18 speech parameters (see GSM TS 06.20)                       |
 |           *  1 speech flag SP (see GSM TS 06.41)                          |
 |           *  1 voice activity flag VAD (see GSM TS 06.42)                 |
 |                                                                           |
 |      to the input file format of the GSM Half Rate Decoder module         |
 |      requiring:                                                           |
 |           * 18 speech parameters (see GSM TS 06.20)                       |
 |           *  1 channel condition flag BFI (see GSM TS 06.21, 05.05)       |
 |           *  1 channel condition flag UFI (see GSM TS 06.21, 05.05)       |
 |           *  1 SID flag (2 bits) (see GSM TS 06.41, 05.05)                |
 |           *  1 time alignment flag TAF (see GSM TS 06.41)                 |
 |                                                                           |
 |      Between SID updates the speech parameters are replaced by random     |
 |      values simulating an interrupted transmission on the air interface   |
 |                                                                           |
 |      The actual implementation only supports error free transmission (EP0)|
 |                                                                           |
 |      The shell for the future use of error patterns (residual error       |
 |      pattern insertion) is already included.  If necessary, byte swapping |
 |      is performed on the input speech parameters so that they are always  |
 |      represented internally in PC byte order (assuming that the byte      |
 |      order of the input file is compatible with the machine on which the  |
 |      program is run).  However, byte swapping is not done on the flag     |
 |      words (input: SP and VAD, output: BFI, UFI, SID, and TAF).  Thus,    |
 |      the residual error pattern insertion code may be written to handle   |
 |      the speech parameter words on a byte basis, but the flag words must  |
 |      always be handled on a word basis.                                   |
 |___________________________________________________________________________|
*/
/*___________________________________________________________________________
 |                                                                           |
 |      Creation: 19.12.94                                                   |
 |                                                                           |
 |  Changes:                                                                 |
 |      22.12.94:  Removal of BCI flag, instead: determination of SID flag   |
 |      12.01.95:  SID update period = 12 (instead of 24)                    |
 |      13.01.95:  When in CNI mode, the parameters between SID updates are  |
 |                 random values. This simulates the interrupted transmission|
 |      03.02.95:  Longword main( Longword...) replaced by int main(int ...),|
 |                 initial value of swTAFCnt set to 1                        |
 |___________________________________________________________________________|
*/

/*___________________________________________________________________________
 |                                                                           |
 |      Include-Files                                                        |
 |___________________________________________________________________________|
*/
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;
#include "typedefs.h"
#include "reid.h"


/*___________________________________________________________________________
 |                                                                           |
 |      local Functions                                                      |
 |___________________________________________________________________________|
*/

/*___________________________________________________________________________
 |                                                                           |
 |      Subroutines                                                          |
 |___________________________________________________________________________|
*/
static Longword error_free( FILE *infile, FILE *outfile)
{

#define SPEECH      1
#define CNIFIRSTSID 2
#define CNICONT     3
#define VALIDSID    11
#define GOODSPEECH  33

    static Shortword swDecoMode = {SPEECH};
    static Shortword swTAFCnt = {1};
    Shortword swInPara[20], i, swFrameType;
    Shortword swOutPara[22],pswErrorFlag[3];
 
    if( EncoderInterface( infile, swInPara )) return( 1 );

    /* Copy input parameters to output parameters (error free transmission) */
    /* -------------------------------------------------------------------- */
    for (i=0;i&lt;18;i++)
       swOutPara[i] = swInPara[i];

    /* Set channel status flags (error free transmission) */
    /* -------------------------------------------------- */
    swOutPara[18] = 0;     /* BFI flag */
    swOutPara[19] = 0;     /* UFI flag */

    /* Evaluate SID flag */
    /* ----------------- */
    pswErrorFlag[0] = 0;   /* BFI flag */
    pswErrorFlag[1] = 0;   /* UFI flag */
    pswErrorFlag[2] = 0;   /* BCI flag */
    swOutPara[20] = swSidDetection(swOutPara, pswErrorFlag);


    /* Evaluate TAF flag */
    /* ----------------- */
    if (swTAFCnt == 0) swOutPara[21] = 1;
    else               swOutPara[21] = 0;
    swTAFCnt = (swTAFCnt + 1) % 12;

   
    /* Frame classification:                                                */
    /* Since the transmission is error free, the received frames are either */
    /* valid speech or valid SID frames                                     */
    /* -------------------------------------------------------------------- */
    if      ( swOutPara[20] == 2) swFrameType = VALIDSID;
    else if ( swOutPara[20] == 0) swFrameType = GOODSPEECH;
    else {
            printf( "Error in SID detection\n" );
            return( 1 );
    }

    /* Update of decoder state */
    /* ----------------------- */
    if (swDecoMode == SPEECH) {
       if      (swFrameType == VALIDSID)   swDecoMode = CNIFIRSTSID;
       else if (swFrameType == GOODSPEECH) swDecoMode = SPEECH;
    }
    else {  /* comfort noise insertion mode */
       if      (swFrameType == VALIDSID)   swDecoMode = CNICONT;
       else if (swFrameType == GOODSPEECH) swDecoMode = SPEECH;
    }


    /* Replace parameters by random data if in CNICONT-mode and TAF=0 */
    /* -------------------------------------------------------------- */
    if ((swDecoMode == CNICONT) &amp;&amp; (swOutPara[21] == 0)){
       RandomParameters(swOutPara);
       /* Set flags such, that an "unusable frame" is produced */
       swOutPara[18] = 1;     /* BFI flag */
       swOutPara[19] = 1;     /* UFI flag */
       swOutPara[20] = 0;     /* SID flag */
    }    



    if( outfile ) {
        if( WriteOutputFile( swOutPara, outfile )) {
            printf( "Error writing File\n" );
            return( 1 );
        }
    }
    return( 0 );
}

static Longword EncoderInterface( FILE *infile, Shortword swInPara[] )
{
    size_t i = 0;    

    i = ReadInputFile( swInPara, infile );

    return(( i == 0 ) ? 1 : 0 );
}

static size_t ReadInputFile( Shortword buffer[], FILE *fp )
{
    size_t i;

    i = fread( buffer, sizeof( Shortword ), 20, fp );
    SwapBytes( buffer, 18 );
    return( i );
}

static size_t WriteOutputFile( Shortword buffer[], FILE *fp )
{
    size_t i;

    SwapBytes( buffer, 18 );
    i = fwrite( buffer, sizeof( Shortword ), 22, fp );
    return( ( i == 22 ) ? 0 : 1 );
}


static void SwapBytes( Shortword buffer[], Longword len )
{
    Byte *pc, tmp;
    Longword i;
    
    if( !ByteOrder())
        return;
    pc = (Byte *)buffer;
    for( i = 0; i &lt; len; i++ ) {
        tmp   = pc[0];
        pc[0] = pc[1];
        pc[1] = tmp;
        pc += 2;
    }
}

static Longword ByteOrder( void )
{
    Shortword si;
    Byte *pc;

    si = 0x1234;
    pc = (Byte *)&amp;si;
    if (pc[1] == 0x12 &amp;&amp; pc[0] == 0x34 )
            return( 0 );
    if (pc[0] == 0x12 &amp;&amp; pc[1] == 0x34 )
            return( 1 );
    printf( "Error in ByteOrder: %X, %X\n", (int)pc[0], (int)pc[1] );
    exit( 1 );
    return( 2 );
}

FILE *OpenBinfile( char *name, char *mode )
{
    FILE *fp;

    if( toupper( *mode ) == 'W' ) { /* Write access */
        if(( fp = fopen( name, OPEN_WB )) == NULL ) { 
            printf( "Can't open output file '%s'\n", name );
            exit( 1 );
        }
    } else { /* Read access */
        if(( fp = fopen( name, OPEN_RB )) == NULL ) { 
            printf( "Can't open file '%s'\n", name );
            exit( 1 );
        } 
    }
    return( fp );
}

Longword Strincmp( const char *s, const char *t, size_t max )
{
    for( ; max &gt; 1; ++s, ++t, --max ) {
        if( toupper( *s ) != toupper( *t ))
            break;
        if( *s == '\0' )
            return( 0 );
    }
    return( toupper( *s ) - toupper( *t ));
}

Longword Stricmp( const char *s, const char *t )
{
    for(; toupper( *s ) == toupper( *t ); ++s, ++t ) {
        if( *s == '\0' )
            return( 0 );
    }
    return( toupper( *s ) - toupper( *t ));
}

/*************************************************************************
 *
 *   FUNCTION NAME: getPnBits
 *
 *   PURPOSE:
 *     
 *     Generate iBits pseudo-random bits using *pL_PNSeed as the
 *     pn-generators seed.
 *
 *   INPUTS:
 *
 *     iBits - integer indicating how many random bits to return.
 *     range [0,15], 0 yields 1 bit output
 *     
 *     *pL_PNSeed - 32 bit seed (changed by function)
 *     
 *   OUTPUTS:
 *     
 *     *pL_PNSeed - 32 bit seed, modified.
 *     
 *   RETURN VALUE:
 *     
 *    random bits in iBits LSB's.  
 *     
 *     
 *   IMPLEMENTATION:
 *     
 *    implementation of x**31 + x**3 + 1 == PN_XOR_REG | PN_XOR_ADD a
 *    PN sequence generator using Longwords generating a 2**31 -1
 *    length pn-sequence.
 *
 *************************************************************************/

static Shortword getPnBits(Shortword iBits, Longword *pL_PNSeed){

#define PN_XOR_REG (Longword)0x00000005L
#define PN_XOR_ADD (Longword)0x40000000L

  Shortword swPnBits=0;
  Longword L_Taps,L_FeedBack;
  Shortword i;

  for (i=0; i &lt; iBits; i++){
    
    /* update the state */                     
    /********************/
    
    L_Taps = *pL_PNSeed &amp; PN_XOR_REG;  
    L_FeedBack = L_Taps; /* Xor tap bits to yield feedback bit */
	L_Taps = L_shr_reid(L_Taps,1);

    while(L_Taps){
      L_FeedBack = L_FeedBack ^ L_Taps;
	  L_Taps = L_shr_reid(L_Taps,1);
    }

    /* LSB of L_FeedBack is next MSB of PN register */

	*pL_PNSeed = L_shr_reid(*pL_PNSeed,1);
    if (L_FeedBack &amp; 1)
       *pL_PNSeed = *pL_PNSeed | PN_XOR_ADD;
    
    /* State update complete.  
       Get the output bit from the state, add/or it into output */

	swPnBits = shl_reid(swPnBits,1);
    swPnBits = swPnBits | (*pL_PNSeed &amp; 1);
    
  }
  return(swPnBits);
}


/***************************************************************************
 *
 *   FUNCTION NAME: L_shl_reid
 *
 *   PURPOSE:
 *
 *     Arithmetic shift left (or right).
 *     Arithmetically shift the input left by var2.   If var2 is
 *     negative then an arithmetic shift right (L_shr_reid) of L_var1 by
 *     -var2 is performed.
 *
 *   INPUTS:
 *
 *     var2

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -