cepsyn.c
来自「speech signal process tools」· C语言 代码 · 共 785 行 · 第 1/2 页
C
785 行
/* * 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) 1997 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: Rod Johnson * Checked by: * Revised by: * * Brief description: sample data from real cepstrum and phase factors */static char *sccs_id = "@(#)cepsyn.c 1.2 5/14/97 ERL";#define VERSION "1.2"#define DATE "5/14/97"#define PROG "cepsyn"int debug_level = 0;/* INCLUDE FILES */#include <math.h>#include <esps/esps.h>#include <esps/fea.h>#include <esps/feasd.h>#include <esps/window.h>#include <esps/func.h>#include <esps/op.h>/* LOCAL CONSTANTS *//* LOCAL MACROS */#define SYNTAX \USAGE(PROG " [-r range1 [-r range2]] [-t output_type] [-x debug_level]\n" \ "\t[-W] phase_in.fea cepst_in.fea output.sd")#define ERROR(text) { \ (void) fprintf(stderr, "%s: %s - exiting\n", PROG, (text)); \ exit(1);}#define REQUIRE(test, text) {if (!(test)) ERROR(text)}#define STRCMP(a, op, b) (strcmp((a), (b)) op 0)/* SYSTEM FUNCTIONS AND VARIABLES */int getopt(); /* parse command-line options */int optind; /* used by getopt */char *optarg; /* used by getopt *//* ESPS FUNCTIONS AND VARIABLES */char *get_cmd_line(int argc, char **argv);char *zero_fill(long num, int type, char *dest);/* LOCAL TYPEDEFS AND STRUCTURES *//* LOCAL FUNCTION DECLARATIONS */int is_type_numeric(int type);void pr_farray(char *text, long n, float *arr);void pr_fcarray(char *text, long n, float_cplx *arr);/* STATIC (LOCAL) GLOBAL VARIABLES */ /* MAIN PROGRAM */intmain(int argc, char **argv){ int ch; /* command-line option letter */ char *r_arg[2]; /* -r option arguments */ int r_num = 0; /* number of -r options */ long startrec[2]; /* first record to process * in each input file */ long endrec[2]; /* last record to process * in each input file */ long nrec; /* number of records to process */ long nan; /* like nrec, but 0 means continue * processing until end of file */ long irec; /* index of current record */ char *t_arg = NULL; /* -t option argument */ int sdtype; /* code for output data type */ int W_flag = NO; /* -W option specified? */ int doing_windows; /* try to normalize overlapped * output sample sums by sums of * window weights? */ int req_overlap; /* required overlap between frames * for normalization to work */ int wtype; /* window type */ float *win; /* window */ float *wt; /* summed weights (win values) for * normalizing samples */ long translen; /* Fourier transform length */ int order; /* Fourier transform order */ long i, j; /* loop indices */ char *phname; /* phase input file name */ struct header *phhd; /* phase input file header */ FILE *phfile; /* phase input file */ long phlen; /* length of phase field */ struct fea_data *phrec; /* phase input data record */ float *phdata; /* phase input data array */ char *cepname; /* cepstrum input file name */ struct header *cephd; /* cepstrum input file header */ FILE *cepfile; /* cepstrum input file */ long ceplen; /* length of cepstrum field */ struct fea_data *ceprec; /* cepstrum input data record */ float *cepdata; /* cepstrum input data array */ double sf; /* sampling frequency (Hz) */ long framelen; /* sampled-data frame length */ double record_freq; /* input record rate */ long step; /* increment between frame endpoints */ long buflen; /* length of output sample buffer */ long wrap; /* circular shift for centering * frame in array of length translen */ long lolim, hilim; /* limits of region in buffer holding * data to be written */ char *sdname; /* output sampled-data file name */ struct header *sdhd; /* output sampled-data file header */ FILE *sdfile; /* output sampled-data file */ struct feasd *sdrec; /* output data record structure */ float *sddata; /* output data array */ double start_time; /* output starting time (sec) */ float *x, *y; /* temp storage for FFT and inverse * FFT computation (real and * imaginary parts) */ float_cplx *z; /* FFT result (complex form) */ float_cplx *expz; /* Complex exponential of FFT */ /* * PARSE COMMAND-LINE OPTIONS. */ while ((ch = getopt(argc, argv, "r:t:x:W")) != EOF) switch (ch) { case 'r': if (r_num < 2) r_arg[r_num++] = optarg; else { fprintf(stderr, "%s: too many -r options.\n", PROG); SYNTAX; } break; case 't': t_arg = optarg; break; case 'x': debug_level = atoi(optarg); break; case 'W': W_flag = YES; break; default: SYNTAX; } /* * PROCESS FILE NAMES. */ if (argc - optind > 3) { fprintf(stderr, "%s: too many file names.\n", PROG); SYNTAX; } if (argc - optind < 3) { fprintf(stderr, "%s: not enough file names.\n", PROG); SYNTAX; } phname = argv[optind++]; cepname = argv[optind++]; sdname = argv[optind++]; REQUIRE(STRCMP(sdname, ==, "-") || STRCMP(sdname, !=, phname) && STRCMP(sdname, !=, cepname), "Output file cannot be same as input file"); REQUIRE(STRCMP(phname, !=, cepname), "Input files cannot be the same"); /* * OPEN AND CHECK INPUT FILES. */ /* PHASE FILE */ phname = eopen(PROG, phname, "r", FT_FEA, NONE, &phhd, &phfile); if (debug_level >= 1) fprintf(stderr, "%s: first input file = \"%s\".\n", PROG, phname); REQUIRE(get_fea_type("phase", phhd) == FLOAT, "Field \"phase\" undefined or wrong type in first input file"); phlen = get_fea_siz("phase", phhd, NULL, NULL); if (debug_level >= 1) fprintf(stderr, "%s: \"phase\" field length = %ld.\n", PROG, phlen); /* CEPSTRUM FILE */ cepname = eopen(PROG, cepname, "r", FT_FEA, NONE, &cephd, &cepfile); if (debug_level >= 1) fprintf(stderr, "%s: first input file = \"%s\".\n", PROG, cepname); REQUIRE(get_fea_type("cepstrum", cephd) == FLOAT, "Field \"cepstrum\" undefined or wrong type in second input file"); ceplen = get_fea_siz("cepstrum", cephd, NULL, NULL); if (debug_level >= 1) fprintf(stderr, "%s: \"cepstrum\" field length = %ld.\n", PROG, ceplen); /* CHECK FIELD SIZES AND GET ORDER */ REQUIRE(ceplen == phlen, "Fields \"phase\" and \"cepstrum\" must have the same length"); REQUIRE(ceplen > 0, "Fields \"phase\" and \"cepstrum\" have length 0"); translen = (ceplen == 1) ? 1 : 2*(ceplen - 1); order = (int) (0.5 + log((double) translen) / log(2.0)); if (debug_level >= 1) fprintf(stderr, "%s: translen = %ld, order = %d.\n", PROG, translen, order); REQUIRE(translen == (long) (0.5 + pow(2.0, (double) order)), "Length of fields \"phase\" and \"cepstrum\" " "doesn't correspond to any integer order"); /* GET SAMPLING FREQ, FRAME LENGTH, OTHER HEADER ITEMS */ /* Rely on phase file for these, since the generics added by cepanal * are more likely to be intact there than in the cepstrum file. * But warn if the files are inconsistent. */ /* Sampling freq */ sf = get_genhd_val("sf", phhd, 0.0); REQUIRE(sf > 0.0, "Header item \"sf\" missing or invalid in phase file"); if (genhd_type("sf", NULL, cephd) != HD_UNDEF) { double cepsf = get_genhd_val("sf", cephd, 0.0); if (cepsf > 1.00001*sf || cepsf < 0.99999*sf) fprintf(stderr, "%s: Inconsistent \"sf\" header items in input files.\n", PROG); } /* Frame length */ framelen = (long) (0.5 + get_genhd_val("frmlen", phhd, 0.0)); REQUIRE(framelen > 0, "Header item \"frmlen\" missing or invalid in phase file"); if (genhd_type("frmlen", NULL, cephd) != HD_UNDEF && framelen != (long) (0.5 + get_genhd_val("frmlen", cephd, 0.0))) { fprintf(stderr, "%s: Inconsistent \"frmlen\" header items " "in input files.\n", PROG); } if (framelen > translen) fprintf(stderr, "%s: Frame length (%ld) exceeds transform length (%sd).\n", PROG, framelen, translen); wrap = MAX(0, (translen - framelen)/2); /* Record freq */ record_freq = get_genhd_val("record_freq", phhd, 0.0); REQUIRE(record_freq > 0.0, "Header item \"record_freq\" missing or invalid in phase file"); if (genhd_type("record_freq", NULL, cephd) != HD_UNDEF && record_freq != get_genhd_val("record_freq", cephd, 0.0)) { fprintf(stderr, "%s: Inconsistent \"record_freq\" header items " "in input files.\n", PROG); } /* Step size */ step = (long) (0.5 + get_genhd_val("step", phhd, 0.0));/*!*//* Check consistency */ if (debug_level >= 1) fprintf(stderr, "%s: sf = %g, framelen = %ld,\n" "\trecord_freq = %g, step = %ld.\n", PROG, sf, framelen, record_freq, step); /* Window type */ wtype = (genhd_type("window_type", NULL, phhd) != CODED) ? WT_RECT : *get_genhd_s("window_type", phhd); /* This may be reset to WT_RECT when the -W option is processed * below. */ if (debug_level >= 1) fprintf(stderr, "%s: wtype = %d (%s).\n", PROG, wtype, window_types[wtype]); /* * PROCESS OPTIONS */ /* RANGE */ nrec = LONG_MAX; startrec[0] = startrec[1] = 1; endrec[0] = endrec[1] = LONG_MAX; for (i = 0; i < r_num; i++) { lrange_switch(r_arg[i], &startrec[i], &endrec[i], NO); REQUIRE(startrec[i] >= 1, "Can't start before beginning of file"); REQUIRE(endrec[i] >= startrec[i], "Empty range of records specified"); if (endrec[i] != LONG_MAX) { if (nrec == LONG_MAX) nrec = endrec[i] - startrec[i] + 1; else REQUIRE(endrec[i] - startrec[i] + 1 == nrec, "Inconsistent range lengths specified"); } } if (r_num == 1) { startrec[1] = startrec[0]; endrec[1] = endrec[0]; } nan = (nrec == LONG_MAX) ? 0 : nrec; if (debug_level >= 1) fprintf(stderr, "%s: startrec = {%ld, %ld}, endrec = {%ld, %ld},\n" "\tnrec = %ld, nan = %ld.\n", PROG, startrec[0], startrec[1], endrec[0], endrec[1], nrec, nan); /* OUTPUT TYPE */ if (t_arg != NULL) { sdtype = lin_search(type_codes, t_arg); REQUIRE(is_type_numeric(sdtype),
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?