#include #include #include #include "idc.h" #define maxit 100 /********************************************************************** * * * File: trainvq.c * * Function: generates the codebook for a vector quantizer. * * Author : K. Sayood * * Last mod: 5/12/95 * * Usage: see usage(). For more details see trainvq.doc or man page. * ***********************************************************************/ /******************************************************************************* *NOTICE: * *This code is believed by the author to be bug free. You are free to use and * *modify this code with the understanding that any use, either direct or * *derivative, must contain acknowledgement of its author and source. The author* *makes no warranty of any kind, expressed or implied, of merchantability or * *fitness for a particular purpose. The author shall not be held liable for any* *incidental or consequential damages in connection with or arising out of the * *furnishing, performance, or use of this software. This software is not * *authorized for use in life support devices or systems. * ********************************************************************************/ void usage(void); main(int argc, char **argv) { FILE *tfp, *icb; int **codebook; int **training_set; int *input, dimension, index, codebook_size, ts_size, c; int i, j, init_code_flag, iteration, tsvec; float measure, total_distortion, total_distortion_old; int **new_codebook, *count; float threshold, distortion; char codefile[80]; extern int optind; extern char *optarg; init_code_flag = 0; codebook_size = -1; dimension = -1; ts_size = -1; /* Obtain the file containing the training set */ if((tfp = fopen(argv[1],"r"))==NULL) { fprintf(stderr,"File open failed for the training set file %s\n",argv[1]); usage(); exit(1); } optind++; /* If the initial codebook is to be read in read filename */ init_code_flag = 0; while((c=getopt(argc,argv,"c:b:d:t:h")) != EOF) { switch (c){ case 'c': strcpy(codefile,optarg); init_code_flag++; if((icb = fopen(codefile,"r"))==NULL) { fprintf(stderr,"Codebook file %s does not exist. The initial\n",codefile); fprintf(stderr,"codebook will be selected from the training set.\n"); init_code_flag--; } break; case 'b': sscanf(optarg,"%d", &codebook_size); break; case 'd': sscanf(optarg,"%d", &dimension); break; case 't': sscanf(optarg,"%d", &ts_size); break; case 'h': usage(); exit(1); } } fprintf(stderr,"\n\n\t\t Vector Quantizer Design Using the LBG algorithm \n\n"); fprintf(stderr,"\t This program can be used to train a vector quantizer using the \n"); fprintf(stderr,"\t LBG (GLA) algorithm. The codebook is initialized by a set \n"); fprintf(stderr,"\t provided by the user or from the training set. If the user \n"); fprintf(stderr,"\t wishes to use the splitting method for initialization s/he \n"); fprintf(stderr,"\t should use the program trvqsplit. This program reads in the \n"); fprintf(stderr,"\t training set as ints. The user can modify this by changing \n"); fprintf(stderr,"\t the read statement. \n"); if(codebook_size < 0) { fprintf(stderr,"Enter codebook size:\n"); scanf("%d",&codebook_size); } if(dimension < 0) { fprintf(stderr,"Enter vector dimension:\n"); scanf("%d",&dimension); } if(ts_size < 0) { fprintf(stderr,"Enter size of training set:\n"); scanf("%d",&ts_size); } /* Allocate space for codebook, training set, input and associated work arrays */ codebook = (int **) calloc(codebook_size,sizeof(int *)); for(i=0; i< codebook_size; i++) codebook[i] = (int *) calloc(dimension,sizeof(int)); training_set = (int **) calloc(ts_size,sizeof(int *)); for(i=0; i< ts_size; i++) training_set[i] = (int *) calloc(dimension,sizeof(int)); input = (int *) calloc(dimension,sizeof(int)); new_codebook = (int **) calloc(codebook_size,sizeof(int *)); for(i=0; i< codebook_size; i++) new_codebook[i] = (int *) calloc(dimension,sizeof(int)); count = (int *) calloc(codebook_size,sizeof(int)); printf("Codebook size = %d, Training set = %d, dimension = %d\n",codebook_size, ts_size, dimension); for(i=0; i< ts_size; i++) for(j=0; j < dimension; j++) fscanf(tfp,"%d",&training_set[i][j]); if(init_code_flag == 1) { for(i=0; i< codebook_size; i++) for(j=0; j < dimension; j++) fscanf(icb,"%d",&codebook[i][j]); } else { for(i=0; i< codebook_size; i++) for(j=0; j < dimension; j++) codebook[i][j] = training_set[i][j]; } measure = 1000.; threshold = 0.1; iteration = 0; fprintf(stderr,"Entering the training loop. The change threshold is %5.2f and\n",threshold); fprintf(stderr,"the maximum number of iteration is %d.\n",maxit); while(measure > threshold && iteration < maxit) { if(iteration > 0) { total_distortion_old = total_distortion; for(i=0;i 0) codebook[i][j] =(int)((float) new_codebook[i][j]/(float) count[i] + .5); } } /* do something about zero count */ for(i=0;i 0) measure = (total_distortion_old - total_distortion)/total_distortion_old; else measure = total_distortion; /* printf("Iteration number %d, measure = %f\n",iteration,measure); */ iteration++; } printf("***************** Final Codebook ********************\n"); for(i=0; i