📄 vqdesasc.c
字号:
/* * This material contains unpublished, proprietary software of * Entropic Research Laboratory, Inc. Any reproduction, distribution, * or publication of this work must be authorized in writing by Entropic * Research Laboratory, Inc., and must bear the notice: * * "Copyright (c) 1986-1990 Entropic Speech, Inc. * "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc. * All rights reserved" * * The copyright notice above does not evidence any actual or intended * publication of this source code. * * Written by: John Shore * Checked by: * Revised by: * * Design a VQ codebook from ASCII input data */static char *sccs_id = "@(#)vqdesasc.c 3.9 1/20/97 ESI/ERL";int debug_level = 2; /*level of history detail*//* * include files */#include <stdio.h>#include <esps/esps.h>#include <esps/fea.h>#include <esps/vq.h>/* * defines */#define MAXLINE 500#define MAXCMNT 2047 /* Number of characters in user entered comments. */#define Fprintf (void)fprintf#define Fflush (void)fflush#define DEBUG(n) if (debug_level >= n) Fprintf#define ERROR_EXIT(text) {(void) fprintf(stderr, "%s: %s - exiting\n", \ ProgName, text); exit(1);}#define SYNTAX USAGE ("vqdesasc [-l max_line_len] [-x debug_level] [-P param_file] [-h histfile]\n [-k checkfile] [-i] [-c comment] [-C comment_file] input output.fea")#define FEA_BUFSIZE 1000000 /*maximum size of training sequence array*/#define MAX_ITER 500/* * system functions and variables */int getopt ();double atof();long time();int atoi(), strcmp(), fclose();char *strtok();char *fgets();void rewind();extern optind;extern char *optarg;char *mktemp(), *ctime(), *strcpy();void rewind();#ifndef DEC_ALPHAchar *calloc();#endif/* * external SPS functions */int add_fea_fld();long *add_genhd_l();short *add_genhd_s();float *add_genhd_f();char *add_genhd_c();char *get_cmd_line(), *getsym_s();void symerr_exit(), write_header(), set_sd_type();int getsym_i(), lin_search();struct header *read_header();struct fea_data *allo_fea_rec();float **f_mat_alloc();int rc_to_lar();void put_fea_rec();char *get_fea_ptr();short fea_encode();struct vqcbk *allo_vqfea_rec();/* * global function declarations */long gen_get_chunk();int checkpnt();double mse_lar_dist();void s_param_err();long find_fea_dim();long read_fea_vec();/* * global variable declarations */char *ProgName = "vqdesasc";char *cmd_line; /*string for command line*/char comment[MAXCMNT + 2]; /*string for command line comment*/char *histfile = "vqdesaschist"; /*output ASCII file for history data*/FILE *histrm;char *input_file = NULL; /*input ASCII file*/FILE *input_strm;char *out_fea = NULL; /*output FEA file for codebook*/struct header *fea_oh;FILE *fea_strm = stdout;char *checkfile = "vqdesasc.chk"; /*checkpoint file*/FILE *chkstrm;struct header *chk_oh;long ndrec; /*num records in input file*/int whole_fits; /*flag for whether all input data fits in feadata*/int max_line_len = MAXLINE; /*max input line length *//* * main program */main (argc, argv)int argc;char **argv;{/* * setup and initialization */ char *Version = "3.9"; char *Date = "1/20/97"; char *paramfile = NULL; /*parameter file name*/ char *cmntfile = NULL; /*comment file name*/ FILE *cmnt_strm; char *init_file = NULL; /*file with initial codebook*/ FILE *init_strm; struct header *init_ih; int init_rec; /*record number of initial codebook*/ int init_behav; /*whether to cluster initial codebook*/ int c; /*for getopt return*/ long tloc; /*for local time*/ long i, j; /*loop variable*/ float **feadata; /*matrix of training RC data*/ long *enc; /*final encode indices for data*/ long fea_len; /*numb. rows in feadata*/ long fea_dim; /*numb. columns in feadata*/ long new_dim; /*dim of input vector */ long c_rows; /*numb. rows in codebook*/ struct vqcbk *cbk; /*the VQ codebook*/ struct vqcbk *icbk; /*initial VQ codebook*/ double (*distort)(); /*routine to use for distortion*/ int vq_ret; /*return value from vqdesign*/ char *line; /*one input ASCII line*/ int cflag = 0; /*flag for -c option*/ int cfile_flag = 0; /*flag for -C option*/ int init_flag = 0; /*flag for -i option*/ int max_iter = MAX_ITER;/*max no. of iterations*//* *Initialization */ cmd_line = get_cmd_line(argc, argv); /*store copy of command line*//* * process command line options */ while ((c = getopt (argc, argv, "x:P:h:c:C:k:l:i")) != EOF) { switch (c) { case 'x': debug_level = atoi (optarg); break; case 'P': paramfile = optarg; break; case 'h': histfile = optarg; break; case 'C': cmntfile = optarg; cfile_flag++; break; case 'c': if ((i = strlen (optarg)) <= MAXCMNT) strcpy (comment, optarg); else { Fprintf(stderr, "vqdesasc: Comment field has %d characters, max is %d\n", i, MAXCMNT); exit (1); } comment[i] = '\n'; comment[i+1] = '\0'; cflag++; break; case 'k': checkfile = optarg; break; case 'i': init_flag++; break; case 'l': max_line_len = atoi(optarg); break; default: SYNTAX; } } line = (char *) malloc(max_line_len); /*Get comment if one wasn't supplied. */ if ((!cflag) && (!cfile_flag)){ printf("vqdesasc: Please enter one-line comment describing training sequence.\n"); fflush (stdout); i = 0; while ((int)(comment[i] = getchar()) != '\n' && i < MAXCMNT) i++; printf("Thank you, processing continuing...\n"); }/* * process file arguments - NOTE: we don't allow stdin for the input (yet) */ if (optind < argc) { input_file = argv[optind++]; TRYOPEN (argv[0], input_file, "r", input_strm); } else { Fprintf(stderr, "vqdesasc: no input file specified.\n"); SYNTAX; } if (optind < argc) { out_fea = argv[optind++]; if (strcmp (out_fea, "-") == 0) out_fea = "<stdout>"; else TRYOPEN (argv[0], out_fea, "w", fea_strm); } else { Fprintf(stderr, "vqdesasc: no output file specified.\n"); SYNTAX; } TRYOPEN(argv[0], histfile, "w", histrm);/* * read first training vector to determine dimension */ ndrec = 1; if (fgets(line, max_line_len, input_strm) != NULL) fea_dim = find_fea_dim(line); if ((line == NULL) || (fea_dim == 0)) ERROR_EXIT("no data in input file"); if (fea_dim < 2) ERROR_EXIT("feature vectors must have 2 or more components");/* * read parameters from parameter file and SPS common - require that * the filename in common match that of the input SD file */ (void) read_params(paramfile, SC_CHECK_FILE, input_file); if(symtype("max_iter") != ST_UNDEF) max_iter = getsym_i("max_iter");/* *create FEA header */ fea_oh = new_header(FT_FEA); c_rows = getsym_i("vq_size"); if (init_vqfea_hd(fea_oh, c_rows, fea_dim) != 0) ERROR_EXIT("error filling FEA header"); (void) strcpy (fea_oh->common.prog, "vqdesasc"); (void) strcpy (fea_oh->common.vers, Version); (void) strcpy (fea_oh->common.progdate, Date); if (init_flag) { /* *get initial codebook from file and record given in *parameter file */ init_file = getsym_s("init_file"); if (strcmp(checkfile, init_file) == 0) ERROR_EXIT("Checkpoint file has same name as init_file.") init_rec = getsym_i("init_rec"); TRYOPEN(argv[0], init_file, "r", init_strm); if ((init_ih = read_header(init_strm)) == NULL) ERROR_EXIT("couldn't read input FEA_VQ file header"); if ((init_ih->common.type != FT_FEA) || (init_ih->hd.fea->fea_type != FEA_VQ)) ERROR_EXIT("initial codebook file not FEA or not FEA_VQ"); add_source_file(fea_oh, init_file, init_ih); } add_comment(fea_oh, cmd_line); if (cfile_flag) { /*comment file given*/ TRYOPEN(argv[0], cmntfile, "r", cmnt_strm); while (fgets(comment, MAXCMNT, cmnt_strm) != NULL) add_comment(fea_oh, comment); } else /*comment line from command line or prompt*/ add_comment(fea_oh, comment);/* *Now that the header is complete, we can write it out */ write_header(fea_oh, fea_strm); Fflush(fea_strm); /*just in case vqdesasc bombs :-)*//* *Allocate FEA_VQ record. */ cbk = allo_vqfea_rec(fea_oh);/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -