diff options
| author | erdgeist@erdgeist.org <erdgeist@bauklotz.fritz.box> | 2019-07-04 23:26:09 +0200 |
|---|---|---|
| committer | erdgeist@erdgeist.org <erdgeist@bauklotz.fritz.box> | 2019-07-04 23:26:09 +0200 |
| commit | f02dfce6e6c34b3d8a7b8a0e784b506178e331fa (patch) | |
| tree | 45556e6104242d4702689760433d7321ae74ec17 /mbest.c | |
stripdown of version 0.9
Diffstat (limited to 'mbest.c')
| -rw-r--r-- | mbest.c | 173 |
1 files changed, 173 insertions, 0 deletions
| @@ -0,0 +1,173 @@ | |||
| 1 | /*---------------------------------------------------------------------------*\ | ||
| 2 | |||
| 3 | FILE........: mbest.c | ||
| 4 | AUTHOR......: David Rowe | ||
| 5 | DATE CREATED: Jan 2017 | ||
| 6 | |||
| 7 | Multistage vector quantiser search algorithm that keeps multiple | ||
| 8 | candidates from each stage. | ||
| 9 | |||
| 10 | \*---------------------------------------------------------------------------*/ | ||
| 11 | |||
| 12 | /* | ||
| 13 | Copyright David Rowe 2017 | ||
| 14 | |||
| 15 | All rights reserved. | ||
| 16 | |||
| 17 | This program is free software; you can redistribute it and/or modify | ||
| 18 | it under the terms of the GNU Lesser General Public License version 2.1, as | ||
| 19 | published by the Free Software Foundation. This program is | ||
| 20 | distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 21 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 22 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | ||
| 23 | License for more details. | ||
| 24 | |||
| 25 | You should have received a copy of the GNU Lesser General Public License | ||
| 26 | along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
| 27 | |||
| 28 | */ | ||
| 29 | |||
| 30 | #include <assert.h> | ||
| 31 | #include <math.h> | ||
| 32 | #include <stdio.h> | ||
| 33 | #include <stdlib.h> | ||
| 34 | #include <string.h> | ||
| 35 | |||
| 36 | #include "mbest.h" | ||
| 37 | |||
| 38 | struct MBEST *mbest_create(int entries) { | ||
| 39 | int i,j; | ||
| 40 | struct MBEST *mbest; | ||
| 41 | |||
| 42 | assert(entries > 0); | ||
| 43 | mbest = (struct MBEST *)malloc(sizeof(struct MBEST)); | ||
| 44 | assert(mbest != NULL); | ||
| 45 | |||
| 46 | mbest->entries = entries; | ||
| 47 | mbest->list = (struct MBEST_LIST *)malloc(entries*sizeof(struct MBEST_LIST)); | ||
| 48 | assert(mbest->list != NULL); | ||
| 49 | |||
| 50 | for(i=0; i<mbest->entries; i++) { | ||
| 51 | for(j=0; j<MBEST_STAGES; j++) | ||
| 52 | mbest->list[i].index[j] = 0; | ||
| 53 | mbest->list[i].error = 1E32; | ||
| 54 | } | ||
| 55 | |||
| 56 | return mbest; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | void mbest_destroy(struct MBEST *mbest) { | ||
| 61 | assert(mbest != NULL); | ||
| 62 | free(mbest->list); | ||
| 63 | free(mbest); | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | /*---------------------------------------------------------------------------*\ | ||
| 68 | |||
| 69 | mbest_insert | ||
| 70 | |||
| 71 | Insert the results of a vector to codebook entry comparison. The | ||
| 72 | list is ordered in order of error, so those entries with the | ||
| 73 | smallest error will be first on the list. | ||
| 74 | |||
| 75 | \*---------------------------------------------------------------------------*/ | ||
| 76 | |||
| 77 | void mbest_insert(struct MBEST *mbest, int index[], float error) { | ||
| 78 | int i, j, found; | ||
| 79 | struct MBEST_LIST *list = mbest->list; | ||
| 80 | int entries = mbest->entries; | ||
| 81 | |||
| 82 | found = 0; | ||
| 83 | for(i=0; i<entries && !found; i++) | ||
| 84 | if (error < list[i].error) { | ||
| 85 | found = 1; | ||
| 86 | for(j=entries-1; j>i; j--) | ||
| 87 | list[j] = list[j-1]; | ||
| 88 | for(j=0; j<MBEST_STAGES; j++) | ||
| 89 | list[i].index[j] = index[j]; | ||
| 90 | list[i].error = error; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | #ifdef MBEST_PRINT_OUT | ||
| 96 | static void mbest_print(char title[], struct MBEST *mbest) { | ||
| 97 | int i,j; | ||
| 98 | |||
| 99 | fprintf(stderr, "%s\n", title); | ||
| 100 | for(i=0; i<mbest->entries; i++) { | ||
| 101 | for(j=0; j<MBEST_STAGES; j++) | ||
| 102 | fprintf(stderr, " %4d ", mbest->list[i].index[j]); | ||
| 103 | fprintf(stderr, " %f\n", mbest->list[i].error); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | #endif | ||
| 107 | |||
| 108 | |||
| 109 | /*---------------------------------------------------------------------------*\ | ||
| 110 | |||
| 111 | mbest_search | ||
| 112 | |||
| 113 | Searches vec[] to a codebbook of vectors, and maintains a list of the mbest | ||
| 114 | closest matches. | ||
| 115 | |||
| 116 | \*---------------------------------------------------------------------------*/ | ||
| 117 | |||
| 118 | void mbest_search( | ||
| 119 | const float *cb, /* VQ codebook to search */ | ||
| 120 | float vec[], /* target vector */ | ||
| 121 | float w[], /* weighting vector */ | ||
| 122 | int k, /* dimension of vector */ | ||
| 123 | int m, /* number on entries in codebook */ | ||
| 124 | struct MBEST *mbest, /* list of closest matches */ | ||
| 125 | int index[] /* indexes that lead us here */ | ||
| 126 | ) | ||
| 127 | { | ||
| 128 | float e; | ||
| 129 | int i,j; | ||
| 130 | float diff; | ||
| 131 | |||
| 132 | for(j=0; j<m; j++) { | ||
| 133 | e = 0.0; | ||
| 134 | for(i=0; i<k; i++) { | ||
| 135 | diff = cb[j*k+i]-vec[i]; | ||
| 136 | e += diff*w[i]*diff*w[i]; | ||
| 137 | } | ||
| 138 | index[0] = j; | ||
| 139 | mbest_insert(mbest, index, e); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | /*---------------------------------------------------------------------------*\ | ||
| 145 | |||
| 146 | mbest_search450 | ||
| 147 | |||
| 148 | Searches vec[] to a codebbook of vectors, and maintains a list of the mbest | ||
| 149 | closest matches. Only searches the first NewAmp2_K Vectors | ||
| 150 | |||
| 151 | \*---------------------------------------------------------------------------*/ | ||
| 152 | |||
| 153 | void mbest_search450(const float *cb, float vec[], float w[], int k,int shorterK, int m, struct MBEST *mbest, int index[]) | ||
| 154 | |||
| 155 | { | ||
| 156 | float e; | ||
| 157 | int i,j; | ||
| 158 | float diff; | ||
| 159 | |||
| 160 | for(j=0; j<m; j++) { | ||
| 161 | e = 0.0; | ||
| 162 | for(i=0; i<k; i++) { | ||
| 163 | //Only search first NEWAMP2_K Vectors | ||
| 164 | if(i<shorterK){ | ||
| 165 | diff = cb[j*k+i]-vec[i]; | ||
| 166 | e += diff*w[i] * diff*w[i]; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | index[0] = j; | ||
| 170 | mbest_insert(mbest, index, e); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
