bit_rev[ 0 ] = 0;
while( p < N )
{
for(q=0; q<p; q++)
{
bit_rev[ q ] = bit_rev[ q ] * 2;
bit_rev[ q + p ] = bit_rev[ q ] + 1;
}
p *= 2;
}
for(i=0; i<N; i++) xx_r[ i ] = x_r[ i ];
for(i=0; i<N; i++) x_r = xx_r[ bit_rev ];
}
// ------------------------此刻序列x重排完毕------------------------
步骤II代码实现
int j;
float TR; //临时变量
float tw1; //旋转因子
/*两点DFT */
for(k=0; k<N; k+=2)
{
//两点DFT简化告诉我们tw1=1
TR = x_r[k]; // TR就是A, x_r[k+b]就是B.
x_r[k] = TR + tw1*x_r[k+b];
x_r[k+b] = TR - tw1*x_r[k+b];
}
在LayerII的时候,我们希望得到z,就需要对y进行DFT.
y[0],y[2]; y[1],y[3]; y[4],y[6]; y[5],y[7];
z[0], z[1]; z[2],z[3]; z[4],z[5]; z[6],z[7];
在LayerIII的时候,我们希望得到v,就需要对z进行DFT.
z[0],z[4]; z[1],z[5]; z[2],z[6]; z[3],z[7];
v[0],v[1]; v[2],v[3]; v[4],v[5]; v[6],v[7];
准备
令输入为x, x[s+N/2],输出为y, y[s+N/2]
这个N绝对不是上面的8,这个N是当前颗粒的输入样本总量
对于LayerI而言N是2;对于LayerII而言N是4;对于LayerIII而言N是8
复数乘法:(a+j*b) * (c+j*d)
实部= a*c – bd;
虚部= ad + bc;
旋转因子:
实现(C描述)
#include
<stdio.h>
#include
<math.h>
#include
<stdlib.h>
//#include "complex.h"
// --------------------------------------------------------------------------
#define N 8 //64
#define M 3 //6 //2^m=N
#define PI 3.1415926
// --------------------------------------------------------------------------
float twiddle[N/2] = {1.0, 0.707, 0.0, -0.707};
float x_r[N] = {1, 1, 1, 1, 0, 0, 0, 0};
float x_i[N]; //N=8
/*
float twiddle[N/2] = {1, 0.9951, 0.9808, 0.9570, 0.9239, 0.8820, 0.8317, 0.7733,
0.7075, 0.6349, 0.5561, 0.4721, 0.3835, 0.2912, 0.1961, 0.0991,
0.0000,-0.0991,-0.1961,-0.2912,-0.3835,-0.4721,-0.5561,-0.6349,
-0.7075,-0.7733, 0.8317,-0.8820,-0.9239,-0.9570,-0.9808,-0.9951}; //N=64
float x_r[N]={1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,};
float x_i[N];
*/
FILE *fp;
// ----------------------------------- func -----------------------------------
/**
*初始化输出虚部
*/
static
void
fft_init(void
)
{
int
i;
for(i=0; i<N; i++) x_i = 0.0;
}
/**
*反转算法.将时域信号重新排序.
*这个算法有改进的空间
*/
static
void
bitrev(void
)
{
int p=1, q, i;
int bit_rev[ N ]; //
float xx_r[ N ]; //
bit_rev[ 0 ] = 0;
while( p < N )
{
for(q=0; q<p; q++)
{
bit_rev[ q ] = bit_rev[ q ] * 2;
bit_rev[ q + p ] = bit_rev[ q ] + 1;
}
p *= 2;
}
for(i=0; i<N; i++) xx_r[ i ] = x_r[ i ];
for(i=0; i<N; i++) x_r = xx_r[ bit_rev ];
}
/* ------------ add by sshc625 ------------ */
static
void
bitrev2(void
)
{
return
;
}
/* */
void
display(void
)
{
printf("\n\n");
int i;
for(i=0; i<N; i++)
printf("%f\t%f\n", x_r, x_i);
}
/**
*
*/
void
fft1(void
)
{ fp = fopen("log1.txt","a+");
int L, i, b, j, p, k, tx1, tx2;
float TR, TI, temp; //临时变量
float tw1, tw2;
/*深M.对层进行循环. L为当前层,总层数为M. */
for(L=1; L<=M; L++)
{
fprintf(fp,"----------Layer=%d----------\n", L);
/* b的意义非常重大,b表示当前层的颗粒具有的输入样本点数 */
b = 1;
i = L - 1;
while(i > 0)
{
b *= 2;
i--;
}
// --------------是否外层对颗粒循环,内层对样本点循环逻辑性更强一些呢! --------------
/*
* outter对参与DFT的样本点进行循环
* L=1,循环了1次(4个颗粒,每个颗粒2个样本点)
* L=2,循环了2次(2个颗粒,每个颗粒4个样本点)
* L=3,循环了4次(1个颗粒,每个颗粒8个样本点)
*/
for(j=0; j<b; j++)
{
/*求旋转因子tw1 */
p = 1;
i = M - L; // M是为总层数, L为当前层.
while(i > 0)
{
p = p*2;
i--;
}
p = p * j;
tx1 = p % N;
tx2 = tx1 + 3*N/4;
tx2 = tx2 % N;
// tw1是cos部分,实部; tw2是sin部分,虚数部分.
tw1 = ( tx1>=N/2)? -twiddle[tx1-N/2] : twiddle[ tx1 ];
tw2 = ( tx2>=N/2)? -twiddle[tx2-(N/2)] : twiddle[tx2];
/*
* inner对颗粒进行循环
* L=1,循环了4次(4个颗粒,每个颗粒2个输入)
* L=2,循环了2次(2个颗粒,每个颗粒4个输入)
* L=3,循环了1次(1个颗粒,每个颗粒8个输入)
*/
for(k=j; k<N; k=k+2*b)
{
TR = x_r[k]; // TR就是A, x_r[k+b]就是B.
TI = x_i[k];
temp = x_r[k+b];
/*
*如果复习一下
(a+j*b)(c+j*d)两个复数相乘后的实部虚部分别是什么
*就能理解为什么会如下运算了,只有在L=1时候输入才是实数,之后层的
*输入都是复数,为了让所有的层的输入都是复数,我们只好让L=1时候的
*输入虚部为0
* x_i[k+b]*tw2是两个虚数相乘
*/
fprintf(fp,"tw1=%f, tw2=%f\n", tw1, tw2);
x_r[k] = TR + x_r[k+b]*tw1 + x_i[k+b]*tw2;
x_i[k] = TI - x_r[k+b]*tw2 + x_i[k+b]*tw1;
x_r[k+b] = TR - x_r[k+b]*tw1 - x_i[k+b]*tw2;
x_i[k+b] = TI + temp*tw2 - x_i[k+b]*tw1; |