tofspec.c
来自「speech signal process tools」· C语言 代码 · 共 677 行 · 第 1/2 页
C
677 行
*/ field_type = get_fea_type(field_name, ihd); REQUIRE(field_type != UNDEF, "specified field not in input file"); size = get_fea_siz(field_name, ihd, &rank, (long **) NULL); if (rank != 1) Fprintf(stderr, "tofspec: WARNING, field %s not a vector.\n", field_name); REQUIRE(size != 0, "size == 0, bad data field definition in input file"); /* sf */ /* If -s is used, we have a forced setting for sf; otherwise we fake up as element numbers. */ if (!sflag) sf = (symtype("sf") != ST_UNDEF) ? getsym_d("sf") : 0; /* means fake up element numbers as frequencies*/ if (sf == 0) sf = 2*(size - 1); /* fake up element numbers as frequencies*/ /* freq_format */ if (!Fflag) { fmt_name = (symtype("freq_format") != ST_UNDEF) ? getsym_s("freq_format") : "SYM_EDGE"; REQUIRE(fmt_name, "Parameter \"freq_format\" not STRING"); } switch (freq_format = lin_search(spfmt_names, fmt_name)) { case SPFMT_ARB_FIXED: if (vflag) { freqs = flist_switch(freq_list, &num_freqs); REQUIRE(num_freqs == size, "wrong number of specified frequencies"); } else { REQUIRE(symtype("freqs") == ST_FARRAY, "Paramter \"freqs\" undefined or not float array"); num_freqs = symsize("freqs"); REQUIRE(num_freqs == size, "wrong number of specified frequencies"); freqs = (float *) malloc(sizeof(float) * size); REQUIRE(freqs, "couldn't allocate storage for freqs array"); (void) getsym_fa("freqs", freqs, (int) num_freqs); } break; case SPFMT_SYM_EDGE: break; case SPFMT_ASYM_EDGE: case SPFMT_SYM_CTR: case SPFMT_ASYM_CTR: case SPFMT_ARB_VAR: ERROR("freq format not yet supported"); break; default: ERROR("freq format not recognized"); break; } /* possible loop to determine data limits*/ irec = allo_fea_rec(ihd); f_ptr = get_fea_ptr(irec, field_name, ihd); REQUIRE(f_ptr, "NULL pointer returned from get_fea_ptr"); data = calloc_f((unsigned) size); REQUIRE(data, "can't allocate space for array of data as doubles"); if (determine_limits) { if (debug_level) Fprintf(stderr, "tofspec: computing display limits from data\n"); datamin = FLT_MAX; datamax = -FLT_MAX; num_read = start_rec - 1, fea_skiprec(ifile, num_read, ihd); for ( ; num_read < end_rec && get_fea_rec(irec, ihd, ifile) != EOF; num_read++ ) { if (debug_level > 2) Fprintf(stderr, "Record number %ld read.\n", num_read + 1); (void) type_convert(size, f_ptr, field_type, (char *) data, FLOAT, (void (*)()) NULL); for (j = 0; j < size; j++) { if (data[j] < datamin) datamin = data[j]; if (data[j] > datamax) datamax = data[j]; } } if (num_read < end_rec && num_recs != 0) Fprintf(stderr, "tofspec: Only %ld records read.\n", num_read - start_rec + 1); if (low_in == FLT_MAX) low_in = datamin; if (high_in == -FLT_MAX) high_in = datamax; rewind(ifile); ihd = read_header(ifile); } if (debug_level) Fprintf(stderr, "tofspec: input range is %g, %g.\n", low_in, high_in); if (dflag) { if (low_in <= 0) { Fprintf(stderr, "tofspec: can't use -d option with negative or zero data\n"); exit(1); } else { log_low_in = log10(low_in); log_high_in = log10(high_in); if (debug_level) Fprintf(stderr, "tofspec: log limits on data values: %g, %g.\n", log_low_in, log_high_in); } } /*set scale values for data */ if (dflag) scale = (high_out - low_out) / (log_high_in - log_low_in); else scale = (high_out - low_out) / (high_in - low_in); if (debug_level) Fprintf(stderr, "tofspec: scale = %g\n", scale); /* * Create and write output-file header */ ohd = new_header(FT_FEA); if (init_feaspec_hd(ohd, NO, freq_format, SPTYP_DB, YES, size, SPFRM_NONE, freqs, sf, (long) 0, BYTE) != 0) { Fprintf(stderr, "tofspec: Error filling FEA_SPEC header\n"); exit(1); } ohd->common.tag = ihd->common.tag; if (ihd->common.tag && !copy_genhd(ohd, ihd, "src_sf")) { if (genhd_type("sf", (int *) NULL, ihd) == ST_UNDEF) Fprintf(stderr, "tofspec: no \"src_sf\" or \"sf\" in tagged file.\n"); else *add_genhd_f("src_sf", (float *) NULL, 1, ohd) = get_genhd_val("sf", ihd, 1.0); } ohd->variable.refer = ihd->variable.refer; ohd->variable.refhd = ihd->variable.refhd; (void) strcpy(ohd->common.prog, ProgName); (void) strcpy(ohd->common.vers, Version); (void) strcpy(ohd->common.progdate, Date); add_source_file(ohd, iname, ihd); add_comment(ohd, get_cmd_line(argc, argv)); *add_genhd_l("nan", (long *) NULL, 1, ohd) = num_recs; *add_genhd_l("start", (long *) NULL, 1, ohd) = start_rec; *add_genhd_f("low_input", (float *) NULL, 1, ohd) = low_in; *add_genhd_f("high_input", (float *) NULL, 1, ohd) = high_in; *add_genhd_f("low_output", (float *) NULL, 1, ohd) = low_out; *add_genhd_f("high_output", (float *) NULL, 1, ohd) = high_out; if (!Sflag) *add_genhd_f("scale_factor", (float *) NULL, 1, ohd) = scale; if (dflag) { *add_genhd_f("log_low_input", (float *) NULL, 1, ohd) = log_low_in; *add_genhd_f("log_high_input", (float *) NULL, 1, ohd) = log_high_in; } update_waves_gen(ihd, ohd, (float)start_rec, 1.0); if (debug_level) Fprintf(stderr, "tofspec: Writing output header to file.\n"); write_header(ohd, ofile); orec = allo_feaspec_rec(ohd, FLOAT); /* * Main read-write loop */ /* now read/write loop for putting out FEA_SPEC file*/ num_read = start_rec - 1, fea_skiprec(ifile, num_read, ihd); for ( ; num_read < end_rec && get_fea_rec(irec, ihd, ifile) != EOF; num_read++ ) { if (debug_level > 1) Fprintf(stderr, "Record number %ld read.\n", num_read + 1); (void) type_convert(size, f_ptr, field_type, (char *) data, FLOAT, (void (*)()) NULL); for (j = 0; j < size; j++) { if (Rflag) /*reverse elements*/ k = size - 1 - j; else /*element order unchanged*/ k = j; if (Sflag) { /*no scaling*/ if (dflag) dtemp = log10(data[k]); else dtemp = data[k]; if (dtemp < low_out) orec->re_spec_val[j] = low_out; else if (dtemp > high_out) orec->re_spec_val[j] = high_out; else orec->re_spec_val[j] = dtemp; } else { /*scale the data to fit*/ if (data[k] <= low_in) orec->re_spec_val[j] = low_out; else if (data[k] >= high_in) orec->re_spec_val[j] = high_out; else { if (dflag) orec->re_spec_val[j] = low_out + scale * (log10(data[k]) - log_low_in); else orec->re_spec_val[j] = low_out + scale * (data[k] - low_in); } } } if (ohd->common.tag == YES) *orec->tag = irec->tag; (void) put_feaspec_rec(orec, ohd, ofile); } if (num_read < end_rec && num_recs != 0) Fprintf(stderr, "tofspec: Only %ld records read.\n", num_read - start_rec + 1); /* * Write common */ if (strcmp(oname, "<stdout>") != 0 && strcmp(iname, "<stdin>") != 0) { (void) putsym_s("filename", iname); (void) putsym_s("prog", ProgName); (void) putsym_i("start", (int) start_rec); (void) putsym_i("nan", (int) num_recs); } exit(0); /*NOTREACHED*/}#define INIT_SIZE 200#define DELIM ", "char *savestring();static float *flist_switch(text, len) char *text; long *len;{ char *list, *token; long alloc_len; float *vals; long num; spsassert(text, "flist_switch: NULL text"); list = savestring(text); alloc_len = INIT_SIZE; vals = (float *) malloc(sizeof(float) * alloc_len); if (!vals) return NULL; for (token = strtok(list, DELIM), num = 0; token; token = strtok((char *) NULL, DELIM), num++) { if (num == alloc_len) { alloc_len += alloc_len/2; vals = (float *) realloc((char *) vals, sizeof(float) * alloc_len); if (!vals) return NULL; } vals[num] = atof(token); } free((char *) list); if (len) *len = num; return (float *) realloc((char *) vals, sizeof(float) * num);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?