📄 vq_test.c
字号:
/*========================================================================*/
/* This program output nearest neighbors of test vectors */
/* the codebook is generated by LVQ package. */
/* */
/* Author : Jialong He */
/* Date : 13 Nov, 1993 */
/* Modify : 31 Jan, 1994 ; allow more test vectors in same file */
/* Modify : 23 Dec, 1994 ; add majority voting for sentence level */
/* Modify : Oct. 5, 1995 ; add threshold for online demonstration */
/* Modify : April. 25, 1998 ; change data structure to linked list */
/*========================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "vq_model.h"
/*------------------------------*/
/* global variable declarations */
/*------------------------------*/
char *CodeFileName; /* Code vector file */
char *DataFileName; /* Test vector file */
char OutFileName[100]="-"; /* distance result file */
char AccuFileName[100]="-"; /* sentence level results */
int IsOutput = 0; /* flag for output distant file */
int AccuScore = 0; /* if output sentence level result */
int Voting = 0; /* accu. distance for sentence level */
int verbose = 0;
int ImpID = 1; /* imposter's start ID, YOHO = 41*/
int TotVec, WinVec; /* Total and wining vectors */
float AvgDist, CurrDist; /* Average and wining distance */
/*-----------------------------------------------*/
/* usage() display usage paramters on screen */
/*-----------------------------------------------*/
void usage() {
fprintf(stderr, "Codebook test program -- Written by Jialong HE\n\n");
fprintf(stderr, "\tUsage: vq_test [options] datafile codebook\n\n");
fprintf(stderr, "\tWhere [options] can be any of the following:\n");
fprintf(stderr, "\t-o [file] nearest distance\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, "o:")) != -1 ) {
switch( c ) {
case 'o': /* output result file */
strcpy(OutFileName, optarg);
IsOutput++;
break;
default:
usage();
break;
}
}
if( (argc - optind) != 2 ) {
usage();
exit( 2 );
}
DataFileName = argv[optind];
CodeFileName = argv[optind+1];
}
/*--------------------------------------------------------------*/
/* *** Main Program *** */
/*--------------------------------------------------------------*/
int main (int argc, char *argv[]) {
DType *CodeHead, *CodeCurr, *CodeMin;
float *DataVec;
int TotalVec=0, TotalSen=0;
int CorrectVec = 0, CorrectSen = 0;
float MinDis, ThisDis;
float *CurrDis, *AccuDis, TmpFloat;
int DataID, VecNum, VecDim;
int DimCnt, VecCnt;
int TotalClass = 0;
int ClassCnt, DataCnt;
char Ch;
FILE *outfile, *codefile, *datafile;
get_comline(argc, argv);
OPEN(codefile, CodeFileName, "rt");
OPEN(datafile, DataFileName, "rt");
if (IsOutput) OPEN(outfile, OutFileName, "wt");
CodeHead = load_codebook(codefile);
/*-------------------------------------------------------*/
/* count TotalClass and MaxCode, use for allocate memory */
/*-------------------------------------------------------*/
CodeCurr = CodeHead;
TotalClass = 0;
while (CodeCurr != NULL) {
TotalClass++;
CodeCurr = CodeCurr->Next;
}
MArray_1D(DataVec, CodeHead->VecDim, float, "DataVec");
MArray_1D(AccuDis, TotalClass, float, "AccuDis");
MArray_1D(CurrDis, TotalClass, float, "CurrDis");
/*------------------------------------------------------------------*/
/* Test each feature vector and accumulate sentence level distance */
/*------------------------------------------------------------------*/
do {
fscanf(datafile, "%d %d %d\n", &VecNum, &DataID, &VecDim);
TotalVec += VecNum;
if (VecDim != CodeHead->VecDim ) {
fprintf (stderr, "inconsistent dimension in data and code\n");
exit(-1);
}
/* clear accumulate buffer */
for (ClassCnt=0; ClassCnt<TotalClass; ClassCnt++) AccuDis[ClassCnt] = 0;
for (DataCnt = 0; DataCnt < VecNum; DataCnt++) {
/*---------------------------*/
/* read in one data vector */
/*---------------------------*/
for (DimCnt=0; DimCnt<VecDim; DimCnt++) {
fscanf(datafile, "%f ", &TmpFloat);
DataVec[DimCnt] = TmpFloat;
}
fscanf(datafile, "\n");
/*---------------------------*/
/* test each codebook */
/*---------------------------*/
CodeCurr = CodeHead;
ClassCnt = 0;
while (CodeCurr != NULL) {
MinDis = LARGE;
for (VecCnt=0; VecCnt<CodeCurr->VecNum; VecCnt++) {
DIST_VEC(DataVec, CodeCurr->Mat[VecCnt], ThisDis, VecDim, float, float);
if (ThisDis < MinDis) MinDis = ThisDis;
}
CurrDis[ClassCnt] = MinDis;
AccuDis[ClassCnt] += MinDis;
ClassCnt++;
CodeCurr = CodeCurr->Next;
} /* while each code class */
/*--------------------------------------*/
/* find winning class for single vector */
/*--------------------------------------*/
CodeCurr = CodeHead;
ClassCnt = 0;
MinDis = LARGE;
while (CodeCurr != NULL) {
if (CurrDis[ClassCnt]<MinDis) {
MinDis = CurrDis[ClassCnt];
CodeMin = CodeCurr;
}
if (IsOutput) fprintf (outfile, "%7.3f ", CurrDis[ClassCnt]);
ClassCnt++;
CodeCurr = CodeCurr->Next;
}
if (IsOutput) fprintf (outfile, "\n");
if (CodeMin->ID == DataID) CorrectVec++;
} /* for each data vector in one sentence */
/*-----------------------------------------*/
/* find winning class for vector sequence */
/*-----------------------------------------*/
CodeCurr = CodeHead;
ClassCnt = 0;
MinDis = LARGE;
while (CodeCurr != NULL) {
if (AccuDis[ClassCnt]<MinDis) {
MinDis = AccuDis[ClassCnt];
CodeMin = CodeCurr;
}
ClassCnt++;
CodeCurr = CodeCurr->Next;
}
if (CodeMin->ID == DataID) CorrectSen++;
TotalSen++;
Ch = fgetc(datafile);
ungetc(Ch, datafile);
} while (!feof(datafile) );
printf ("Total Vec. %d, Correct %d (%4.1f\%) \n", TotalVec, CorrectVec, (float)CorrectVec/TotalVec*100.0);
printf ("Total Sen. %d, Correct %d (%4.1f\%) \n", TotalSen, CorrectSen, (float)CorrectSen/TotalSen*100.0);
if (IsOutput) fclose(outfile);
MFree_1D(DataVec);
MFree_1D(AccuDis);
MFree_1D(CurrDis);
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -