📄 vq_model.h
字号:
#ifndef _VQ_MODEL_H#define _VQ_MODEL_H#include <stdio.h>#include <stdlib.h>#define OPEN(FILEVAR, FILENAME, MODE) {if (!(FILEVAR = fopen(FILENAME, MODE))) {fprintf(stderr, "open %s failed!\n", FILENAME); exit(-1);}}#define LARGE 1.0e+30#define TINY 1.0e-10/*----------------------------------------------*//* Data structure of training patterns *//*----------------------------------------------*/typedef struct DI { int ID; /* Class ID */ int VecNum; /* Accum. Vector number for this class */ int VecDim; /* Vector Dimension */} Info_Type;/*----------------------------------------------*//* Data structure of training patterns *//*----------------------------------------------*/typedef struct DT { int ID; /* Class ID of this training pattern */ int VecDim; /* Vector dimension in Mat */ int VecNum; /* Number of vectors in Mat */ float **Mat; /* Vector sequence */ struct DT *Next;} DType;#define MArray_1D(Target, Dim, Type, Msg) { \ if (!(Target =(Type*) calloc((Dim), sizeof(Type)))) \ { fprintf(stderr, "No enough memory for %s\n", Msg); \ exit(-1); } }#define MArray_2D(Target, Dim1, Dim2, Type, Msg) { \ Type *Pnt; \ int _Cnt; \ if (!(Pnt = (Type*) calloc((Dim1)*(Dim2), sizeof(Type)))) { \ fprintf(stderr, "No enough memory for %s\n", Msg); \ exit(-1); \ }; \ if (!(Target = (Type **) calloc((Dim1), sizeof(Type*)))) { \ fprintf(stderr, "No enough memory for %s\n", Msg); \ exit(-1); \ }; \ for(_Cnt = 0; _Cnt < (Dim1) ; _Cnt++) \ Target[_Cnt] = Pnt + _Cnt * (Dim2); \ }#define MFree_1D(Target) { if (Target !=NULL) free (Target); }#define MFree_2D(Target) { if (Target != NULL && *Target != NULL) \ { free(*Target); free(Target); } }/*DIST_VEC macro:Euclidean DISTANCE OF TWO VECTORS (a,b) ANDPUTS THE RESULT IN THE PREVIOUSLY DEFINED VARIABLE s.DIST_VEC(a,b,s,len,typea,typeb) a pointer to first vector. b pointer to second vector. s variable used to store result (not a pointer). len length of vectors (integer). typea legal C type describing the type of a data. typeb legal C type describing the type of b data.WARNING: The input data vectors are not cast to the type of s. This means that at least one of the input types must be able to represent the individual products without overflow.*/#define DIST_VEC(a,b,s,len,typea,typeb) { \ typea *_PTA = a; \ typeb *_PTB = b; \ typea buf_a; \ typeb buf_b; \ int _IX; \ buf_a = (*_PTA++) ;\ buf_b = (*_PTB++) ;\ s = (buf_a - buf_b) * (buf_a - buf_b); \ for (_IX = 1 ; _IX < (len) ; _IX++) { \ buf_a = (*_PTA++) ;\ buf_b = (*_PTB++) ;\ s += (buf_a - buf_b) * (buf_a - buf_b); \ } \ }/*MIN_IND macro:find minmum value from array a, return index in sMIN_IND(a,s,len,typea) a pointer to first vector. s integer variable for retruned index. len length of vector (integer). typea legal C type describing the type of a data.*/#define MIN_IND(a,s,len,typea) { \ typea *_PTA = a, a_MIN; \ int _IX; \ a_MIN = (*_PTA++); s = 0; \ for(_IX = 1 ; _IX < (len) ; _IX++, _PTA++) \ if ((*_PTA) < a_MIN) { a_MIN = (*_PTA); s = _IX;}; \ }long my_random(void); /* same as UNIX random() */void my_srandom(unsigned int x);int scan_file(FILE *datafile, Info_Type *DataInfo);int load_data(FILE *datafile, DType *DataHead, int TotalClass);void save_code(FILE *codefile, DType *CodeHead, int TotalClass);DType *load_codebook(FILE *codefile);void LBG_engine (DType *DataHead, DType *CodeHead, int ClassCnt);void LVQ_engine (DType *DataHead, DType *CodeHead, int TotalClass, float alpha, float epsilon, float WinSize, int epoch);void GVQ_engine (DType *DataHead, DType *CodeHead, int TotalClass, float alpha, float WinSize, int epoch, int OptN, int SeqSel);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -