printf ("exc: "); for (i=0;i<nb_subvect;i++) printf ("%f ", gains[i]); printf ("\n");
void split_cb_search(float target[],			/* target vector */float ak[],			/* LPCs for this subframe */float awk1[],			/* Weighted LPCs for this subframe */float awk2[],			/* Weighted LPCs for this subframe */void *par,                      /* Codebook/search parameters*/int   p,                        /* number of LPC coeffs */int   nsf,                      /* number of samples in subframe */float *exc,FrameBits *bits,float *stack){   int i,j;   float *resp, *E, *Ee;   float *t, *r, *e;   float *gains;   int *ind;   float *shape_cb, *gain_cb;   int shape_cb_size, gain_cb_size, subvect_size, nb_subvect;   split_cb_params *params;   params = (split_cb_params *) par;   subvect_size = params->subvect_size;   nb_subvect = params->nb_subvect;   shape_cb_size = 1<<params->shape_bits;   shape_cb = params->shape_cb;   gain_cb_size = 1<<params->gain_bits;   gain_cb = params->gain_cb;   resp = PUSH(stack, shape_cb_size*subvect_size);   E = PUSH(stack, shape_cb_size);   Ee = PUSH(stack, shape_cb_size);   t = PUSH(stack, nsf);   r = PUSH(stack, nsf);   e = PUSH(stack, nsf);   gains = PUSH(stack, nb_subvect);   ind = (int*)PUSH(stack, nb_subvect);      for (i=0;i<nsf;i++)      t[i]=target[i];   for (i=0;i<shape_cb_size;i++)   {      float *res = resp+i*subvect_size;      residue_zero(shape_cb+i*subvect_size, awk1, res, subvect_size, p);      syn_filt_zero(res, ak, res, subvect_size, p);      syn_filt_zero(res, awk2, res, subvect_size,p);      E[i]=0;      for(j=0;j<subvect_size;j++)         E[i]+=res[j]*res[j];      Ee[i]=0;      for(j=0;j<subvect_size;j++)         Ee[i]+=shape_cb[i*subvect_size+j]*shape_cb[i*subvect_size+j];         }   for (i=0;i<nb_subvect;i++)   {      int best_index=0;      float g, corr, best_gain=0, score, best_score=-1;      for (j=0;j<shape_cb_size;j++)      {         corr=xcorr(resp+j*subvect_size,t+subvect_size*i,subvect_size);         score=corr*corr/(.001+E[j]);         g = corr/(.001+E[j]);         if (score>best_score)         {            best_index=j;            best_score=score;            best_gain=corr/(.001+E[j]);         }      }      frame_bits_pack(bits,best_index,params->shape_bits);      if (best_gain>0)         frame_bits_pack(bits,0,1);      else          frame_bits_pack(bits,1,1);              ind[i]=best_index;      gains[i]=best_gain*Ee[ind[i]];      for (j=0;j<nsf;j++)         e[j]=0;      for (j=0;j<subvect_size;j++)         e[subvect_size*i+j]=best_gain*shape_cb[best_index*subvect_size+j];      residue_zero(e, awk1, r, nsf, p);      syn_filt_zero(r, ak, r, nsf, p);      syn_filt_zero(r, awk2, r, nsf,p);      for (j=0;j<nsf;j++)         t[j]-=r[j];   }   {      int best_vq_index=0, max_index;      float max_gain=0, log_max, min_dist=0, *sign;      if (gain_cb) /*If no gain codebok, do not quantize (for testing/debugging) */      {         sign = PUSH(stack, nb_subvect);         for (i=0;i<nb_subvect;i++)         {            if (gains[i]<0)            {               gains[i]=-gains[i];               sign[i]=-1;            } else {               sign[i]=1;            }         }         for (i=0;i<nb_subvect;i++)            if (gains[i]>max_gain)               max_gain=gains[i];         log_max=log(max_gain+1);         max_index = (int)(floor(.5+log_max-3));         if (max_index>7)            max_index=7;         if (max_index<0)            max_index=0;         max_gain=1/exp(max_index+3.0);         for (i=0;i<nb_subvect;i++)            gains[i]*=max_gain;         frame_bits_pack(bits,max_index,3);         /*Vector quantize gains[i]*/         best_vq_index = vq_index(gains, gain_cb, nb_subvect, gain_cb_size);         frame_bits_pack(bits,best_vq_index,params->gain_bits);         printf ("best_gains_vq_index %d %f %d\n", best_vq_index, min_dist, max_index);         for (i=0;i<nb_subvect;i++)            gains[i]= sign[i]*gain_cb[best_vq_index*nb_subvect+i]/max_gain/(Ee[ind[i]]+.001);             POP(stack);      } else {         for (i=0;i<nb_subvect;i++)            gains[i]= gains[i]/(Ee[ind[i]]+.001);      }      for (i=0;i<nb_subvect;i++)         for (j=0;j<subvect_size;j++)            exc[subvect_size*i+j]+=gains[i]*shape_cb[ind[i]*subvect_size+j];   }   /*TODO: Perform joint optimization of gains*/      for (i=0;i<nsf;i++)      target[i]=t[i];   POP(stack);   POP(stack);   POP(stack);   POP(stack);   POP(stack);   POP(stack);   POP(stack);   POP(stack);}