scatplot.c

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

C
841
字号
			if (debug_level >= 3)			    Fprintf(stderr, " %ld", nrec);		    }		    if (debug_level >= 3)			Fprintf(stderr, "\n");		    h[i_file] = tmphd;		    if (filep[i_file] == stdin)		    {			si_read = YES;			si_temp = tmpname;			si_hdr = h[i_file];			si_nrec = nrec;			if (debug_level >= 2)			    Fprintf(stderr, "si_read = %d.\n", si_read);		    }		    else		    {			/* ndrec[i_file] == -1 but filep[i_file] != stdin.			 * Probably file with variable record size.			 */			unlink(tmpname);			free(tmpname);		    }		    free_fea_rec(tmprec);		    rewind(tmpstrm);		    filep[i_file] = tmpstrm;		    ndrec[i_file] = nrec;		}		else		/* obsolete ESPS file types */		{		    int		recsize; /* number of bytes in input record */		    char	*tmpbuf; /* buffer for input data */		    recsize = size_rec(h[i_file]);		    if (recsize < 0)		    {			Fprintf(stderr,				" variable record size not allowed in %s.\n",				files[i_file]);			exit(1);		    }		    TRYALLOC(char, recsize, tmpbuf, "temporary record buffer");		    if (debug_level >= 2)			Fprintf(stderr,			    "Allocated space for %d-byte buffer.\n", recsize);		    if (debug_level >= 3)			Fprintf(stderr, "Reading records:");		    nrec = 0;		    while (fread(tmpbuf, recsize, 1, filep[i_file]) == 1)		    {			if (fwrite(tmpbuf, recsize, 1, tmpstrm) != 1)			{			    Fprintf(stderr, "%s: write error on %s\n",				ProgName, tmpname);			    exit(1);			}			nrec++;			if (debug_level >= 3)			    Fprintf(stderr, " %ld", nrec);		    }		    if (debug_level >= 3)			Fprintf(stderr, "\n");		    if (filep[i_file] == stdin)		    {			si_read = YES;			si_temp = tmpname;			si_hdr = h[i_file];			si_nrec = nrec;			if (debug_level >= 2)			    Fprintf(stderr, "si_read = %d.\n", si_read);		    }		    else		    {			/* Here only if ndrec[i_file] == -1 but			   filep[i_file] != stdin.  Not sure this			   can happen. */			unlink(tmpname);			free(tmpname);		    }		    free(tmpbuf);		    rewind(tmpstrm);		    filep[i_file] = tmpstrm;		    ndrec[i_file] = nrec;		}	    }	}	if ( (elem_array[2*i_file] == -1 || elem_array[2*i_file + 1] == -1)	    && !h[i_file]->common.tag )	{		Fprintf(stderr,		    "%s: data is not tagged in %s, cannot plot element 0.\n",		    ProgName, files[i_file]);		exit(1);	}	if (debug_level >= 2)	    Fprintf(stderr, "%ld records in file.\n", ndrec[i_file]);		    }  /* end for (i_file ... ) */    if (si_read) unlink(si_temp);/* read data into arrays, find extreme x and y values */    if (debug_level >= 1)	Fprintf(stderr, "Reading data into memory.\n");    for (i_file = 0; i_file < nfiles; i_file++)    {	int	rec_len;	/* number of elements in input record */	long	start_p, last_p;    /* first and last records in range */	long	nan;		/* number of records in range */	long	x_ele, y_ele;	/* element numbers for x and y data */	static	first = YES;	/* first trip through loop? */	if (debug_level >= 2)	    Fprintf(stderr,		"File number %d (%s).\n", i_file + 1, files[i_file]);	start_p = 1;	last_p = ndrec[i_file];	if (rnum > 0)	    lrange_switch(rrange[MIN(i_file, rnum-1)], &start_p, &last_p, 0);	nans[i_file] =	    nan = last_p - start_p + 1;	if (debug_level >=2)	{	    Fprintf(stderr,		"start_p = %ld, nan = %ld, last_p = %ld\n",		start_p, nan, last_p);	}	if (start_p < 1)	{	    Fprintf(stderr,		"%s: start point less than one for file %d.\n",		ProgName, i_file);	    exit(1);	}	if (start_p > last_p)	{	    Fprintf(stderr,		"%s: start point after end point for file %d.\n",		ProgName);	    exit(1);	}	if (last_p > ndrec[i_file])	{	    Fprintf(stderr,		"%s: only %ld points in file %d.\n",		    ProgName, ndrec[i_file], i_file);	    exit(1);	}	fea_skiprec(filep[i_file], start_p - 1, h[i_file]);	rec_len = get_rec_len(h[i_file]);	if (debug_level >= 2)	    Fprintf(stderr,		"allocating space for arrays of lengths %d, %d, %d.\n",		rec_len, nan, nan);	TRYALLOC(double, rec_len, data, "input data array")	TRYALLOC(double, nan, xdata[i_file], "x data")	TRYALLOC(double, nan, ydata[i_file], "y data")	x_ele = elem_array[2*i_file];	y_ele = elem_array[2*i_file + 1];	if (debug_level >= 2)	{	    Fprintf(stderr,		"Getting data from elements %d, %d.\n",		x_ele + 1, y_ele + 1);	}	for (i = 0; i < nan; i++)	{	    if (get_gen_recd(data, &tag, h[i_file], filep[i_file]) == EOF)	    {		Fprintf(stderr,		    "%s: only %d records read in %s\n",		    ProgName, i, files[i_file]);		exit(1);	    }	    xdata[i_file][i] = (x_ele == -1) ? tag : data[x_ele];	    ydata[i_file][i] = (y_ele == -1) ? tag : data[y_ele];	    if (debug_level >= 3)		Fprintf(stderr,		    "Read %g, %g.\n", xdata[i_file][i], ydata[i_file][i]);	    if (first)	    {		first = NO;		if (!Xflag)		    x_min = x_max = xdata[i_file][i];		if (!Yflag)		    y_min = y_max = ydata[i_file][i];	    }	    else	    {		if (!Xflag)		    if (xdata[i_file][i] < x_min) x_min = xdata[i_file][i];		    else		    if (xdata[i_file][i] > x_max) x_max = xdata[i_file][i];		if (!Yflag)		    if (ydata[i_file][i] < y_min) y_min = ydata[i_file][i];		    else		    if (ydata[i_file][i] > y_max) y_max = ydata[i_file][i];	    }	}	free((char *) data);    }    if (debug_level >= 1)    {	Fprintf(stderr, "Done reading data.\n");	Fprintf(stderr,	    "x_min = %g, x_max = %g, y_min = %g, y_max = %g\n",	    x_min, x_max, y_min, y_max);    }/* scale and draw axes */    if (x_min == x_max)    {	if (x_min < 0) x_max = 0.0;	else	if (x_max > 0) x_min = 0.0;	else	{	    x_min = 0-1.0;	    x_max = 1.0;	}    }    if (y_min == y_max)    {	if (y_min < 0) y_max = 0.0;	else	if (y_max > 0) y_min = 0.0;	else	{	    y_min = 0-1.0;	    y_max = 1.0;	}    }    if (Xflag)	plotexscale(x_min, x_max, 1.0, &xlow, &xhigh, &xstep, &xdp);    else	plotscale(x_min, x_max, 1.0, &xlow, &xhigh, &xstep, &xdp);    if (Yflag)	plotexscale(y_min, y_max, 1.0, &ylow, &yhigh, &ystep, &ydp);    else	plotscale(y_min, y_max, 1.0, &ylow, &yhigh, &ystep, &ydp);    if (debug_level >= 1)	Fprintf(stderr,	    "xlow = %g, xhigh = %g, ylow = %g, yhigh = %g\n",	    xlow, xhigh, ylow, yhigh);    draw_box(xlow, xhigh, xstep, xdp, ylow, yhigh, ystep, ydp,		    upperx, uppery, lowerx, lowery);    Printf("c 2\n");/* plot data */    for (i_file = 0; i_file < nfiles; i_file++)    {	if (debug_level >= 2)	    Fprintf(stderr, "Plotting data from file %d.\n", i_file + 1);	plot_letter( xdata[i_file], ydata[i_file], (int) nans[i_file], 5,		     xlow, xhigh, ylow, yhigh, symbol[i_file],		     upperx, uppery, lowerx, lowery );    }/* print titles, etc. */    /*     * Print command line on top of page.     * Break the line into two lines if it is more than COM_CHARS     * characters long.     */    if (strlen(command_line) > COM_CHARS)    {	Printf("c 5\nm 375 %d\nd 375 %d\n", upperx, lowerx);	(void) strncpy(txtbuf, command_line, COM_CHARS);	txtbuf[COM_CHARS] = '\0';	tmpptr = &command_line[COM_CHARS];	Printf("c 5\nm 300 %d\nt 2 1\n%s\n", upperx, txtbuf);	if (strlen(tmpptr) > COM_CHARS)	   Fprintf(stderr,	   "%s: command line too long: truncating string.\n",	   ProgName);	Printf("c 5\nm 350 %d\nt 2 1\n%s\n", upperx, tmpptr);    } else {	Printf("c 5\nm 325 %d\nd 325 %d\n", upperx, lowerx);	Printf("c 5\nm 300 %d\nt 2 1\n%s\n", upperx, command_line);    }    /*     * If the -t option was given, place the text above the graph,     * otherwise print the default string there.  Likewise for the     * -H and -V options.     */    if (tflag)	Printf("c 5\nm 500 %d\nt 5 1\n%s\n", upperx, text[0]);    else	if (nfiles > 1)	   Printf("c 5\nm 500 %d\nt 5 1\nScatter Plot of %d ESPS Files\n",	   upperx, nfiles); 	else	   Printf("c 5\nm 500 %d\nt 5 1\nFile: %s\n", upperx, files[0]);    if (!Hflag && nelem == 2)	if (elem_array[0] == -1)	    Htext = savestring("tag");	else {	    Sprintf(txtbuf, "element%ld\n", elem_array[0] + 1);	    Htext = savestring(txtbuf);	}    nchars = strlen(Htext);    Printf("c 5\nm %d %d\nt 5 1\n", lowery + 350,	(lowerx - upperx) / 2 + upperx - nchars * CHAR_SIZE);    Printf("%s\n", Htext);    if (!Vflag && nelem == 2)	if (elem_array[1] == -1)	    Vtext = savestring("tag");	else {	    Sprintf(txtbuf, "element%ld\n", elem_array[1] + 1);	    Vtext = savestring(txtbuf);	}    nchars = strlen(Vtext);    Printf("c 5\nm %d 90\nt 5 2\n",	(lowery - uppery) / 2 + uppery + nchars * CHAR_SIZE);    Printf("%s\n", Vtext);    print_time(500, 4600);    exit(0);    /*NOTREACHED*/}/* * For debug printout of double arrays */static voidpr_darray(text, n, arr)    char    *text;    long    n;    double  *arr;{    int	    i;    Fprintf(stderr, "%s -- %d points:\n", text, n);    for (i = 0; i < n; i++)    {	Fprintf(stderr, "%g ", arr[i]);	if (i%5 == 4 || i == n - 1) Fprintf(stderr, "\n");    }}/* * for debug printout of arrays of longs */static voidpr_larray(text, n, arr)    char    *text;    int	    n;    long    *arr;{    int	    i;    Fprintf(stderr, "%s - %d points:\n", text, n);    for (i = 0; i < n; i++)    {	Fprintf(stderr, "%ld ", arr[i]);	if (i%5 == 4 || i == n - 1) Fprintf(stderr, "\n");    }}

⌨️ 快捷键说明

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