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"
43 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #define max(a,b) ((a) > (b) ? (a) : (b))
47 static float scal_gains4[16] = {
59 /*---------------------------------------------------------------------------*\
61 void overlap_cb_search()
63 Searches a gain/shape codebook consisting of overlapping entries for the
64 closest vector to the target. Gives identical results to search() above
65 buts uses fast end correction algorithm for the synthesis of response
68 \*---------------------------------------------------------------------------*/
70 float overlap_cb_search(
71 float target[], /* target vector */
72 float ak[], /* LPCs for this subframe */
73 float awk1[], /* Weighted LPCs for this subframe */
74 float awk2[], /* Weighted LPCs for this subframe */
75 float codebook[], /* overlapping codebook */
76 int entries, /* number of overlapping entries to search */
77 float *gain, /* gain of optimum entry */
78 int *index, /* index of optimum entry */
79 int p, /* number of LPC coeffs */
80 int nsf, /* number of samples in subframe */
84 float *resp; /* zero state response to current entry */
85 float *h; /* impulse response of synthesis filter */
86 float *impulse; /* excitation vector containing one impulse */
87 float d,e,g,score; /* codebook searching variables */
88 float bscore; /* score of "best" vector so far */
89 int i,k; /* loop variables */
93 /*resp = (float*)malloc(sizeof(float)*nsf);
94 h = (float*)malloc(sizeof(float)*nsf);
95 impulse = (float*)malloc(sizeof(float)*nsf);
97 resp=PUSH(stack, nsf);
99 impulse=PUSH(stack, nsf);
109 /* Calculate impulse response of A(z/g1) / ( A(z)*(z/g2) ) */
110 residue_zero(impulse, awk1, h, nsf, p);
111 syn_filt_zero(h, ak, h, nsf, p);
112 syn_filt_zero(h, awk2, h, nsf,p);
114 /* Calculate codebook zero-response */
115 residue_zero(&codebook[entries-1],awk1,resp,nsf,p);
116 syn_filt_zero(resp,ak,resp,nsf,p);
117 syn_filt_zero(resp,awk2,resp,nsf,p);
119 /* Search codebook backwards using end correction for synthesis */
121 for(k=entries-1; k>=0; k--) {
124 for(i=0; i<nsf; i++) {
125 d += target[i]*resp[i];
126 e += resp[i]*resp[i];
130 /*printf ("score: %f %f %f %f\n", target[0],d,e,score);*/
131 if (score >= bscore) {
137 /* Synthesise next entry */
140 for(i=nsf-1; i>=1; i--)
141 resp[i] = resp[i-1] + codebook[k-1]*h[i];
142 resp[0] = codebook[k-1]*h[0];
157 void split_cb_search(
158 float target[], /* target vector */
159 float ak[], /* LPCs for this subframe */
160 float awk1[], /* Weighted LPCs for this subframe */
161 float awk2[], /* Weighted LPCs for this subframe */
162 void *par, /* Codebook/search parameters*/
163 int p, /* number of LPC coeffs */
164 int nsf, /* number of samples in subframe */
176 int shape_cb_size, subvect_size, nb_subvect;
178 split_cb_params *params;
180 params = (split_cb_params *) par;
181 subvect_size = params->subvect_size;
182 nb_subvect = params->nb_subvect;
183 shape_cb_size = 1<<params->shape_bits;
184 shape_cb = params->shape_cb;
185 resp = PUSH(stack, shape_cb_size*subvect_size);
186 E = PUSH(stack, shape_cb_size);
187 t = PUSH(stack, nsf);
188 r = PUSH(stack, nsf);
189 e = PUSH(stack, nsf);
190 gains = PUSH(stack, nb_subvect);
191 ind = (int*)PUSH(stack, nb_subvect);
193 /* Compute energy of the "real excitation" */
194 syn_filt_zero(target, awk1, e, nsf, p);
195 residue_zero(e, ak, e, nsf, p);
196 residue_zero(e, awk2, e, nsf, p);
198 exc_energy += e[i]*e[i];
199 exc_energy=sqrt(exc_energy/nb_subvect);
201 /* Quantize global ("average") gain */
202 q=log(exc_energy+.1);
209 speex_bits_pack(bits, id, 4);
210 exc_energy=exp(.5*q+2);
219 residue_zero(e, awk1, r, nsf, p);
220 syn_filt_zero(r, ak, r, nsf, p);
221 syn_filt_zero(r, awk2, r, nsf,p);
223 /* Pre-compute codewords response and energy */
224 for (i=0;i<shape_cb_size;i++)
226 float *res = resp+i*subvect_size;
228 /* Compute codeword response */
230 for(j=0;j<subvect_size;j++)
232 for(j=0;j<subvect_size;j++)
234 for (k=j;k<subvect_size;k++)
235 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
237 /* Compute energy of codeword response */
239 for(j=0;j<subvect_size;j++)
244 for (i=0;i<nb_subvect;i++)
246 int best_index=0, k, m;
247 float g, corr, best_gain=0, score, best_score=-1;
248 /* Find best codeword for current sub-vector */
249 for (j=0;j<shape_cb_size;j++)
251 corr=xcorr(resp+j*subvect_size,t+subvect_size*i,subvect_size);
252 score=corr*corr*E[j];
254 if (score>best_score)
261 speex_bits_pack(bits,best_index,params->shape_bits);
266 best_gain /= .01+exc_energy;
269 best_gain=-best_gain;
273 /* Find gain index (it's a scalar but we use the VQ code anyway)*/
274 best_id = vq_index(&best_gain, scal_gains4, 1, 8);
276 best_gain=scal_gains4[best_id];
277 /*printf ("gain_quant: %f %d %f\n", best_gain, best_id, scal_gains4[best_id]);*/
279 best_gain=-best_gain;
280 best_gain *= exc_energy;
281 speex_bits_pack(bits,s,1);
282 speex_bits_pack(bits,best_id,3);
286 /* Update target for next subvector */
287 for (j=0;j<subvect_size;j++)
289 g=best_gain*shape_cb[best_index*subvect_size+j];
290 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
295 /* Put everything back together */
296 for (i=0;i<nb_subvect;i++)
297 for (j=0;j<subvect_size;j++)
298 e[subvect_size*i+j]=gains[i]*shape_cb[ind[i]*subvect_size+j];
300 /* Update excitation */
305 residue_zero(e, awk1, r, nsf, p);
306 syn_filt_zero(r, ak, r, nsf, p);
307 syn_filt_zero(r, awk2, r, nsf,p);
322 void split_cb_search_nogain(
323 float target[], /* target vector */
324 float ak[], /* LPCs for this subframe */
325 float awk1[], /* Weighted LPCs for this subframe */
326 float awk2[], /* Weighted LPCs for this subframe */
327 void *par, /* Codebook/search parameters*/
328 int p, /* number of LPC coeffs */
329 int nsf, /* number of samples in subframe */
340 int shape_cb_size, subvect_size, nb_subvect;
341 split_cb_params *params;
343 params = (split_cb_params *) par;
344 subvect_size = params->subvect_size;
345 nb_subvect = params->nb_subvect;
346 shape_cb_size = 1<<params->shape_bits;
347 shape_cb = params->shape_cb;
348 resp = PUSH(stack, shape_cb_size*subvect_size);
349 t = PUSH(stack, nsf);
350 r = PUSH(stack, nsf);
351 e = PUSH(stack, nsf);
352 ind = (int*)PUSH(stack, nb_subvect);
360 residue_zero(e, awk1, r, nsf, p);
361 syn_filt_zero(r, ak, r, nsf, p);
362 syn_filt_zero(r, awk2, r, nsf,p);
364 /* Pre-compute codewords response and energy */
365 for (i=0;i<shape_cb_size;i++)
367 float *res = resp+i*subvect_size;
369 /* Compute codeword response */
371 for(j=0;j<subvect_size;j++)
373 for(j=0;j<subvect_size;j++)
375 for (k=j;k<subvect_size;k++)
376 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
380 for (i=0;i<nb_subvect;i++)
382 int best_index=0, k, m;
383 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++)
412 /* Put everything back together */
413 for (i=0;i<nb_subvect;i++)
414 for (j=0;j<subvect_size;j++)
415 e[subvect_size*i+j]=shape_cb[ind[i]*subvect_size+j];
417 /* Update excitation */
422 residue_zero(e, awk1, r, nsf, p);
423 syn_filt_zero(r, ak, r, nsf, p);
424 syn_filt_zero(r, awk2, r, nsf,p);
440 void split_cb_search2(
441 float target[], /* target vector */
442 float ak[], /* LPCs for this subframe */
443 float awk1[], /* Weighted LPCs for this subframe */
444 float awk2[], /* Weighted LPCs for this subframe */
445 void *par, /* Codebook/search parameters*/
446 int p, /* number of LPC coeffs */
447 int nsf, /* number of samples in subframe */
459 int shape_cb_size, subvect_size, nb_subvect;
461 split_cb_params *params;
463 params = (split_cb_params *) par;
464 subvect_size = params->subvect_size;
465 nb_subvect = params->nb_subvect;
466 shape_cb_size = 1<<params->shape_bits;
467 shape_cb = params->shape_cb;
468 resp = PUSH(stack, shape_cb_size*subvect_size);
469 E = PUSH(stack, shape_cb_size);
470 t = PUSH(stack, nsf);
471 r = PUSH(stack, nsf);
472 e = PUSH(stack, nsf);
473 gains = PUSH(stack, nb_subvect);
474 ind = (int*)PUSH(stack, nb_subvect);
475 gain_ind = (int*)PUSH(stack, nb_subvect);
477 /* Compute energy of the "real excitation" */
478 syn_filt_zero(target, awk1, e, nsf, p);
479 residue_zero(e, ak, e, nsf, p);
480 residue_zero(e, awk2, e, nsf, p);
482 exc_energy += e[i]*e[i];
483 exc_energy=sqrt(exc_energy/nb_subvect);
485 /* Quantize global ("average") gain */
486 q=log(exc_energy+.1);
493 speex_bits_pack(bits, id, 4);
494 exc_energy=exp(.5*q+2);
503 residue_zero(e, awk1, r, nsf, p);
504 syn_filt_zero(r, ak, r, nsf, p);
505 syn_filt_zero(r, awk2, r, nsf,p);
507 /* Pre-compute codewords response and energy */
508 for (i=0;i<shape_cb_size;i++)
510 float *res = resp+i*subvect_size;
512 /* Compute codeword response */
514 for(j=0;j<subvect_size;j++)
516 for(j=0;j<subvect_size;j++)
518 for (k=j;k<subvect_size;k++)
519 res[k]+=shape_cb[i*subvect_size+j]*r[k-j];
521 /* Compute energy of codeword response */
523 for(j=0;j<subvect_size;j++)
528 for (i=0;i<nb_subvect;i++)
530 int best_index[2]={0,0}, k, m, best_gain_ind[2]={0,0};
531 float g, corr, best_gain[2]={0,0}, score, best_score[2]={-1,-1};
532 /* Find best codeword for current sub-vector */
533 for (j=0;j<shape_cb_size;j++)
535 corr=xcorr(resp+j*subvect_size,t+subvect_size*i,subvect_size);
536 score=corr*corr*E[j];
538 if (score>best_score[0])
540 best_index[1]=best_index[0];
541 best_score[1]=best_score[0];
542 best_gain[1]=best_gain[0];
547 } else if (score>best_score[1]) {
557 best_gain[k] /= .01+exc_energy;
560 best_gain[k]=-best_gain[k];
564 /* Find gain index (it's a scalar but we use the VQ code anyway)*/
565 best_id = vq_index(&best_gain[k], scal_gains4, 1, 8);
567 best_gain_ind[k]=best_id;
568 best_gain[k]=scal_gains4[best_id];
569 /*printf ("gain_quant: %f %d %f\n", best_gain, best_id, scal_gains4[best_id]);*/
571 best_gain[k]=-best_gain[k];
572 best_gain[k] *= exc_energy;
577 if (i<nb_subvect-1) {
579 float best_score2=-1, best_gain2=0;
582 float *tt=PUSH(stack,nsf);
583 for (nbest=0;nbest<2;nbest++)
587 for (j=0;j<subvect_size;j++)
589 g=best_gain[nbest]*shape_cb[best_index[nbest]*subvect_size+j];
590 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
595 for (j=0;j<shape_cb_size;j++)
597 corr=xcorr(resp+j*subvect_size,tt+subvect_size*(i+1),subvect_size);
598 score=corr*corr*E[j];
600 if (score>best_score2)
610 best_gain2 /= .01+exc_energy;
613 best_gain2=-best_gain2;
616 best_id = vq_index(&best_gain2, scal_gains4, 1, 8);
617 best_gain2=scal_gains4[best_id];
619 best_gain2=-best_gain2;
620 best_gain2 *= exc_energy;
623 for (j=0;j<subvect_size;j++)
625 g=best_gain2*shape_cb[best_index2*subvect_size+j];
626 for (k=subvect_size*(i+1)+j,m=0;k<nsf;k++,m++)
629 for (j=subvect_size*i;j<subvect_size*(i+2);j++)
630 err[nbest]-=tt[j]*tt[j];
632 best_score[nbest]=err[nbest];
635 if (best_score[1]>best_score[0])
637 best_index[0]=best_index[1];
638 best_score[0]=best_score[1];
639 best_gain[0]=best_gain[1];
640 best_gain_ind[0]=best_gain_ind[1];
648 ind[i]=best_index[0];
649 gain_ind[i]=best_gain_ind[0];
650 gains[i]=best_gain[0];
651 /* Update target for next subvector */
652 for (j=0;j<subvect_size;j++)
654 g=best_gain[0]*shape_cb[best_index[0]*subvect_size+j];
655 for (k=subvect_size*i+j,m=0;k<nsf;k++,m++)
659 for (i=0;i<nb_subvect;i++)
661 speex_bits_pack(bits, ind[i], params->shape_bits);
663 speex_bits_pack(bits, 1, 1);
665 speex_bits_pack(bits, 0, 1);
666 speex_bits_pack(bits, gain_ind[i], 3);
667 /*printf ("encode split: %d %d %f\n", i, ind[i], gains[i]);*/
670 /* Put everything back together */
671 for (i=0;i<nb_subvect;i++)
672 for (j=0;j<subvect_size;j++)
673 e[subvect_size*i+j]=gains[i]*shape_cb[ind[i]*subvect_size+j];
675 /* Update excitation */
680 residue_zero(e, awk1, r, nsf, p);
681 syn_filt_zero(r, ak, r, nsf, p);
682 syn_filt_zero(r, awk2, r, nsf,p);
701 void split_cb_unquant(
703 void *par, /* non-overlapping codebook */
704 int nsf, /* number of samples in subframe */
713 float *shape_cb, exc_energy;
714 int shape_cb_size, subvect_size, nb_subvect;
715 split_cb_params *params;
717 params = (split_cb_params *) par;
718 subvect_size = params->subvect_size;
719 nb_subvect = params->nb_subvect;
720 shape_cb_size = 1<<params->shape_bits;
721 shape_cb = params->shape_cb;
723 ind = (int*)PUSH(stack, nb_subvect);
724 gains = PUSH(stack, nb_subvect);
725 sign = PUSH(stack, nb_subvect);
727 /* Decode global (average) gain */
730 id = speex_bits_unpack_unsigned(bits, 4);
731 exc_energy=exp(.5*id+2);
734 /* Decode codewords and gains */
735 for (i=0;i<nb_subvect;i++)
738 ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits);
739 if (speex_bits_unpack_unsigned(bits, 1))
744 gain_id = speex_bits_unpack_unsigned(bits, 3);
745 gains[i]=scal_gains4[gain_id];
747 gains[i] *= exc_energy;
749 /*printf ("decode split: %d %d %f\n", i, ind[i], gains[i]);*/
752 /* Compute decoded excitation */
753 for (i=0;i<nb_subvect;i++)
754 for (j=0;j<subvect_size;j++)
755 exc[subvect_size*i+j]+=gains[i]*shape_cb[ind[i]*subvect_size+j];
764 void split_cb_nogain_unquant(
766 void *par, /* non-overlapping codebook */
767 int nsf, /* number of samples in subframe */
775 int shape_cb_size, subvect_size, nb_subvect;
776 split_cb_params *params;
778 params = (split_cb_params *) par;
779 subvect_size = params->subvect_size;
780 nb_subvect = params->nb_subvect;
781 shape_cb_size = 1<<params->shape_bits;
782 shape_cb = params->shape_cb;
784 ind = (int*)PUSH(stack, nb_subvect);
786 /* Decode codewords and gains */
787 for (i=0;i<nb_subvect;i++)
788 ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits);
790 /* Compute decoded excitation */
791 for (i=0;i<nb_subvect;i++)
792 for (j=0;j<subvect_size;j++)
793 exc[subvect_size*i+j]+=shape_cb[ind[i]*subvect_size+j];