void *analyzer()
{
    clock_t t1, t2;
    //short ana_arr[540] = {0};
    float act_time,msec;
    unsigned char enc_speech[11];
    unsigned char array1[176] = {0x00};
    int b = 0, loc = 0, count_64blocks = 0;
    int i = 0;

    while(count != 0)
    {
        sem_wait(&id_analyzer);
        //sem_wait(&mutx);
        //if (count_64blocks == 0)

        t1 = clock();

        memcpy(ana_arr, buff, 1080);

        sem_post(&empty);

        //t1 = clock();
        num_frames = (int16_t) ((rate == RATE2400) ? 1 : NF);

        //for (i = 0; i < num_frames; i++)
        //{
        //	dc_rmv(&ana_arr[i * FRAME], &hpspeech[IN_BEG + i * FRAME],
        //			dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        //	//melp_ana(&hpspeech[i * FRAME], &melp_par[i], i);
        //}

        dc_rmv(&ana_arr[0], &hpspeech[IN_BEG + 0],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[0], &melp_par[0],0);

        dc_rmv(&ana_arr[FRAME], &hpspeech[IN_BEG + FRAME],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[FRAME], &melp_par[1], 1);

        dc_rmv(&ana_arr[2 * FRAME], &hpspeech[IN_BEG + 2 * FRAME],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[2 * FRAME], &melp_par[2], 2);

        /*sem_post(&start_melp_1ana);
        sem_post(&start_melp_2ana);
        sem_post(&start_melp_3ana);


        sem_wait(&melp_1ana);
        sem_wait(&melp_2ana);
        sem_wait(&melp_3ana);*/


        sc_ana(melp_par);

        /* ======== Quantization ======== */

        lpc[0] = ONE_Q12;

        lsf_vq(melp_par);

        pitch_vq(melp_par);

        gain_vq(melp_par);

        quant_u(&melp_par[0].jitter, &(quant_par.jit_index[0]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);
        quant_u(&melp_par[1].jitter, &(quant_par.jit_index[1]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);
        quant_u(&melp_par[2].jitter, &(quant_par.jit_index[2]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);

        //for (i = 0; i < NF; i++)
        //	quant_u(&melp_par[i].jitter, &(quant_par.jit_index[i]), 0,
        //		MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);

        /* Quantize bandpass voicing */

        quant_bp(melp_par, num_frames);

        quant_jitter(melp_par);

        /* Calculate Fourier coeffs of residual from quantized LPC */
        //for (i = 0; i < num_frames; i++)
        //{
        //	/* The following fill() action is believed to be unnecessary. */
        //	//fill(melp_par[i].fs_mag, ONE_Q13, NUM_HARM);
        //
        //	if (!melp_par[i].uv_flag)
        //	{
        //		lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
        //		zerflt(&hpspeech
        //			   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
        //			   sigbuf, LPC_ORD, LPC_FRAME);
        //
        //		window(sigbuf, win_cof, sigbuf, LPC_FRAME);
        //
        //		find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
        //			  LPC_FRAME);
        //	}
        //}
        i = 0;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }
        i = 1;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }
        i = 2;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }

        quant_fsmag(melp_par);

        //for (i = 0; i < num_frames; i++)
        //	quant_par.uv_flag[i] = melp_par[i].uv_flag;
        quant_par.uv_flag[0] = melp_par[0].uv_flag;
        quant_par.uv_flag[1] = melp_par[1].uv_flag;
        quant_par.uv_flag[2] = melp_par[2].uv_flag;

        /* Write channel bitstream */

#if !SKIP_CHANNEL
        low_rate_chn_write(&quant_par);
#endif

        /* Update delay buffers for next block */
        v_equ(hpspeech, &(hpspeech[num_frames * FRAME]), IN_BEG);

        memcpy(enc_speech,chbuf,11);



        /*======================old content of melp thread======================*/
        //melpe_a(enc_speech, buff);
        //fwrite(enc_speech,sizeof(char),11,fptr);
        //=======================================================================
        // ather_jawad work for 2400 baudrate
        //=======================================================================
        for (b=0; b<11; b++)
        {
            array1[loc] = enc_speech[b];
            loc++;
        }

        count_64blocks = count_64blocks + 1;
        if(count_64blocks == 16)
        {
            loc = 0;
            memcpy(array2,array1,176);
            sem_post(&last);

            count_64blocks = 0;
        }

        t2 = clock() - t1;
        //printf("Time %d = %f\n\n",count_64blocks,(float)t2/CLOCKS_PER_SEC);

        //sem_post(&full);
    }
    pthread_exit(NULL);
}
예제 #2
0
파일: qnt12.c 프로젝트: ZF0085/onionphone
void lsf_vq(struct melp_param *par)
{
    register int16_t i, j, k;
    static BOOLEAN firstTime = TRUE;
    static int16_t qplsp[LPC_ORD];	/* Q15 */
    const int16_t melp_cb_size[4] = { 256, 64, 32, 32 };	/* !!! (12/15/99) */
    const int16_t res_cb_size[4] = { 256, 64, 64, 64 };
    const int16_t melp_uv_cb_size[1] = { 512 };
    int16_t uv_config;	/* Bits of uv_config replace uv1, uv2 and cuv. */
    int16_t *lsp[NF];
    int32_t err, minErr, acc, bcc;	/* !!! (12/15/99), Q11 */
    int16_t temp1, temp2;
    int16_t lpc[LPC_ORD];	/* Q12 */
    int16_t wgt[NF][LPC_ORD];	/* Q11 */
    int16_t mwgt[2 * LPC_ORD];	/* Q11 */
    int16_t bestlsp0[LPC_ORD], bestlsp1[LPC_ORD];	/* Q15 */
    int16_t res[2 * LPC_ORD];	/* Q17 */

    /* The original program declares lsp_cand[LSP_VQ_CAND][] and              */
    /* lsp_index_cand[LSP_VQ_CAND*LSP_VQ_STAGES] with LSP_VQ_CAND == 8.  The  */
    /* program only uses up to LSP_INP_CAND == 5 and the declaration is       */
    /* modified.                                                              */

    int16_t lsp_cand[LSP_INP_CAND][LPC_ORD];	/* Q15 */
    int16_t lsp_index_cand[LSP_INP_CAND * LSP_VQ_STAGES];
    int16_t ilsp0[LPC_ORD], ilsp1[LPC_ORD];	/* Q15 */
    int16_t cand, inp_index_cand, tos, intfact;

    if (firstTime) {
        temp2 = melpe_shl(LPC_ORD, 10);	/* Q10 */
        temp1 = X08_Q10;	/* Q10 */
        for (i = 0; i < LPC_ORD; i++) {
            /*      qplsp[i] = (i+1)*0.8/LPC_ORD; */
            qplsp[i] = melpe_divide_s(temp1, temp2);
            temp1 = melpe_add(temp1, X08_Q10);
        }
        firstTime = FALSE;
    }

    /* ==== Compute weights ==== */
    for (i = 0; i < NF; i++) {
        lsp[i] = par[i].lsf;
        lpc_lsp2pred(lsp[i], lpc, LPC_ORD);
        vq_lspw(wgt[i], lsp[i], lpc, LPC_ORD);
    }

    uv_config = 0;
    for (i = 0; i < NF; i++) {
        uv_config = melpe_shl(uv_config, 1);
        if (par[i].uv_flag) {
            uv_config |= 0x0001;

            /* ==== Adjust weights ==== */
            if (i == 0)	/* Testing for par[0].uv_flag == 1 */
                v_scale(wgt[0], X02_Q15, LPC_ORD);
            else if (i == 1)
                v_scale(wgt[1], X02_Q15, LPC_ORD);
        }
    }

    /* ==== Quantize the lsp according to the UV decisions ==== */
    switch (uv_config) {
    case 7:		/* 111, all frames are NOT voiced ---- */
        lspVQ(lsp[0], wgt[0], lsp[0], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[0], LPC_ORD, FALSE);
        lspVQ(lsp[1], wgt[1], lsp[1], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[1], LPC_ORD, FALSE);
        lspVQ(lsp[2], wgt[2], lsp[2], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[2], LPC_ORD, FALSE);
        break;
    case 6:		/* 110 */
        lspVQ(lsp[0], wgt[0], lsp[0], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[0], LPC_ORD, FALSE);
        lspVQ(lsp[1], wgt[1], lsp[1], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[1], LPC_ORD, FALSE);
        lspVQ(lsp[2], wgt[2], lsp[2], lsp_v_256x64x32x32, 4, melp_cb_size,	/* !!! (12/15/99) */
              quant_par.lsf_index[2], LPC_ORD, FALSE);
        break;
    case 5:		/* 101 */
        lspVQ(lsp[0], wgt[0], lsp[0], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[0], LPC_ORD, FALSE);
        lspVQ(lsp[1], wgt[1], lsp[1], lsp_v_256x64x32x32, 4, melp_cb_size,	/* !!! (12/15/99) */
              quant_par.lsf_index[1], LPC_ORD, FALSE);
        lspVQ(lsp[2], wgt[2], lsp[2], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[2], LPC_ORD, FALSE);
        break;
    case 3:		/* 011 */
        lspVQ(lsp[0], wgt[0], lsp[0], lsp_v_256x64x32x32, 4, melp_cb_size,	/* !!! (12/15/99) */
              quant_par.lsf_index[0], LPC_ORD, FALSE);
        lspVQ(lsp[1], wgt[1], lsp[1], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[1], LPC_ORD, FALSE);
        lspVQ(lsp[2], wgt[2], lsp[2], lsp_uv_9, 1, melp_uv_cb_size,
              quant_par.lsf_index[2], LPC_ORD, FALSE);
        break;
    default:
        if (uv_config == 1) {	/* 001 case, if (!uv1 && !uv2 && uv3). */
            /* ---- Interpolation [4 inp + (8+6+6+6) res + 9 uv] ---- */
            tos = 1;
            lspVQ(lsp[2], wgt[2], lsp_cand[0], lsp_uv_9, tos,
                  melp_uv_cb_size, lsp_index_cand, LPC_ORD, TRUE);
        } else {
            tos = 4;
            lspVQ(lsp[2], wgt[2], lsp_cand[0], lsp_v_256x64x32x32, tos,	/* !!! (12/15/99) */
                  melp_cb_size, lsp_index_cand, LPC_ORD, TRUE);
        }

        minErr = LW_MAX;
        cand = 0;
        inp_index_cand = 0;
        for (k = 0; k < LSP_INP_CAND; k++) {
            for (i = 0; i < 16; i++) {

                err = 0;

                /* Originally we have two for loops here.  One computes       */
                /* ilsp0[] and ilsp1[] and the other one computes "err".  If  */
                /* "err" already exceeds minErr, we can stop the loop and     */
                /* there is no need to compute the remaining ilsp0[] and      */
                /* ilsp1[] entries.  Hence the two for loops are joined.      */

                for (j = 0; j < LPC_ORD; j++) {

                    /*      ilsp0[j] = (inpCoef[i][j] * qplsp[j] +
                       (1.0 - inpCoef[i][j]) * lsp_cand[k][j]); */
                    intfact = inpCoef[i][j];	/* Q14 */
                    acc = melpe_L_mult(intfact, qplsp[j]);	/* Q30 */
                    intfact = melpe_sub(ONE_Q14, intfact);	/* Q14 */
                    acc = melpe_L_mac(acc, intfact, lsp_cand[k][j]);	/* Q30 */
                    ilsp0[j] = melpe_extract_h(melpe_L_shl(acc, 1));
                    acc =
                        melpe_L_sub(acc,
                                    melpe_L_shl(melpe_L_deposit_l(lsp[0][j]),
                                                15));

                    /*      ilsp1[j] = inpCoef[i][j + LPC_ORD] * qplsp[j] +
                       (1.0 - inpCoef[i][j + LPC_ORD]) * lsp_cand[k][j]; */
                    intfact = inpCoef[i][j + LPC_ORD];	/* Q14 */
                    bcc = melpe_L_mult(intfact, qplsp[j]);
                    intfact = melpe_sub(ONE_Q14, intfact);
                    bcc = melpe_L_mac(bcc, intfact, lsp_cand[k][j]);	/* Q30 */
                    ilsp1[j] = melpe_extract_h(melpe_L_shl(bcc, 1));
                    bcc =
                        melpe_L_sub(bcc,
                                    melpe_L_shl(melpe_L_deposit_l(lsp[1][j]),
                                                15));

                    /*      err += wgt0[j]*(lsp0[j] - ilsp0[j])*
                       (lsp0[j] - ilsp0[j]); */
                    temp1 = melpe_norm_l(acc);
                    temp2 = melpe_extract_h(melpe_L_shl(acc, temp1));
                    if (temp2 == MONE_Q15)
                        temp2 = -32767;
                    temp2 = melpe_mult(temp2, temp2);
                    acc = melpe_L_mult(temp2, wgt[0][j]);
                    temp1 = melpe_shl(melpe_sub(1, temp1), 1);
                    acc = melpe_L_shl(acc, melpe_sub(temp1, 3));	/* Q24 */
                    err = melpe_L_add(err, acc);

                    /*      err += wgt1[j]*(lsp1[j] - ilsp1[j])*
                       (lsp1[j] - ilsp1[j]); */
                    temp1 = melpe_norm_l(bcc);
                    temp2 = melpe_extract_h(melpe_L_shl(bcc, temp1));
                    if (temp2 == MONE_Q15)
                        temp2 = -32767;
                    temp2 = melpe_mult(temp2, temp2);
                    bcc = melpe_L_mult(temp2, wgt[1][j]);
                    temp1 = melpe_shl(melpe_sub(1, temp1), 1);
                    bcc = melpe_L_shl(bcc, melpe_sub(temp1, 3));	/* Q24 */
                    err = melpe_L_add(err, bcc);

                    /* computer the err for the last frame */
                    acc = melpe_L_shl(melpe_L_deposit_l(lsp[2][j]), 15);
                    acc =
                        melpe_L_sub(acc,
                                    melpe_L_shl(melpe_L_deposit_l
                                                (lsp_cand[k][j]), 15));
                    temp1 = melpe_norm_l(acc);
                    temp2 = melpe_extract_h(melpe_L_shl(acc, temp1));
                    if (temp2 == MONE_Q15)
                        temp2 = -32767;
                    temp2 = melpe_mult(temp2, temp2);
                    acc = melpe_L_mult(temp2, wgt[2][j]);
                    temp1 = melpe_shl(melpe_sub(1, temp1), 1);
                    acc = melpe_L_shl(acc, melpe_sub(temp1, 3));	/* Q24 */
                    err = melpe_L_add(err, acc);
                }

                if (err < minErr) {
                    minErr = err;
                    cand = k;
                    inp_index_cand = i;
                    v_equ(bestlsp0, ilsp0, LPC_ORD);
                    v_equ(bestlsp1, ilsp1, LPC_ORD);
                }
            }
        }

        v_equ(lsp[2], lsp_cand[cand], LPC_ORD);
        v_equ(quant_par.lsf_index[0], &(lsp_index_cand[cand * tos]),
              tos);
        quant_par.lsf_index[1][0] = inp_index_cand;

        for (i = 0; i < LPC_ORD; i++) {
            temp1 = melpe_sub(lsp[0][i], bestlsp0[i]);	/* Q15 */
            temp2 = melpe_sub(lsp[1][i], bestlsp1[i]);	/* Q15 */
            res[i] = melpe_shl(temp1, 2);	/* Q17 */
            res[i + LPC_ORD] = melpe_shl(temp2, 2);	/* Q17 */
        }
        v_equ(mwgt, wgt[0], LPC_ORD);
        v_equ(mwgt + LPC_ORD, wgt[1], LPC_ORD);

        /* Note that in the following IF block, the lspVQ() is quantizing on  */
        /* res[] and res256x64x64x64[], and both of them are Q17 instead of   */
        /* Q15, unlike the other calling instances in this function.          */

        if (uv_config == 1)	/* if (!uv1 && !uv2 && uv3) */
            lspVQ(res, mwgt, res, res256x64x64x64, 4, res_cb_size,
                  quant_par.lsf_index[2], 2 * LPC_ORD, FALSE);
        else
            lspVQ(res, mwgt, res, res256x64x64x64, 2, res_cb_size,
                  quant_par.lsf_index[2], 2 * LPC_ORD, FALSE);

        /* ---- reconstruct lsp for later stability check ---- */
        for (i = 0; i < LPC_ORD; i++) {
            temp1 = melpe_shr(res[i], 2);
            lsp[0][i] = melpe_add(temp1, bestlsp0[i]);
            temp2 = melpe_shr(res[i + LPC_ORD], 2);
            lsp[1][i] = melpe_add(temp2, bestlsp1[i]);
        }
        break;
    }

    /* ---- Stability checking ---- */
    /* The sortings on lsp[0] and lsp[1] are not necessary because they are   */
    /* variables local to this function and they are discarded upon exit.     */
    /* We only check whether they fit the stability test and issue a warning. */

    (void)lspStable(lsp[0], LPC_ORD);
    (void)lspStable(lsp[1], LPC_ORD);
    if (!lspStable(lsp[2], LPC_ORD))
        lspSort(lsp[2], LPC_ORD);

    v_equ(qplsp, lsp[2], LPC_ORD);
}
예제 #3
0
void melp_ana(float sp_in[],struct melp_param *par)
{

    int i;
    int begin;
    float sub_pitch;
    float temp,pcorr,bpthresh;
    float r[LPC_ORD+1],refc[LPC_ORD+1],lpc[LPC_ORD+1];
    float weights[LPC_ORD];
        
    /* Remove DC from input speech */
    dc_rmv(sp_in,&speech[IN_BEG],dcdel,FRAME);
    
    /* Copy input speech to pitch window and lowpass filter */
    v_equ(&sigbuf[LPF_ORD],&speech[PITCH_BEG],PITCH_FR);
    v_equ(sigbuf,lpfsp_del,LPF_ORD);
    polflt(&sigbuf[LPF_ORD],lpf_den,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
    v_equ(lpfsp_del,&sigbuf[FRAME],LPF_ORD);
    zerflt(&sigbuf[LPF_ORD],lpf_num,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
    
    /* Perform global pitch search at frame end on lowpass speech signal */
    /* Note: avoid short pitches due to formant tracking */
    fpitch[END] = find_pitch(&sigbuf[LPF_ORD+(PITCH_FR/2)],&temp,
			     (2*PITCHMIN),PITCHMAX,PITCHMAX);
    
    /* Perform bandpass voicing analysis for end of frame */
    bpvc_ana(&speech[FRAME_END], fpitch, &par->bpvc[0], &sub_pitch);
    
    /* Force jitter if lowest band voicing strength is weak */    
    if (par->bpvc[0] < VJIT)
	par->jitter = MAX_JITTER;
    else
	par->jitter = 0.0;
    
    /* Calculate LPC for end of frame */
    window(&speech[(FRAME_END-(LPC_FRAME/2))],win_cof,sigbuf,LPC_FRAME);
    autocorr(sigbuf,r,LPC_ORD,LPC_FRAME);
    lpc[0] = 1.0;
    lpc_schur(r,lpc,refc,LPC_ORD);
    lpc_bw_expand(lpc,lpc,BWFACT,LPC_ORD);
    
    /* Calculate LPC residual */
    zerflt(&speech[PITCH_BEG],lpc,&sigbuf[LPF_ORD],LPC_ORD,PITCH_FR);
        
    /* Check peakiness of residual signal */
    begin = (LPF_ORD+(PITCHMAX/2));
    temp = peakiness(&sigbuf[begin],PITCHMAX);
    
    /* Peakiness: force lowest band to be voiced  */
    if (temp > PEAK_THRESH) {
	par->bpvc[0] = 1.0;
    }
    
    /* Extreme peakiness: force second and third bands to be voiced */
    if (temp > PEAK_THR2) {
	par->bpvc[1] = 1.0;
	par->bpvc[2] = 1.0;
    }
		
    /* Calculate overall frame pitch using lowpass filtered residual */
    par->pitch = pitch_ana(&speech[FRAME_END], &sigbuf[LPF_ORD+PITCHMAX], 
			   sub_pitch,pitch_avg,&pcorr);
    bpthresh = BPTHRESH;
    
    /* Calculate gain of input speech for each gain subframe */
    for (i = 0; i < NUM_GAINFR; i++) {
	if (par->bpvc[0] > bpthresh) {

	    /* voiced mode: pitch synchronous window length */
	    temp = sub_pitch;
	    par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
				    temp,MIN_GAINFR,2*PITCHMAX);
	}
	else {
	    temp = 1.33*GAINFR - 0.5;
	    par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
				    temp,0,2*PITCHMAX);
	}
    }
    
    /* Update average pitch value */
    if (par->gain[NUM_GAINFR-1] > SILENCE_DB)
      temp = pcorr;
    else
      temp = 0.0;
    pitch_avg = p_avg_update(par->pitch, temp, VMIN);
    
    /* Calculate Line Spectral Frequencies */
    lpc_pred2lsp(lpc,par->lsf,LPC_ORD);
    
    /* Force minimum LSF bandwidth (separation) */
    lpc_clamp(par->lsf,BWMIN,LPC_ORD);
    
    /* Quantize MELP parameters to 2400 bps and generate bitstream */
    
    /* Quantize LSF's with MSVQ */
    vq_lspw(weights, &par->lsf[1], lpc, LPC_ORD);
    msvq_enc(&par->lsf[1], weights, &par->lsf[1], vq_par);
    par->msvq_index = vq_par.indices;
    
    /* Force minimum LSF bandwidth (separation) */
    lpc_clamp(par->lsf,BWMIN,LPC_ORD);
    
    /* Quantize logarithmic pitch period */
    /* Reserve all zero code for completely unvoiced */
    par->pitch = log10(par->pitch);
    quant_u(&par->pitch,&par->pitch_index,PIT_QLO,PIT_QUP,PIT_QLEV);
    par->pitch = pow(10.0,par->pitch);
    
    /* Quantize gain terms with uniform log quantizer	*/
    q_gain(par->gain, par->gain_index,GN_QLO,GN_QUP,GN_QLEV);
    
    /* Quantize jitter and bandpass voicing */
    quant_u(&par->jitter,&par->jit_index,0.0,MAX_JITTER,2);
    par->uv_flag = q_bpvc(&par->bpvc[0],&par->bpvc_index,bpthresh,
			  NUM_BANDS);
    
    /*	Calculate Fourier coefficients of residual signal from quantized LPC */
    fill(par->fs_mag,1.0,NUM_HARM);
    if (par->bpvc[0] > bpthresh) {
	lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
	zerflt(&speech[(FRAME_END-(LPC_FRAME/2))],lpc,sigbuf,
	       LPC_ORD,LPC_FRAME);
	window(sigbuf,win_cof,sigbuf,LPC_FRAME);
	find_harm(sigbuf, par->fs_mag, par->pitch, NUM_HARM, LPC_FRAME);
    }
    
    /* quantize Fourier coefficients */
    /* pre-weight vector, then use Euclidean distance */
    window(&par->fs_mag[0],w_fs,&par->fs_mag[0],NUM_HARM);
    fsvq_enc(&par->fs_mag[0], &par->fs_mag[0], fs_vq_par);
    
    /* Set MELP indeces to point to same array */
    par->fsvq_index = fs_vq_par.indices;

    /* Update MSVQ information */
    par->msvq_stages = vq_par.num_stages;
    par->msvq_bits = vq_par.num_bits;

    /* Write channel bitstream */
    melp_chn_write(par);

    /* Update delay buffers for next frame */
    v_equ(&speech[0],&speech[FRAME],IN_BEG);
    fpitch[BEGIN] = fpitch[END];
}