scatplot.c

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

C
841
字号
/* * 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-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:  Ajaipal S. Virdy * Checked by: * Revised by: * * Generate a scatter plot. * */static char *sccs_id = "@(#)scatplot.c	3.8	1/27/97	ESI/ERL";#include <stdio.h>#include <esps/esps.h>#include <esps/unix.h>#include <esps/fea.h>#include <esps/esignal_fea.h>#define SYNTAX \USAGE("scatplot [-e elements] [-r range] [-s symbols] [-t text] [-x debug]\n\ [-H text] [-V text] [-X range] [-Y range] file1 [file2] ...")#define TRYALLOC(type, num, var, name) { \    if (((var) = (type *) malloc((unsigned)(num)*sizeof(type)))==NULL) \    { Fprintf(stderr, "%s:  can't allocate memory for %s.\n", \		ProgName, (name)); \	exit(1); }}#define Printf (void) printf#define MAXTEXT 1/* * The following defines are the coordinates of the upper left hand corner * and the lower right corner which define the drawing frame. * * The coordinate system is defined as follows: * *		 *		--------------------------------- *		|(0,0)		      (0,lowerx)| *		|				| *		|				| *		|				| *		|	(y1,x1)			| *		|				| *		|				| *		|(lowery,0)	 (lowery,lowerx)| *		--------------------------------------> increasing x *		| *		| *		v increasing y * */#define	DEF_UPPER_X 600		/* this number shouldn't be modified */#define	DEF_UPPER_Y 600		/* this number shouldn't be modified */#define	DEF_LOWER_X 6000	/* this can be adjusted */#define	DEF_LOWER_Y 4100	/* this can be adjusted, also */#define	CHAR_SIZE 35#define	COM_CHARS 157	/* maximum number of characters that fit on a line */#define	ODD(n)	(((n/2) * 2) != n)char	    *eopen();void	    addstr();double	    **d_mat_alloc();char	    *get_cmd_line();void	    frange_switch();void	    lrange_switch();long	    *grange_switch();void	    plotscale();void	    plotexscale();char	    *savestring();short	    get_rec_len();char        *e_temp_name();static void	pr_darray(), pr_larray();extern int	optind;		/* used by getopt() */extern char	*optarg;	/* used by getopt() */char	    *ProgName = "scatplot";int		debug_level = 0;/* * Main Program */main(argc, argv)    int	    argc;    char    **argv;{    int		c;		/* command-line option letter */    /*     *  C O M M A N D     *  L I N E     *  O P T I O N S     */    char	*erange;	/* -e option argument */    int		eflag = NO;	/* -e option specified? */    long	*elem_array;	/* element numbers specified with -e */    long	nelem;		/* number of elements specified with -e */    char	**rrange;	/* -r option arguments */    int		rnum = 0;	/* number of -r options specified */    long	*nans;		/* number of records in range for each file */    long	*ndrec;		/* number of records in each file */    int		Xflag = NO;	/* -X option specified? */    int		Yflag = NO;	/* -Y option specified? */    char	*symbol = "xo*abcdefghijklmnpqrstuvwyz";				/* plotting symbols */    char	*text[MAXTEXT];	/* text strings specified with -t option */    int		ntext = 0;	/* number of -t options specified */    int		tflag = NO;	/* -t option specified? */    char	*Htext = "";	/* text specified with -H option */    char	Hflag = NO;	/* -H option specified? */    char	*Vtext = "";	/* text specified with -V option */    char	Vflag = NO;	/* -V option specified? */    /*     * I N P U T     * F I L E S     *     */    char	**files;	/* file names */    FILE	**filep;	/* file pointers */    int		i_file;		/* file number (loop index) */    int		nfiles;		/* number of input files */    struct header **h;		/* file headers */    int		si_read = NO;	/* had stdin been copied to temp file? */    char	*si_temp;	/* temp file for stdin records */    struct header		*si_hdr;	/* header read from stdin */    long	si_nrec;	/* number of records read from stdin */    /*     * T E M P O R A R Y     * V A R I A B L E S     *     */    int		i;		/* loop index */    int		xdp, ydp;	/* number of decimal places for axis labels */    double	*data;		/* buffer for input data */    double	**xdata;	/* vectors of x data from each file */    double	**ydata;	/* vectors of y data from each file */    double	x_min, xlow;	/* min x data value, x-axis lower limit */    double	x_max, xhigh;	/* max x data value, x-axis upper limit */    double	y_min, ylow;	/* min y data value, y-axis lower limit */    double	y_max, yhigh;	/* max y data value, y-axis upper limit */    double	xstep, ystep;	/* spacing between axis labels */    /*     *  P L O T T I N G     *   V A R I A B L E S     */    int		upperx = DEF_UPPER_X;	/* plotter coord of x-axis right end */    int		uppery = DEF_UPPER_Y;	/* plotter coord of y-axis top */    int		lowerx = DEF_LOWER_X;	/* plotter coord of x-axis left end */    int		lowery = DEF_LOWER_Y;	/* plotter coord of y-axis bottom */    int		nchars;		/* length of H or V text string */    long	tag;		/* tag value read from input record */    char	command_line[300];  /* command line to print above plot*/    char	txtbuf[300];	/* used when command_line must be broken */    char	*tmpptr;	/* pointer into txtbuf *//* parse command line for parameters */    Sprintf(command_line, "%% %s", get_cmd_line(argc, argv));    while ((c = getopt(argc, argv, "e:r:s:t:x:H:V:X:Y:")) != EOF)	switch (c)         {	case 'e':	    erange = optarg;	    if (eflag) {		Fprintf(stderr,		    "%s: please give only one -e option.\n", ProgName);		exit(1);	    }	    elem_array = grange_switch(erange, &nelem);	    if (ODD(nelem)) {		Fprintf(stderr,		    "%s: -e option: even number of elements required.\n",		ProgName);		exit(1);	    }	    eflag = YES;	    break;	case 'r':	    if (rnum == 0)	    {		TRYALLOC(char *, 1, rrange, "-r option arguments")		rrange[0] = NULL;	    }	    addstr(optarg, &rrange);	    rnum++;	    break;	case 's':	    symbol = optarg;	    break;	case 't':	    tflag = YES;	    if (ntext < MAXTEXT)		text[ntext++] = optarg;	    else	    {		Fprintf(stderr, "plotsd: Too many -t options\n");		exit(1);	    }	    break;	case 'x':	    debug_level = atoi(optarg);	    break;	case 'H':	    Htext = optarg;	    Hflag = YES;	    break;	case 'V':	    Vtext = optarg;	    Vflag = YES;	    break;	case 'X':	    frange_switch(optarg, &x_min, &x_max);	    Xflag = YES;	    break;	case 'Y':	    frange_switch(optarg, &y_min, &y_max);	    Yflag = YES;	    break;	default:	    SYNTAX	    ;	}    if (!eflag) {	Fprintf(stderr,	    "%s: please specify an element range with the -e option.\n",	    ProgName);	SYNTAX	;    }/* miscellaneous initializations and consistency checks */    nfiles = argc - optind;    if (nfiles == 0)    {	Fprintf(stderr, "%s: no input file specified.\n", ProgName);	SYNTAX	;    }    if (debug_level >= 1)    {	Fprintf(stderr, "%d input files.\n", nfiles);	Fprintf(stderr, "Allocating storage for 7 arrays.\n");    }    TRYALLOC(long, nfiles, ndrec, "number of data records in each file")    TRYALLOC(long, nfiles, nans, "number of records in range for each file")    TRYALLOC(char *, nfiles, files, "file names")    TRYALLOC(FILE *, nfiles, filep, "file pointers")    TRYALLOC(struct header *, nfiles, h, "file headers")    TRYALLOC(double *, nfiles, xdata, "pointers to x data vectors")    TRYALLOC(double *, nfiles, ydata, "pointers to y data vectors")    for (i = 0; i < nelem; i++)	elem_array[i] -= 1;	/* element numbers to data array indices */    if (nelem < 2*nfiles)    {	if (debug_level >= 1)	    Fprintf(stderr, "Extending elem_array.\n");	if (( elem_array =		(long *) realloc((char *) elem_array, 2*nfiles*sizeof(long)) )	    == NULL)	{	    Fprintf(stderr, "%s: can't reallocate storage.\n", ProgName);	    exit(1);	}	for (i = nelem; i < 2*nfiles; i += 2)	{	    elem_array[i] = elem_array[nelem - 2];	    elem_array[i + 1] = elem_array[nelem - 1];	}    }    if (debug_level >= 1)	pr_larray("element numbers (decremented)", 2*nfiles, elem_array);    if (strlen(symbol) < nfiles)    {	Fprintf(stderr,	    "%s: please specify %d symbols with the -s option.\n",	    ProgName, nfiles);	exit(1);    }/* open files, read headers, count records, *//* copy to temp file if necessary. */    if (debug_level >= 1)	Fprintf(stderr, "Opening files.\n");    for (i_file = 0; i_file < nfiles; i_file++, optind++)    {	if (debug_level >= 2)	    Fprintf(stderr,		"File number %d (%s).\n", i_file + 1, argv[optind]);	if (strcmp(argv[optind], "-") == 0 && si_read)	{	    if (debug_level >= 2)		Fprintf(stderr,		    "File is <stdin>.  Opening temp file %s.\n",		    si_temp);	    files[i_file] = "<stdin>";	    TRYOPEN(ProgName, si_temp, "r", filep[i_file]);	    h[i_file] = si_hdr;	    ndrec[i_file] = si_nrec;	}	else	{	    files[i_file] =		eopen(ProgName, argv[optind], "r", NONE, NONE,					&h[i_file], &filep[i_file]);	    /*	     * Check for complex data	    */	    if(is_file_complex(h[i_file]) != 0){	      Fprintf(stderr, "scatplot: %s contains complex data.\n\tComplex data files are not yet supported.\n", files[i_file]);	      exit(1);	    }	    if (debug_level >= 2)		Fprintf(stderr, "File is %s.\n", files[i_file]);	    ndrec[i_file] = h[i_file]->common.ndrec;	    if (ndrec[i_file] == -1 || filep[i_file] == stdin		|| get_esignal_hdr(h[i_file]))	    {		char	*tmpname;   /* name of temporary file */		FILE	*tmpstrm;   /* temp file pointer */		long	nrec;	    /* number of records read */		tmpname = e_temp_name("scatplXXXXXX");		TRYOPEN(ProgName, tmpname, "w+", tmpstrm);		if (debug_level >= 2)		    Fprintf(stderr,			    "Copying records to temp file %s.\n", tmpname);		if (h[i_file]->common.type == FT_FEA)				/* FEA file or non-ESPS converted to FEA */		{		    struct fea_data *tmprec; /* input data record */		    struct header   *tmphd;		    tmprec = allo_fea_rec(h[i_file]);		    if (tmprec == NULL)		    {			Fprintf(stderr, "%s: can't allocate memory for %s.\n",				ProgName, "temporary FEA record");			exit(1);		    }		    write_header(copy_header(h[i_file]), tmpstrm);		    rewind(tmpstrm);		    tmphd = read_header(tmpstrm);		    rewind(tmpstrm);		    if (debug_level >= 2)			Fprintf(stderr,			    "Allocated space for FEA record.\n");		    if (debug_level >= 3)			Fprintf(stderr, "Reading records:");		    nrec = 0;		    while (get_fea_rec(tmprec, h[i_file], filep[i_file])			   != EOF)		    {			put_fea_rec(tmprec, tmphd, tmpstrm);			nrec++;

⌨️ 快捷键说明

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