⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testsd.c

📁 speech signal process tools
💻 C
字号:
/* * This material contains proprietary software of Entropic Speech, Inc. * Any reproduction, distribution, or publication without the prior * written permission of Entropic Speech, Inc. is strictly prohibited. * Any public distribution of copies of this work authorized in writing by * Entropic Speech, Inc. must bear the notice * *    "Copyright (c) 1986, 1987 Entropic Speech, Inc.; All rights reserved" * * Program:	testsd	 * * Written by:  John Shore * Checked by:  Alan Parker * * This is the testsd program, which generates test data and puts it into * an SD file.  Either a sine wave, Gaussian noise, or a pulse train can * be generated.  Data can also be taken from an ASCII file */#ifndef lintstatic char *sccs_id = "@(#)testsd.c	1.4	4/16/87 ESI";#endif/* * include files */#include <sps/sps.h>#include <sps/sd.h>#include <stdio.h>#include <assert.h>/* * defines */#define Fprintf (void)fprintf#define ABS(a) (((a) >= 0) ? (a) : (- a))#define SYNTAX USAGE ("testsd [-x debug_level] [-f frequency] [-l level] [-r sampling_rate] [-{ps} length] [-t type] [-a asciifile] [-S seed] [-i] sdfile")#define MAXLINE 512	/*max line length for input ascii file*/#define PI 3.14159265#define SEED 1234567	/*seed for random number generator*//* defines for test data type*/#define SINE 0#define GAUSS 1#define PULSES 2#define ASCII 3/* * system functions and variables */int getopt ();extern  optind;extern	char *optarg;void perror();void exit();double	atof(), sin();int atoi();long atol();char *strcpy();int srandom();void rewind();char *calloc();char *realloc();char *strtok();char *fgets();/* * external SPS functions */char *get_cmd_line();void set_sd_type(), put_sd_recf(), write_header();int add_comment();float gauss();float *atoarrayf();/* * global declarations */int debug_level = 0;char *program = "testsd";char *version = "1.4";char *date = "4/16/87";char *test_types[] = {"SINE", "GAUSS", "PULSES", "ASCII", NULL};/* * main program */main (argc, argv)int argc;char **argv;{/* * setup and initialization */float gauss();float twopi = 2 * PI;int c;			    /*for getopt return*/int i;			    /*loop counter*/char *sd_file = NULL;	    /*file name for output SD file*/FILE *ostrm = stdout;	    /*output SD file stream*/char *infile = NULL;	    /*file name for input ASCII file*/FILE *instrm = stdin;struct header *oh;	    /*pointer to SD file header*/short data_format = FLOAT;  /*data type for SD file*/int freq_flag = 0;	    /*flag for -f option*/int sec_flag = 0;	    /*flag for -s option*/int pnt_flag = 0;	    /*flag for -p option*/int a_flag = 0;		    /*flag for -a option*/int t_flag = 0;		    /*flag for -t option*/float freq = 500;	    /*frequency of sine wave or pulse train*/float srate = 8000;	    /*sampling rate of SD file*/float level = 500;	    /*level of test data (interpretation differs with type)*/long points = 8000;	    /*number of points to generate*/int interval;		    /*number of points between pulses*/float delta;		    /*time interval between sampled data points*/float seconds;		    /*number of seconds of data to generate*/float pulse_amp;	    /*amplitude of pulse train*/float rms_amp;		    /*rms amplitude of sine wave or Gaussian noise*/float sine_amp;		    /*amplitude of sine wave*/int type = SINE;	    /*type of test data*/float maxval = 0;	    /*maximum value of generated test data*/long seed = SEED;	    /*seed for random numbers*/float *sdata;		    /*array to hold test data*/float value;		    /*used as temp storage for Gauss value*//* * process command line options */    if (debug_level) Fprintf (stderr, "testsd: processing options\n");    while ((c = getopt (argc, argv, "x:f:l:r:p:s:T:S:a:i")) != EOF) {	switch (c) {	    case 'x': 		debug_level = atoi (optarg);		break;	    case 'f':		freq = atof(optarg);		freq_flag++;		break;	    case 'l':		level = atof(optarg);		break;	    case 'r':		srate = atof(optarg);		break;	    case 'p':		points = atol(optarg);		pnt_flag++;		break;	    case 's':		seconds = atof(optarg);		sec_flag++;		break;	    case 'T':		t_flag++;		if((type = lin_search(test_types, optarg)) == -1) 		    {			Fprintf(stderr, "testsd: unknown test data type %s\n",			    optarg);			exit(1);		    }		if (debug_level) 		    Fprintf (stderr, "testsd: type = %s\n", test_types[type]);		break;	    case 'S':		seed = atol(optarg);		break;	    case 'a':		infile = optarg;		a_flag++;		type = ASCII;		break;	    case 'i':		data_format = SHORT;		break;	    default:		SYNTAX;	}    }/* * process file argument and open output file */    if (optind < argc) {	sd_file = argv[optind++];	if (strcmp (sd_file, "-") == 0)	    sd_file = "<stdout>";	else	    TRYOPEN (argv[0], sd_file, "w", ostrm);	}    else {	Fprintf(stderr, "testsd: no output file specified.\n");	SYNTAX;        }/* * open input ASCII file for -a option */    if (a_flag) {	if (strcmp (infile, "-") == 0)	    infile = "<stdout>";	else 	    TRYOPEN(argv[0], infile, "r", instrm);    }/* * check for inconsistencies */    if (pnt_flag && sec_flag) {	Fprintf(stderr, "testsd: can't use both -p and -s\n");	exit (1);    }    if (freq_flag && (type == GAUSS)) {	Fprintf(stderr, "testsd: can't use -f with type gauss\n");	exit(1);    }    if (a_flag && (pnt_flag || sec_flag)) {	Fprintf(stderr, "testsd: can't use -p or -s with -a\n");	exit(1);    }    if (a_flag && t_flag) {	Fprintf(stderr, "testsd: can't use -t with -a\n");	exit(1);    }/* * complete initialization based on options */    /*compute number of points and allocate zeroed data array*/    if (sec_flag) points = seconds * srate;    if (debug_level && !a_flag) Fprintf(stderr, "number of points is %d\n", points);    if (!a_flag) {	sdata = (float*) calloc ((unsigned)points, sizeof(float));	spsassert(sdata != NULL, "testsd: couldn't allocate enough memory");    }/* * generate test data */    switch (type) {	case SINE:	    if (debug_level) Fprintf (stderr, 		"testsd: computing %d points of a sine wave, freq = %g\n", 		points, freq);	    /*compute sine wave amplitude*/	    sine_amp = level;	    maxval = sine_amp;	    /*compute time interval between SD points*/	    delta = 1 / srate;	    /*fill in data array*/	    for (i = 0; i < points; i++) 		sdata[i] = sine_amp * sin(twopi * i * freq * delta);	    break;	case GAUSS:	    /*set rms amplitude*/	    rms_amp = level;	    if (debug_level) Fprintf (stderr, 		"testsd: computing %d points of %g rms amp. Gaussian noise\n", 		points, rms_amp);	    /*fill in data array*/	    (void) srandom(seed);	    for (i = 0; i < points; i++) {		value = rms_amp * gauss();		sdata[i] = value;		if (ABS(value) > maxval) maxval = ABS(value);	    }	    break;	case PULSES:	    /*set pulse amplitude*/	    pulse_amp = level;	    maxval = pulse_amp;	    /*compute number of points between pulses*/	    interval = (int) (srate / freq);	    if (debug_level) Fprintf (stderr, 		"testsd: computing %d points, pulses spaced %d apart\n", 		points, interval);	    /*compute pulse positions and fill in*/	    i = 0;	    while (i < points) {		sdata[i] = pulse_amp;		i = i + interval;    	    }	    break;	case ASCII:	    sdata = atoarrayf(instrm, &points, &maxval);	    if (debug_level) Fprintf(stderr, 		"testsd: read %d points from ASCII file %s\n", points,		infile);	    break;	default:	    Fprintf(stderr, "testsd: unknown test data type\n");	    exit(1);	    break;    }/* * write header of output file */    oh = new_header(FT_SD);    (void) strcpy (oh->common.prog, program);    (void) strcpy (oh->common.vers, version);    (void) strcpy (oh->common.progdate, date);    (void) add_comment (oh, get_cmd_line(argc,argv));    oh->common.tag = NO;    oh->common.ndrec = points;    oh->hd.sd->sf = srate;    oh->hd.sd->src_sf = 0;    oh->hd.sd->dcrem = 0;    oh->hd.sd->nchan = 1;    oh->hd.sd->equip = NONE;    oh->hd.sd->scale = 0;    oh->hd.sd->max_value = maxval;    *add_genhd_e("test_type", NULL, 1, test_types, oh) = type;    if (type == GAUSS) *add_genhd_l("seed", NULL, 1, oh) = seed;    (void) set_sd_type (oh, data_format);    (void) write_header(oh, ostrm);/* * write data in output file */    (void) put_sd_recf(sdata, (int) points, oh, ostrm);/* * put info in ESPS common */    if (strcmp(sd_file, "<stdout>") != 0) {	(void) putsym_s("filename", sd_file);	(void) putsym_s("prog","testsd");	(void) putsym_i("start", (int) 1);	(void) putsym_i("nan", (int) points);    }/* * clean up and exit */    exit(0);}

⌨️ 快捷键说明

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