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

📄 make_coeffs.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的各种滤波器源码
💻 C
字号:
#include	<stdio.h>
#include	<math.h>
#include	<assert.h>

/*
gcc -g -o make_coeffs make_coeffs.c -lm
make_coeffs > coeffs.h
emacs coeffs.h &
*/

#define	SCALE_UP	1

#define	DEBUG	0
#define	min(a,b) ((a<b)? a: b)
#define	max(a,b) ((a>b)? a: b)
#define	SCALE	(double)(1 << 15)

/*		scaling factor for cosine coefficients */

#define	ADJUST	sqrt(2.)
#if	(SCALE_UP == 1)
#define	MSK_SC	((double)(1 << 14) * ADJUST)
#else
#define	MSK_SC	(double)(1 << 14)
#endif

#define	MSK_XC	MSK_SC



#define	pr(l, a, n, s, v, i, b)	\
for (i = 0; i < l; i++) printf("#define\t%s%d%s0x%08x\n", a, n, s,\
(v[!b][i] << 16) | (v[b][i] & 0xffff)); printf("\n")

long	 c_h[2][12],  d_h[2][12],  mask_h[2][12];
double	fc_h[2][12], fd_h[2][12], fmask_h[2][12];

main()
{
	long	
	int	i, j;
	long	smart_round(double	fx);

	/* set floating point values in masks */

	fd_h[0][0] = MSK_SC * (cos(3. * M_PI / 16.) - cos(5. * M_PI / 16.));
	fd_h[1][0] = MSK_SC * (cos(M_PI / 16.) + cos(7. * M_PI / 16.));

	fd_h[0][1] = MSK_SC * (cos(7. * M_PI / 16.) - cos(M_PI / 16.));
	fd_h[1][1] = - MSK_SC * (cos(3. * M_PI / 16.) + cos(5. * M_PI / 16.));

	fd_h[0][2]	= - (MSK_SC * (cos(7. * M_PI / 16.) - cos(M_PI / 16.)));
	fd_h[1][2]	= (MSK_SC * (cos(3. * M_PI / 16.) - cos(5. * M_PI / 16.)));

	fd_h[0][3]	= - MSK_SC * (cos(3. * M_PI / 16.) + cos(5. * M_PI / 16.));
	fd_h[1][3]	= MSK_SC * (cos(M_PI / 16.) + cos(7. * M_PI / 16.));

	fd_h[0][4]	= MSK_SC * (cos(M_PI / 16.) + cos(7. * M_PI / 16.));
	fd_h[1][4]	= - MSK_SC * (cos(3. * M_PI / 16.) + cos(5. * M_PI / 16.));

	fd_h[0][5]	= - (MSK_SC * (cos(3. * M_PI / 16.) - cos(5. * M_PI / 16.)));
	fd_h[1][5]	= (MSK_SC * (cos(7. * M_PI / 16.) - cos(M_PI / 16.)));

	fd_h[0][6]	= MSK_SC * (cos(3. * M_PI / 16.) + cos(5. * M_PI / 16.));
	fd_h[1][6]	= - MSK_SC * (cos(7. * M_PI / 16.) - cos(M_PI / 16.));

	fd_h[0][7]	= MSK_SC * (cos(M_PI / 16.) + cos(7. * M_PI / 16.));
	fd_h[1][7]	= MSK_SC * (cos(3. * M_PI / 16.) - cos(5. * M_PI / 16.));

	fmask_h[0][0] = MSK_SC * (cos(2. * M_PI / 16.) - cos(6. * M_PI / 16.));
	fmask_h[1][0]	= - MSK_SC * (cos(6. * M_PI / 16.) + cos(2. * M_PI / 16.));

	fmask_h[0][1] = MSK_SC * (cos(6. * M_PI / 16.) + cos(2. * M_PI / 16.));
	fmask_h[1][1]	= MSK_SC * (cos(2. * M_PI / 16.) - cos(6. * M_PI / 16.));

	fmask_h[0][2] = MSK_SC;
	fmask_h[1][2]	= MSK_SC;

	fmask_h[0][3]	= MSK_SC;
	fmask_h[1][3]	= - MSK_SC;

	fc_h[0][0] = -MSK_XC * (cos(3.*M_PI/16.) + sin(3.*M_PI/16.));
	fc_h[1][0] =  MSK_XC * (cos(   M_PI/16.) + sin(   M_PI/16.));

	fc_h[0][1] =  MSK_XC * (cos(3.*M_PI/16.) - sin(3.*M_PI/16.));
	fc_h[1][1] =  MSK_XC * (sin(   M_PI/16.) - cos(   M_PI/16.));

	fc_h[0][2] = -MSK_XC * sqrt(2.) * sin(3.*M_PI/16.);
	fc_h[1][2] = -MSK_XC * sqrt(2.) * cos(   M_PI/16.);

	fc_h[0][3] =  MSK_XC * sqrt(2.) * cos(3.*M_PI/16.);
	fc_h[1][3] = -MSK_XC * sqrt(2.) * sin(   M_PI/16.);

	fc_h[0][4] =  MSK_XC * sqrt(2.) * cos(3.*M_PI/16.);
	fc_h[1][4] =  MSK_XC * sqrt(2.) * sin(   M_PI/16.);

	fc_h[0][5] =  MSK_XC * sqrt(2.) * sin(3.*M_PI/16.); 
        fc_h[1][5] = -MSK_XC * sqrt(2.) * cos(  M_PI/16.);  

	fc_h[0][6] =  MSK_XC * (cos(3.*M_PI/16.) - sin(3.*M_PI/16.));
        fc_h[1][6] =  MSK_XC * (cos(   M_PI/16.) - sin(   M_PI/16.));

	fc_h[0][7] =  MSK_XC * (cos(3.*M_PI/16.) + sin(3.*M_PI/16.));
        fc_h[1][7] =  MSK_XC * (cos(   M_PI/16.) + sin(   M_PI/16.));

	fc_h[0][8] =  MSK_XC;
        fc_h[1][8] =  MSK_XC;

	fc_h[0][9] =  MSK_XC;
        fc_h[1][9] = -MSK_XC;

	fc_h[0][10]=  MSK_XC * sqrt(2.) * cos(    M_PI/8.);
        fc_h[1][10]=  MSK_XC * sqrt(2.) * cos( 3.*M_PI/8.);

	fc_h[0][11]=  MSK_XC * sqrt(2.) * cos( 3.*M_PI/8.); 
        fc_h[1][11]= -MSK_XC * sqrt(2.) * cos(    M_PI/8.); 


	  

	/* convert into integers */


	for	(i = 0; i < 12; i++)
	{
		for	(j = 0; j < 2; j++)
		{
			mask_h[j][i]	= smart_round(fmask_h[j][i]);
			d_h[j][i]	= smart_round(fd_h[j][i]);
			c_h[j][i] = smart_round(fc_h[j][i]);
		}
	}

	/*	generate include file	*/
	
	printf(
"/********************************************************/\n\
/*  This header file has been automatically generated   */\n\
/*  by program make_coeffs. Coefficients are scaled by  */\n\
/*  factor : 2** 14 * %.5lf                           */\n\
/********************************************************/\n\n",
		MSK_SC / (double)(1 << 14));


	for	(j = 0; j < 2; j++)
	{
		printf	("#%s\n", j ? "else" : "if\tLITTLE_ENDIAN==1");
		pr(4, "MASK", i + 1, "\t", mask_h, i, j);
		pr(8, "D", i, "\t", d_h, i, j);
		pr(12,"C", i, "\t", c_h, i, j);
	}
	printf	("#endif\n");
	exit(0);
}


#define	HALF_WAY_THR	0.00		/* threshold for smart rounding -- was 0.05 */
long	smart_round(			/* rounded integer */
			double	fx)	/* floating point number */
{
	long	ix;
	static	double	cumul_round_error	= 0.0;
	double	fraction;

	fraction	= fx - floor(fx); 
	if	(fabs(fraction - 0.5) < HALF_WAY_THR)
	{	/* number is nearly half-way : round for minimizing cumul */
		ix	= 	(cumul_round_error > 0) ?
				(long)floor(fx):
				(long)ceil(fx);
		cumul_round_error	+= (double)ix - fx;
	}
	else	
	  ix	=	rint(fx);
	assert(ix>=-32768 && ix<32768);
	return	ix;
}

⌨️ 快捷键说明

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