createCB.c

Go to the documentation of this file.
00001 
00002 
00003    /******************************************************************
00004 
00005        iLBC Speech Coder ANSI-C Source Code
00006 
00007        createCB.c
00008 
00009        Copyright (C) The Internet Society (2004).
00010        All Rights Reserved.
00011 
00012    ******************************************************************/
00013 
00014 
00015 
00016 
00017 
00018    #include "iLBC_define.h"
00019    #include "constants.h"
00020    #include <string.h>
00021    #include <math.h>
00022 
00023    /*----------------------------------------------------------------*
00024     *  Construct an additional codebook vector by filtering the
00025     *  initial codebook buffer. This vector is then used to expand
00026     *  the codebook with an additional section.
00027     *---------------------------------------------------------------*/
00028 
00029    void filteredCBvecs(
00030        float *cbvectors,   /* (o) Codebook vectors for the
00031                                   higher section */
00032        float *mem,         /* (i) Buffer to create codebook
00033                                   vector from */
00034        int lMem        /* (i) Length of buffer */
00035    ){
00036        int j, k;
00037        float *pp, *pp1;
00038        float tempbuff2[CB_MEML+CB_FILTERLEN];
00039        float *pos;
00040 
00041        memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float));
00042        memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float));
00043        memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,
00044            (CB_HALFFILTERLEN+1)*sizeof(float));
00045 
00046        /* Create codebook vector for higher section by filtering */
00047 
00048        /* do filtering */
00049        pos=cbvectors;
00050        memset(pos, 0, lMem*sizeof(float));
00051        for (k=0; k<lMem; k++) {
00052            pp=&tempbuff2[k];
00053            pp1=&cbfiltersTbl[CB_FILTERLEN-1];
00054            for (j=0;j<CB_FILTERLEN;j++) {
00055                (*pos)+=(*pp++)*(*pp1--);
00056            }
00057            pos++;
00058        }
00059    }
00060 
00061    /*----------------------------------------------------------------*
00062     *  Search the augmented part of the codebook to find the best
00063     *  measure.
00064     *----------------------------------------------------------------*/
00065 
00066 
00067 
00068 
00069 
00070 
00071    void searchAugmentedCB(
00072        int low,        /* (i) Start index for the search */
00073        int high,           /* (i) End index for the search */
00074        int stage,          /* (i) Current stage */
00075        int startIndex,     /* (i) Codebook index for the first
00076                                   aug vector */
00077        float *target,      /* (i) Target vector for encoding */
00078        float *buffer,      /* (i) Pointer to the end of the buffer for
00079                                   augmented codebook construction */
00080        float *max_measure, /* (i/o) Currently maximum measure */
00081        int *best_index,/* (o) Currently the best index */
00082        float *gain,    /* (o) Currently the best gain */
00083        float *energy,      /* (o) Energy of augmented codebook
00084                                   vectors */
00085        float *invenergy/* (o) Inv energy of augmented codebook
00086                                   vectors */
00087    ) {
00088        int icount, ilow, j, tmpIndex;
00089        float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
00090        float weighted, measure, nrjRecursive;
00091        float ftmp;
00092 
00093        /* Compute the energy for the first (low-5)
00094           noninterpolated samples */
00095        nrjRecursive = (float) 0.0;
00096        pp = buffer - low + 1;
00097        for (j=0; j<(low-5); j++) {
00098            nrjRecursive += ( (*pp)*(*pp) );
00099            pp++;
00100        }
00101        ppe = buffer - low;
00102 
00103 
00104        for (icount=low; icount<=high; icount++) {
00105 
00106            /* Index of the codebook vector used for retrieving
00107               energy values */
00108            tmpIndex = startIndex+icount-20;
00109 
00110            ilow = icount-4;
00111 
00112            /* Update the energy recursively to save complexity */
00113            nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
00114            ppe--;
00115            energy[tmpIndex] = nrjRecursive;
00116 
00117            /* Compute cross dot product for the first (low-5)
00118               samples */
00119 
00120 
00121 
00122 
00123 
00124            crossDot = (float) 0.0;
00125            pp = buffer-icount;
00126            for (j=0; j<ilow; j++) {
00127                crossDot += target[j]*(*pp++);
00128            }
00129 
00130            /* interpolation */
00131            alfa = (float) 0.2;
00132            ppo = buffer-4;
00133            ppi = buffer-icount-4;
00134            for (j=ilow; j<icount; j++) {
00135                weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
00136                ppo++;
00137                ppi++;
00138                energy[tmpIndex] += weighted*weighted;
00139                crossDot += target[j]*weighted;
00140                alfa += (float)0.2;
00141            }
00142 
00143            /* Compute energy and cross dot product for the
00144               remaining samples */
00145            pp = buffer - icount;
00146            for (j=icount; j<SUBL; j++) {
00147                energy[tmpIndex] += (*pp)*(*pp);
00148                crossDot += target[j]*(*pp++);
00149            }
00150 
00151            if (energy[tmpIndex]>0.0) {
00152                invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS);
00153            } else {
00154                invenergy[tmpIndex] = (float) 0.0;
00155            }
00156 
00157            if (stage==0) {
00158                measure = (float)-10000000.0;
00159 
00160                if (crossDot > 0.0) {
00161                    measure = crossDot*crossDot*invenergy[tmpIndex];
00162                }
00163            }
00164            else {
00165                measure = crossDot*crossDot*invenergy[tmpIndex];
00166            }
00167 
00168            /* check if measure is better */
00169            ftmp = crossDot*invenergy[tmpIndex];
00170 
00171            if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
00172 
00173 
00174 
00175 
00176 
00177                *best_index = tmpIndex;
00178                *max_measure = measure;
00179                *gain = ftmp;
00180            }
00181        }
00182    }
00183 
00184 
00185    /*----------------------------------------------------------------*
00186     *  Recreate a specific codebook vector from the augmented part.
00187     *
00188     *----------------------------------------------------------------*/
00189 
00190    void createAugmentedVec(
00191        int index,      /* (i) Index for the augmented vector
00192                               to be created */
00193        float *buffer,  /* (i) Pointer to the end of the buffer for
00194                               augmented codebook construction */
00195        float *cbVec/* (o) The construced codebook vector */
00196    ) {
00197        int ilow, j;
00198        float *pp, *ppo, *ppi, alfa, alfa1, weighted;
00199 
00200        ilow = index-5;
00201 
00202        /* copy the first noninterpolated part */
00203 
00204        pp = buffer-index;
00205        memcpy(cbVec,pp,sizeof(float)*index);
00206 
00207        /* interpolation */
00208 
00209        alfa1 = (float)0.2;
00210        alfa = 0.0;
00211        ppo = buffer-5;
00212        ppi = buffer-index-5;
00213        for (j=ilow; j<index; j++) {
00214            weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
00215            ppo++;
00216            ppi++;
00217            cbVec[j] = weighted;
00218            alfa += alfa1;
00219        }
00220 
00221        /* copy the second noninterpolated part */
00222 
00223        pp = buffer - index;
00224        memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));
00225 
00226 
00227 
00228 
00229 
00230    }
00231 

Generated on Thu Apr 16 06:27:32 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6