float el=0,eh=0,g;
float el=0,eh=0,eb=0,g;
void sb_encode(SBEncState *st, float *in, FrameBits *bits){   int i, roots, sub;   /* Compute the two sub-bands by filtering with h0 and h1*/   fir_mem(in, h0, st->x0, st->full_frame_size, QMF_ORDER, st->h0_mem);   fir_mem(in, h1, st->x1, st->full_frame_size, QMF_ORDER, st->h1_mem);   /* Down-sample x0 and x1 */   for (i=0;i<st->frame_size;i++)   {      st->x0d[i]=st->x0[i<<1];      st->x1d[i]=st->x1[i<<1];   }   /* Encode the narrowband part*/   encode(&st->st_low, st->x0d, bits);   /* High-band buffering / sync with low band */   for (i=0;i<st->frame_size;i++)   {      st->excBuf[i]=st->exc[i];      st->high[i]=st->high[st->frame_size+i];      st->high[st->frame_size+i]=st->x1d[i];   }      /* Start encoding the high-band */   for (i=0;i<st->windowSize;i++)      st->buf[i] = st->high[i] * st->window[i];   /* Compute auto-correlation */   autocorr(st->buf, st->autocorr, st->lpcSize+1, st->windowSize);   st->autocorr[0] += 1;        /* prevents NANs */   st->autocorr[0] *= st->lpc_floor; /* Noise floor in auto-correlation domain */   /* Lag windowing: equivalent to filtering in the power-spectrum domain */   for (i=0;i<st->lpcSize+1;i++)      st->autocorr[i] *= st->lagWindow[i];   /* Levinson-Durbin */   wld(st->lpc+1, st->autocorr, st->rc, st->lpcSize);   st->lpc[0]=1;   /* LPC to LSPs (x-domain) transform */   roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 6, 0.002, st->stack);   if (roots!=st->lpcSize)   {      fprintf (stderr, "roots!=st->lpcSize (found only %d roots)\n", roots);      exit(1);   }   /* x-domain to angle domain*/   for (i=0;i<st->lpcSize;i++)      st->lsp[i] = acos(st->lsp[i]);   /* LSP quantization */   lsp_quant_high(st->lsp, st->qlsp, st->lpcSize, bits);      /*printf ("high_lsp:");   for (i=0;i<st->lpcSize;i++)      printf (" %f", st->lsp[i]);      printf ("\n");   for (i=0;i<st->lpcSize;i++)   st->qlsp[i]=st->lsp[i];   */   if (st->first)   {      for (i=0;i<st->lpcSize;i++)         st->old_lsp[i] = st->lsp[i];      for (i=0;i<st->lpcSize;i++)         st->old_qlsp[i] = st->qlsp[i];   }      for (sub=0;sub<st->nbSubframes;sub++)   {      float *exc, *sp, *mem, *res, *target, *sw, tmp, filter_ratio;      int offset;            offset = st->subframeSize*sub;      sp=st->high+offset;      exc=st->excBuf+offset;      res=st->res+offset;      target=st->target+offset;      sw=st->sw+offset;      mem=PUSH(st->stack, st->lpcSize);            /* LSP interpolation (quantized and unquantized) */      tmp = (.5 + sub)/st->nbSubframes;      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = (1-tmp)*st->old_lsp[i] + tmp*st->lsp[i];      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = (1-tmp)*st->old_qlsp[i] + tmp*st->qlsp[i];      /* Compute interpolated LPCs (quantized and unquantized) */      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = cos(st->interp_lsp[i]);      lsp_to_lpc(st->interp_lsp, st->interp_lpc, st->lpcSize,st->stack);      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = cos(st->interp_qlsp[i]);      lsp_to_lpc(st->interp_qlsp, st->interp_qlpc, st->lpcSize, st->stack);      bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);      bw_lpc(st->gamma2, st->interp_lpc, st->bw_lpc2, st->lpcSize);      {         float tmp, rl=0, rh=0;         tmp=1;         for (i=0;i<=st->lpcSize;i++)         {            rh += tmp*st->interp_qlpc[i];            tmp = -tmp;         }         rl = st->st_low.pi_gain[sub];         rl=1/(fabs(rl)+.001);         rh=1/(fabs(rh)+.001);         filter_ratio=fabs(.001+rh)/(.001+fabs(rl));         printf ("filter_ratio: %f\n", filter_ratio);      }#if 0 /* 1 for spectral folding excitation, 0 for stochastic */      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      residue_mem(sp, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, st->mem_sp);      {         float el=0,eh=0,g;         for (i=0;i<st->subframeSize;i++)            eh+=sqr(exc[i]);         for (i=0;i<st->subframeSize;i++)           el+=sqr(st->st_low.exc[offset+i]);         g=eh/(.01+el);         printf ("ratio: %f\n", g);         g=sqrt(g);         for (i=0;i<st->subframeSize;i++)           exc[i]=g*st->st_low.exc[offset+i];      }      syn_filt_mem(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, mem);#else      /* Reset excitation */      for (i=0;i<st->subframeSize;i++)         exc[i]=0;      /* Compute zero response of A(z/g1) / ( A(z/g2) * Aq(z) ) */      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      syn_filt_mem(exc, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, mem);      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      residue_mem(exc, st->bw_lpc1, res, st->subframeSize, st->lpcSize, mem);      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sw[i];      syn_filt_mem(res, st->bw_lpc2, res, st->subframeSize, st->lpcSize, mem);      /* Compute weighted signal */      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      residue_mem(sp, st->bw_lpc1, sw, st->subframeSize, st->lpcSize, mem);      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sw[i];      syn_filt_mem(sw, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, mem);      /* Compute target signal */      for (i=0;i<st->subframeSize;i++)         target[i]=sw[i]-res[i];      {#if 0                  float el=0,eh=0,g;         residue_mem(sp, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, st->mem_sp2);                  for (i=0;i<st->subframeSize;i++)            eh+=sqr(exc[i]);         overlap_cb_search(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,                           &stoc[0], 512, &gain, &ind, st->lpcSize,                           st->subframeSize);         for (i=0;i<st->subframeSize;i++)            exc[i]=gain*stoc[ind+i];         for (i=0;i<st->subframeSize;i++)            el+=sqr(exc[i]);         g=sqrt(eh/(el+.001));         for (i=0;i<st->subframeSize;i++)            exc[i]*=g;         #else         int k,N=4;         float el=0,eh=0,g;         int *index;         float *gains;         gains = PUSH(st->stack, N);         index = (int*) PUSH(st->stack, N);         residue_mem(sp, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, st->mem_sp2);                  for (i=0;i<st->subframeSize;i++)            eh+=sqr(exc[i]);         for (i=0;i<st->subframeSize;i++)            exc[i]=0;         for (k=0;k<N;k++)         {            int of=k*st->subframeSize/N;         overlap_cb_search(target+of, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,                           &stoc[0], 64, &gains[k], &index[k], st->lpcSize,                           st->subframeSize/N);         frame_bits_pack(bits,index[k],6);         for (i=0;i<st->subframeSize;i++)            res[i]=0;         for (i=0;i<st->subframeSize/N;i++)            res[of+i]=gains[k]*stoc[index[k]+i];         residue_zero(res, st->bw_lpc1, res, st->subframeSize, st->lpcSize);         syn_filt_zero(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize);         syn_filt_zero(res, st->bw_lpc2, res, st->subframeSize, st->lpcSize);         for (i=0;i<st->subframeSize;i++)            target[i]-=res[i];         for (i=0;i<st->subframeSize/N;i++)            exc[of+i]+=gains[k]*stoc[index[k]+i];         }         for (i=0;i<st->subframeSize;i++)            el+=sqr(exc[i]);         g=sqrt(eh/(el+.001));         for (i=0;i<st->subframeSize;i++)            el+=sqr(st->st_low.exc[offset+i]);         for (k=0;k<N;k++)         {            int sign=0;            float quant, dist, min_dist;            int best_ind;            int of=k*st->subframeSize/N;            gains[k]*=g;            if (gains[k]<0)            {               sign=1;               gains[k] = -gains[k];            }            quant = log((1+gains[k])*filter_ratio/(1+sqrt(el/st->subframeSize)));            min_dist = sqr(quant-quant_high_gain2[0]);            best_ind=0;            for (i=1;i<8;i++)            {               dist = sqr(quant-quant_high_gain2[i]);               if (dist<min_dist)               {                  best_ind=i;                  min_dist=dist;               }            }            quant=quant_high_gain2[best_ind];            frame_bits_pack(bits,sign,1);            frame_bits_pack(bits,best_ind,3);            gains[k]=exp(quant)*(1+sqrt(el/st->subframeSize))/filter_ratio;            if (sign)               gains[k] = -gains[k];            for (i=0;i<st->subframeSize/N;i++)               exc[of+i]=gains[k]*stoc[index[k]+i];         }         /*for (i=0;i<st->subframeSize;i++)           exc[i]*=g;*/         POP(st->stack);         POP(st->stack);#endif      }      /*Keep the previous memory*/      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      /* Final signal synthesis from excitation */      syn_filt_mem(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp);             /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */      residue_mem(sp, st->bw_lpc1, sw, st->subframeSize, st->lpcSize, mem);      syn_filt_mem(sw, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);#endif      POP(st->stack);   }   /* Up-sample coded low-band and high-band*/   for (i=0;i<st->frame_size;i++)   {      st->x0[(i<<1)]=st->x0d[i];      st->x1[(i<<1)]=st->high[i];      st->x0[(i<<1)+1]=0;      st->x1[(i<<1)+1]=0;   }   /* Reconstruct the original */   fir_mem(st->x0, h0, st->y0, st->full_frame_size, QMF_ORDER, st->g0_mem);   fir_mem(st->x1, h1, st->y1, st->full_frame_size, QMF_ORDER, st->g1_mem);   for (i=0;i<st->full_frame_size;i++)      in[i]=2*(st->y0[i]-st->y1[i]);   for (i=0;i<st->lpcSize;i++)      st->old_lsp[i] = st->lsp[i];   for (i=0;i<st->lpcSize;i++)      st->old_qlsp[i] = st->qlsp[i];   st->first=0;}