ftp.nice.ch/pub/next/unix/audio/cmix.s.tar.gz#/cmix/lpc/plot/kwnewplot.c

This is kwnewplot.c in view mode; [Download] [Up]

/* cc lpplot.c -O -o lpplot */
/* Program for plotting an lpc analysis output file.  Produces output for ~130 
		column device by default or can be used with -c option
		to produce output for another number of colums.  e.g., for
		most terminals, 
			'lpplot -c 80' 
		will do.
		Now in use with Paul Lanksy's lpc routines.  Can be altered 
		to plot other lpc data formats by altering the RESID...
		indexes.
Ken Worthy.
*/
#define READ 0
#define WRITE 1
#define RDWRT 2
#include <stdio.h> 

#define RESID 0
#define	AMPLI 1
#define ERRNO 2
#define PITCH 3

#define FLOAT sizeof(float)

long lseek();			/* !!!!!!!!!!!!!!!!!!!!!!!!*/

#define MARGIN	10
#define VALMAR	10
int plotcols;
int bpframe;
int npoles = 18;
float vals[50], thres;	/* enuf for 46 poles and 4 data values */
int infd;		/* the input file descriptors */
FILE *outfp;		/* the output file pointer */
struct range_s {float minimum; 
		float maximum;
		};
struct range_s a_range, p_range;

float a_rngfact, p_rngfact;

