1 /*-----------------------------------------------------------------------*\
3 FILE........: GAINSHAPE.C
5 AUTHOR......: David Rowe
6 COMPANY.....: Voicetronix
9 General gain-shape codebbok search.
11 \*-----------------------------------------------------------------------*/
13 /* Modified by Jean-Marc Valin 2002 */
17 #include <cb_search.h>
20 #define min(a,b) ((a) < (b) ? (a) : (b))
22 /*---------------------------------------------------------------------------*\
24 void overlap_cb_search()
26 Searches a gain/shape codebook consisting of overlapping entries for the
27 closest vector to the target. Gives identical results to search() above
28 buts uses fast end correction algorithm for the synthesis of response
31 \*---------------------------------------------------------------------------*/
33 void overlap_cb_search(
34 float target[], /* target vector */
35 float ak[], /* LPCs for this subframe */
36 float awk1[], /* Weighted LPCs for this subframe */
37 float awk2[], /* Weighted LPCs for this subframe */
38 float codebook[], /* overlapping codebook */
39 int entries, /* number of overlapping entries to search */
40 float *gain, /* gain of optimum entry */
41 int *index, /* index of optimum entry */
42 int p, /* number of LPC coeffs */
43 int nsf /* number of samples in subframe */
46 float *resp; /* zero state response to current entry */
47 float *h; /* impulse response of synthesis filter */
48 float *impulse; /* excitation vector containing one impulse */
49 float d,e,g,score; /* codebook searching variables */
50 float bscore; /* score of "best" vector so far */
51 int i,j,k; /* loop variables */
55 resp = (float*)malloc(sizeof(float)*nsf);
56 h = (float*)malloc(sizeof(float)*nsf);
57 impulse = (float*)malloc(sizeof(float)*nsf);
67 /* Calculate impulse response of A(z/g2) / ( A(z)*(z/g1) ) */
68 residue_zero(impulse, awk1, h, nsf, p);
69 syn_filt_zero(h, ak, h, nsf, p);
70 syn_filt_zero(h, awk2, h, nsf,p);
72 /* Calculate codebook zero-response */
73 residue_zero(&codebook[entries-1],awk1,resp,nsf,p);
74 syn_filt_zero(resp,ak,resp,nsf,p);
75 syn_filt_zero(resp,awk2,resp,nsf,p);
77 /* Search codebook backwards using end correction for synthesis */
79 for(k=entries-1; k>=0; k--) {
81 /* residue_zero(&codebook[k],awk1,resp,nsf,p);
82 syn_filt_zero(resp,ak,resp,nsf,p);
83 syn_filt_zero(resp,awk2,resp,nsf,p);
86 for(i=0; i<nsf; i++) {
87 d += target[i]*resp[i];
88 e += resp[i]*resp[i]+1;
92 printf ("score: %f %f %f %f\n", target[0],d,e,score);
93 if (score >= bscore) {
99 /* Synthesise next entry */
102 for(i=nsf-1; i>=1; i--)
103 resp[i] = resp[i-1] + codebook[k-1]*h[i];
104 resp[0] = codebook[k-1]*h[0];