ICETEK-DM642-C.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:4k
源码类别:

DSP编程

开发平台:

C/C++

  1. #include "math.h"
  2. #include <csl_cache.h>
  3. #include "ICETEK-DM642-C.h"
  4. //工作变量
  5. unsigned char m_dbFrameBuffer[SIMGWIDTH*SIMGHEIGHT];
  6. unsigned char m_dbTargetImage[TIMGWIDTH*TIMGHEIGHT];
  7. unsigned char m_dbFFTImage[FFTIMGWIDTH*FFTIMGHEIGHT];
  8. unsigned int m_uVideoStatus,m_bFreeze;
  9. complex TD[TIMGWIDTH*TIMGHEIGHT];
  10. complex FD[TIMGWIDTH*TIMGHEIGHT];
  11. complex w[TIMGWIDTH];
  12. complex x1[TIMGWIDTH];
  13. complex x2[TIMGWIDTH];
  14. void ICETEKDM642CInit()
  15. {
  16. m_uVideoStatus=m_bFreeze=0;
  17. }
  18. void ICETEKDM642CFourier()
  19. {
  20. // 中间变量
  21. float fTemp,fWork1,fWork2;
  22. unsigned char *pWork,*pWork3;
  23. int nWork,ii,jj;
  24. complex *pWork1,*pWork2;
  25. // 循环变量
  26. int i;
  27. int j;
  28. // 进行付立叶变换的宽度和高度(2的整数次方)
  29. int w;
  30. int h;
  31. int wp;
  32. int hp;
  33. // 赋初值
  34. w = 1;
  35. h = 1;
  36. wp = 0;
  37. hp = 0;
  38. // 计算进行付立叶变换的宽度和高度(2的整数次方)
  39. while ( w*2<=TIMGWIDTH )
  40. {
  41. w*=2; wp++;
  42. }
  43. while ( h*2<=TIMGHEIGHT )
  44. {
  45. h*=2; hp++;
  46. }
  47. pWork1=TD;
  48. for ( i=0;i<h;i++ ) // 行
  49. {
  50. pWork=m_dbFrameBuffer+i*SIMGWIDTH;
  51. for ( j=0;j<w;j++,pWork++,pWork1++ ) // 列
  52. {
  53. pWork1->real=(*pWork);
  54. pWork1->img=0;
  55. }
  56. }
  57. pWork1=TD; pWork2=FD;
  58. // 对y方向进行快速付立叶变换
  59. for ( i=0;i<h;i++ ) FFT(pWork1+w*i,pWork2+w*i,wp);
  60. // 保存变换结果
  61. for ( i=0;i<h;i++ )
  62. for ( j=0;j<w;j++ )
  63. TD[j*h+i]=FD[i*w+j];
  64. // 对x方向进行快速付立叶变换
  65. for ( i=0;i<w;i++ ) FFT(pWork1+i*h,pWork2+i*h,hp);
  66. for ( i=0;i<w;i++ )
  67. for ( j=0;j<h;j++ )
  68. TD[j*w+i]=FD[i*h+j];
  69. pWork1=TD; pWork2=FD; pWork=m_dbTargetImage;
  70. for ( i=0;i<h;i++ ) // 行
  71. {
  72. for ( j=0;j<w;j++ ) // 列
  73. {
  74. // 计算频谱
  75. fWork1=(pWork2+j*h+i)->real;
  76. fWork2=(pWork2+j*h+i)->img;
  77. fTemp=sqrt(fWork1*fWork1+fWork2*fWork2)/100; //将幅度适当缩小后转换成灰度显示
  78. if ( fTemp>255 ) // 判断是否超过255
  79. fTemp = 255; // 对于超过的,直接设置为255
  80. pWork3=pWork+TIMGWIDTH*(TIMGHEIGHT-1-(i<h/2?i+h/2:i-h/2))+(j<w/2?j+w/2:j-w/2);
  81. (*pWork3)=(unsigned char)fTemp;
  82. }
  83. }
  84. pWork3=m_dbFFTImage;
  85. for ( i=0;i<FFTIMGHEIGHT;i++ )
  86. {
  87. for ( j=0;j<FFTIMGWIDTH;j++,pWork3++ )
  88. {
  89. pWork=m_dbTargetImage+TIMGHEIGHT/FFTIMGHEIGHT*i*TIMGHEIGHT+TIMGWIDTH/FFTIMGWIDTH*j;
  90. nWork=0;
  91. for ( ii=0;ii<TIMGHEIGHT/FFTIMGHEIGHT;ii++,pWork+=TIMGWIDTH )
  92. for ( jj=0;jj<TIMGWIDTH/FFTIMGWIDTH;jj++ )
  93. nWork+=pWork[jj];
  94. nWork/=((TIMGHEIGHT/FFTIMGHEIGHT)*(TIMGWIDTH/FFTIMGWIDTH));
  95. (*pWork3)=nWork;
  96. }
  97. }
  98. CACHE_clean(CACHE_L2ALL, 0, 0);
  99. }
  100. void FFT(complex *TD,complex *FD,int r)
  101. {
  102. // 付立叶变换点数
  103. int count;
  104. // 循环变量
  105. int i,j,k;
  106. // 中间变量
  107. int bfsize,p;
  108. // 角度
  109. float angle;
  110. complex *W,*X1,*X2,*X;
  111. W=w; X1=x1; X2=x2;
  112. // 计算付立叶变换点数
  113. count=1<<r;
  114. // 计算加权系数
  115. for ( i=0;i<count/2;i++ )
  116. {
  117. angle=-i*PI*2/count;
  118. (W+i)->real=cos(angle);
  119. (W+i)->img=sin(angle);
  120. }
  121. // 将时域点写入X1
  122. for ( i=0;i<count;i++ )
  123. {
  124. (X1+i)->real=(TD+i)->real;
  125. (X1+i)->img=(TD+i)->img;
  126. }
  127. // 采用蝶形算法进行快速付立叶变换
  128. for ( k=0;k<r;k++ )
  129. {
  130. for ( j=0;j<1<<k;j++ )
  131. {
  132. bfsize=1<<(r-k);
  133. for ( i=0;i<bfsize/2;i++ )
  134. {
  135. p=j*bfsize;
  136. (X2+i+p)->real=(X1+i+p)->real+(X1+i+p+bfsize/2)->real;
  137. (X2+i+p)->img=(X1+i+p)->img+(X1+i+p+bfsize/2)->img;
  138. (X2+i+p+bfsize/2)->real=((X1+i+p)->real-(X1+i+p+bfsize/2)->real)*(W+i*(1<<k))->real 
  139.                           -((X1+i+p)->img-(X1+i+p+bfsize/2)->img)*(W+i*(1<<k))->img;
  140. (X2+i+p+bfsize/2)->img=((X1+i+p)->real-(X1+i+p+bfsize/2)->real)*(W+i*(1<<k))->img 
  141.                           +((X1+i+p)->img-(X1+i+p+bfsize/2)->img)*(W+i*(1<<k))->real;             
  142. }
  143. }
  144. X=X1; X1=X2; X2=X;
  145. }
  146. // 重新排序
  147. for ( j=0;j<count;j++ )
  148. {
  149. p = 0;
  150. for ( i=0;i<r;i++ )
  151. if ( j&(1<<i) )
  152. p+=1<<(r-i-1);
  153. (FD+j)->real=(X1+p)->real;
  154. (FD+j)->img=(X1+p)->img;
  155. }
  156. CACHE_clean(CACHE_L2ALL, 0, 0);
  157. }