clip.c
来自「speech signal process tools」· C语言 代码 · 共 662 行 · 第 1/2 页
C
662 行
/* * 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-1991 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: Rodney Johnson * Checked by: * Revised by: * * Apply clipping and center-clipping operations to FEA fields. * */static char *sccs_id = "@(#)clip.c 1.2 8/31/95 ESI/ERL";/* Based on feafunc.c.*/#define VERSION "1.2"#define DATE "8/31/95"#include <stdio.h>#include <esps/esps.h>#include <esps/unix.h>#include <esps/fea.h>#define REQUIRE(test, text) {if (!(test)) {(void) fprintf(stderr, \"%s: %s - exiting\n", ProgName, text); exit(1);}}#define SYNTAX \USAGE("clip [-c range] [-f input_field [-f output_field]] [-m range] [-r range] [-x debug_level] [-C const_value] [-P params] input.fea output.fea") ;#define ABS(x) ((x) < 0 ? -(x) : (x))#define CONVERT(src, dst, type) \((void) type_convert(1L, (char *) &src, DOUBLE, (char *) &dst, type, \(void (*)()) NULL))double floor(), ceil();extern char *type_codes[];char *get_cmd_line();void lrange_switch(), frange_switch();char *arr_alloc();static int numeric(); /* tests whether type is numeric */char *ProgName = "clip";char *Version = VERSION;char *Date = DATE;char sbuf[256]; /* to hold comment */int debug_level = 0; /* debug level, global for library*/long num_read; /* number of records skipped or read *//* * MAIN PROGRAM */main(argc, argv) int argc; char **argv;{ extern int optind; /* for use of getopt() */ extern char *optarg; /* for use of getopt() */ int ch; /* command-line option letter */ int cflag = NO; /* -c option specified? */ char *crange; /* arguments of -c option */ double ctr_min, ctr_max; /* limits of center-clipping interval */ int ctr_clip; /* do center-clipping? */ int Cflag = NO; /* -C option specified? */ double const_val; /* argument of -C option */ int fflag = 0; /* -f option specified 0, 1, or 2 times? */ char *inf_name; /* field name for input field */ int inf_type; /* data type of input field */ char *outf_name; /* field name for output field */ int warn_if_repl = YES; /* print warning if output field exists in input file? */ char **fld_names; /* fields to be copied unchanged */ int mflag = NO; /* -m option specified? */ char *mrange; /* arguments of -m option */ double clip_min, clip_max; /* limits of clipping interval */ int rflag = NO; /* -r option specified? */ char *rrange; /* arguments of -r option */ long start_rec; /* starting record number */ long end_rec; /* ending record number */ long num_recs; /* number of records to read (0 means all up to end of file) */ char *param_name = NULL; /* parameter file name */ char *iname; /* input file name */ FILE *ifile; /* input stream */ struct header *ihd; /* input file header */ struct fea_data *irec; /* input record */ char *inf_ptr; /* pointer (untyped) to field in input record */ char *outf_ptr; /* pointer (untyped) to field in output record*/ long size; /* size of input data field */ short rank; /* number of dimensions of input field */ long *dimens; /* dimensions of input field */ char *oname; /* output file name */ FILE *ofile; /* output stream */ struct header *ohd; /* output file header */ struct fea_data *orec; /* output record */ int i; /* loop index */ /* * Parse command-line options. */ while ((ch = getopt(argc, argv, "c:f:m:r:x:C:P:")) != EOF) switch (ch) { case 'c': cflag = YES; crange = optarg; break; case 'f': switch (fflag) { case 0: inf_name = optarg; fflag++; break; case 1: outf_name = optarg; fflag++; break; default: Fprintf(stderr, "%s: -f option may be specified at most twice.\n", ProgName); exit(1); } break; case 'm': mflag = YES; mrange = optarg; break; case 'r': rflag = YES; rrange = optarg; break; case 'x': debug_level = atoi(optarg); break; case 'C': Cflag = YES; const_val = atof(optarg); break; case 'P': param_name = optarg; break; default: SYNTAX break; } /* * Process file names and open files. */ if (argc - optind > 2) { Fprintf(stderr, "%s: too many file names specified.\n", ProgName); SYNTAX } if (argc - optind < 2) { Fprintf(stderr, "%s: too few file names specified.\n", ProgName); SYNTAX } iname = eopen(ProgName, argv[optind++], "r", FT_FEA, NONE, &ihd, &ifile); if (ifile != stdin) REQUIRE(strcmp(iname, argv[optind]), "output file same as input"); oname = eopen(ProgName, argv[optind++], "w", NONE, NONE, &ohd, &ofile); if (debug_level) Fprintf(stderr, "Input file: %s\nOutput file: %s\n", iname, oname); /* * Get parameter values. */ if (ifile != stdin) (void) read_params(param_name, SC_CHECK_FILE, iname); else (void) read_params(param_name, SC_NOCOMMON, iname); /* -f: field names */ if (fflag < 1) inf_name = (symtype("input_field") != ST_UNDEF) ? getsym_s("input_field") : "samples"; if (fflag < 2) outf_name = (symtype("output_field") != ST_UNDEF) ? getsym_s("output_field") : NULL; if ((outf_name == NULL) || (strcmp(outf_name, "[input_name]_CLIP") == 0)) { outf_name = malloc((unsigned)(strlen(inf_name) + strlen("_CLIP") + 1)); REQUIRE(outf_name, "can't allocate space for output field name"); (void) strcpy(outf_name, inf_name); (void) strcat(outf_name, "_CLIP"); } else if (strcmp(outf_name, "-") == 0) { outf_name = inf_name; warn_if_repl = NO; } if (debug_level) { Fprintf(stderr, "input_field: \"%s\". output_field: \"%s\".\n", inf_name, outf_name); } inf_type = get_fea_type(inf_name, ihd); if (inf_type == UNDEF) { Fprintf(stderr, "%s: input_field %s not in input file\n", ProgName, inf_name); exit(1); } if (!numeric(inf_type)) { Fprintf(stderr, "%s: invalid input data type.\n", ProgName); exit(1); } /* -c & -C: center-clipping limits and const_value */ if (cflag) { ctr_max = -DBL_MAX; ctr_min = DBL_MAX; frange_switch(crange, &ctr_min, &ctr_max); } else if (symtype("ctr_max") != ST_UNDEF && symtype("ctr_min") != ST_UNDEF) { ctr_max = getsym_d("ctr_max"); ctr_min = getsym_d("ctr_min"); } else { ctr_max = -DBL_MAX; ctr_min = DBL_MAX; } ctr_clip = (ctr_min <= ctr_max); if (ctr_clip && !Cflag) { const_val = (symtype("const_value") != ST_UNDEF) ? getsym_d("const_value") : 0.0; } if (debug_level) { Fprintf(stderr, "ctr_min: %g. ctr_max: %g.\n", ctr_min, ctr_max); if (ctr_clip) Fprintf(stderr, "const_value: %g.\n", const_val); else Fprintf(stderr, "No center-clipping."); } /* -m: clipping limits */ if (mflag) { clip_max = DBL_MAX; clip_min = -DBL_MAX; frange_switch(mrange, &clip_min, &clip_max); } else { clip_max = (symtype("clip_max") != ST_UNDEF) ? getsym_d("clip_max") : DBL_MAX; clip_min = (symtype("clip_min") != ST_UNDEF) ? getsym_d("clip_min") : -DBL_MAX; } if (debug_level) Fprintf(stderr, "clip_min: %g. clip_max: %g.\n", clip_min, clip_max); /* -r: range of records */ if (rflag) { start_rec = 1; end_rec = LONG_MAX; lrange_switch(rrange, &start_rec, &end_rec, 0); num_recs = (end_rec != LONG_MAX) ? end_rec - start_rec + 1 : 0; } else { start_rec = (symtype("start") != ST_UNDEF) ? getsym_i("start") : 1; num_recs = (symtype("nan") != ST_UNDEF) ? getsym_i("nan") : 0; end_rec = (num_recs != 0) ? start_rec - 1 + num_recs : LONG_MAX; } if (debug_level) Fprintf(stderr, "start_rec: %ld. end_rec: %ld. num_recs: %ld.\n", start_rec, end_rec, num_recs);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?