From 30325d24d107dbf133da39f7c96d1510fd1c9449 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Fri, 15 Aug 2025 12:42:40 +0200 Subject: Bump to codec2 version 1.2.0 --- mbest.c | 194 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 106 insertions(+), 88 deletions(-) (limited to 'mbest.c') diff --git a/mbest.c b/mbest.c index 6216b77..9c1a6c5 100644 --- a/mbest.c +++ b/mbest.c @@ -27,42 +27,48 @@ */ +#include "mbest.h" + #include #include #include #include #include -#include "mbest.h" - struct MBEST *mbest_create(int entries) { - int i,j; - struct MBEST *mbest; + int i, j; + struct MBEST *mbest; - assert(entries > 0); - mbest = (struct MBEST *)malloc(sizeof(struct MBEST)); - assert(mbest != NULL); + assert(entries > 0); + mbest = (struct MBEST *)malloc(sizeof(struct MBEST)); + assert(mbest != NULL); - mbest->entries = entries; - mbest->list = (struct MBEST_LIST *)malloc(entries*sizeof(struct MBEST_LIST)); - assert(mbest->list != NULL); + mbest->entries = entries; + mbest->list = + (struct MBEST_LIST *)malloc(entries * sizeof(struct MBEST_LIST)); + assert(mbest->list != NULL); - for(i=0; ientries; i++) { - for(j=0; jlist[i].index[j] = 0; - mbest->list[i].error = 1E32; - } + for (i = 0; i < mbest->entries; i++) { + for (j = 0; j < MBEST_STAGES; j++) mbest->list[i].index[j] = 0; + mbest->list[i].error = 1E32; + } - return mbest; + return mbest; } - void mbest_destroy(struct MBEST *mbest) { - assert(mbest != NULL); - free(mbest->list); - free(mbest); + assert(mbest != NULL); + free(mbest->list); + free(mbest); } +/* apply weighting to VQ for efficient VQ search */ + +void mbest_precompute_weight(float cb[], float w[], int k, int m) { + for (int j = 0; j < m; j++) { + for (int i = 0; i < k; i++) cb[k * j + i] *= w[i]; + } +} /*---------------------------------------------------------------------------*\ @@ -75,36 +81,31 @@ void mbest_destroy(struct MBEST *mbest) { \*---------------------------------------------------------------------------*/ void mbest_insert(struct MBEST *mbest, int index[], float error) { - int i, j, found; - struct MBEST_LIST *list = mbest->list; - int entries = mbest->entries; - - found = 0; - for(i=0; ii; j--) - list[j] = list[j-1]; - for(j=0; jlist; + int entries = mbest->entries; + + found = 0; + for (i = 0; i < entries && !found; i++) + if (error < list[i].error) { + found = 1; + memmove(&list[i + 1], &list[i], + sizeof(struct MBEST_LIST) * (entries - i - 1)); + memcpy(&list[i].index[0], &index[0], sizeof(int) * MBEST_STAGES); + list[i].error = error; + } } +void mbest_print(char title[], struct MBEST *mbest) { + int i, j; -#ifdef MBEST_PRINT_OUT -static void mbest_print(char title[], struct MBEST *mbest) { - int i,j; - - fprintf(stderr, "%s\n", title); - for(i=0; ientries; i++) { - for(j=0; jlist[i].index[j]); - fprintf(stderr, " %f\n", mbest->list[i].error); - } + fprintf(stderr, "%s\n", title); + for (i = 0; i < mbest->entries; i++) { + for (j = 0; j < MBEST_STAGES; j++) + fprintf(stderr, " %4d ", mbest->list[i].index[j]); + fprintf(stderr, " %f\n", (double)mbest->list[i].error); + } } -#endif - /*---------------------------------------------------------------------------*\ @@ -115,31 +116,48 @@ static void mbest_print(char title[], struct MBEST *mbest) { \*---------------------------------------------------------------------------*/ -void mbest_search( - const float *cb, /* VQ codebook to search */ - float vec[], /* target vector */ - float w[], /* weighting vector */ - int k, /* dimension of vector */ - int m, /* number on entries in codebook */ - struct MBEST *mbest, /* list of closest matches */ - int index[] /* indexes that lead us here */ -) -{ - float e; - int i,j; - float diff; - - for(j=0; jlist[mbest->entries - 1].error) + mbest_insert(mbest, index, e); + } +} /*---------------------------------------------------------------------------*\ @@ -148,26 +166,26 @@ void mbest_search( Searches vec[] to a codebbook of vectors, and maintains a list of the mbest closest matches. Only searches the first NewAmp2_K Vectors - \*---------------------------------------------------------------------------*/ +\*---------------------------------------------------------------------------*/ -void mbest_search450(const float *cb, float vec[], float w[], int k,int shorterK, int m, struct MBEST *mbest, int index[]) +void mbest_search450(const float *cb, float vec[], float w[], int k, + int shorterK, int m, struct MBEST *mbest, int index[]) { - float e; - int i,j; - float diff; - - for(j=0; j