/*
* zx_fft.c
*
* Implementation of Fast Fourier Transform(FFT)
* and reversal Fast Fourier Transform(IFFT)
*
* Created on: 2013-8-5
* Author: monkeyzx
*/
#include "zx_fft.h"
#include
#include
/*
* Bit Reverse
* === Input ===
* x : complex numbers
* n : nodes of FFT. @N should be power of 2, that is 2^(*)
* l : count by bit of binary format, @l=CEIL{log2(n)}
* === Output ===
* r : results after reversed.
* Note: I use a local variable @temp that result @r can be set
* to @x and won't overlap.
*/
static void BitReverse(complex *x, complex *r, int n, int l)
{
int i = 0;
int j = 0;
short stk = 0;
static complex *temp = 0;
temp = (complex *)malloc(sizeof(complex) * n);
if (!temp) {
return;
}
for(i=0; i>(j++)) & 0x01;
if(j
程序在TMS320C6713上实验,主函数中调用zx_fft()函数即可。
FFT的采样点数为128,输入信号的实数域为正弦信号,虚数域为0,数据精度定义FFT_TYPE为float类型,MakeInput和MakeOutput函数分别用于产生输入数据INPUT和输出数据OUTPUT的函数,便于使用CCS 的Graph功能绘制波形图。这里调试时使用CCS v5中的Tools -> Graph功能得到下面的波形图(怎么用自己琢磨,不会的使用CCS 的Help)。输入波形 输入信号的频域幅值表示 FFT运算结果
#define TYPE_FFT_E float /* Type is the same with COMPLEX member */
#ifndef PI
#define PI (3.14159265f)
#endif
typedef COMPLEX TYPE_FFT; /* Define COMPLEX in Config.h */
externint fft(TYPE_FFT *x, int N);
externint ifft(TYPE_FFT *x, int N);
#endif /* ZX_FFT_H_ */
/*
* zx_fft.h
*
* Created on: 2013-8-5
* Author: monkeyzx
*/
#ifndef _ZX_FFT_H
#define _ZX_FFT_H
#include "../Headers/Global.h"
#define TYPE_FFT_E float /* Type is the same with COMPLEX member */
#ifndef PI
#define PI (3.14159265f)
#endif
typedef COMPLEX TYPE_FFT; /* Define COMPLEX in Config.h */
extern int fft(TYPE_FFT *x, int N);
extern int ifft(TYPE_FFT *x, int N);
#endif /* ZX_FFT_H_ */ [cpp]
view plain
copyprint?
/*
* zx_fft.c
*
* Implementation of Fast Fourier Transform(FFT)
* and reversal Fast Fourier Transform(IFFT)
*
* Created on: 2013-8-5
* Author: monkeyzx
*
* TEST OK 2014.01.14
* == 2014.01.14
* Replace @BitReverse(x,x,N,M) by refrence to
*
*/
#include "zx_fft.h"
/*
* FFT Algorithm
* === Inputs ===
* x : complex numbers
* N : nodes of FFT. @N should be power of 2, that is 2^(*)
* === Output ===
* the @x contains the result of FFT algorithm, so the original data
* in @x is destroyed, please store them before using FFT.
*/
int fft(TYPE_FFT *x, int N)
{
int i,j,l,k,ip;
staticint M = 0;
staticint le,le2;
static TYPE_FFT_E sR,sI,tR,tI,uR,uI;
M = (int)(log(N) / log(2));
/*
* bit reversal sorting
*/
l = N / 2;
j = l;
//BitReverse(x,x,N,M);
for (i=1; i<=N-2; i++) {
if (i < j) {
tR = x[j].real;
tI = x[j].imag;
x[j].real = x[i].real;
x[j].imag = x[i].imag;
x[i].real = tR;
x[i].imag = tI;
}
k = l;
while (k <= j) {
j = j - k;
k = k / 2;
}
j = j + k;
}
/*
* For Loops
*/
for (l=1; l<=M; l++) { /* loop for ceil{log2(N)} */
le = (int)pow(2,l);
le2 = (int)(le / 2);
uR = 1;
uI = 0;
sR = cos(PI / le2);
sI = -sin(PI / le2);
for (j=1; j<=le2; j++) { /* loop for each sub DFT */
//jm1 = j - 1;
for (i=j-1; i<=N-1; i+=le) { /* loop for each butterfly */
ip = i + le2;
tR = x[ip].real * uR - x[ip].imag * uI;
tI = x[ip].real * uI + x[ip].imag * uR;
x[ip].real = x[i].real - tR;
x[ip].imag = x[i].imag - tI;
x[i].real += tR;
x[i].imag += tI;
} /* Next i */
tR = uR;
uR = tR * sR - uI * sI;
uI = tR * sI + uI *sR;
} /* Next j */
} /* Next l */
return 0;
}
/*
* Inverse FFT Algorithm
* === Inputs ===
* x : complex numbers
* N : nodes of FFT. @N should be power of 2, that is 2^(*)
* === Output ===
* the @x contains the result of FFT algorithm, so the original data
* in @x is destroyed, please store them before using FFT.
*/
int ifft(TYPE_FFT *x, int N)
{
int k = 0;
for (k=0; k<=N-1; k++) {
x[k].imag = -x[k].imag;
}
fft(x, N); /* using FFT */
for (k=0; k<=N-1; k++) {
x[k].real = x[k].real / N;
x[k].imag = -x[k].imag / N;
}
return 0;
}
/*
* zx_fft.c
*
* Implementation of Fast Fourier Transform(FFT)
* and reversal Fast Fourier Transform(IFFT)
*
* Created on: 2013-8-5
* Author: monkeyzx
*
* TEST OK 2014.01.14
* == 2014.01.14
* Replace @BitReverse(x,x,N,M) by refrence to
*
*/
#include "zx_fft.h"
/*
* FFT Algorithm
* === Inputs ===
* x : complex numbers
* N : nodes of FFT. @N should be power of 2, that is 2^(*)
* === Output ===
* the @x contains the result of FFT algorithm, so the original data
* in @x is destroyed, please store them before using FFT.
*/
int fft(TYPE_FFT *x, int N)
{
int i,j,l,k,ip;
static int M = 0;
static int le,le2;
static TYPE_FFT_E sR,sI,tR,tI,uR,uI;
M = (int)(log(N) / log(2));
/*
* bit reversal sorting
*/
l = N / 2;
j = l;
//BitReverse(x,x,N,M);
for (i=1; i<=N-2; i++) {
if (i < j) {
tR = x[j].real;
tI = x[j].imag;
x[j].real = x[i].real;
x[j].imag = x[i].imag;
x[i].real = tR;
x[i].imag = tI;
}
k = l;
while (k <= j) {
j = j - k;
k = k / 2;
}
j = j + k;
}
/*
* For Loops
*/
for (l=1; l<=M; l++) { /* loop for ceil{log2(N)} */
le = (int)pow(2,l);
le2 = (int)(le / 2);
uR = 1;
uI = 0;
sR = cos(PI / le2);
sI = -sin(PI / le2);
for (j=1; j<=le2; j++) { /* loop for each sub DFT */
//jm1 = j - 1;
for (i=j-1; i<=N-1; i+=le) { /* loop for each butterfly */
ip = i + le2;
tR = x[ip].real * uR - x[ip].imag * uI;
tI = x[ip].real * uI + x[ip].imag * uR;
x[ip].real = x[i].real - tR;
x[ip].imag = x[i].imag - tI;
x[i].real += tR;
x[i].imag += tI;
} /* Next i */
tR = uR;
uR = tR * sR - uI * sI;
uI = tR * sI + uI *sR;
} /* Next j */
} /* Next l */
return 0;
}
/*
* Inverse FFT Algorithm
* === Inputs ===
* x : complex numbers
* N : nodes of FFT. @N should be power of 2, that is 2^(*)
* === Output ===
* the @x contains the result of FFT algorithm, so the original data
* in @x is destroyed, please store them before using FFT.
*/
int ifft(TYPE_FFT *x, int N)
{
int k = 0;
for (k=0; k<=N-1; k++) {
x[k].imag = -x[k].imag;
}
fft(x, N); /* using FFT */
for (k=0; k<=N-1; k++) {
x[k].real = x[k].real / N;
x[k].imag = -x[k].imag / N;
}
return 0;
}
另外,可能还需要您在其它头文件中定义COMPLEX的复数类型
[cpp]
view plain
copyprint?