main(argc, argv)
int argc; char **argv;
{

	int n;
	char devtype[10], input[60], output[60];
	int frame1, frame2, nframe, ncols = 0;
	char *cp;

	while((*++argv)[0] == '-') {
	  	argc -= 2; /* Take away two args */
	 	for(cp = argv[0]+1; *cp; cp++) {
	    	  switch(*cp) { /* Grap optifns */
			case 'c': ncols = atoi(*++argv);
				  break;
	        	default:  
			printf("Don't know about option: [%c]\n",*cp);
	    	  }
	   	}
	}
	if(ncols <= 0) {
		ncols = 130;
	}
	printf(" Lpc data file and number of poles:\t");
	scanf("%s %d", input,&npoles);
	printf(" New data file:\t");
	scanf("%s", output);
	printf(" Frame1 and frame2:\t");
	scanf("%d %d", &frame1,&frame2);
	printf(" Threshold:\t");
	scanf("%f", &thres);

	if((frame1 > frame2) || (frame1 < 1)) {
		fprintf(stderr,"Error: bad frame values.");
		exit(-1);
	}
	if( (infd = open(input,RDWRT)) < 0) {
		fprintf(stderr," Error on opening input file.\n");
		exit(1);
	}
	if( (outfp = fopen(output,"w") ) == NULL) {
		fprintf(stderr," Error on creating output file.\n");
		exit(1);
	}
	nframe = frame2 - frame1 + 1;
	bpframe = (npoles + 4) * FLOAT;
	if(getrange(infd,AMPLI,frame1,frame2,&a_range) < 0) {
		fprintf(stderr,"bad return from getrange\n");
		exit(-1);
	}

	if(getrange(infd,PITCH,frame1,frame2,&p_range) < 0) {
		fprintf(stderr,"bad pitch getran\n");
		exit(-1);
	}

		/* plotcols is the actual num of cols to plot on 
				for each param */
	plotcols = (ncols - ((2*VALMAR) + (2*MARGIN)) - 4) / 2;
	a_rngfact = (plotcols - 1) / 
			(a_range.maximum - a_range.minimum);
/*	p_rngfact = (COLEACH - VALMAR - 1) / 	*/
	p_rngfact = (plotcols - 1) / 
			(p_range.maximum - p_range.minimum);
	if( windout(infd, (int)(frame1 - 1) ) != 0) { /* count start 1 */
		fprintf(stderr," Error on seeking into the input file.\n");
		exit(1);
	} 
	putheadr(outfp,input);

	for(n = frame1; n <= frame2; n++) {
		if(getfr(infd, vals) < 0) {
			fprintf(stderr," Error in getting frame %d.\n",n);
			exit(-1);
		}

		if(plotfr(outfp, n, vals) < 0) {
			fprintf(stderr,"Error from plotfr.\n");
			exit(1);
		}
	}
	
	puttail(outfp,input);
}
getrange(fd,which,loframe,hiframe,rangptr)
int fd,which,loframe,hiframe; 
struct range_s *rangptr;
{
	int n;
	if( windout(fd,(int)(loframe - 1)) < 0 ) {
		fprintf(stderr,"Couldn't windout for the rangeget.\n");
		return(-1);
	}
	rangptr->maximum = 0.0;
	rangptr->minimum = 999999999999999.0;
	for(n = loframe; n <= hiframe; n++) {
		if(getfr(fd, vals) > 0 ) {
	  		if(vals[which] > rangptr->maximum) 
				rangptr->maximum = vals[which];
	  		if(vals[which] < rangptr->minimum) 
				rangptr->minimum = vals[which];
		}
		else {
			fprintf(stderr,"getrang(): couldn't getfr\n");
			return(-1);
		}
	}
}
reputch(fp, c, num)
FILE *fp; char c; int num;
{
	if(num < 0) {
		num = -num;
		c = '\b';
	}
	for(;num > 0; num--) {
		if( putc(c,fp) < 0) {
			fprintf(stderr,"bad reputch\n");
			exit(-1);
		}
	}
}
getfr(fd, farr)  /* get the next frame */
int fd; float *farr;
{
	int n;
	if((n =  read(fd,(char *)farr,bpframe)) != bpframe) {
		fprintf(stderr,"Couldn't read another frame.\n");
		return(-1);
	}
}
plotfr(fp,framno,farr)
FILE *fp; int framno; float *farr;/*OK*/
{
	int a_colmn, p_colmn;
	char buf[30];

	fprintf(fp,"%5d:  ", framno);
		/* massage the amplitude funtion to have more
			'depth' (re: abv assign of a_rngfact) */
	fprintf(fp,"v=%7.5f  ", farr[ERRNO]);
	a_colmn = (int) ( ((farr[AMPLI] - a_range.minimum) * a_rngfact) + 1);
	p_colmn = (int) ( ((farr[PITCH] - p_range.minimum) * p_rngfact) + 1);

	fprintf(fp, "a=%-10.4f", farr[AMPLI]);
	reputch(fp, (vals[ERRNO] < thres ? '*' : '-'), a_colmn);
	reputch(fp, ' ', ((plotcols + 1) - a_colmn) );
	fprintf(fp, "p=%-10.4f", farr[PITCH]);
	reputch(fp, (vals[ERRNO] < thres ? '*' : '-'), p_colmn);
	fprintf(fp, "\n");		/* Ready for the next one */

}
putheadr(fp,infile)
FILE *fp; char *infile;
{
	if(fprintf(fp,
		"\n\nPlot of LPC analysis file '%s'.\n",
		infile) < 0 ) {
		fprintf(stderr,"Error on writing the headr.\n");
		exit(-1);
	}
	fprintf(fp,"\nAmplitude range:  \t%-12f to %-12f\n",
			a_range.minimum, a_range.maximum);
	fprintf(fp,"Pitch range:  \t\t%-12f to %-12f\n",
			p_range.minimum, p_range.maximum);
	fprintf(fp,"Key:");
	fprintf(fp,"\t\tframe no. at left margin. "); 
	fprintf(fp,"\n\t\t\tv=error number.");
	fprintf(fp,"\n\t\t\ta=rms(amplitude). p=pitch (in Hz). ");
	fprintf(fp,"\n\t\t\tThreshold is %f", thres);
	fprintf(fp,"\n\t\t\t-- = unvoiced, ** = voiced.\n\n");
}
puttail(fp, infile)
FILE *fp; char *infile;
{
	if(fprintf(fp, "\n\nEnd of plot of '%s'.\n\n", infile) < 0) {
		fprintf(stderr,"Bad puttail.\n");
		return(-1);
	}
}
		
windout(fd, nfram)
int fd; int nfram;
{
	long L;

	if((L = lseek(fd, (long) (nfram * bpframe), 0)) < 0) {
		fprintf(stderr,"windout() : couldn't L is %D\n", L);
		return(-1);
	}
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.