# Matlab实现FFT变换

FFT信号流图：

Matlab的位逆转程序：

function a=bitreverse(Nbit, num)

%Nbit = 4;

%num = 8;

a = 0;

b = bitshift(1,Nbit-1);

for i = 1:Nbit;

if((bitand(num,1)) == 1)

a = bitor(a,b);

end

num = bitshift(num,-1);

b = bitshift(b,-1);

end;

i , j, k 三者，反别表示三层循环，然后得出循环次数的关系

stages = log2(PointNum)

i  0stages – 1

j  0    其实就是

k 0

PointNum  = 512;

PointBitNum = 9;

fs = 1024*2;

t = 0:1:PointNum - 1;

%for u = 1:1:PointNum;

sampletab = cos(2*pi*543*t/fs) + cos(2*pi*100*t/fs) + 0.2 + cos(2*pi*857*t/fs) + cos(2*pi*222*t/fs);

%end

zeros(1,PointNum);

sampletab1 = sampletab;

index = 0;

for i = 1:PointNum

k = i - 1

index = bitreverse(PointBitNum,i - 1)

sampletab(i) = sampletab1(index + 1);

end

%sampletab1

%sampletab

REX = sampletab;

IMX = zeros(1,PointNum);

i = 0; �T Loop for Log2N stages

j = 0; �T loop for leach sub-DFT

k = 0; �T Loop for each butterfly

stages = log2(PointNum);

for i = 0 : stages - 1

lenNum = 0;

for j = 0 : 2^(stages - (i + 1)) - 1

for k = 0 : 2^i - 1

R1 = REX(lenNum + 2^i + 1) * cos(2*pi*k*2^(stages - (i + 1))/PointNum);

R2 = IMX(lenNum + 2^i + 1) * sin(2*pi*k*2^(stages - (i + 1))/PointNum);

T1 = REX(lenNum + 2^i + 1) * sin(2*pi*k*2^(stages - (i + 1))/PointNum);

T2 = IMX(lenNum + 2^i + 1) * cos(2*pi*k*2^(stages - (i + 1))/PointNum);

REX(lenNum + 2^i + 1) = REX(lenNum + 1) - R1 - R2;

IMX(lenNum + 2^i + 1) = IMX(lenNum + 1)  + T1 - T2;

REX(lenNum + 1) = REX(lenNum + 1) + R1 + R2;

IMX(lenNum + 1) = IMX(lenNum + 1) - T1 + T2 ;

lenNum = lenNum + 1;

endNum = lenNum + 2^i;

end

lenNum = endNum;

end

end

subplot(3,1,1);

fft(sampletab1, PointNum);

x1 = abs(fft(sampletab1, PointNum));

plot([0 : PointNum/2 - 1], x1(1:PointNum/2));

grid on

subplot(3,1,2);

% [REX IMX]

am = sqrt(abs(REX.*REX) + abs(IMX.*IMX));

plot(0:1:PointNum/2 - 1, am(1:PointNum/2));

grid on

subplot(3,1,3);

plot(t, sampletab);

grid on

N点基-2 FFT算法的实现方法

1.对于输入数据序列进行倒位序变换。

temp = A(6);  A(6) = A(3);  A(3) = A(6);

2.蝶形运算的循环结构。
从图4中我们可以看到对于点数为N = 2^L的fft运算，可以分解为L阶蝶形图级联，每一阶蝶形图内又分为M个蝶形组，每个蝶形组内包含K个蝶形。根据这一点我们就可以构造三阶循环来实现蝶形运算。编程过程需要注意旋转因子与蝶形阶数和蝶形分组内的蝶形个数存在关联。

3.浮点到定点转换需要注意的关键问题

4.计算过程中的溢出问题