📄 vqdst.c
字号:
/* * Get field_rep info, if appropriate */ if(data_ih->hd.fea->fea_type == FEA_ANA){ /*get character representation corresponding to spec_rep field*/ spec_rep = *(short *)get_genhd("spec_rep", data_ih); field_rep = spec_reps[spec_rep]; if(debug_level > 0){ Fprintf(stderr, "spec_rep = %d; field_rep = %s\n", spec_rep, field_rep); } } /* * Allocate space for mean distortion array */ dist_mean = (double *)calloc((unsigned)ncbkrec, sizeof(double)); dist_val = (double *)calloc((unsigned)ncbkrec, sizeof(double));/* * Check for inconsistencies */ if(((fea_size = get_fea_siz(field, data_ih,(short *)NULL,(long **)NULL))) == 0) ERROR_EXIT("Field does not exist in input record"); if(cflag){ if(data_ih->hd.fea->fea_type == FEA_ANA){ for(i = 0; i < ncbkrec; i++) if(strcmp(field_rep, codebooks[i]->field_rep) != 0){ Fprintf(stderr, "Input FEA and codebook file #%d have different field_reps\n" , i+1); exit(1); } } for(i = 0; i < ncbkrec; i++) if(strcmp(field, codebooks[i]->field) != 0){ Fprintf(stderr, "Input FEA and codebook file #%d have different field names\n" , i+1); exit(1); } } if(sflag){ /*compare input header input_source with the source_name value in all the codebook records*/ for(i = 0; i < ncbkrec; i++) if(strcmp(get_genhd_c("input_source", data_ih), codebooks[i]->source_name) != 0){ Fprintf(stderr, "Input FEA and codebook #%d SOURCE names differ\n", i+1); exit(1); } } if(tflag){ /*check FEA's input_signal with signal_name in all codebooks*/ for(i = 0; i < ncbkrec; i++) if(strcmp(get_genhd_c("input_signal", data_ih), codebooks[i]->signal_name) != 0){ Fprintf(stderr, "Input FEA and codebook #%d SIGNAL names differ\n", i+1); exit(1); } } for(i = 0; i< ncbkrec; i++) if(fea_size != *codebooks[i]->dimen){ Fprintf(stderr, "vqdst: Codeword in codebook %d and input field dimensions differ\n",i); exit(1); }/* * convert distortion string to integer representation */ if((dist_type = lin_search(dist_types, enc_dist)) == -1) ERROR_EXIT("Invalid encodeing distortion specified"); if(debug_level > 0){ Fprintf(stderr,"vqdst: enc_dist = %s; dist_type = %d\n", enc_dist, dist_type); }/* *select distortion function */ switch (dist_type) { case MSE: distort = NULL; break; case GOIS: ERROR_EXIT("GOIS distortion not supported yet"); break; case GNIS: ERROR_EXIT("GNIS distortion not supported yet"); break; case IS: ERROR_EXIT("IS distortion is not supported yet"); break; default: ERROR_EXIT("invalid distortion type"); }/* * main processing goes here */ nfea = 0; /* * Allocate space for input feature vector */ input_vec = (float *)calloc((unsigned)fea_size, sizeof(float)); /* * determine data type of input field - exit if not floats */ if ( get_fea_type(field, data_ih) != FLOAT ) ERROR_EXIT("Numeric type of input field must be float"); /* *Start Processing */ while (get_fea_rec(data_rec, data_ih, data_strm) != EOF) { nfea++; /* *get a vector to encode */ input_vec = (float *) get_fea_ptr(data_rec, field, data_ih); /* *encode the vector */ switch (*codebooks[0]->cbk_struct) { case FULL_SEARCH: if(vqdist(input_vec, ncbkrec, codebooks, dist_val, distort) > 0) ERROR_EXIT("Invalid distortion mesure passed to vqdist"); break; default: ERROR_EXIT("invalid cbk_struct type"); } /* sum distortion for each codebook*/ for(i=0;i<ncbkrec;i++) dist_mean[i] += dist_val[i]; } /* * Now compute average distortion */ for(i=0; i<ncbkrec; i++) dist_mean[i] = dist_mean[i] / nfea; if(debug_level > 0){ Fprintf(stderr, "vqdst: Number of input vectors = %d\n", nfea); Fprintf(stderr, "vqdst: Mean square error:\n"); for(i=0; i<ncbkrec; i++) Fprintf(stderr, "\tfor codebook %d = %f\n", i, dist_mean[i]); } /* * Write output FEA_DST records */ dst_rec = allo_feadst_rec(dst_oh); for(i=0; i< ncbkrec; i++){ *dst_rec->data_sect_mthd = SM_ONE; *dst_rec->data_sect_num = 1; *dst_rec->cbk_sect_mthd = SM_ONE; *dst_rec->cbk_sect_num = 1; (void)strcpy(dst_rec->quant_field, field); (void)strcpy(dst_rec->quant_field_rep, field_rep); *dst_rec->cbk_struct = *codebooks[i]->cbk_struct; *dst_rec->cbk_type = *codebooks[i]->cbk_type; *dst_rec->dsgn_dst = *codebooks[i]->dist_type; *dst_rec->encde_dst = dist_type; *dst_rec->cbk_sect_size = *codebooks[i]->current_size; (void)strncpy(dst_rec->cbk_name, cbk_in,15);/* temporary fix*/ *dst_rec->cbk_rec = i+1; (void)strncpy(dst_rec->data_name, data_in,15);/*temporary fix*/ (void)strcpy(dst_rec->cbk_source, codebooks[i]->source_name); (void)strcpy(dst_rec->cbk_signal, codebooks[i]->signal_name); (void)strcpy(dst_rec->source_type, codebooks[i]->source_type); if(genhd_type("input_source", &header_size, data_ih) != HD_UNDEF) (void)strcpy(dst_rec->input_source, get_genhd_c("input_source", data_ih)); if(genhd_type("input_signal", &header_size, data_ih) != HD_UNDEF) (void)strcpy(dst_rec->input_signal, get_genhd_c("input_signal", data_ih)); *dst_rec->in_rep_number = (short)rep_number; *dst_rec->data_sect_dst = dist_mean[i]; *dst_rec->data_sect_size = (short)nfea; *dst_rec->average_dst = dist_mean[i]; *dst_rec->cbk_train_dst = (float)*codebooks[i]->final_dist; put_feadst_rec(dst_rec, dst_oh, dst_strm); } exit(0);}intvqdist(in_vec, ncdbks, codebooks, dist_val, distort)float *in_vec; /*feature vector to be encoded*/int ncdbks; /*number of codebooks*/struct vqcbk **codebooks; /*the full search vq codebook*/double *dist_val; /*dists between in_vec and selected codewords*/double (*distort)(); /*routine to compute distortions*//* *This routine encodes a single feature vector in each of ncdbks full-search *VQ codebooks. The codebooks are passed through a pointer to a pointer * to the vqcbk struct. The distortion values are returned in dist_val. *If distort == NULL, a mean-square-error distortion is used. Otherwise *the pointer is used to call a distortion routine. */{ int i, j, k; int cbk_size, cdwd_dimen; if(debug_level > 1){/* check input parameters*/ Fprintf(stderr,"vqdst: vqdist: number of codebooks = %d\n", ncdbks); } /* * All codebooks must have the same size and shape for this version */ cbk_size = (*codebooks[0]->current_size); cdwd_dimen = (*codebooks[0]->dimen); if(debug_level > 0){ Fprintf(stderr,"vqdst:vqdist: cbk_size = %d; cdwd_dimen = %d\n", cbk_size, cdwd_dimen); } if (distort == NULL) { /*use mean-square-error distortion*/ double se, diff; for(i = 0; i < ncdbks; i++){ dist_val[i] = DBL_MAX; for(j = 0; j < cbk_size; j++) { se = 0.0; for(k = 0; k < cdwd_dimen; k++){ diff = (in_vec[k] - codebooks[i]->codebook[j][k]); se += diff * diff; } if(debug_level >2) Fprintf(stderr,"vqdst: vqdist: codebook %d, codeword %d distortion = %lf\n", i, j, se); if (se < dist_val[i]) { dist_val[i] = se; } } /* * find average distortion */ dist_val[i] = dist_val[i]/cdwd_dimen; } } else { /*encode using externally supplied distort function */ /* no other distortion measures supported. If we reach here, we have an error*/ return(1); } return (0);}/* * Get number of records in a file. * Replace input stream with temporary file if input is a pipe * or record length is variable. */static longn_rec(file, hd) FILE **file; struct header **hd;{ if ((*hd)->common.ndrec != -1) /* Input is file with fixed record size. */ return (*hd)->common.ndrec; /* Get ndrec from header. */ else /* Input is pipe, or record length * is variable. */ { FILE *tmpstrm = tmpfile(); struct header *tmphdr; /* header for writing and reading temp file */ struct fea_data *tmprec; /* record for writing and reading temp file */ long ndrec = 0; /* * Get version of header without any Esignal header, mu-law * flag, etc. Otherwise we risk getting garbage by writing the * temp file as an ESPS FEA file and reading it back as some * other format. */ tmphdr = copy_header(*hd); write_header(tmphdr, tmpstrm); tmprec = allo_fea_rec(tmphdr); for (ndrec = 0; get_fea_rec(tmprec, *hd, *file) != EOF; ndrec++) put_fea_rec(tmprec, tmphdr, tmpstrm); Fclose(*file); (void) rewind(tmpstrm); *hd = read_header(tmpstrm); *file = tmpstrm; return ndrec; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -