⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ana.c

📁 speech signal process tools
💻 C
📖 第 1 页 / 共 2 页
字号:
    unlink (tmp_name);    exit (0);}short        flag_interp (pstring)char   *pstring;{    char   *string;    string = getsym_s (pstring);    if (strcmp (string, "Y") == 0)	return yes;    if (strcmp (string, "y") == 0)	return yes;    if (strcmp (string, "yes") == 0)	return yes;    if (strcmp (string, "YES") == 0)	return yes;    if (strcmp (string, "n") == 0)	return no;    if (strcmp (string, "N") == 0)	return no;    if (strcmp (string, "NO") == 0)	return no;    if (strcmp (string, "no") == 0)	return no;    else	parm_err (string, pstring);}short   get_window (pstring)char   *pstring;{    char   *string;    string = getsym_s (pstring);    if (strcmp (string, "Y") == 0)	return yes;    if (strcmp (string, "y") == 0)	return yes;    if (strcmp (string, "yes") == 0)	return yes;    if (strcmp (string, "YES") == 0)	return yes;    if (strcmp (string, "HAMMING") == 0)	return yes;    if (strcmp (string, "hamming") == 0)	return yes;    if (strcmp (string, "n") == 0)	return no;    if (strcmp (string, "N") == 0)	return no;    if (strcmp (string, "NO") == 0)	return no;    if (strcmp (string, "no") == 0)	return no;    if (strcmp (string, "none") == 0)	return no;    if (strcmp (string, "NONE") == 0)	return no;    else	parm_err (string, pstring);}short   get_anal_mthd (pstring)char   *pstring;{    char   *string;    string = getsym_s (pstring);    if (strcmp (string, "auto") == 0)	return 1;    if (strcmp (string, "AUTO") == 0)	return 1;    if (strcmp (string, "a") == 0)	return 1;    if (strcmp (string, "BURG") == 0)	return 2;    if (strcmp (string, "burg") == 0)	return 2;    if (strcmp (string, "b") == 0)	return 2;    if (strcmp (string, "COV") == 0)	return 3;    if (strcmp (string, "cov") == 0)	return 3;    if (strcmp (string, "c") == 0)	return 3;    if (strcmp (string, "MBURG") == 0)	return 4;    if (strcmp (string, "mburg") == 0)	return 4;    if (strcmp (string, "m") == 0)	return 4;    if (strcmp (string, "STRCOV") == 0)	return 5;    if (strcmp (string, "strcov") == 0)	return 5;    if (strcmp (string, "s") == 0)	return 5;    if (strcmp (string, "VBURG") == 0)	return 6;    if (strcmp (string, "vburg") == 0)	return 6;    if (strcmp (string, "v") == 0)	return 6;    else	parm_err (string, pstring);}void parm_err (string, pstring)char   *string, *pstring;{    Fprintf (stderr, "ana: unknown string %s for %s in SPS parameter file\n",	    string, pstring);    exit (1);}short        process_frame (locn, blksze, anarec, datatype, maxpulses, maxraw, maxlpc)int     locn, blksze, maxraw, maxlpc, maxpulses;struct ana_data *anarec;char    datatype;{    int     correct_flag;    int     i, j, k, ind, error = no;    short   firstflag;    float   lpcfilter[100], rc[100], sig_energy, res_energy, framsize;    static int  mem_ptr = 0, beg_addr = -BUFSIZE, mem_size = BUFSIZE;    float   inpbuf[BUFSIZE], iobfr[256], gain;    blksze += matsiz + NPR - 2;    locn -= (p_offset + NPR - 1);    if (debug_level)	fprintf (stderr, "process_frame(%d,%d)\n", locn, blksze);    if (beg_addr == -BUFSIZE)    {	beg_addr = istan - p_offset - NPR - mem_size + 1;	for (i = 0; i <= order; i++)	    lpc_filt_state[i] = prev_lpcfilter[i] = 0;    }    if (locn < beg_addr)    {	fprintf (stderr, "ana: locn(%d) < beg_addr(%d)\n", locn, beg_addr);	exit (2);    }    for (i = locn; i < locn + blksze; i++)    {	if (i >= beg_addr + mem_size)	{	    if (debug_level)		fprintf (stderr, "Reading %d points\n", vced_frmlen);/*the next line needs rewriting in terms of new get_sd_rec function*/	    get_fdata (ifd, datatype, iobfr, vced_frmlen);	    if (dcrem == yes)	    {		dc_val_rem += remove_dc (iobfr, vced_frmlen);		dc_pts_rem += vced_frmlen;	    }	    beg_addr += vced_frmlen;	    for (j = 0; j < vced_frmlen; j++)	    {		data_mem[mem_ptr] = iobfr[j];		mem_ptr = (mem_ptr + 1) % mem_size;	    }	}	j = (i - beg_addr + mem_ptr) % mem_size;	inpbuf[i - locn] = data_mem[j];    }    if (debug_level)	fprintf (stderr, "analyze: ");/* perform pre-emphasis filtering */   pre_emphasis (inpbuf, blksze, num, den, pstate);   blksze -= NPR - 1;   locn += NPR - 1;   for (i = 0; i < blksze; i++)	inpbuf[i] = inpbuf[i + NPR - 1];    if (john_flag)    {	float   t;	for (i = 0; i < order; i++)	{	    lpc_filt_state[order - i + 1] = inpbuf[i];	    inpbuf[i] = 0.0;	}	t = 0.0;	for (i = order; i < blksze; i++)	    inpbuf[i] -= fowrd_filter (prev_lpcfilter, lpc_filt_state, order, t);    }    analyze (inpbuf, blksze, order, method, &sig_energy, lpcfilter,	    rc, &res_energy, window, matsiz);    blksze -= matsiz - 1;    if (john_flag)	for (i = 1; i <= order; i++)	    prev_lpcfilter[i] = lpcfilter[i];    gain = sig_energy / res_energy;    if (smartana_flag && pulse_count)	smartana (locn + p_offset, lpcfilter, order, inpbuf,		blksze, &correct_flag, sig_energy);    else	correct_flag = 0;    if (debug_level)    {	for (j = 0; j < order; j++)	    fprintf (stderr, "rc[%d] = %g\t", j + 1, rc[j + 1]);	fprintf (stderr, "\n");	fprintf (stderr, "gain = %f\n", gain);    }/* Write an ana unvoiced record */    if (pulse_count == 0 || correct_flag)    {	if (correct_flag)	{	    j = head_ptr - pulse_count;	    if (j < 0)		j += lntpbuf;	    k = 0;	    locn = 0;	    while (to_be_analyzed[j] == no)	    {		locn += period[j];		k++;		j = (j + 1) % lntpbuf;	    }	    anarec -> tag = plocn[j];	    sig_energy = 0.0;	    for (i = locn; i < blksze; i++)		sig_energy += inpbuf[i + p_offset] * inpbuf[i + p_offset];	    while (k < pulse_count)	    {		to_be_analyzed[j] = no;		j = (j + 1) % lntpbuf;		k++;	    }	    sig_energy = sig_energy / (blksze - locn);	    anarec -> frame_len = blksze - locn;	}	else	{	    anarec -> tag = locn + p_offset;	    anarec -> frame_len = blksze;	}	anarec -> p_pulse_len[0] = 0;	anarec -> raw_power[0] = sig_energy;	if (maxraw > 1)	    anarec -> raw_power[1] = -1.;	anarec -> lpc_power[0] = res_energy;	if (maxlpc > 1)	    anarec -> lpc_power[1] = -1.;/* NOTE: rc[0] is gain - not the first reflection coefficient */	for (i = 1; i <= order; i++)	    anarec -> ref_coeff[i - 1] = rc[i];	put_ana_rec (anarec, tmphd, anaptr);	n_ana_rec++;	return (error);    }/* Sinc weighting applied only during voiced segments */    if (sinc_flg)	apply_sinc (rc, lpcfilter, sig_energy, &res_energy, order, blksze / pulse_count);/*write an ana voiced record  */    if (debug_level)	fprintf (stderr, "Writing voiced frame\n");    j = head_ptr - pulse_count - 1;    if (j < 0)	j += lntpbuf;    locn = 0;    framsize = 0;    firstflag = 1;    ind = 0;    for (k = 0; k < pulse_count; k++)    {	j = (j + 1) % lntpbuf;	if (to_be_analyzed[j] == yes)	{	    if (firstflag)	    {		firstflag = 0;		anarec -> tag = plocn[j];	    }	    if (ind == maxpulses || ind == maxraw)	    {		Fprintf (stderr,			"ANA.process_frame: This frame (%d) has more than %d pulse durations or %d raw pulse powers in it; ana has been aborted\n",			n_ana_rec, maxpulses, maxraw);		error = yes;		return (error);	    }	    to_be_analyzed[j] = no;	    sig_energy = 0.0;	    for (i = 0; i < period[j]; i++)		sig_energy += inpbuf[locn + i] * inpbuf[locn + i];	    sig_energy /= period[j];	    anarec -> raw_power[ind] = sig_energy;	    anarec -> p_pulse_len[ind] = period[j];	    framsize += period[j];	    ind++;	}	locn += period[j];    }/* NOTE: rc[0] is gain - not the first reflection coefficient */    for (i = 1; i <= order; i++)	anarec -> ref_coeff[i - 1] = rc[i];    anarec -> frame_len = framsize;    if (ind <= maxpulses)	anarec -> p_pulse_len[ind] = 0;    if (ind <= maxraw)	anarec -> raw_power[ind] = -1.;/* at this point in time, we only know how to compute the residual over the    entire frame of data - maybe latter we will use the residual on a per pulse   basis */    if (maxlpc < 1)    {	Fprintf (stderr,		"ANA.process_frame: This frame (%d) has more than %d lpc error powers in it; ana has been aborted\n",		n_ana_rec, maxlpc);	error = yes;	return (error);    }    anarec -> lpc_power[0] = res_energy;    if (ind <= maxlpc)	anarec -> lpc_power[1] = -1.;/* put the ana data record on the temporary file */    put_ana_rec (anarec, tmphd, anaptr);    n_ana_rec++;    return (error);}/* A routine for reading label file  */#define voiced 1#define unvoiced 0#define lntpbuf 10get_pitch_info (pstrm, locn, ptr_seglnt, ptr_segtype, p)/*pstrm: pitch streamlocn - starting location of the frameptr_seglnt - p(size of data block)ptr_segtype - p(voice/unvoice information)p - pitch record data structure pointer*/FILE * pstrm;int    *ptr_seglnt, locn, *ptr_segtype;struct pitch   *p;{    extern float    pitch_pos, pitch_period;    static int  init_rdlab_flag = 1;    int     lnt;    lnt = *ptr_seglnt;    /* initialization phase */    if (init_rdlab_flag)    {	init_rdlab_flag = 0;	pitch_pos = pitch_period = -1.0;    }    while (pitch_pos < locn)    {	if (get_pitch (pstrm, &pitch_pos, &pitch_period, p) == EOF)	{	    /* eof encountered in reading label file */	    if (debug_level)		fprintf (stderr, "EOF encountered in reading .pit file\n");	    *ptr_seglnt = uvced_frmlen;	    *ptr_segtype = unvoiced;	    return;	}    }    if (pitch_pos == locn)    {	lnt = pitch_period;	*ptr_seglnt = lnt;	*ptr_segtype = voiced;	return;    }    if (pitch_pos > locn)    {	lnt = pitch_pos - locn;	if (lnt > 2 * uvced_frmlen)	    lnt = uvced_frmlen;	*ptr_segtype = unvoiced;	*ptr_seglnt = lnt;	return;    }}get_pitch (stream, p_tag, p_pitch, p)FILE * stream;float  *p_tag, *p_pitch;struct pitch   *p;{    if (get_pitch_rec (p, stream) != EOF)    {	*p_tag = p -> tag;	*p_pitch = p -> pulse_dist;	return (0);    }    else	return (EOF);}apply_sinc (rc, a, sig_energy, ptr_res_energy, order, pulse_dur_est)int     order, pulse_dur_est;float   rc[], a[], sig_energy, *ptr_res_energy;{    double  r[100], sin (), w;    float   state[100], nrc[100], NZ[100];    float   fowrd_filter (), gain;    int     i;/*  Flip reflection coefficients and compute corresponding lpc filter */    for (i = 1; i <= order; i++)    {	state[i] = 0.0;	nrc[i] = -rc[i];    }    rctoc (nrc, order, NZ, &gain);    NZ[0] = 0.5;    for (i = 1; i <= order; i++)	NZ[i] = -0.5 * NZ[i];    for (i = 0; i <= order; i++)	r[i] = fowrd_filter (a, state, order, NZ[i]);    r[0] = 2.0 * r[0];/* Multiplication of auto-correlation by sin(x)/(x) function */    w = 3.141592654 / pulse_dur_est;    for (i = 1; i <= order; i++)    {	if (debug_level)	    fprintf (stderr, "Rold[%d] = %f\t", i, r[i]);	r[i] = r[i] * sin (w * i) / (w * i);	if (debug_level)	    fprintf (stderr, "Rnew[%d] = %f\n", i, r[i]);    }    get_atal (r, order, a, rc, &gain);    *ptr_res_energy = gain * sig_energy;}

⌨️ 快捷键说明

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