📄 vq_model.c
字号:
/*----------------------------------------------------------------*/
/* Main program to generate VQ codebooks. */
/* */
/* Author : Jialong He */
/* Date : Aug. 16, 1995 */
/* Modify : April 23, 1998: change data structure to linked list */
/* */
/*----------------------------------------------------------------*/
#define MaxClass 1000 /* maximum classes */
#define LBG 1
#define LVQ 2
#define GVQ 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vq_model.h"
/*------------------------------*/
/* global variable declarations */
/*------------------------------*/
char *DataFileName; /* training vector file */
char *CodeFileName; /* output code vector file */
float alpha = 0.2; /*------------------------------*/
float epsilon = 0.1; /* all parameters have the same */
float WinSize = 1; /* meaning as in LVQ */
int epoch = 1; /*------------------------------*/
int Csize = 8;
int Method = GVQ; /* 1: LBG, 2: LVQ, 3: GVQ */
int verbose = 0;
int OptN = 4; /* number of vector group */
int SeqSel = 1; /* 1: Sequential select, 0: random select */
int Retrain = 0; /* 0: init code with LBG, 1: load from file */
/*-----------------------------------------------*/
/* usage(): display usage paramters */
/*-----------------------------------------------*/
void usage() {
fprintf(stderr, "\nGenerating VQ Codebook -- written by Jialong He\n\n");
fprintf(stderr, "\tUsage: vq_model [options] datafile codefile\n\n");
fprintf(stderr, "\tWhere [options] are:\n");
fprintf(stderr, "\t-v verbose\n");
fprintf(stderr, "\t-c [%4d] codebook size (2, 4, 8, 16, 32 ...)\n", Csize);
fprintf(stderr, "\t-m [%1d] training method (1:LBG, 2:LVQ. 3:GVQ)\n", Method);
fprintf(stderr, "\n\t---- options applied for both LVQ and GVQ ----\n");
fprintf(stderr, "\t-r retrain codebook\n");
fprintf(stderr, "\t-a [%4.2f] alpha\n", alpha);
fprintf(stderr, "\t-e [%4.2f] epsilon\n", epsilon);
fprintf(stderr, "\t-w [%4.2f] window size\n", WinSize);
fprintf(stderr, "\t-l [%4d] training epoch\n", epoch);
fprintf(stderr, "\n\t---- options applied for GVQ only ----\n");
fprintf(stderr, "\t-n [%d] group size\n", OptN);
fprintf(stderr, "\t-R Random selecttion method, [default sequential]\n");
exit(1);
}
/*-----------------------------------------------*/
/* get_comline(int argc, char **argv) */
/* read and interpret command line */
/*-----------------------------------------------*/
void get_comline(int argc, char **argv) {
int c;
extern int optind;
extern char *optarg;
while( (c = getopt( argc, argv, "a:e:l:w:c:n:m:rRv")) != -1 ) {
switch( c ) {
case 'a': /* alpha */
alpha = (float)atof( optarg );
break;
case 'e': /* epsilon */
epsilon = (float)atof( optarg );
break;
case 'w': /* window size */
WinSize = (float)atof( optarg );
break;
case 'l': /* epoch */
epoch = atoi( optarg );
break;
case 'c': /* code vector number per class */
Csize = atoi( optarg );
break;
case 'n': /* size of vector group */
OptN = atoi( optarg );
break;
case 'm': /* training algorithm */
Method = atoi(optarg);
break;
case 'r': /* retrain the codebook */
Retrain = 1;
break;
case 'R': /* Random select vector */
SeqSel = 0;
break;
case 'v': /* verbose */
verbose++;
break;
default:
usage();
break;
} /* switch */
} /* while */
if( (argc - optind) != 2 ) usage();
DataFileName = argv[optind];
CodeFileName = argv[optind+1];
}
/*--------------------------------------------------*/
/* ******* Main Program *********** */
/*--------------------------------------------------*/
int main (int argc, char *argv[]) {
int TotalClass, TotalCode, TotalVector;
int ClassCnt;
Info_Type *DataInfo;
DType *DataHead, *CodeHead;
FILE *datafile, *codefile;
get_comline(argc, argv);
OPEN(datafile, DataFileName, "rt");
/*-----------------------------------------------------*/
/* scan the whole data file to determine total classes */
/* and vector numbers in each class. */
/*-----------------------------------------------------*/
MArray_1D(DataInfo, MaxClass, Info_Type, "DataInfo");
TotalClass = scan_file(datafile, DataInfo);
rewind(datafile); /* fseek(datafile, 0L, 0); */
/*--------------------------------------------------*/
/* Allocate memory for training data and codebooks */
/*--------------------------------------------------*/
MArray_1D(DataHead, TotalClass, DType, "DataHead");
MArray_1D(CodeHead, TotalClass, DType, "CodeHead");
for (ClassCnt=0; ClassCnt<TotalClass; ClassCnt++) {
/*-----------------------------------*/
/* data buffer for one class */
/*-----------------------------------*/
MArray_2D(DataHead[ClassCnt].Mat, DataInfo[ClassCnt].VecNum,
DataInfo[ClassCnt].VecDim, float, "DataMat");
DataHead[ClassCnt].ID = DataInfo[ClassCnt].ID;
DataHead[ClassCnt].VecNum = DataInfo[ClassCnt].VecNum;
DataHead[ClassCnt].VecDim = DataInfo[ClassCnt].VecDim;
DataHead[ClassCnt].Next = NULL;
/*-----------------------------------*/
/* codebook buffer of one class */
/*-----------------------------------*/
MArray_2D(CodeHead[ClassCnt].Mat, Csize,
DataInfo[ClassCnt].VecDim, float, "CodeMat");
CodeHead[ClassCnt].ID = DataInfo[ClassCnt].ID;
CodeHead[ClassCnt].VecNum = Csize;
CodeHead[ClassCnt].VecDim = DataInfo[ClassCnt].VecDim;
CodeHead[ClassCnt].Next = NULL;
}
MFree_1D(DataInfo);
/*--------------------------------------------------*/
/* load training data and codebooks if retrain */
/*--------------------------------------------------*/
TotalVector = load_data(datafile, DataHead, TotalClass);
printf("%d vectors in %d classes loaded!\n", TotalVector, TotalClass);
if (Retrain) {
int Len, Class, Dim;
OPEN(codefile, CodeFileName, "rt");
/*----------------------------------------------*/
/* check codefile if have specifed dim and size */
/*----------------------------------------------*/
fscanf(codefile, "%d %d %d\n", &Len, &Class, &Dim);
if (Len != Csize || Dim != CodeHead[0].VecDim) {
fprintf (stderr, "ERROR: Codebook size or dimension wrong\n");
exit(-1);
}
rewind(codefile); /* fseek(datafile, 0L, 0); */
TotalCode = load_data(codefile, CodeHead, TotalClass);
fclose(codefile);
printf("%d code vectors loaded!\n", TotalCode);
}
else {
/*-----------------------------------------*/
/* Init codebook using the LBG algorithm */
/*-----------------------------------------*/
for (ClassCnt=0; ClassCnt<TotalClass; ClassCnt++) {
LBG_engine (DataHead, CodeHead, ClassCnt);
printf("Codebook for class %d generated\n\n", DataHead[ClassCnt].ID);
}
}
if (Method == LVQ) {
LVQ_engine (DataHead, CodeHead, TotalClass,
alpha, epsilon, WinSize, epoch);
}
if (Method == GVQ) {
GVQ_engine (DataHead, CodeHead, TotalClass,
alpha, WinSize, epoch, OptN, SeqSel);
}
OPEN(codefile, CodeFileName, "wt");
save_code(codefile, CodeHead, TotalClass);
fclose(codefile);
/*-------------------------*/
/* Free allocated memory */
/*-------------------------*/
for (ClassCnt=0; ClassCnt<TotalClass; ClassCnt++) {
MFree_2D(DataHead[ClassCnt].Mat);
MFree_2D(CodeHead[ClassCnt].Mat);
}
MFree_1D(DataHead);
MFree_1D(CodeHead);
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -