1 /* Original copyright */
2 /*-----------------------------------------------------------------------*\
4 FILE........: GAINSHAPE.C
6 AUTHOR......: David Rowe
7 COMPANY.....: Voicetronix
10 General gain-shape codebook search.
12 \*-----------------------------------------------------------------------*/
15 /* Modified by Jean-Marc Valin 2002
17 This library is free software; you can redistribute it and/or
18 modify it under the terms of the GNU Lesser General Public
19 License as published by the Free Software Foundation; either
20 version 2.1 of the License, or (at your option) any later version.
22 This library is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Lesser General Public License for more details.
27 You should have received a copy of the GNU Lesser General Public
28 License along with this library; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <cb_search.h>
39 #include "stack_alloc.h"
42 #define min(a,b) ((a) < (b) ? (a) : (b))
43 #define max(a,b) ((a) > (b) ? (a) : (b))
46 static float scal_gains4[16] = {
58 /*---------------------------------------------------------------------------*\
60 void overlap_cb_search()
62 Searches a gain/shape codebook consisting of overlapping entries for the
63 closest vector to the target. Gives identical results to search() above
64 buts uses fast end correction algorithm for the synthesis of response
67 \*---------------------------------------------------------------------------*/
69 float overlap_cb_search(
70 float target[], /* target vector */
71 float ak[], /* LPCs for this subframe */
72 float awk1[], /* Weighted LPCs for this subframe */
73 float awk2[], /* Weighted LPCs for this subframe */
74 float codebook[], /* overlapping codebook */
75 int entries, /* number of overlapping entries to search */
76 float *gain, /* gain of optimum entry */
77 int *index, /* index of optimum entry */
78 int p, /* number of LPC coeffs */
79 int nsf, /* number of samples in subframe */
83 float *resp; /* zero state response to current entry */
84 float *h; /* impulse response of synthesis filter */
85 float *impulse; /* excitation vector containing one impulse */
86 float d,e,g,score; /* codebook searching variables */
87 float bscore; /* score of "best" vector so far */
88 int i,k; /* loop variables */
92 /*resp = (float*)malloc(sizeof(float)*nsf);
93 h = (float*)malloc(sizeof(float)*nsf);
94 impulse = (float*)malloc(sizeof(float)*nsf);
96 resp=PUSH(stack, nsf);
98 impulse=PUSH(stack, nsf);
108 /* Calculate impulse response of A(z/g1) / ( A(z)*(z/g2) ) */
109 residue_zero(impulse, awk1, h, nsf, p);
110 syn_filt_zero(h, ak, h, nsf, p);
111 syn_filt_zero(h, awk2, h, nsf,p);
113 /* Calculate codebook zero-response */
114 residue_zero(&codebook[entries-1],awk1,resp,nsf,p);
115 syn_filt_zero(resp,ak,resp,nsf,p);
116 syn_filt_zero(resp,awk2,resp,nsf,p);
118 /* Search codebook backwards using end correction for synthesis */
120 for(k=entries-1; k>=0; k--) {
123 for(i=0; i<nsf; i++) {
124 d += target[i]*resp[i];
125 e += resp[i]*resp[i];
129 /*printf ("score: %f %f %f %f\n", target[0],d,e,score);*/
130 if (score >= bscore) {
136 /* Synthesise next entry */
139 for(i=nsf-1; i>=1; i--)
140 resp[i] = resp[i-1] + codebook[k-1]*h[i];
141 resp[0] = codebook[k-1]*h[0];
156 void split_cb_search(
157 float target[], /* target vector */
158 float ak[], /* LPCs for this subframe */
159 float awk1[], /* Weighted LPCs for this subframe */
160 float awk2[], /* Weighted LPCs for this subframe */
161 void *par, /* Codebook/search parameters*/
162 int p, /* number of LPC coeffs */
163 int nsf, /* number of samples in subframe */
175 int shape_cb_size, subvect_size, nb_subvect;
177 split_cb_params *params;
179 params = (split_cb_params *) par;
180 subvect_size = params->subvect_size;
181 nb_subvect = params->nb_subvect;
182 shape_cb_size = 1<<params->shape_bits;
183 shape_cb = params->shape_cb;
184 resp = PUSH(stack, shape_cb_size*subvect_size);
185 E = PUSH(stack, shape_cb_size);
186 t = PUSH(stack, nsf);
187 r = PUSH(stack, nsf);
188 e = PUSH(stack, nsf);
189 gains = PUSH(stack, nb_subvect);
190 ind = (int*)PUSH(stack, nb_subvect);
192 /* Compute energy of the "real excitation" */
193 syn_filt_zero(target, awk1, e, nsf, p);
194 residue_zero(e, ak, e, nsf, p);
195 residue_zero(e, awk2, e, nsf, p);
197 exc_energy += e[i]*e[i];
198 exc_energy=sqrt(exc_energy/nb_subvect);
200 /* Quantize global ("average") gain */
201 q=log(exc_energy+.1);
208 speex_bits_pack(bits, id, 4);
209 exc_energy=exp(.5*q+2);
218 residue_zero(e, awk1, r, nsf, p);
219 syn_filt_zero(r, ak, r, nsf, p);
220 syn_filt_zero(r, awk2, r, nsf,p);
222 /* Pre-compute codewords response and energy */
223 for (i=0;i<shape_cb_size;i++)
225 float *res = resp+i*subvect_size;
227 /* Compute codeword response */
229 for(j=0;j<subvect_size;j++)
231 for(j=0;j<subvect_size;j++)
233 for (k=j;k<subvect_size;k++)
234 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
236 /* Compute energy of codeword response */
238 for(j=0;j<subvect_size;j++)
243 for (i=0;i<nb_subvect;i++)
245 int best_index=0, k, m;
246 float g, corr, best_gain=0, score, best_score=-1;
247 /* Find best codeword for current sub-vector */
248 for (j=0;j<shape_cb_size;j++)
250 corr=xcorr(resp+j*subvect_size,t+subvect_size*i,subvect_size);
251 score=corr*corr*E[j];
253 if (score>best_score)
260 speex_bits_pack(bits,best_index,params->shape_bits);
265 best_gain /= .01+exc_energy;
268 best_gain=-best_gain;
272 /* Find gain index (it's a scalar but we use the VQ code anyway)*/
273 best_id = vq_index(&best_gain, scal_gains4, 1, 8);
275 best_gain=scal_gains4[best_id];
276 /*printf ("gain_quant: %f %d %f\n", best_gain, best_id, scal_gains4[best_id]);*/
278 best_gain=-best_gain;
279 best_gain *= exc_energy;
280 speex_bits_pack(bits,s,1);
281 speex_bits_pack(bits,best_id,3);
285 /* Update target for next subvector */
286 for (j=0;j<subvect_size;j++)
288 g=best_gain*shape_cb[best_index*subvect_size+j];
289 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
294 /* Put everything back together */
295 for (i=0;i<nb_subvect;i++)
296 for (j=0;j<subvect_size;j++)
297 e[subvect_size*i+j]=gains[i]*shape_cb[ind[i]*subvect_size+j];
299 /* Update excitation */
304 residue_zero(e, awk1, r, nsf, p);
305 syn_filt_zero(r, ak, r, nsf, p);
306 syn_filt_zero(r, awk2, r, nsf,p);
321 void split_cb_search_nogain(
322 float target[], /* target vector */
323 float ak[], /* LPCs for this subframe */
324 float awk1[], /* Weighted LPCs for this subframe */
325 float awk2[], /* Weighted LPCs for this subframe */
326 void *par, /* Codebook/search parameters*/
327 int p, /* number of LPC coeffs */
328 int nsf, /* number of samples in subframe */
339 int shape_cb_size, subvect_size, nb_subvect;
340 split_cb_params *params;
342 params = (split_cb_params *) par;
343 subvect_size = params->subvect_size;
344 nb_subvect = params->nb_subvect;
345 shape_cb_size = 1<<params->shape_bits;
346 shape_cb = params->shape_cb;
347 resp = PUSH(stack, shape_cb_size*subvect_size);
348 t = PUSH(stack, nsf);
349 r = PUSH(stack, nsf);
350 e = PUSH(stack, nsf);
351 ind = (int*)PUSH(stack, nb_subvect);
359 residue_zero(e, awk1, r, nsf, p);
360 syn_filt_zero(r, ak, r, nsf, p);
361 syn_filt_zero(r, awk2, r, nsf,p);
363 /* Pre-compute codewords response and energy */
364 for (i=0;i<shape_cb_size;i++)
366 float *res = resp+i*subvect_size;
368 /* Compute codeword response */
370 for(j=0;j<subvect_size;j++)
372 for(j=0;j<subvect_size;j++)
374 for (k=j;k<subvect_size;k++)
375 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
379 for (i=0;i<nb_subvect;i++)
381 int best_index=0, k, m;
382 float g, dist, best_dist=-1;
385 /* Find best codeword for current sub-vector */
386 for (j=0;j<shape_cb_size;j++)
389 a=resp+j*subvect_size;
391 for (k=0;k<subvect_size;k++)
392 dist += (a[k]-b[k])*(a[k]-b[k]);
393 if (dist<best_dist || j==0)
399 /*printf ("best index: %d/%d\n", best_index, shape_cb_size);*/
400 speex_bits_pack(bits,best_index,params->shape_bits);
403 /* Update target for next subvector */
404 for (j=0;j<subvect_size;j++)
406 g=shape_cb[best_index*subvect_size+j];
407 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
413 /* Put everything back together */
414 for (i=0;i<nb_subvect;i++)
415 for (j=0;j<subvect_size;j++)
416 e[subvect_size*i+j]=shape_cb[ind[i]*subvect_size+j];
418 /* Update excitation */
423 residue_zero(e, awk1, r, nsf, p);
424 syn_filt_zero(r, ak, r, nsf, p);
425 syn_filt_zero(r, awk2, r, nsf,p);
439 void split_cb_search_nogain2(
440 float target[], /* target vector */
441 float ak[], /* LPCs for this subframe */
442 float awk1[], /* Weighted LPCs for this subframe */
443 float awk2[], /* Weighted LPCs for this subframe */
444 void *par, /* Codebook/search parameters*/
445 int p, /* number of LPC coeffs */
446 int nsf, /* number of samples in subframe */
454 float *t, *r, *e, *E;
457 int shape_cb_size, subvect_size, nb_subvect;
458 split_cb_params *params;
460 params = (split_cb_params *) par;
461 subvect_size = params->subvect_size;
462 nb_subvect = params->nb_subvect;
463 shape_cb_size = 1<<params->shape_bits;
464 shape_cb = params->shape_cb;
465 resp = PUSH(stack, shape_cb_size*subvect_size);
466 t = PUSH(stack, nsf);
467 r = PUSH(stack, nsf);
468 e = PUSH(stack, nsf);
469 E = PUSH(stack, shape_cb_size);
470 ind = (int*)PUSH(stack, nb_subvect);
478 residue_zero(e, awk1, r, nsf, p);
479 syn_filt_zero(r, ak, r, nsf, p);
480 syn_filt_zero(r, awk2, r, nsf,p);
482 /* Pre-compute codewords response and energy */
483 for (i=0;i<shape_cb_size;i++)
485 float *res = resp+i*subvect_size;
487 /* Compute codeword response */
489 for(j=0;j<subvect_size;j++)
491 for(j=0;j<subvect_size;j++)
493 for (k=j;k<subvect_size;k++)
494 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
497 for(j=0;j<subvect_size;j++)
501 for (i=0;i<nb_subvect;i++)
503 int best_index[2]={0,0}, k, m;
504 float g, dist, best_dist[2]={-1,-1};
509 for (k=0;k<subvect_size;k++)
511 /* Find best codeword for current sub-vector */
512 for (j=0;j<shape_cb_size;j++)
515 a=resp+j*subvect_size;
517 for (k=0;k<subvect_size;k++)
519 if (dist<best_dist[0] || best_dist[0]<0)
521 best_dist[1]=best_dist[0];
522 best_index[1]=best_index[0];
525 } else if (dist<best_dist[1] || best_dist[1]<0)
537 for (nbest=0;nbest<2;nbest++)
541 for (j=0;j<subvect_size;j++)
543 g=shape_cb[best_index[nbest]*subvect_size+j];
544 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
549 int i2=vq_index(&tt[subvect_size*(i+1)], resp, subvect_size, shape_cb_size);
550 for (j=0;j<subvect_size;j++)
552 g=shape_cb[i2*subvect_size+j];
553 for (k=subvect_size*(i+1)+j,m=0;k<nsf;k++,m++)
559 for (j=subvect_size*i;j<subvect_size*(i+2);j++)
560 err[nbest]-=tt[j]*tt[j];
562 best_score[nbest]=err[nbest];
565 if (best_score[1]>best_score[0])
567 best_index[0]=best_index[1];
568 best_score[0]=best_score[1];
574 ind[i]=best_index[0];
576 /*printf ("best index: %d/%d\n", best_index, shape_cb_size);*/
577 speex_bits_pack(bits,ind[i],params->shape_bits);
579 /* Update target for next subvector */
580 for (j=0;j<subvect_size;j++)
582 g=shape_cb[ind[i]*subvect_size+j];
583 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
588 /* Put everything back together */
589 for (i=0;i<nb_subvect;i++)
590 for (j=0;j<subvect_size;j++)
591 e[subvect_size*i+j]=shape_cb[ind[i]*subvect_size+j];
593 /* Update excitation */
598 residue_zero(e, awk1, r, nsf, p);
599 syn_filt_zero(r, ak, r, nsf, p);
600 syn_filt_zero(r, awk2, r, nsf,p);
613 void split_cb_search_shape_sign(
614 float target[], /* target vector */
615 float ak[], /* LPCs for this subframe */
616 float awk1[], /* Weighted LPCs for this subframe */
617 float awk2[], /* Weighted LPCs for this subframe */
618 void *par, /* Codebook/search parameters*/
619 int p, /* number of LPC coeffs */
620 int nsf, /* number of samples in subframe */
628 float *t, *r, *e, *E;
631 int shape_cb_size, subvect_size, nb_subvect;
632 split_cb_params *params;
634 params = (split_cb_params *) par;
635 subvect_size = params->subvect_size;
636 nb_subvect = params->nb_subvect;
637 shape_cb_size = 1<<params->shape_bits;
638 shape_cb = params->shape_cb;
639 resp = PUSH(stack, shape_cb_size*subvect_size);
640 t = PUSH(stack, nsf);
641 r = PUSH(stack, nsf);
642 e = PUSH(stack, nsf);
643 E = PUSH(stack, shape_cb_size);
644 ind = (int*)PUSH(stack, nb_subvect);
645 signs = (int*)PUSH(stack, nb_subvect);
653 residue_zero(e, awk1, r, nsf, p);
654 syn_filt_zero(r, ak, r, nsf, p);
655 syn_filt_zero(r, awk2, r, nsf,p);
657 /* Pre-compute codewords response and energy */
658 for (i=0;i<shape_cb_size;i++)
660 float *res = resp+i*subvect_size;
662 /* Compute codeword response */
664 for(j=0;j<subvect_size;j++)
666 for(j=0;j<subvect_size;j++)
668 for (k=j;k<subvect_size;k++)
669 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
672 for(j=0;j<subvect_size;j++)
676 for (i=0;i<nb_subvect;i++)
678 int best_index[2]={0,0}, k, m;
679 float g, dist, best_dist[2]={-1,-1}, best_sign[2]={0,0};
684 for (k=0;k<subvect_size;k++)
686 /* Find best codeword for current sub-vector */
687 for (j=0;j<shape_cb_size;j++)
691 a=resp+j*subvect_size;
693 for (k=0;k<subvect_size;k++)
702 if (dist<best_dist[0] || best_dist[0]<0)
704 best_dist[1]=best_dist[0];
705 best_index[1]=best_index[0];
706 best_sign[1]=best_sign[0];
710 } else if (dist<best_dist[1] || best_dist[1]<0)
723 for (nbest=0;nbest<2;nbest++)
726 if (best_sign[nbest])
730 for (j=0;j<subvect_size;j++)
732 g=s*shape_cb[best_index[nbest]*subvect_size+j];
733 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
738 int best_index2, best_sign2, sign2;
740 x=t+subvect_size*(i+1);
741 for (j=0;j<shape_cb_size;j++)
743 a=resp+j*subvect_size;
745 for (k=0;k<subvect_size;k++)
754 if (dist<best_dist2 || j==0)
764 /*int i2=vq_index(&tt[subvect_size*(i+1)], resp, subvect_size, shape_cb_size);*/
766 for (j=0;j<subvect_size;j++)
768 g=s*shape_cb[best_index2*subvect_size+j];
769 for (k=subvect_size*(i+1)+j,m=0;k<nsf;k++,m++)
775 for (j=subvect_size*i;j<subvect_size*(i+2);j++)
776 err[nbest]-=tt[j]*tt[j];
778 best_score[nbest]=err[nbest];
781 if (best_score[1]>best_score[0])
783 best_sign[0]=best_sign[1];
784 best_index[0]=best_index[1];
785 best_score[0]=best_score[1];
791 ind[i]=best_index[0];
792 signs[i] = best_sign[0];
794 /*printf ("best index: %d/%d\n", best_index, shape_cb_size);*/
795 speex_bits_pack(bits,signs[i],1);
796 speex_bits_pack(bits,ind[i],params->shape_bits);
798 /* Update target for next subvector */
799 for (j=0;j<subvect_size;j++)
801 g=shape_cb[ind[i]*subvect_size+j];
804 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
809 /* Put everything back together */
810 for (i=0;i<nb_subvect;i++)
815 for (j=0;j<subvect_size;j++)
816 e[subvect_size*i+j]=s*shape_cb[ind[i]*subvect_size+j];
818 /* Update excitation */
823 residue_zero(e, awk1, r, nsf, p);
824 syn_filt_zero(r, ak, r, nsf, p);
825 syn_filt_zero(r, awk2, r, nsf,p);
840 void split_cb_search2(
841 float target[], /* target vector */
842 float ak[], /* LPCs for this subframe */
843 float awk1[], /* Weighted LPCs for this subframe */
844 float awk2[], /* Weighted LPCs for this subframe */
845 void *par, /* Codebook/search parameters*/
846 int p, /* number of LPC coeffs */
847 int nsf, /* number of samples in subframe */
859 int shape_cb_size, subvect_size, nb_subvect;
861 split_cb_params *params;
863 params = (split_cb_params *) par;
864 subvect_size = params->subvect_size;
865 nb_subvect = params->nb_subvect;
866 shape_cb_size = 1<<params->shape_bits;
867 shape_cb = params->shape_cb;
868 resp = PUSH(stack, shape_cb_size*subvect_size);
869 E = PUSH(stack, shape_cb_size);
870 t = PUSH(stack, nsf);
871 r = PUSH(stack, nsf);
872 e = PUSH(stack, nsf);
873 gains = PUSH(stack, nb_subvect);
874 ind = (int*)PUSH(stack, nb_subvect);
875 gain_ind = (int*)PUSH(stack, nb_subvect);
877 /* Compute energy of the "real excitation" */
878 syn_filt_zero(target, awk1, e, nsf, p);
879 residue_zero(e, ak, e, nsf, p);
880 residue_zero(e, awk2, e, nsf, p);
882 exc_energy += e[i]*e[i];
883 exc_energy=sqrt(exc_energy/nb_subvect);
885 /* Quantize global ("average") gain */
886 q=log(exc_energy+.1);
893 speex_bits_pack(bits, id, 4);
894 exc_energy=exp(.5*q+2);
903 residue_zero(e, awk1, r, nsf, p);
904 syn_filt_zero(r, ak, r, nsf, p);
905 syn_filt_zero(r, awk2, r, nsf,p);
907 /* Pre-compute codewords response and energy */
908 for (i=0;i<shape_cb_size;i++)
910 float *res = resp+i*subvect_size;
912 /* Compute codeword response */
914 for(j=0;j<subvect_size;j++)
916 for(j=0;j<subvect_size;j++)
918 for (k=j;k<subvect_size;k++)
919 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
921 /* Compute energy of codeword response */
923 for(j=0;j<subvect_size;j++)
928 for (i=0;i<nb_subvect;i++)
930 int best_index[2]={0,0}, k, m, best_gain_ind[2]={0,0};
931 float g, corr, best_gain[2]={0,0}, score, best_score[2]={-1,-1};
932 /* Find best codeword for current sub-vector */
933 for (j=0;j<shape_cb_size;j++)
935 corr=xcorr(resp+j*subvect_size,t+subvect_size*i,subvect_size);
936 score=corr*corr*E[j];
938 if (score>best_score[0])
940 best_index[1]=best_index[0];
941 best_score[1]=best_score[0];
942 best_gain[1]=best_gain[0];
947 } else if (score>best_score[1]) {
957 best_gain[k] /= .01+exc_energy;
960 best_gain[k]=-best_gain[k];
964 /* Find gain index (it's a scalar but we use the VQ code anyway)*/
965 best_id = vq_index(&best_gain[k], scal_gains4, 1, 8);
967 best_gain_ind[k]=best_id;
968 best_gain[k]=scal_gains4[best_id];
969 /*printf ("gain_quant: %f %d %f\n", best_gain, best_id, scal_gains4[best_id]);*/
971 best_gain[k]=-best_gain[k];
972 best_gain[k] *= exc_energy;
977 if (i<nb_subvect-1) {
979 float best_score2=-1, best_gain2=0;
982 float *tt=PUSH(stack,nsf);
983 for (nbest=0;nbest<2;nbest++)
987 for (j=0;j<subvect_size;j++)
989 g=best_gain[nbest]*shape_cb[best_index[nbest]*subvect_size+j];
990 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
995 for (j=0;j<shape_cb_size;j++)
997 corr=xcorr(resp+j*subvect_size,tt+subvect_size*(i+1),subvect_size);
998 score=corr*corr*E[j];
1000 if (score>best_score2)
1010 best_gain2 /= .01+exc_energy;
1013 best_gain2=-best_gain2;
1016 best_id = vq_index(&best_gain2, scal_gains4, 1, 8);
1017 best_gain2=scal_gains4[best_id];
1019 best_gain2=-best_gain2;
1020 best_gain2 *= exc_energy;
1023 for (j=0;j<subvect_size;j++)
1025 g=best_gain2*shape_cb[best_index2*subvect_size+j];
1026 for (k=subvect_size*(i+1)+j,m=0;k<nsf;k++,m++)
1029 for (j=subvect_size*i;j<subvect_size*(i+2);j++)
1030 err[nbest]-=tt[j]*tt[j];
1032 best_score[nbest]=err[nbest];
1035 if (best_score[1]>best_score[0])
1037 best_index[0]=best_index[1];
1038 best_score[0]=best_score[1];
1039 best_gain[0]=best_gain[1];
1040 best_gain_ind[0]=best_gain_ind[1];
1048 ind[i]=best_index[0];
1049 gain_ind[i]=best_gain_ind[0];
1050 gains[i]=best_gain[0];
1051 /* Update target for next subvector */
1052 for (j=0;j<subvect_size;j++)
1054 g=best_gain[0]*shape_cb[best_index[0]*subvect_size+j];
1055 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
1059 for (i=0;i<nb_subvect;i++)
1061 speex_bits_pack(bits, ind[i], params->shape_bits);
1063 speex_bits_pack(bits, 1, 1);
1065 speex_bits_pack(bits, 0, 1);
1066 speex_bits_pack(bits, gain_ind[i], 3);
1067 /*printf ("encode split: %d %d %f\n", i, ind[i], gains[i]);*/
1070 /* Put everything back together */
1071 for (i=0;i<nb_subvect;i++)
1072 for (j=0;j<subvect_size;j++)
1073 e[subvect_size*i+j]=gains[i]*shape_cb[ind[i]*subvect_size+j];
1075 /* Update excitation */
1080 residue_zero(e, awk1, r, nsf, p);
1081 syn_filt_zero(r, ak, r, nsf, p);
1082 syn_filt_zero(r, awk2, r, nsf,p);
1101 void split_cb_unquant(
1103 void *par, /* non-overlapping codebook */
1104 int nsf, /* number of samples in subframe */
1113 float *shape_cb, exc_energy;
1114 int shape_cb_size, subvect_size, nb_subvect;
1115 split_cb_params *params;
1117 params = (split_cb_params *) par;
1118 subvect_size = params->subvect_size;
1119 nb_subvect = params->nb_subvect;
1120 shape_cb_size = 1<<params->shape_bits;
1121 shape_cb = params->shape_cb;
1123 ind = (int*)PUSH(stack, nb_subvect);
1124 gains = PUSH(stack, nb_subvect);
1125 sign = PUSH(stack, nb_subvect);
1127 /* Decode global (average) gain */
1130 id = speex_bits_unpack_unsigned(bits, 4);
1131 exc_energy=exp(.5*id+2);
1134 /* Decode codewords and gains */
1135 for (i=0;i<nb_subvect;i++)
1138 ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits);
1139 if (speex_bits_unpack_unsigned(bits, 1))
1144 gain_id = speex_bits_unpack_unsigned(bits, 3);
1145 gains[i]=scal_gains4[gain_id];
1146 gains[i] *= sign[i];
1147 gains[i] *= exc_energy;
1149 /*printf ("decode split: %d %d %f\n", i, ind[i], gains[i]);*/
1152 /* Compute decoded excitation */
1153 for (i=0;i<nb_subvect;i++)
1154 for (j=0;j<subvect_size;j++)
1155 exc[subvect_size*i+j]+=gains[i]*shape_cb[ind[i]*subvect_size+j];
1164 void split_cb_nogain_unquant(
1166 void *par, /* non-overlapping codebook */
1167 int nsf, /* number of samples in subframe */
1175 int shape_cb_size, subvect_size, nb_subvect;
1176 split_cb_params *params;
1178 params = (split_cb_params *) par;
1179 subvect_size = params->subvect_size;
1180 nb_subvect = params->nb_subvect;
1181 shape_cb_size = 1<<params->shape_bits;
1182 shape_cb = params->shape_cb;
1184 ind = (int*)PUSH(stack, nb_subvect);
1186 /* Decode codewords and gains */
1187 for (i=0;i<nb_subvect;i++)
1188 ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits);
1190 /* Compute decoded excitation */
1191 for (i=0;i<nb_subvect;i++)
1192 for (j=0;j<subvect_size;j++)
1193 exc[subvect_size*i+j]+=shape_cb[ind[i]*subvect_size+j];
1198 void split_cb_shape_sign_unquant(
1200 void *par, /* non-overlapping codebook */
1201 int nsf, /* number of samples in subframe */
1209 int shape_cb_size, subvect_size, nb_subvect;
1210 split_cb_params *params;
1212 params = (split_cb_params *) par;
1213 subvect_size = params->subvect_size;
1214 nb_subvect = params->nb_subvect;
1215 shape_cb_size = 1<<params->shape_bits;
1216 shape_cb = params->shape_cb;
1218 ind = (int*)PUSH(stack, nb_subvect);
1219 signs = (int*)PUSH(stack, nb_subvect);
1221 /* Decode codewords and gains */
1222 for (i=0;i<nb_subvect;i++)
1224 signs[i] = speex_bits_unpack_unsigned(bits, 1);
1225 ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits);
1227 /* Compute decoded excitation */
1228 for (i=0;i<nb_subvect;i++)
1233 for (j=0;j<subvect_size;j++)
1234 exc[subvect_size*i+j]+=s*shape_cb[ind[i]*subvect_size+j];