filter2.c

来自「speech signal process tools」· C语言 代码 · 共 585 行 · 第 1/2 页

C
585
字号
/* * 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) 1990-1996 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:  Derek Lin * Checked by: * Revised by:  David Burton * * Brief description: FIR, IIR filtering * */static char *sccs_id = "@(#)filter2.c	1.12	5/1/98	ERL";#include <stdio.h>#include <signal.h>#include <esps/unix.h>#include <esps/esps.h>#include <esps/fea.h>#include <esps/feasd.h>#include <esps/feafilt.h>#define SYNTAX USAGE("filter2 [-P param_file] [-p range] [-r range] [-d data_type] [ -f filter_string] [ -F filt_file] [-z] [-x debug_level] in_file out_file")#define ERROR_EXIT(text) {(void) Fprintf(stderr, "%s: %s - exiting\n", \                                         argv[0], text); exit(1);}#define BUF_LEN 1024#define MAX_NO_CHANNELS 512#define NAME_SIZE 20/* * Global declaration */int debug_level = 0;/* * ESPS Functions */char *eopen();long *grange_switch();short get_fea_type();int lin_search();void fea_skiprec();int is_field_complex();char *get_cmd_line();char *arr_alloc();void lrange_switch();void trange_switch();int getopt();/* * Main program */intmain(argc, argv)     int argc;     char **argv;{  char *Version = "1.12";  char *Date = "5/1/98";  extern char *optarg;  extern int optind;  void get_range();  char *param_file = NULL;       /* default parameter file name */  FILE *isdfile = stdin,             /* input and output file streams */       *osdfile = stdout,       *filfile;                     /* filter file stream */  char *iname = NULL,                /* input and output file names */       *oname = NULL,       *filter_string = "filter",      /* filtername */       *filter_file_name = NULL,             /* filt_file name */       *fname = NULL;  struct header *ihd,                /* input and output file headers */                *ohd,                *fhd = NULL;         /* filter file header */  struct feafilt *filt_rec;           /* filter record */  struct fdata **frec = NULL;          /* general filter strucure */  struct sdata *in = NULL, *out = NULL;/* general sd data structure */  double samp_rate;  short in_dtype,                     /* input/output data type */        out_dtype;  double *ostart_time,            /* input/output start time, delay */         *istart_time,         delay = 0;  char nsiz_name[NAME_SIZE],           /* pole,zero,num,den name for -P */       den_name[NAME_SIZE],       dsiz_name[NAME_SIZE],       num_name[NAME_SIZE],       psiz_name[NAME_SIZE],       zsiz_name[NAME_SIZE],       pole_name[NAME_SIZE],       zero_name[NAME_SIZE],       gain_name[NAME_SIZE];  long nsiz, dsiz, psiz, zsiz;  double gain = 1;  double *den_coeff = NULL, *num_coeff = NULL;  double *parry = NULL, *zarry = NULL;  double_cplx *poles = NULL, *zeros = NULL;  long *channels = NULL;  long num_channels = 1;  char *range = NULL;  long s_rec, e_rec;                     /* starting/ending record */  long samps_left=0, nsamp, actsize, tot_samp_read = 0;  int rflag = 0;  int fflag = 0;  int Fflag = 0;                      int zflag = 0;  int cflag = 0;  int dflag = 0;  int common_file = 0;  int ch, size, i, j;  int type;    /* channel selection is not implemented */  while((ch = getopt(argc, argv, "P:p:r:e:d:f:F:x:z")) != EOF)    switch (ch) {    case 'P':      param_file = optarg;      break;    case 'p':    case 'r':      rflag++;      range = optarg;      break;    case 'c':      channels = grange_switch(optarg, &num_channels);      cflag++;      break;    case 'd':      out_dtype = lin_search(type_codes, optarg);      dflag++;      break;    case 'f':      filter_string = optarg;      fflag++;      break;    case 'F':      filter_file_name = optarg;      Fflag++;      break;    case 'x':      debug_level = atoi(optarg);      break;    case 'z':      zflag++;      break;    default:      SYNTAX;    }/* * Determine and open output file */  if (argc - optind < 1) {    Fprintf(stderr, "%s: no output file specified.\n",argv[0]);    SYNTAX;  }  oname = eopen(argv[0], argv[argc - 1], "w", NONE, NONE, &ohd, &osdfile);  if (debug_level)    Fprintf(stderr, "%s: output file is %s\n", argv[0], oname);/* * Determine input file, either from command line or common file */    if (argc - optind < 2) {      common_file = 1;      if (debug_level)        Fprintf(stderr, "%s: no input file on command line\n",argv[0]);    }    else {      iname = eopen(argv[0],argv[argc-2],"r", FT_FEA, FEA_SD, &ihd, &isdfile);    }  (void) read_params(param_file, SC_CHECK_FILE, iname);  if ( common_file )    {      if (symtype("filename") == ST_UNDEF) {            ERROR_EXIT("no input file name on command line or in common");        }        else {          if (debug_level)            Fprintf(stderr, "%s: input file name from common is %s\n",                    argv[0], getsym_s("filename"));          if (strcmp(oname, getsym_s("filename")) == 0)            ERROR_EXIT("input name from common same as output file");          iname = eopen(argv[0], getsym_s("filename"), "r",                              FT_FEA, FEA_SD, &ihd, &isdfile);        }    }/* * Determine parameters */  get_range( &s_rec, &e_rec, range, rflag, 0, ihd );  if( genhd_type("record_freq",&size,ihd) == HD_UNDEF)    ERROR_EXIT("record_freq header item is missing from input file");  samp_rate = *get_genhd_d("record_freq", ihd);  num_channels = get_fea_siz("samples", ihd, (short *)NULL,(long **)NULL);   if( num_channels > MAX_NO_CHANNELS )    ERROR_EXIT(" Input file exceeds 512 maximum allowable channels");  if(debug_level) Fprintf(stderr,"main: no. of channels is %ld\n",num_channels);/* * Determine filter data, either from the parameter file or filter file */  if (Fflag || symtype("filter_file_name") != ST_UNDEF) {    if (!Fflag)      filter_file_name = getsym_s("filter_file_name");    if (!fflag)      filter_string = "1";    fname = eopen(argv[0], filter_file_name, "r", 		  FT_FEA, FEA_FILT, &fhd, &filfile);    filt_rec = allo_feafilt_rec(fhd);    i = atoi(filter_string);    if ((fhd->common.ndrec != -1 && i > fhd->common.ndrec) || (i <= 0))      ERROR_EXIT("specified a non-existing filter record, check -f option");    fea_skiprec(filfile, (long) (i-1), fhd);    if (get_feafilt_rec(filt_rec, fhd, filfile) == EOF)      ERROR_EXIT("can't read specified filter record, check -f option");  }  else{    Sprintf(nsiz_name, "%s_nsiz", filter_string);    Sprintf(dsiz_name, "%s_dsiz", filter_string);    Sprintf(num_name, "%s_num", filter_string);    Sprintf(den_name, "%s_den", filter_string);    Sprintf(psiz_name, "%s_psiz", filter_string);    Sprintf(zsiz_name, "%s_zsiz", filter_string);    Sprintf(pole_name, "%s_poles", filter_string);    Sprintf(zero_name, "%s_zeros", filter_string);    Sprintf(gain_name, "%s_gain", filter_string);    if( symtype(psiz_name) != ST_UNDEF) psiz = getsym_i(psiz_name);    if( symtype(zsiz_name) != ST_UNDEF) zsiz = getsym_i(zsiz_name);    if( symtype(gain_name) != ST_UNDEF) gain = getsym_d(gain_name);    if( symtype(nsiz_name) != ST_UNDEF ) nsiz = getsym_i(nsiz_name);    if( symtype(dsiz_name) != ST_UNDEF) dsiz = getsym_i(dsiz_name);    if( symtype(num_name ) != ST_UNDEF || symtype(nsiz_name) != ST_UNDEF){      num_coeff = (double *) malloc(nsiz * sizeof(double));      spsassert(num_coeff,"main: num_coeff maolloc failed");      if( nsiz != getsym_da(num_name, num_coeff, nsiz)) 	ERROR_EXIT("inconsistent numerator coefficient size");    }    if( symtype(den_name ) != ST_UNDEF && symtype(dsiz_name) != ST_UNDEF){      den_coeff = (double *) malloc(dsiz *sizeof(double));      spsassert(den_coeff,"main: den_coeff malloc failed");      if( dsiz != getsym_da(den_name, den_coeff, dsiz)) 	ERROR_EXIT("inconsistent denomenator coefficient size");    }    if( symtype(pole_name) != ST_UNDEF && symtype(psiz_name) != ST_UNDEF){      parry = (double *) malloc(2*psiz*sizeof(double));      spsassert(parry, "main: parry malloc failed");      if( psiz*2 != getsym_da(pole_name, parry, 2*psiz)) 	ERROR_EXIT("inconsistent poles size");    }    if( symtype(zero_name) != ST_UNDEF && symtype(zsiz_name) != ST_UNDEF){      zarry = (double *) malloc(2*zsiz*sizeof(double));      spsassert(zarry,"main: zarry maolloc failed");      if( zsiz*2 != getsym_da(zero_name, zarry, 2*zsiz)) 	ERROR_EXIT("inconsistent zeros size");    }    if(!( (zarry && parry ) || ( !zarry && !parry && den_coeff && num_coeff)))      ERROR_EXIT("Full zeros/poles or full denominator/numerator coeff should be specified in the parameter file, or check the -f argument.");    /* IIR gain is the same as 1st element in numerator coefficient array */    if( zarry && parry ) {      if(num_coeff == NULL){	nsiz = 1;	num_coeff = (double *) malloc(nsiz * sizeof(double));	spsassert(num_coeff,"main: num_coeff maolloc for gain failed");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?