加入收藏
设为首页
联系站长
您现在的位置: 61IC电子在线 >> TI应用 >> C6000 >> TMS320C674x DSP >> 正文
  C实现Matlab中的xcorr 互相关         ★★★ 【字体:
C实现Matlab中的xcorr 互相关
作者:BlueDrea…    文章来源:BlueDream    点击数:    更新时间:2011-12-31    

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NN 10

void xcorr(float *r, unsigned short *x, unsigned short *y, int N);

int main()
{
unsigned short x[10]={1,1,2,2,3,4,5,6,7,8};
unsigned short y[10]={1,3,4,5,6,7,8,9,5,2};
float r[19] = {0};

FILE *fp_out;
int delay;

xcorr(r, x, y, NN);
//Open the file to write
if((fp_out=fopen("out_xcorr.txt","wt")) == NULL)
{
printf("Cannot open this file!\n");
exit(0);
}

for(delay = -NN + 1; delay < NN; delay++)
fprintf(fp_out,"%d %f\n",delay,r[delay + NN - 1]);

fclose(fp_out);
system("pause");
return 0;
}

void xcorr(float *r, unsigned short *x, unsigned short *y, int N)
{
float sxy;
int delay,i,j;

for(delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
sxy = 0;
for(i=0; i<N; i++)
{
j = i + delay;
if((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
sxy += (x[i] * y[j]);
}

//Calculate the correlation series at "delay"
r[delay + N - 1] = sxy;
}
}

参考来源:http://www.mathworks.com/access/helpdesk/help/toolbox/signal/xcorr.html

这种方法在DSP中运行起来很慢,目前正在寻找更有效的方法。
TI的DSP库中有自相关的代码,没有互相关的。

在网上还有说可以在频域相乘,再IFFT回来。我要240个点算一次,算一下复杂度:

xcorr()需要256*256=65536 次乘法;

频域相关:256点FFT要做1024次乘法,256点IFFT要做1024次乘法,再加频域的256次乘法,一共1024*4+256=4352次乘法,貌似效率提高了65536/4352 = 15倍。

我在C6747上算,C674x的核的速度是2400MIPS,1800MFLOPS,按MIPS算的话,xcorr要240*240/2400=24s(这个算错了,是Million没注意到,呵呵,具体在DSP中算一次乘法需要多少个指令周期,我还不太清楚),哦,mygod,黄花菜都凉了!

频域相关的matlab实现见:http://hi.baidu.com/yorkbluedream/blog/item/caedfa3eef6272f1828b13fc.html

算相关应该还有一种方法,就是先把一个序列反向,再用TI的DSPlib中卷积来算,不知道速度会不会提高。

               欢迎点击进入:TI德州中文网   (国内唯一针对TI应用的中文技术网站)    文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    关于C6747的PLL中SYSCLK1/2/…
    C6747不能读写SDRAM
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    站长:61IC 湘ICP备05002478号