www.pudn.com > LBGwlee.rar > LBG.cpp


/*========================================================================== 
	 Production of Vector Codebook based on LBG Method 
 
		       C language program : LBG.cpp 
 
               Li  Wei     06/01/2006 
 
===========================================================================*/ 
/*  read from wlee.hex    write to test.hex   */ 
 
#include 
#include 
#include 
#include  
#include  
const int LEN=1250;                  //训练序列长度 
const int TEST=250;                  //测试序列长度 
const int DIMENSION=2;               //矢量维数 
const int CODESIZE=32;             //码本个数 
int main() 
{ 
	FILE *fp,*fout; 
    float input[TEST+LEN][DIMENSION];        //读入的序列 
    float train_x[LEN][DIMENSION];           //输入矢量  训练序列 
	float test[TEST][DIMENSION],test1[TEST][DIMENSION];             //测试序列 
	float outtest[TEST*DIMENSION]; 
	float codebook[CODESIZE][DIMENSION]={0};          //输出的码字 
	float distance[CODESIZE];                 //距离误差 
	int train_code[LEN];             //训练序列第i个矢量与生成码书中的哪个码字最近 
	int test_code[TEST];             //测试序列第i个矢量与生成码书中的哪个码字最近 
	int label;                          //标号,隶属哪个码本 
	int codebooksize[CODESIZE];            //表示每个胞腔的大小 
	float d1=1,d2=1;               //计算误差用 
	float nd;               //相对误差   
	float min=80000;        //最小距离 
    int i,j,k;                 //做循环时用的标号 
	int time,suptime;                //生成初始码书的次数   
	int power=1;               //每次生成码本的个数 
	float temp;               //临时替换变量 
	int n;                      
    float e=0.01;             //设定循环结束的相对误差 
	int max=0,max_label=0;     //去除空胞腔时使用 
	 
 
	fp=fopen("f:\\wlee.hex","rb");                  //从f:\\wlee.hex中读入数据   
	if((fout=fopen("f:\\test.hex","wb"))==NULL)        //量化后的测试数据存放到f:\\test.hex 
	{  
		cout<<"error!"<e) 
		{    
		   if(n!=1)             //保证了第一次读入的数据不被处理掉 
		   { 
			   for(i=0;imax) 
							   { 
								   max=codebooksize[i]; 
								   max_label=i; 
							   } 
						   } 
						   for(k=0;k