Skip to content

Commit ff69fae

Browse files
committed
documented fast search algorithm, working fore newamp1
1 parent c0c93dc commit ff69fae

File tree

3 files changed

+38
-58
lines changed

3 files changed

+38
-58
lines changed

src/mbest.c

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void mbest_destroy(struct MBEST *mbest) {
6666

6767
/* precompyte table for efficient VQ search */
6868

69-
void mbest_precompute_cbsq(float cbsq[], float cb[], int k, int m) {
69+
void mbest_precompute_cbsq(float cbsq[], const float cb[], int k, int m) {
7070
for (int j=0; j<m; j++) {
7171
cbsq[j] = 0.0;
7272
for(int i=0; i<k; i++)
@@ -132,79 +132,60 @@ void mbest_search(
132132
int index[] /* indexes that lead us here */
133133
)
134134
{
135-
float e;
136-
int j;
135+
int j;
137136

137+
float vecsq = 0.0;
138+
for(int i=0; i<k; i++)
139+
vecsq += vec[i]*vec[i];
140+
138141
for(j=0; j<m; j++) {
139-
float diff;
140-
int i;
141142

142143
/*
143-
float cbsq = 0.0;
144-
for(int i = 0; i < k; i++) {
145-
cbsq += cb[j*k+i]*cb[j*k+i];
146-
}
147-
*/
148-
/*
149-
e = 0.0;
150-
for(i=0; i<k; i++) {
151-
diff = cb[j*k+i]-vec[i];
152-
e += diff*w[i]*diff*w[i];
153-
}
144+
float e = 0.0;
145+
for(i=0; i<k; i++)
146+
e += (cb[j*k+i] - vec[i])*(cb[j*k+i] - vec[i]);
147+
148+
|
149+
\|/
150+
151+
float e = 0.0;
152+
for(i=0; i<k; i++)
153+
e += cb[j*k+i]*cb[j*k+i] - 2*cb[j*k+i]*vec[i] + vec[i]*vec[i];
154+
155+
|
156+
\|/
157+
158+
float e = 0.0; float corr = 0.0;
159+
for(i=0; i<k; i++)
160+
e += cb[j*k+i]*cb[j*k+i]; .... (1)
161+
for(i=0; i<k; i++)
162+
e -= 2*cb[j*k+i]*vec[i]; .... (2)
163+
for(i=0; i<k; i++)
164+
e += vec[i]*vec[i]; .... (3)
165+
166+
(1) can be precomputed, so we just need to compute (2) for each search,
167+
(3) can be computed outside the search loop
154168
*/
169+
155170
float corr = 0.0;
156-
for(i=0; i<k; i++)
171+
for(int i=0; i<k; i++)
157172
corr += cb[j*k+i]*vec[i];
158-
float e = cbsq[j] - 2*corr;
173+
float e = cbsq[j] - 2*corr + vecsq;
174+
159175
index[0] = j;
160176
if (e < mbest->list[mbest->entries - 1].error)
161177
mbest_insert(mbest, index, e);
162178
}
163179
}
164180

165-
/*---------------------------------------------------------------------------*\
166-
167-
mbest_search_equalweight
168-
169-
Searches vec[] to a codebbook of vectors, and maintains a list of the mbest
170-
closest matches. Similar to mbest_search() but with w[] = 1.
171-
172-
\*---------------------------------------------------------------------------*/
173-
174-
void mbest_search_equalweight(
175-
const float *cb, /* VQ codebook to search */
176-
float vec[], /* target vector */
177-
int k, /* dimension of vector */
178-
int m, /* number on entries in codebook */
179-
struct MBEST *mbest, /* list of closest matches */
180-
int index[] /* indexes that lead us here */
181-
)
182-
{
183-
float e;
184-
185-
for(int j = 0; j < m; j++) {
186-
e = 0.0;
187-
for(int i = 0; i < k; i++) {
188-
float diff = (*cb++) - vec[i];
189-
float diff2 = diff * diff;
190-
e += diff2;
191-
}
192-
193-
index[0] = j;
194-
if (e < mbest->list[mbest->entries - 1].error)
195-
mbest_insert(mbest, index, e);
196-
}
197-
}
198-
199-
200181
/*---------------------------------------------------------------------------*\
201182
202183
mbest_search450
203184
204185
Searches vec[] to a codebbook of vectors, and maintains a list of the mbest
205186
closest matches. Only searches the first NewAmp2_K Vectors
206187
207-
\*---------------------------------------------------------------------------*/
188+
\*---------------------------------------------------------------------------*/
208189

209190
void mbest_search450(const float *cb, float vec[], float w[], int k,int shorterK, int m, struct MBEST *mbest, int index[])
210191

src/mbest.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ struct MBEST {
4444

4545
struct MBEST *mbest_create(int entries);
4646
void mbest_destroy(struct MBEST *mbest);
47-
void mbest_precompute_cbsq(float cbsq[], float cb[], int k, int m);
47+
void mbest_precompute_cbsq(float cbsq[], const float cb[], int k, int m);
4848
void mbest_insert(struct MBEST *mbest, int index[], float error);
49-
void mbest_search(const float *cb, const float *cbsq, float vec[], float w[], int k, int m, struct MBEST *mbest, int index[]);
50-
void mbest_search_equalweight(const float *cb, float vec[], int k, int m, struct MBEST *mbest, int index[]);
49+
void mbest_search(const float *cb, const float *cbsq, float vec[], float w[], int k, int m, struct MBEST *mbest, int index[]);
5150
void mbest_search450(const float *cb, float vec[], float w[], int k,int shorterK, int m, struct MBEST *mbest, int index[]);
5251

5352
void mbest_print(char title[], struct MBEST *mbest);

src/newamp1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ float rate_K_mbest_encode(int *indexes, float *x, float *xq, int ndim, int mbest
174174

175175
/* precompute tables for efficient search */
176176
mbest_precompute_cbsq(codebook1sq, codebook1, newamp1vq_cb[0].k, newamp1vq_cb[0].m);
177-
mbest_precompute_cbsq(codebook2sq, codebook2, newamp1vq_cb[0].k, newamp1vq_cb[0].m);
177+
mbest_precompute_cbsq(codebook2sq, codebook2, newamp1vq_cb[1].k, newamp1vq_cb[1].m);
178178

179179
/* codebook is compiled for a fixed K */
180180

0 commit comments

Comments
 (0)