[開發記錄] 函式庫調用 - 使用C6accel 資料庫進行FFT函式調用 之其二

本文记录了使用C6Accel DSP库进行FFT函数调用的过程,包括解决输入数据标准化问题,确保输出与Matlab一致。通过调整数据格式和应用twiddle因子,成功实现了FFT和IFFT运算,并探讨了将代码整合进appMain的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int c6accel_test_code(C6accel_Handle hC6accel,unsigned int n)
{
	int i,j;
	float k,l;
	double T;
	short *WN16;
	short *inBufs;
	short *outBufs;
	int Npoint = 16;
	short *FFT_Value;
	float *testBufs;

	printf("not used set parameter n, use %d instand of it \n",Npoint);
	if (IsPowOfTwo(Npoint) != 1  ){ printf("number of FFT data must power of 4 \n"); return(0);}

	WN16 = (short*)pWorkingBuf_16bpp;
	gen_twiddle_fft16x16(WN16,Npoint);	//產生 混合基 迴轉因子
	inBufs = (short *)pSrcBuf_16bpp;
	memset (inBufs,0x00,2*Npoint*sizeof(short));	/* Clear input arrays*/
	testBufs=(float*)malloc(sizeof(float)*2*Npoint);
	for (i=0;i<Npoint;i++)
	{
   		testBufs[2*i] =(float)cos(2.0*PI*i/Npoint);
		//testBufs[2*i]=1.0;
		testBufs[2*i+1] = 0.0;
	}

	for (i=0;i<2*Npoint;i++)
	{
		testBufs[i]=testBufs[i]*32767/(Npoint/2);
		inBufs[i]=(short)testBufs[i];

		//test 產生小數點誤差
		inBufs[i]=inBufs[i]/100;
		inBufs[i]=inBufs[i]*100;

	}	//trans to Q.15 Format

	printf("building Data (with scale 2/Npoint )are : \n ");
	showData(inBufs,2*Npoint);			//show FFT data on screen

	printf("twiddle Data are : \n ");
	showData(WN16,2*Npoint);			//show WN data on screen

	outBufs = (short*)pOutBuf_16bpp;
	memset (outBufs,0x00,2*Npoint*sizeof(short));	/* Clear output arrays*/

    // Call the fft16x16 function in C6Accel
	C6accel_DSP_fft16x16 (hC6accel,WN16,Npoint,inBufs,(short*)outBufs);
	if (C6Accel_readCallType(hC6accel) == ASYNC)
	{
	// Now wait for the callback
	C6accel_waitAsyncCall(hC6accel);
	}

	printf("Data after FFT are : \n ");
	showData(outBufs,2*Npoint);			//show data before FFT

	printf("Data after FFT values are : \n ");
	for(i=0;i<Npoint;i++)
	{
		k=outBufs[2*i];
		l=outBufs[2*i+1];

		k=k/32767.0;
		l=l/32767.0;

		k=k*k;
		l=l*l;

		k = sqrt(k+l);
		printf("\t%f",k);
		if ( (i+1) % 8 == 0 ){printf("\n");	}
	}
	printf("\n");

	memset (inBufs,0x00,2*Npoint*sizeof(short));	// Clear input arrays
	//move data from outBuf to inBuf
	for (i = 0; i < 2*Npoint ; i++ )
	{	inBufs[i]=outBufs[i];	}

	// Clear output arrays
	memset (outBufs,0x00,2*Npoint*sizeof(short));
	gen_twiddle_ifft16x16(WN16,Npoint);
	C6accel_DSP_ifft16x16 (hC6accel,WN16,Npoint,inBufs,(short*)outBufs);
	if (C6Accel_readCallType(hC6accel) == ASYNC)
	{
	// Now wait for the callback
	C6accel_waitAsyncCall(hC6accel);
	}

	for (i = 0; i < 2*Npoint ; i++ )
	{	outBufs[i]=outBufs[i]*2;	}

	printf("Data after IFFT(with ifft wn ) are : \n ");
	showData(outBufs,2*Npoint);			//show data before IFFT

    return (1);
}

上述程式,可加在c6accel的DSP_function內,在appMain進行調用,便可測試。


目前已可正確進行FFT、IFFT,

上一篇的問題:

1、使用 實部/虛部 為一組的方式擺放,進行FFT時因其格式為Q.15,輸出最大值只有+1,

測試後,發現將所有輸入資料 標準化後 乘上 2/N 後,FFT輸出資料可正確以標準化顯示,最大值為1 (代表能量最大值)

資料乘上N後,就跟Matlab測試資料相符。

2、使用C6Accel的FFT為混合基,使用C6accel內所提供的代碼 gen_twiddle_fft16x16 可正確產生,參數使用N,會產生2*N筆資料量大小。

3、不同,IFFT使用gen_twiddle_ifft16x16,若使用上述FFT後的資料,Q.15的資料拿來 ifft 使用,輸出資料*2後,可還原到原始產生資料。

4、仍未確認,但可將該程式清空,將程式整併 到 appMain的 .C檔 以及.H檔,刪除不需要的部分。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值