genplot.c

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

C
1,724
字号
	    Fprintf (stderr,		"%s: reading stdin into a temporary file...\n", ProgName);	tmp_file = e_temp_name(template);	TRYOPEN(ProgName, tmp_file, "w+", tmp_strm)	(void) unlink(tmp_file);	recsize = size_rec(hd);	if (recsize == -1)	{	    ERROR("variable record size not supported.");	}	if (debug_level == 6)	    Fprintf(stderr,		"%s: opened temp file %s, recsize = %d; allocate memory..\n",		ProgName, tmp_file, recsize);	TRYALLOC(char, recsize, tmp_buf)	ndrec = 0;	while (fread (tmp_buf, recsize, 1, stdin) == 1)	{	    if (fwrite (tmp_buf, recsize, 1, tmp_strm) != 1) {		Fprintf (stderr, "%s: write error on %s\n",		ProgName, tmp_file);		exit (1);	    }	    ndrec++;	}	if (debug_level == 6) {	    Fprintf (stderr,	    "%s: %ld records from stdin written to temporary file (%s).\n",	    ProgName, ndrec, tmp_file);	}	(void) rewind(tmp_strm);	Fclose(stdin);	infile = tmp_strm;    }    if (iflag)    {	firstrec = start;	lastrec = start;    }    else    {	firstrec = start;	lastrec = ndrec + start - 1;    }    lrange_switch(rrange, &firstrec, &lastrec, 0);    if (lastrec < firstrec) {	Fprintf(stderr, "%s: bad range given, last record < first record.\n",		ProgName);	exit(1);	}    if (firstrec < start) 	firstrec = start;    if (lastrec > ndrec + start - 1)	lastrec = ndrec + start - 1;    nrec = lastrec - firstrec + 1;    if (debug_level > 1)    {	Fprintf(stderr,	    "%s: firstrec = %ld, lastrec = %ld, nrec = %ld.\n",	    ProgName, firstrec, lastrec, nrec);	(void) fflush(stderr);    }    /* get number of elements to plot */    if (eflag)    {	if (nfld == 0)	{	    elem_array = grange_switch(erange, &nelem);     	    first_ele = elem_array[0];	    last_ele = elem_array[nelem - 1];	}	else	{	    if (is_file_complex(hd)) {		Fprintf(stderr,		 "genplot: Sorry, cannot deal with a feature file with complex fields this way, yet.\n");	 	Fprintf(stderr,		 "genplot: Use fea_element and then plot by element numbers.\n");		exit(1);	    }	    elem_array =		fea_range_switch(fea_fields, hd,			&nelem, &fea_names, &fea_indices);	    first_ele = LONG_MAX;	    last_ele = LONG_MIN;	    for (i = 0; i < nelem; i++)	    {		if (elem_array[i] < first_ele) first_ele = elem_array[i];		if (elem_array[i] > last_ele) last_ele = elem_array[i];	    }	}	if (first_ele < 0)	{	    Fprintf(stderr,		"%s: element number should not be less than zero.\n",		ProgName);	    exit(1);	}	if (last_ele > get_rec_len(hd) )	{	    last_ele = get_rec_len(hd);	    Fprintf(stderr,		"%s: only %d elements in a record, truncating specified range.\n",		ProgName, last_ele);	}	if ((first_ele == 0) && (hd->common.tag == NO))	{	    Fprintf(stderr,		"%s: can't plot element zero: data is not tagged.\n",		ProgName);	    exit(1);	}	  /*	   * Subtract 1 from first and last element because dbuf in	   * get_gen_recd starts at element 0.	   *	   */	   first_ele -= 1;	   last_ele -= 1;	   for (i = 0; i < nelem; i++)		elem_array[i] -= 1;	if (debug_level > 1)	{	    Fprintf(stderr, "%s: first_ele = %ld, last_ele = %ld.\n",		ProgName, first_ele, last_ele);	    (void) fflush(stderr);	}	if (debug_level > 10)	{	    Fprintf(stderr,		"\n%s: num. of elements selected: nelem = %ld\n",		ProgName, nelem);	    for (i = 0; i < nelem; i++)		Fprintf(stderr, "elem_array[%d] = %d\n", i, elem_array[i]);	}    } /* end if (eflag) */    /* get items from feature file for plotting */    if (iflag)    {	if (debug_level > 5)	    Fprintf(stderr, "%s: irange = %s\n", ProgName, irange);	if (nfld == 0)	{	    item_array = grange_switch(irange, &nitem);	     	    first_itm = item_array[0];	    last_itm = item_array[nitem - 1];	}	else	{	    if (is_file_complex(hd)) {		Fprintf(stderr,		 "genplot: Sorry, cannot deal with a feature file with complex fields this way, yet.\n");	 	Fprintf(stderr,		 "genplot: Use fea_element and then plot by element numbers.\n");		exit(1);	    }	    item_array =		fea_range_switch(fea_fields, hd,			&nitem, &fea_names, &fea_indices);	    first_itm = LONG_MAX;	    last_itm = LONG_MIN;	    for (i = 0; i < nitem; i++)	    {		if (item_array[i] < first_itm) first_itm = item_array[i];		if (item_array[i] > last_itm) last_itm = item_array[i];	    }	}	if (first_itm < 0)	{	    Fprintf(stderr,		 "item range should not be less than zero.\n", ProgName);	    exit(1);	}	if (item_array[nitem - 1] > get_rec_len(hd) )	{	    Fprintf(stderr,		"%s: only %d items in a record.\n",		ProgName, get_rec_len(hd));	    exit(1);	}	if ((first_itm == 0) && (hd->common.tag == NO))	{	    Fprintf(stderr,		"%s: can't plot item zero: data is not tagged.\n",		ProgName);	    exit(1);	}	for (i = 0; i < nitem; i++)	    item_array[i] -= 1;	if (debug_level > 10)	{	    Fprintf(stderr,		"\n%s: number of items selected: nitem = %ld\n",		ProgName, nitem);	    for (i = 0; i < nitem; i++)		Fprintf(stderr,		    "item_array[%d] = %d\n", i, item_array[i]);	}    } /* end if (iflag) */    /* get ordinate range */    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);	}    }    if (debug_level > 1)    {	Fprintf(stderr, "%s: skipping %ld records...\n",	    ProgName, firstrec - start);	(void) fflush(stderr);    }    if (firstrec > start)	fea_skiprec(infile, firstrec - start, hd);    xinc = deltau / xscale;/* * We must compute the maximum ordinate of each element that * we are going to plot.  I'll compute in the main loop. * *	yscale = deltav / (yhigh - ylow); * *	yticint = (yhigh - ylow) / DEF_Y_SUBDIV; *//* * N O T E: * * If the -e option is given, then there will be nelem different *	graphs to plot (i.e. n_graphs will be nelem and *			     n_points will be nrec). * * If the -i option is given, then there will be nrec different *	graphs to plot (i.e. n_graphs will be nrec and *			     n_points will be nitem). * */    if (eflag)    {	n_graphs = nelem;	n_points = nrec;    }    if (iflag)    {	n_graphs = nrec;	n_points = nitem;    }    if (debug_level > 1)    {	Fprintf(stderr, "%s: xinc = %ld\n", ProgName, xinc);	Fprintf(stderr, "%s: n_graphs = %d, n_points = %d\n",	    ProgName, n_graphs, n_points);	Fprintf(stderr, "%s: allocating memory for data, u, and v....\n",	    ProgName);	(void) fflush(stderr);    }/* * Allocate memory for data, u, and v. * We need to allocate: * *	(number of graphs to plot) [n_graphs] *			by *	(number of data points in one "window") [xinc] * * memory space. * * Note: data will be xinc by n_graphs, whereas *	 u and v are n_graphs by xinc * */    (void) allo_d_matrix(n_graphs, n_points);    (void) allo_d_array(n_graphs);    if (debug_level > 2)	Fprintf(stderr,	    "%s: record length is %d\n", ProgName, get_rec_len(hd));    if ((data = (double *) calloc((unsigned) get_rec_len(hd), sizeof(double)))	== NULL )    {	Fprintf(stderr,	    "%s: calloc:  could not allocate memory for data.\n",	ProgName);	exit(1);    }    if ((Tag_Array = (long *) calloc((unsigned) nrec, sizeof(long)))	== NULL )    {	Fprintf(stderr,	    "%s: calloc:  could not allocate memory for Tag_Array.\n",	    ProgName);	exit(1);    }    /* read data from the ESPS generic file and store in data array */    if (debug_level > 3)    {	Fprintf(stderr,	    "%s: reading %ld data records from ESPS file...\n",	    ProgName, nrec);	if (func_codes[function_type] != NULL)	    Fprintf(stderr,		"%s: The %s function will be applied to all data records.\n",		ProgName, func_codes[function_type]);	(void) fflush(stderr);    }    /*     * R E A D     *   D A T A     *     */    if ( eflag )    {	if (debug_level > 2)	    Fprintf(stderr,		"%s: reading data from generic file (eflag set).\n",		ProgName);	for ( i = 0; i < nrec; i++ )	{	    if (get_gen_recd(data, &tag, hd, infile) == EOF)	    {		Fprintf(stderr, "%s: only %d records read.\n",		    ProgName, i);		exit(1);	    }	    Tag_Array[i] = tag;	    for (i_ele = 0; i_ele < nelem; ++i_ele)	    {		if (elem_array[i_ele] == -1)		    Elem_Matrix[i_ele][i] = Tag_Array[i];		else		    switch (function_type)		    {		    case NONE:			Elem_Matrix[i_ele][i] = data[elem_array[i_ele]];			break;		    case SQRT:			if (data[elem_array[i_ele]] < 0.0)			{			    Fprintf(stderr,				"%s: fatal error: negative argument for sqrt.\n",				ProgName);			    Fprintf(stderr,				"%s: at element %d, record number %d\n",				ProgName, elem_array[i_ele] + 1, i);			    exit(1);			}			Elem_Matrix[i_ele][i] = sqrt(data[elem_array[i_ele]]);			break;		    case LOG:			if (data[elem_array[i_ele]] <= 0.0)			{			    Fprintf(stderr,				"%s: fatal error: argument out of range for log.\n",				ProgName);			    Fprintf(stderr,				"%s: at element %d, record number %d\n",				ProgName, elem_array[i_ele] + 1, i);			    exit(1);			}			Elem_Matrix[i_ele][i] = log(data[elem_array[i_ele]]);			break;		    default:			Fprintf(stderr, "%s: illegal function specified.\n",			    ProgName);			exit(1);			break;		    } /* end switch (function_type) */		/*		 * Compute the maximum and minimum values of the each element		 * across all records as the data is read in.		 */		if (alo_flag)		    ylow[i_ele] = alow;		else		{		    if (i == 0)			ylow[i_ele] = Elem_Matrix[i_ele][i];		    else		    {			if (Elem_Matrix[i_ele][i] < ylow[i_ele])			    ylow[i_ele] = Elem_Matrix[i_ele][i];		    }		}		if (ahi_flag)		    yhigh[i_ele] = ahigh;		else		{		    if (i == 0)			yhigh[i_ele] = Elem_Matrix[i_ele][i];		    else		    {			if (Elem_Matrix[i_ele][i] > yhigh[i_ele])			    yhigh[i_ele] = Elem_Matrix[i_ele][i];		    }		}		if (debug_level > 5)		{		    Fprintf(stderr,			"\n%s: loop index i = %d, elem_array[%d] = %d\n",			ProgName, i, i_ele, elem_array[i_ele]);		    Fprintf(stderr,			"%s: Elem_Matrix[%d][%d] = %g, data[%d] = %g, tag = %d\n",			ProgName, i_ele, i, Elem_Matrix[i_ele][i],			elem_array[i_ele], data[elem_array[i_ele]], tag);		    Fprintf(stderr,			"%s: yhigh[%d] = %g, ylow[%d] = %g\n",			ProgName, i_ele, yhigh[i_ele], i_ele, ylow[i_ele]);		}	    }	} /* end for (i =0; i < nrec; i++) */	for (i_ele = 0; i_ele < nelem; ++i_ele)	    if (ylow[i_ele] >= yhigh[i_ele])	    {		Fprintf(stderr, "%s: element %ld has zero range.",		    ProgName, elem_array[i_ele] + 1);		exit(1);	    }        }  /* end if ( eflag ) */    /*     * R E A D     *   D A T A     *     */    if ( iflag )    {	if (debug_level > 2)	    Fprintf(stderr,		"%s: reading data from feature file (iflag set).\n",		ProgName);	for ( i = 0; i < nrec; i++ )	{	    if (get_gen_recd(data, &tag, hd, infile) == EOF)	    {		Fprintf(stderr, "%s: only %d records read.\n",		    ProgName, i);		exit(1);	    }	    Tag_Array[i] = tag;	    for (i_ele = 0; i_ele < nitem; ++i_ele)	    {		if (item_array[i_ele] == -1)		    Elem_Matrix[i][i_ele] = Tag_Array[i];		else		    switch (function_type)		    {		    case NONE:			Elem_Matrix[i][i_ele] = data[item_array[i_ele]];			break;		    case SQRT:			if (data[item_array[i_ele]] < 0.0)			{			    Fprintf(stderr,				"%s: fatal error: argument out of range for sqrt.\n",				ProgName);			    Fprintf(stderr,				"%s: at item %d record number %d\n",				ProgName, item_array[i_ele], i);			    exit(1);			}			Elem_Matrix[i][i_ele] = sqrt(data[item_array[i_ele]]);			break;		    case LOG:			if (data[item_array[i_ele]] <= 0.0)			{			    Fprintf(stderr,				"%s: fatal error: argument out of range for log.\n",				ProgName);			    Fprintf(stderr,				"%s: at item %d, record number %d\n",				ProgName, item_array[i_ele], i);			    exit(1);			}			Elem_Matrix[i][i_ele] = log(data[item_array[i_ele]]);			break;		    default:			Fprintf(stderr, "%s: illegal function specified.\n",			    ProgName);			exit(1);			break;		    } /* end switch (function_type) */		/*		 * Compute the maximum and minimum values of the each element		 * across all records as the data is read in.		 */		if (alo_flag)		    ylow[i] = alow;		else		{		    if (i_ele == 0)			ylow[i] = Elem_Matrix[i][i_ele];		    else		    {			if (Elem_Matrix[i][i_ele] < ylow[i])			    ylow[i] = Elem_Matrix[i][i_ele];		    }

⌨️ 快捷键说明

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