1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*--------------------------------------------------------------------------*\
FILE........: VQTRAIN.C
AUTHOR......: David Rowe
DATE CREATED: 23/2/95
This program trains vector quantisers using K dimensional Lloyd-Max
method.
\*--------------------------------------------------------------------------*/
/*
Copyright (C) 2009 David Rowe
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as
published by the Free Software Foundation. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*-----------------------------------------------------------------------*\
INCLUDES
\*-----------------------------------------------------------------------*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <getopt.h>
/*-----------------------------------------------------------------------*\
DEFINES
\*-----------------------------------------------------------------------*/
#define DELTAQ 0.005 /* quiting distortion */
#define MAX_STR 80 /* maximum string length */
/*-----------------------------------------------------------------------*\
FUNCTION PROTOTYPES
\*-----------------------------------------------------------------------*/
void zero(float v[], int k);
void acc(float v1[], float v2[], int k);
void norm(float v[], int k, long n);
long quantise(float cb[], float vec[], int k, int m, float *beste, float *se);
/*-----------------------------------------------------------------------* \
MAIN
\*-----------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
long k,m; /* dimension and codebook size */
float *vec; /* current vector */
float *cb; /* vector codebook */
float *cent; /* centroids for each codebook entry */
long *n; /* number of vectors in this interval */
long J; /* number of vectors in training set */
long i,j;
long ind; /* index of current vector */
float e; /* sqaured error for current vector */
float se; /* squared error for this iteration */
float var,var_1; /* current and previous iterations distortion */
float delta; /* improvement in distortion */
long noutliers[3];/* number of vectors quantisers with > 3*sd */
FILE *ftrain; /* file containing training set */
FILE *fvq; /* file containing vector quantiser */
int ret;
float deltaq_stop = DELTAQ;
FILE *fres = NULL;
int o = 0;
int opt_idx = 0;
while( o != -1 ) {
static struct option long_opts[] = {
{"help", no_argument, 0, 'h'},
{"residual", required_argument, 0, 'r'},
{"stop", required_argument, 0, 's'},
{0, 0, 0, 0}
};
o = getopt_long(argc,argv,"hr:s:",long_opts,&opt_idx);
switch(o) {
case 'r':
fres = fopen(optarg,"wb"); assert(fres != NULL);
//fprintf(stderr, "writing res to : %s \n", optarg);
break;
case 's':
deltaq_stop = atof(optarg);
//fprintf(stderr, "deltaq_stop :%f\n", deltaq_stop);
break;
case 'h':
case '?':
goto helpmsg;
break;
}
}
int dx = optind;
//fprintf(stderr, "argc: %d dx: %d\n", argc, dx);
if ((argc - dx) < 4) {
fprintf(stderr, "Too few arguments\n");
helpmsg:
fprintf(stderr, "usage: %s [Options] TrainFile.f32 K(dimension) M(codebook size) VQFile.f32\n", argv[0]);
fprintf(stderr, " -r --residual VQResidualErrorFile.f32usage\n");
fprintf(stderr, " -s --stop StopDelta\n");
exit(1);
}
/* Open training file */
ftrain = fopen(argv[dx],"rb");
if (ftrain == NULL) {
printf("Error opening training database file: %s\n",argv[dx]);
exit(1);
}
/* determine k and m, and allocate arrays */
k = atol(argv[dx+1]);
m = atol(argv[dx+2]);
printf("vector dimension K=%ld codebook size M=%ld ", k, m);
vec = (float*)malloc(sizeof(float)*k);
cb = (float*)malloc(sizeof(float)*k*m);
cent = (float*)malloc(sizeof(float)*k*m);
n = (long*)malloc(sizeof(long)*m);
if (cb == NULL || cb == NULL || cent == NULL || vec == NULL) {
printf("Error in malloc.\n");
exit(1);
}
/* determine size of training set */
J = 0; zero(cent, k);
while(fread(vec, sizeof(float), k, ftrain) == (size_t)k) {
J++;
acc(cent, vec, k);
}
printf("J=%ld vectors in training set\n", J);
/* Interation is a 0 bit VQ (i.e. mean of training set) as starting point */
norm(cent, k, J);
memcpy(cb, cent, k*sizeof(float));
se = 0.0;
rewind(ftrain);
for(i=0; i<J; i++) {
ret = fread(vec, sizeof(float), k, ftrain);
assert(ret == k);
quantise(cb, vec, k, 1, &e, &se);
}
var = se/(J*k);
printf("\r Iteration 0, var = %f, sd = %f\n", var, sqrt(var));
/* set up initial codebook state from samples of training set */
for(i=0; i<m; i++) {
j = i*(J/m);
fseek(ftrain, j*k*sizeof(float), SEEK_SET);
ret = fread(&cb[i*k], sizeof(float), k, ftrain);
assert(ret == k);
}
/* main loop */
j = 1;
do {
var_1 = var;
/* zero centroids */
for(i=0; i<m; i++) {
zero(¢[i*k], k);
n[i] = 0;
}
/* quantise training set */
se = 0.0; noutliers[0] = noutliers[1] = noutliers[2] = 0;
rewind(ftrain);
for(i=0; i<J; i++) {
ret = fread(vec, sizeof(float), k, ftrain);
assert(ret == k);
ind = quantise(cb, vec, k, m, &e, &se);
n[ind]++;
acc(¢[ind*k], vec, k);
//if (i < 100)
// printf("e: %f sqrt(e/k): %f sd: %f noutliers: %ld\n", e, sqrt(e/k), sd, noutliers[0]);
if (sqrt(e/k) > 1.0) noutliers[0]++;
if (sqrt(e/k) > 2.0) noutliers[1]++;
if (sqrt(e/k) > 3.0) noutliers[2]++;
}
var = se/(J*k);
delta = (var_1-var)/var;
printf("\r Iteration %ld, var = %4.2f, sd = %4.2f outliers > 1/2/3 dB = %3.2f/%f3.2/%3.2f Delta = %5.4f\n", j, var, sqrt(var),
(float)noutliers[0]/J, (float)noutliers[1]/J, (float)noutliers[2]/J, delta);
j++;
/* determine new codebook from centroids */
if (delta > deltaq_stop)
for(i=0; i<m; i++) {
if (n[i] != 0) {
norm(¢[i*k], k, n[i]);
memcpy(&cb[i*k], ¢[i*k], k*sizeof(float));
}
}
} while (delta > deltaq_stop);
/* save VQ to disk */
fvq = fopen(argv[dx+3],"wt");
if (fvq == NULL) {
printf("Error opening VQ file: %s\n",argv[dx+3]);
exit(1);
}
fwrite(cb, sizeof(float), m*k, fvq);
/* optionally output residual error for next stage VQ */
if (fres != NULL) {
float res[k];
rewind(ftrain);
for(j=0; j<J; j++) {
ret = fread(vec, sizeof(float), k, ftrain);
ind = quantise(cb, vec, k, m, &e, &se);
for(i=0; i<k; i++) {
res[i] = vec[i] - cb[k*ind+i];
}
fwrite(res, sizeof(float), k, fres);
}
fclose(fres);
}
fclose(fvq);
fclose(ftrain);
free(vec);
free(n);
return 0;
}
/*-----------------------------------------------------------------------*\
FUNCTIONS
\*-----------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
FUNCTION....: zero()
AUTHOR......: David Rowe
DATE CREATED: 23/2/95
Zeros a vector of length k.
\*---------------------------------------------------------------------------*/
void zero(float v[], int k)
/* float v[]; ptr to start of vector */
/* int k; lngth of vector */
{
int i;
for(i=0; i<k; i++)
v[i] = 0.0;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: acc()
AUTHOR......: David Rowe
DATE CREATED: 23/2/95
Adds k dimensional vectors v1 to v2 and stores the result back in v1.
\*---------------------------------------------------------------------------*/
void acc(float v1[], float v2[], int k)
/* float v1[]; ptr to start of vector to accumulate */
/* float v2[]; ptr to start of vector to add */
/* int k; dimension of vectors */
{
int i;
for(i=0; i<k; i++)
v1[i] += v2[i];
}
/*---------------------------------------------------------------------------*\
FUNCTION....: norm()
AUTHOR......: David Rowe
DATE CREATED: 23/2/95
Divides each element in k dimensional vector v by n.
\*---------------------------------------------------------------------------*/
void norm(float v[], int k, long n)
/* float v[]; ptr to start of vector */
/* int k; dimension of vectors */
/* int n; normalising factor */
{
int i;
assert(n != 0);
for(i=0; i<k; i++)
v[i] /= n;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: quantise()
AUTHOR......: David Rowe
DATE CREATED: 23/2/95
Quantises vec by choosing the nearest vector in codebook cb, and
returns the vector index. The squared error of the quantised vector
is added to se.
\*---------------------------------------------------------------------------*/
long quantise(float cb[], float vec[], int k, int m, float *beste, float *se)
/* float cb[][K]; current VQ codebook */
/* float vec[]; vector to quantise */
/* int k; dimension of vectors */
/* int m; size of codebook */
/* float beste; current squared error */
/* float *se; accumulated squared error */
{
long besti; /* best index so far */
long j;
int i;
float diff,e;
besti = 0;
*beste = 1E32;
for(j=0; j<m; j++) {
e = 0.0;
for(i=0; i<k; i++) {
diff = cb[j*k+i]-vec[i];
e += diff*diff;
}
if (e < *beste) {
*beste = e;
besti = j;
}
}
*se += *beste;
return(besti);
}
|