📄 vqdes.c
字号:
*/ if (init_flag) { if ((init_behav = lin_search(vq_init, getsym_s("init_behav"))) == -1){ Fprintf(stderr, "vqdes: invalid init_behav in parameter file\n"); exit(1); } /* error and consistency checking */ if (init_ih->common.ndrec < init_rec && init_ih->common.ndrec != -1) ERROR_EXIT("not enough records in initial codebook file"); /* * get the initial codebook and do more consistency checking */ fea_skiprec(init_strm, (long) init_rec - 1, init_ih); icbk = allo_vqfea_rec(init_ih); if (get_vqfea_rec(icbk, init_ih, init_strm) == EOF) ERROR_EXIT("EOF when trying to read initial codebook"); if (*icbk->current_size > c_rows) ERROR_EXIT("initial codebook is too big"); if (*icbk->current_size == c_rows && init_behav == INIT_NOCLUSTER) ERROR_EXIT( "initial codebook size == design size with INIT_NOCLUSTER specified"); if ((cbk_type != -1) && (*icbk->cbk_type != cbk_type)) { Fprintf(histrm, "vqdes: initial codebook type %s differs from param file value %s\n", vq_cbk_types[*icbk->cbk_type], vq_cbk_types[cbk_type]); Fprintf(stderr, "vqdes: initial codebook type %s differs from param file value %s\n", vq_cbk_types[*icbk->cbk_type], vq_cbk_types[cbk_type]); exit(1); } if (*icbk->dimen != fea_dim) ERROR_EXIT("dimension in initial codebook doesn't match training input"); /* *check for generic header item giving FEA field */ vqfea_type = genhd_type("fea_field", (int *)NULL, init_ih); if (vqfea_type == HD_UNDEF || vqfea_type != CHAR) { Fprintf(histrm, "vqdes: WARNING - initial codebook has no generic header item fea_field\n"); Fprintf(stderr, "vqdes: WARNING - initial codebook has no generic header item fea_field\n"); } if (strcmp(get_genhd("fea_field", init_ih), fea_field) != 0) { Fprintf(histrm, "vqdes: WARNING - inconsistent fea_field in initial codebook\n"); Fprintf(histrm, "\tfield from parameter file: %s\n", fea_field); Fprintf(histrm, "\tfield from initial codebook: %s\n", get_genhd("fea_field",init_ih)); Fprintf(stderr, "vqdes: WARNING - inconsistent fea_field in initial codebook\n"); Fprintf(stderr, "\tfield from parameter file: %s\n", fea_field); Fprintf(stderr, "\tfield from initial codebook: %s\n", get_genhd("fea_field",init_ih)); } /* Now copy the initial codebook */ *cbk->current_size = *icbk->current_size; for (i = 0; i < *cbk->current_size; i++) for (j = 0; j < fea_dim; j++) cbk->codebook[i][j] = icbk->codebook[i][j]; } else { /* no initial codebook -- start from scratch */ *cbk->current_size = 0; init_behav = INIT_NOCLUSTER; } /*These parameters apply whether or not there's an initial codebook. */ *cbk->num_iter = 0; *cbk->dimen = fea_dim; *cbk->cbk_struct = fea_encode(getsym_s("cbk_struct"), "cbk_struct", vq_oh); if (*cbk->cbk_struct == -1) s_param_err(getsym_s("cbk_struct")); *cbk->design_size = c_rows; *cbk->conv_ratio = getsym_d("conv_ratio"); *cbk->cbk_type = cbk_type; *cbk->dist_type = fea_encode(getsym_s("dist_type"), "dist_type", vq_oh); if (*cbk->dist_type == -1) s_param_err(getsym_s("dist_type")); symerr_exit(); /*exit if any of the parameters were missing*//* *Create and write header for checkpoint file. */ chk_oh = copy_header(vq_oh); TRYOPEN(argv[0], checkfile, "w", chkstrm); (void) strcpy (chk_oh->common.prog, "vqdes"); (void) strcpy (chk_oh->common.vers, Version); (void) strcpy (chk_oh->common.progdate, Date); add_source_file(chk_oh, input_ana, fea_ih); if (init_flag) add_source_file(chk_oh, init_file, init_ih); add_comment(chk_oh, cmd_line); (void) add_comment(chk_oh, "This is a checkpoint file."); write_header(chk_oh, chkstrm); /* *Allocate and do pre-processing of FEA data array */ if (ndrec_ana != -1 && ndrec_ana * fea_dim <= FEA_BUFSIZE) /* number of FEAs is known, and all will fit */ { whole_fits = 1; fea_len = ndrec_ana; } else /* unknown number, or too many to process at once */ { whole_fits = 0; fea_len = FEA_BUFSIZE / fea_dim; } feadata = f_mat_alloc((unsigned) fea_len, (unsigned) fea_dim); if (feadata == NULL) ERROR_EXIT("couldn't allocate enough memory"); if (whole_fits) { /*go ahead and fill the array now*/ fea_ptr = (float *) get_fea_ptr(fea_rec, fea_field, fea_ih); for (i = 0; i < ndrec_ana; i++) { (void) get_fea_rec(fea_rec, fea_ih, fea_strm); vec_copy_f(feadata[i], fea_ptr, fea_dim); } }/* *initialize the space for the code points returned by vqdesign */ enc = (long *) calloc((unsigned) fea_len, sizeof(long)); /* *select distortion function */ switch (*cbk->dist_type) { case MSE: distort = NULL; break; case MSE_LAR: distort = mse_lar_dist; break; default: ERROR_EXIT("invalid distortion type"); }/* * history output of design parameters */ if (debug_level > 0) { Fprintf(histrm, "\nParameter values:\n"); Fprintf(histrm, "\tdimension of training vectors = %d\n", fea_dim); Fprintf(histrm, "\tname of FEA field with training vectors = %s\n", fea_field); Fprintf(histrm, "\tfractional conv. thresh = %g\n", *cbk->conv_ratio); Fprintf(histrm, "\tmax number of iterations on one level = %d\n", max_iter); Fprintf(histrm, "\tsize of VQ codebook= %d\n", *cbk->design_size); Fprintf(histrm, "\tdistortion type = %s\n", dist_types[*cbk->dist_type]); Fprintf(histrm, "\tcodebook structure = %s\n", cbk_structs[*cbk->cbk_struct]); if (init_flag) Fprintf(histrm, "Initial codebook supplied from file %s, record %d\n", init_file, init_rec); } /* * main processing goes here */ switch (*cbk->cbk_struct) { case FULL_SEARCH: vq_ret = vqdesign(histrm, debug_level, feadata, fea_len, fea_dim, cbk, enc, init_behav, SPLIT_DIST, rc_get_chunk, (int (*)())NULL, distort, (int (*)())NULL, checkpnt, max_iter); if (vq_ret == VQ_NOCONVG) Fprintf(stderr, "vqdes: FAILED - codebook didn't converge, see history file\n"); if (debug_level > 0) Fprintf(histrm, "vqdes: vqdesign return %s\n", vq_returns[vq_ret]); break; default: ERROR_EXIT("invalid distortion type"); }/* *write FEA record for final codebook */ put_vqfea_rec(cbk, vq_oh, vq_strm); exit(0);}voids_param_err(string)char *string;{ Fprintf(stderr, "vqdes: unknown parameter string %s\n", string); exit (1);}longrc_get_chunk(data, len, dim, num_prev, error)float **data;long len;long dim;long num_prev;int *error;{/*get_chunk routine (for vqdesign) to get FEA training data */ long i, lim; float *fea_ptr; *error = 0; if (whole_fits) { if (num_prev == 0) return len; else return 0; } /*we get here only if the whole FEA file didn't fit in data */ if (num_prev == ndrec_ana) return 0; if (num_prev == 0) { rewind(fea_strm); fea_ih = read_header(fea_strm); } fea_ptr = (float *) get_fea_ptr(fea_rec, fea_field, fea_ih); /* If the number of remaining input records is unknown or * greater than "len", try to fill "data" completely; * otherwise try for the known number of remaining records. */ lim = (ndrec_ana == -1 || ndrec_ana - num_prev > len) ? len : ndrec_ana - num_prev; for (i = 0; i < lim; i++) { if (get_fea_rec(fea_rec, fea_ih, fea_strm) == EOF) { if (ndrec_ana != -1) *error = 1; break; } vec_copy_f(data[i], fea_ptr, dim); } return i;}intcheckpnt(cdbk, chk_type)struct vqcbk *cdbk; /*codebook pointer*/int chk_type; /*type of checkpoint*/{/*this checkpoint routine writes the codebook to the output file *right before each split operation and also to the checkpoint file *right after every encode operation (pass through the training sequence). *Only one record is kept in the checkpoint file. */ if (chk_type == CHK_SPLIT) { put_vqfea_rec(cdbk, vq_oh, vq_strm); Fflush(vq_strm); } rewind(chkstrm); write_header(chk_oh, chkstrm); put_vqfea_rec(cdbk, chk_oh, chkstrm); Fflush(chkstrm);}#define LOG10 2.302585093doublemse_lar_dist(fea_vector, vec_dim, codeword, cdwd_dim, error)float *fea_vector;long vec_dim;float *codeword;long cdwd_dim;int *error;/* *This distortion function assumes both the feature vector and *the codeword are reflection coefficients; it converts both to *log area ratios and then does mean-square error. This program is *quite inefficient - if we really want to design large codebooks *using mse_lar, training sequence should be converted to LARs *before encoding. */{ double diff; float fea_lar, cd_lar; double msedist = 0.0; double temp; while (vec_dim-- != 0) { temp = *fea_vector++; fea_lar = log((1.0 + temp) / (1.0 - temp)); temp = *codeword++; cd_lar = log((1.0 + temp) / (1.0 - temp)); diff = fea_lar - cd_lar; msedist += diff * diff; } *error = 0; return msedist / cdwd_dim;} voidvec_copy_f(dst, src, n)float *dst, *src; /*pointers to destination and source vectors*/long n; /*size of vectors*//* *This routine copies src to dst. */{ while (n-- != 0) *dst++ = *src++;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -