mlplot.c

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

C
1,298
字号
/* * 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:  Rodney W. Johnson, Ajaipal S. Virdy * Checked by: * Revised by: * * Plot ESPS Sampled-Data files in multiline format. * * Plots ESPS Sampled-Data files at fixed x-axis scale with * continuation on additional lines as needed to accommodate the data. * Permits integral number of device resolution units per unit. */static char *sccs_id = "@(#)mlplot.c	3.14	1/18/97	ESI/ERL";#include <stdio.h>#include <esps/unix.h>#include <esps/esps.h>#include <esps/fea.h>#include <esps/feasd.h>#include "mlplot.h"#include <esps/esignal_fea.h>#define	PMODE	0640		/* default directory protection */#define M_CONN 1		/* default: connected lines on plot */#define M_POINT 2		/* plot individual data points */#define M_VERT 3		/* plot vertical lines connected to x-axis */#define NOAXIS 0		/* do not draw x-axis */#define AXIS 1			/* draw x-axis */#define CHR_GRID_TOP 7#define CHR_GRID_BOT -3#define CHR_GRID_RT 6#define LBL_MAX 50/* * The following are *default* settings for the Imagen Laser Printer * with 300 dots/inch. Note: some of the settings can be changed by * command line options. * * U and V correspond to X and Y axes, respectively. * * The drawing frame is the rectangular box in which the graph is * plotted. * */#define DEF_PAGEHEIGHT 2400	/* pageheight set to 8 inches */#define DEF_PAGEWIDTH 3000	/* pagewidth set to 10.0 inches */#define DEF_DELTAV 400		/* height of drawing frame (rectangular box) */#define DEF_DELTAU 2400		/* width of drawing frame */#define DEF_U0 345		/* origin location for first frame */#define DEF_V0 1675		/* origin location for first frame */#define DEF_VSHIFT 500		/* difference between each frame origin */#define DEF_XTICINT 100		/* mark tics on abscissa every 100th sample */#define DEF_Y_SUBDIV 4		/* divide ordinate into 4 intervals */#define DEF_XSCALE 4		/* 4 pixels/point */#define DEF_MAXLINES 4		/* draw 4 frames on a page */#define TAG_MAXLINES 3		/* draw 3 frames on a page */#define TAG_EXTRAV 144		/* leave extra space between frames for TAG */#define DEF_TTL_CHARSP 30	/* title character spacing */#define DEF_COM_CHARSP 20	/* command line character spacing */#define DEF_LBL_CHARSP 24	/* label character spacing */#define DEF_TAG_CHARSP 24	/* tag character spacing */#define DEF_X_TTLBASE 75	/* x title base */#define DEF_Y_TTLBASE 35	/* y title base */#define DEF_HDRBASE 2135	/* header base *//* * The following defines apply to MASSCOMP Universe coordinate system. */#define	MC_UNIVERSE	65535#define	MC_REGION	MC_UNIVERSE/5#define	MC_U_ORIGIN	-31767		/* horizontal coordinate */#define	MC_V_ORIGIN	-21592		/* vertical coordinate */#define	MAX_PAGES	25		/* only 25 regions can be displayed */#define	MC_U0	-31767#define	MC_V0	-31767/* miscellanuous define's */#define	MAX_FILES   10#define	COM_CHARS   150		/* No. of characters of command line to */				/* put on one printed line. */#define MAX_COM_LEN 2*COM_CHARS	/* Fit command on two printed lines. */#define COM_FMT	    "%% %.298s"	/* Truncate string to MAX_COM_LEN, */				/* allowing two chars for initial "% ". *//* * MACROS used in this program: */#define ERROR(text) {Fprintf(stderr, "%s: %s\n", ProgName, text); exit(1);}#define ERROR2(text1,text2) {Fprintf(stderr, "%s: %s %s\n", \			ProgName, text1, text2); exit(1);}#define SYNTAX USAGE( \"mlplot [-l int][-m{cpv}][-n][-o outdir][-{pr} range][-s start]\n\[-t title][-x debug level][-y range][-z][-L file.esps][-N]\n\[-T device][-V title][-X scale][fil1.sd ... filen.sd]")#define TRYALLOC(type,num,var) { \    if (((var) = (type *) malloc((unsigned)(num)*sizeof(type)))==NULL) \    { Fprintf(stderr, "%s:  can't allocate memory for %d points.\n", \		ProgName, (int)(num)); \	exit(1); }}/* ESPS functions referenced */int	getopt();void	lrange_switch();void	frange_switch();short	get_rec_len();int	get_gen_recd();char	*get_cmd_line();double	*allo_gen_recd();char	*arr_alloc();char    *e_temp_name();/* G L O B A L   V A R I A B L E S */char	*ProgName = "mlplot";int	page_num;	/* current page (region) being printed */char	*outdir = NULL;	/* write pages in this directory */FILE	*outfp;		/* file pointer for storing pages in directory */int	nflag = 0;	/* suppress output to stdout if this flag's set */int	num_of_files;	/* number of sampled data files to plot */char	*device = "gps"; /* default output format */int	gps = YES;	/* gps format (default) */int	tek = NO;	/* tektronix format (incl. imagen) */int	imagen = NO;	/* imagen format */int	debug_level = 0;long	**u;		/* x-coordinate value to plot in a drawing frame */long	**v;		/* y-coordinate value to plot in a drawing frame */double 	**data;  	/* array to store records read from			   ESPS sampled data files */double	*ylow;		/* minimum amplitude */double	*yhigh;		/* maximum amplitude */double	*yticint;	/* interval for marking tics on y-axis */double	*yscale;	/* y-axis scaling/division */long	P0 = MC_U0;long	Q0 = MC_V0;char	*env_bundle;int	gps_bundle = 20;/* plotting functions */int	gps_plotline();int	tek_plotline();void	tek_plotpoints();/* pointer to determine which function to use for plotting */int	(*plotting_func)();/* * B E G I N *  M A I N *   P R O G R A M */main(argc, argv)    int	    argc;    char    *argv[];{    extern int	optind;		/* used by getopt() */    extern char	*optarg;	/* used by getopt() */    int		ch;		/* used for parsing command line */    FILE	*sdfile[MAX_FILES];    char	*sdfilename[MAX_FILES];    int		isfile = NO;    /* is input a pipe? */    char	*sdname = NULL;    FILE	*tagfile = NULL;    char	*tagfilename = NULL;    int		Lflag = 0;    struct header *hd[MAX_FILES];    struct header *th;    double	*tag_rec;    /*     *  C O M M A N D     *    L I N E     *      O P T I O N     *     *  V A R I A B L E S     *     */    char	*prange = NULL;	/* range of samples to plot */    long	firstrec;	/* first record to plot */    long	lastrec;	/* last record to plot */    long	nrec;		/* number of records to plot */    long	nread;		/* number of records read from file */    char 	*yrange = NULL;	/* ampliude range */    int		yflag = 0;    int		ahi_flag = 0,		alo_flag = 0;    double	alow = -DBL_MAX;    /* lower amplitude limit */    double	ahigh = DBL_MAX;    /* upper amplitude limit */    long	start_index = 0;    /* offset starting index */    long	start[MAX_FILES];   /* offset starting record */    int		mode = M_CONN;	/* default mode for plotting points */    int		axflag = AXIS;	/* default: plot x-axis (y=0) */    int		Nflag = 0;    /*     *  I N D I C E S     *      F O R     *  A R R A Y S     *    A N D     *    L O O P S     */    long	i = 0;		/* temporary index */    long	indx;		/* index to data */    long	tag;    int		i_file;		/* temporary file index */    long	x;		/* temporary index */    long	xmin;		/* starting sample number for a frame */    long	xmax;		/* number of samples to plot in a frame */    long	xlow;		/* sample number at beginning of each frame */    long	xinc;		/* no. of samples to increment for each frame */    int		tag_eof;	/* flag for end-of-tagged-file */     int		nlines = 0;	/* frame number being plotted on a page */    int		line_num = 0;	/* frame number being plotted on a page */		/* number of frames to plot on a page */    int		maxlines = DEF_MAXLINES;    /*     *  V A R I A B L E S     *     U S E D      *      F O R     *   P L O T T I N G     *     */    long	u0 = DEF_U0;	/* x-coordinate of drawing frame */    long	v0 = DEF_V0;	/* y-coordinate of drawing frame */    long	xticint = DEF_XTICINT;    long	xscale = DEF_XSCALE;    long	ulow;		/* left border of drawing frame */    long	vlow;		/* bottom border of drawing frame */    long	old_vlow;	/* temporary variable */    long	deltav = DEF_DELTAV;    long	deltau = DEF_DELTAU;		/* distance between frames on a page */    long	vshift = DEF_VSHIFT;    		/* character size for labeling text */    long	lbl_charsp = DEF_LBL_CHARSP;    long	tag_charsp = DEF_TAG_CHARSP;		/* variables for header information */    long	hdrleft = 0;    long	hdrright = DEF_PAGEWIDTH;    long	hdr_charsp = DEF_TTL_CHARSP;    long	hdrbase = DEF_HDRBASE;    long	x_ttl_center = DEF_PAGEWIDTH/2;    long	x_ttl_base = DEF_X_TTLBASE;    long	x_ttl_charsp = DEF_TTL_CHARSP;    long	y_ttl_center = DEF_PAGEHEIGHT/2;    long	y_ttl_base = DEF_Y_TTLBASE;    long	y_ttl_charsp = DEF_TTL_CHARSP;    char	*x_ttl_text =  "";  /* text string for labeling abscissa */    char	*y_ttl_text =  "";  /* text string for labeling ordinate */    /*     * M I S C E L L A N E O U S     *   V A R I A B L E S     *     */    double	delta_y;    int		continue_plotting = YES;    char	command_line[MAX_COM_LEN+1];	/* "+1" for termnal null */    char	*file_text;    long	x_com_charsp = DEF_COM_CHARSP;    double	sample_range;    char	comtxt[COM_CHARS+1];	/* "+1" for termnal null */		/* function to perform device initialization */    void	initialize();    /* Initialize `start' array to ones before -s options encountered. */    for (i_file = 0; i_file < MAX_FILES; i_file++)	start[i_file] = 1;    Sprintf(command_line, COM_FMT, get_cmd_line(argc, argv));    /* Parse command line.  Get options and determine input file. */    while ((ch = getopt(argc, argv, "l:m:no:p:r:s:t:x:y:zL:NT:V:X:")) != EOF)	switch (ch)	{	case 'l':	    xticint = atol(optarg);	    break;	case 'm':	    switch (*optarg)	    {	    case 'p':		mode = M_POINT;		break;	    case 'c':		mode = M_CONN;		break;	    case 'v':		mode = M_VERT;		break;	    default:		SYNTAX;		break;	    }	    break;	case 'n':	    nflag++;	    break;	case 'o':	    outdir = optarg;	    break;	case 'p':	case 'r':	    prange = optarg;	    break;	case 's':	    start[start_index++] = atol(optarg);	    break;	case 't':	    x_ttl_text = optarg;	    break;	case 'x':	    debug_level = atoi(optarg);	    break;	case 'y':	    yrange = optarg;	    yflag++;	    break;	case 'z':	    axflag = NOAXIS;	    break;	case 'L':	    tagfilename = optarg;	    Lflag++;	    break;	case 'N':	    Nflag++;	    break;	case 'T':	    device = optarg;	    break;	case 'V':	    y_ttl_text = optarg;	    break;	case 'X':	    xscale = atol(optarg);	    break;	default:	    SYNTAX;	    break;	}    num_of_files = argc - optind;    if(debug_level) fprintf(stderr,"num_of_files: %d\n",num_of_files);    /* Read the headers. */    for (i_file = 0; i_file < num_of_files; i_file++, optind++)    {	if (strcmp(argv[optind], "-") == 0)	{	    if (i_file == 0)	    {		sdfilename[0] = "<stdin>";		eopen(ProgName, "-", "r", FT_FEA, FEA_SD,			&(hd[0]), &(sdfile[0]));	    }	    else		ERROR("Only first sample data file may be standard input.")	}	else	{	    sdfilename[i_file] = argv[optind];	    eopen(ProgName, sdfilename[i_file], "r", FT_FEA, FEA_SD, 		  &(hd[i_file]), &(sdfile[i_file]));	}	if (get_fea_siz("samples", hd[i_file], (short) NULL, 			(long) NULL) != 1) {	    Fprintf(stderr, 		    "%s: sorry, can't work on multi-channel files (%s)\n",		    ProgName, sdfilename[i_file]);	    exit(1);	}	if (debug_level > 1)	{	    Fprintf(stderr, "%s: sdfilename[%d] is %s\n",	    ProgName, i_file, sdfilename[i_file]);	    (void) fflush(stderr);	}    }    if (tagfilename != NULL)    {	if (strcmp(tagfilename, "-") != 0)	    {TRYOPEN(ProgName, tagfilename, "r", tagfile)}	else if (sdfile[0] != stdin)	{	    tagfilename = "<stdin>";	    tagfile = stdin;	}	else	{	    ERROR("Tagged file and sampled-data file cannot both be standard input.")	}	if (debug_level > 0)	    Fprintf(stderr,	    "%s: Tagged file specified: %s\n", ProgName, tagfilename);	maxlines = TAG_MAXLINES;	v0 -= TAG_EXTRAV + LROUND(10 * hdr_charsp / 6.0);	vshift += TAG_EXTRAV;	if ((th = read_header(tagfile)) == NULL)	{	    NOTSPS(ProgName, tagfilename)	}	if (get_esignal_hdr(th))	{	    ERROR("Esignal format not supported for tagged file.")	}			if (!th->common.tag)	{	    ERROR2(tagfilename, "is not tagged.")	}	if (num_of_files == 0)	{	    if (strcmp(th->variable.source[0], "<stdin>") == 0)	    {		ERROR2(tagfilename, "source was <stdin>.")	    }	    sdfilename[0] = th->variable.source[0];	    if (debug_level > 0)		Fprintf(stderr,		"%s: Sampled Data file specified: %s\n",		ProgName, sdfilename[0]);	    num_of_files = 1;	    eopen(ProgName, sdfilename[0], "r", FT_FEA, FEA_SD, 		  &(hd[0]), &(sdfile[0]));	    if (get_fea_siz("samples", hd[0], (short)NULL, (long)NULL) != 1) {	      Fprintf(stderr, 		      "%s: sorry, can't work on multi-channel files (%s)\n",		      ProgName, sdfilename[0]);	      exit(1);	    }	}    }    if (num_of_files == 0)	SYNTAX;    if (debug_level > 0)	Fprintf(stderr,	"%s: number of ESPS Sampled Data files to process: %d\n",	ProgName, num_of_files);    if (num_of_files == 1)	sdname = sdfilename[0];    initialize();    if (gps)    {	env_bundle = getenv("BUNDLE");	if (env_bundle != NULL) gps_bundle = atoi(env_bundle);    }    /* get the range to be plotted */    assert(hd[0]);    if (hd[0]->common.ndrec != -1) isfile++;    if (debug_level > 0)	Fprintf (stderr,	"%s: first file will be read from a %s.\n",	ProgName, (isfile ? "file" : "pipe"));    firstrec = start[0];    if (isfile)	lastrec = hd[0]->common.ndrec + start[0] - 1;    else	lastrec = LONG_MAX;    lrange_switch(prange, &firstrec, &lastrec, 0);    if (lastrec < firstrec) {	Fprintf(stderr, "%s: bad range given, last record < first record.\n",		ProgName);	exit(1);    }    for (i_file = 0; i_file < num_of_files; i_file++)	if (firstrec < start[i_file]) 	    firstrec = start[i_file];    if (isfile)	if (lastrec > hd[0]->common.ndrec + start[0] - 1) 	    lastrec = hd[0]->common.ndrec + start[0] - 1;    if (debug_level > 1)	Fprintf(stderr,	    "%s: firstrec = %ld, lastrec = %ld.\n",	    ProgName, firstrec, lastrec);    (void) fflush(stderr);    if (yflag)    {	frange_switch(yrange, &alow, &ahigh);	if (alow == ahigh) alow = -ahigh;	if (alow != -DBL_MAX) alo_flag++;	if (ahigh != DBL_MAX) ahi_flag++;	if (debug_level > 1)	{	    Fprintf(stderr, "%s: alow = %e, ahigh = %e.\n",		ProgName, alow, ahigh);	    (void) fflush(stderr);	}    }    for (i_file = 0; i_file < num_of_files; i_file++)    {	if (debug_level > 1)	{	    Fprintf(stderr, "%s: skipping %ld records in %s\n",	    ProgName, firstrec - start[i_file], sdfilename[i_file]);	    (void) fflush(stderr);	}	fea_skiprec(sdfile[i_file], firstrec - start[i_file], hd[i_file]);    }    if (debug_level > 1)    {	Fprintf(stderr,	"%s: allocating memory for data, u, v, ylow, yhigh, yscale, yticint.\n",	ProgName);	(void) fflush(stderr);    }    /*     * Allocate memory for data, u, and v.     *     * We need to allocate:     *     *	(number of files to plot) [num_of_files]     *			by     *	(number of data records) [nrec]     *     * memory space if the first file on the command     * line is not a pipe.  If we have to read from a     * pipe, we read the data into a temporary file to     * determine nrec for that file.  Then we can proceed     * as normal.     *     * Allocate memory for ylow, yhigh, yscale, yticint also.     * These arrays should have num_of_files elements.     *     */    if (!isfile)

⌨️ 快捷键说明

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